1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX
28 #include "defaultobjectnamecheck.hxx"
29 #endif
30 
31 #ifndef _DBU_MISC_HRC_
32 #include "dbu_misc.hrc"
33 #endif
34 
35 #ifndef _DBAUI_MODULE_DBU_HXX_
36 #include "moduledbu.hxx"
37 #endif
38 
39 /** === begin UNO includes === **/
40 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_
41 #include <com/sun/star/lang/IllegalArgumentException.hpp>
42 #endif
43 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
44 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
45 #endif
46 #ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_
47 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
48 #endif
49 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
50 #include <com/sun/star/sdb/CommandType.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_SDB_TOOLS_XCONNECTIONTOOLS_HPP_
53 #include <com/sun/star/sdb/tools/XConnectionTools.hpp>
54 #endif
55 /** === end UNO includes === **/
56 
57 #ifndef _DBHELPER_DBEXCEPTION_HXX_
58 #include <connectivity/dbexception.hxx>
59 #endif
60 #ifndef CONNECTIVITY_INC_CONNECTIVITY_DBMETADATA_HXX
61 #include <connectivity/dbmetadata.hxx>
62 #endif
63 
64 #ifndef _RTL_USTRBUF_HXX_
65 #include <rtl/ustrbuf.hxx>
66 #endif
67 
68 #ifndef TOOLS_DIAGNOSE_EX_H
69 #include <tools/diagnose_ex.h>
70 #endif
71 #ifndef _STRING_HXX
72 #include <tools/string.hxx>
73 #endif
74 #ifndef _CPPUHELPER_EXC_HLP_HXX_
75 #include <cppuhelper/exc_hlp.hxx>
76 #endif
77 
78 #include <vector>
79 #include <boost/shared_ptr.hpp>
80 
81 //........................................................................
82 namespace dbaui
83 {
84 //........................................................................
85 
86 	/** === begin UNO using === **/
87     using ::com::sun::star::uno::Reference;
88     using ::com::sun::star::container::XNameAccess;
89     using ::com::sun::star::lang::IllegalArgumentException;
90     using ::com::sun::star::container::XHierarchicalNameAccess;
91     using ::com::sun::star::sdbc::SQLException;
92     using ::com::sun::star::uno::Exception;
93     using ::com::sun::star::sdbc::XConnection;
94     using ::com::sun::star::sdbcx::XTablesSupplier;
95     using ::com::sun::star::sdb::XQueriesSupplier;
96     using ::com::sun::star::uno::UNO_QUERY_THROW;
97     using ::com::sun::star::uno::makeAny;
98     using ::com::sun::star::uno::Any;
99     using ::com::sun::star::sdb::tools::XObjectNames;
100     using ::com::sun::star::sdb::tools::XConnectionTools;
101     using ::com::sun::star::uno::UNO_QUERY;
102 	/** === end UNO using === **/
103 
104     using namespace dbtools;
105 
106     namespace CommandType = ::com::sun::star::sdb::CommandType;
107 
108 	//====================================================================
109 	//= helper
110 	//====================================================================
111     namespace
112     {
lcl_fillNameExistsError(const::rtl::OUString & _rObjectName,SQLExceptionInfo & _out_rErrorToDisplay)113         void    lcl_fillNameExistsError( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay )
114         {
115             String sErrorMessage = String( ModuleRes( STR_NAMED_OBJECT_ALREADY_EXISTS ) );
116 		    sErrorMessage.SearchAndReplaceAllAscii( "$#$", _rObjectName );
117             SQLException aError;
118             aError.Message = sErrorMessage;
119             _out_rErrorToDisplay = aError;
120         }
121 
122     }
123 
124 	//====================================================================
125 	//= HierarchicalNameCheck_Impl
126 	//====================================================================
127     struct HierarchicalNameCheck_Impl
128     {
129         Reference< XHierarchicalNameAccess >    xHierarchicalNames;
130         ::rtl::OUString                         sRelativeRoot;
131     };
132 
133     //====================================================================
134 	//= HierarchicalNameCheck
135 	//====================================================================
136     //--------------------------------------------------------------------
HierarchicalNameCheck(const Reference<XHierarchicalNameAccess> & _rxNames,const::rtl::OUString & _rRelativeRoot)137     HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const ::rtl::OUString& _rRelativeRoot )
138         :m_pImpl( new HierarchicalNameCheck_Impl )
139     {
140         m_pImpl->xHierarchicalNames = _rxNames;
141         m_pImpl->sRelativeRoot = _rRelativeRoot;
142 
143         if ( !m_pImpl->xHierarchicalNames.is() )
144             throw IllegalArgumentException();
145     }
146 
147     //--------------------------------------------------------------------
~HierarchicalNameCheck()148     HierarchicalNameCheck::~HierarchicalNameCheck()
149     {
150     }
151 
152     //--------------------------------------------------------------------
isNameValid(const::rtl::OUString & _rObjectName,SQLExceptionInfo & _out_rErrorToDisplay) const153     bool HierarchicalNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const
154     {
155         try
156         {
157             ::rtl::OUStringBuffer aCompleteName;
158 			if ( m_pImpl->sRelativeRoot.getLength() )
159             {
160                 aCompleteName.append( m_pImpl->sRelativeRoot );
161 				aCompleteName.appendAscii( "/" );
162             }
163             aCompleteName.append( _rObjectName );
164 
165             ::rtl::OUString sCompleteName( aCompleteName.makeStringAndClear() );
166 			if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) )
167                 return true;
168         }
169         catch( const Exception& )
170         {
171         	DBG_UNHANDLED_EXCEPTION();
172         }
173 
174         lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
175         return false;
176     }
177 
178     //====================================================================
179     //= DynamicTableOrQueryNameCheck_Impl
180     //====================================================================
181     struct DynamicTableOrQueryNameCheck_Impl
182     {
183         sal_Int32                   nCommandType;
184         Reference< XObjectNames >   xObjectNames;
185     };
186 
187     //====================================================================
188     //= DynamicTableOrQueryNameCheck
189     //====================================================================
190     //--------------------------------------------------------------------
DynamicTableOrQueryNameCheck(const Reference<XConnection> & _rxSdbLevelConnection,sal_Int32 _nCommandType)191     DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType )
192         :m_pImpl( new DynamicTableOrQueryNameCheck_Impl )
193     {
194         Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY );
195         if ( xConnTools.is() )
196             m_pImpl->xObjectNames.set( xConnTools->getObjectNames() );
197         if ( !m_pImpl->xObjectNames.is() )
198             throw IllegalArgumentException();
199 
200         if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) )
201             throw IllegalArgumentException();
202         m_pImpl->nCommandType = _nCommandType;
203     }
204 
205     //--------------------------------------------------------------------
~DynamicTableOrQueryNameCheck()206     DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
207     {
208     }
209 
210     //--------------------------------------------------------------------
isNameValid(const::rtl::OUString & _rObjectName,::dbtools::SQLExceptionInfo & _out_rErrorToDisplay) const211     bool DynamicTableOrQueryNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const
212     {
213         try
214         {
215             m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName );
216             return true;
217         }
218         catch( const SQLException& )
219         {
220             _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
221         }
222         return false;
223     }
224 
225 //........................................................................
226 } // namespace dbaui
227 //........................................................................
228 
229