LibreOffice Module dbaccess (master) 1
defaultobjectnamecheck.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <core_resource.hxx>
22
23#include <strings.hrc>
24
25#include <com/sun/star/lang/IllegalArgumentException.hpp>
26#include <com/sun/star/sdb/CommandType.hpp>
27#include <com/sun/star/sdbc/SQLException.hpp>
28
31
32#include <rtl/ustrbuf.hxx>
33
36
37#include <memory>
38#include <string_view>
39
40namespace dbaui
41{
42
43 using ::com::sun::star::uno::Reference;
44 using ::com::sun::star::lang::IllegalArgumentException;
45 using ::com::sun::star::container::XHierarchicalNameAccess;
46 using ::com::sun::star::sdbc::SQLException;
47 using ::com::sun::star::uno::Exception;
48 using ::com::sun::star::sdbc::XConnection;
49 using ::com::sun::star::sdb::tools::XConnectionTools;
50 using ::com::sun::star::uno::UNO_QUERY;
51
52 using namespace dbtools;
53
54 namespace CommandType = ::com::sun::star::sdb::CommandType;
55
56 // helper
57 namespace
58 {
59 void lcl_fillNameExistsError( std::u16string_view _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay )
60 {
61 SQLException aError;
62 OUString sErrorMessage = DBA_RES(STR_NAMED_OBJECT_ALREADY_EXISTS);
63 aError.Message = sErrorMessage.replaceAll("$#$", _rObjectName);
64 _out_rErrorToDisplay = aError;
65 }
66
67 }
68
69 // HierarchicalNameCheck
70 HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const OUString& _rRelativeRoot )
71 {
72 mxHierarchicalNames = _rxNames;
73 msRelativeRoot = _rRelativeRoot;
74
75 if ( !mxHierarchicalNames.is() )
76 throw IllegalArgumentException();
77 }
78
80 {
81 }
82
83 bool HierarchicalNameCheck::isNameValid( const OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const
84 {
85 try
86 {
87 OUStringBuffer aCompleteName;
88 if ( !msRelativeRoot.isEmpty() )
89 {
90 aCompleteName.append( msRelativeRoot + "/" );
91 }
92 aCompleteName.append( _rObjectName );
93
94 OUString sCompleteName( aCompleteName.makeStringAndClear() );
95 if ( !mxHierarchicalNames->hasByHierarchicalName( sCompleteName ) )
96 return true;
97 }
98 catch( const Exception& )
99 {
100 DBG_UNHANDLED_EXCEPTION("dbaccess");
101 }
102
103 lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
104 return false;
105 }
106
107 // DynamicTableOrQueryNameCheck
108 DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
109 {
110 Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY );
111 if ( xConnTools.is() )
112 mxObjectNames.set( xConnTools->getObjectNames() );
113 if ( !mxObjectNames.is() )
114 throw IllegalArgumentException();
115
116 if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) )
117 throw IllegalArgumentException();
118 mnCommandType = _nCommandType;
119 }
120
122 {
123 }
124
125 bool DynamicTableOrQueryNameCheck::isNameValid( const OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
126 {
127 try
128 {
129 mxObjectNames->checkNameForCreate( mnCommandType, _rObjectName );
130 return true;
131 }
132 catch( const SQLException& )
133 {
134 _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
135 }
136 return false;
137 }
138
139} // namespace dbaui
140
141/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool isNameValid(const OUString &_rObjectName, ::dbtools::SQLExceptionInfo &_out_rErrorToDisplay) const override
determines whether a given object name is valid
css::uno::Reference< css::sdb::tools::XObjectNames > mxObjectNames
DynamicTableOrQueryNameCheck(const css::uno::Reference< css::sdbc::XConnection > &_rxSdbLevelConnection, sal_Int32 _nCommandType)
constructs a DynamicTableOrQueryNameCheck instance
virtual bool isNameValid(const OUString &_rObjectName, ::dbtools::SQLExceptionInfo &_out_rErrorToDisplay) const override
determines whether a given object name is valid
css::uno::Reference< css::container::XHierarchicalNameAccess > mxHierarchicalNames
HierarchicalNameCheck(const css::uno::Reference< css::container::XHierarchicalNameAccess > &_rxNames, const OUString &_rRelativeRoot)
constructs a HierarchicalNameCheck instance
#define DBA_RES(id)
#define DBG_UNHANDLED_EXCEPTION(...)
@ Exception