1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifndef DBACCESS_SOURCE_SDBTOOLS_CONNECTION_OBJECTNAMES_HXX
28cdf0e10cSrcweir #include "objectnames.hxx"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #ifndef DBACCESS_MODULE_SDBT_HXX
32cdf0e10cSrcweir #include "module_sdbt.hxx"
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #ifndef DBACCESS_SDBT_RESOURCE_HRC
35cdf0e10cSrcweir #include "sdbt_resource.hrc"
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /** === begin UNO includes === **/
39cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp>
40cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
41cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
42cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
43cdf0e10cSrcweir #include <com/sun/star/sdb/ErrorCondition.hpp>
44cdf0e10cSrcweir /** === end UNO includes === **/
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <connectivity/dbmetadata.hxx>
47cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
48cdf0e10cSrcweir #include <connectivity/sqlerror.hxx>
49cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
50cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
51cdf0e10cSrcweir #include <tools/string.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //........................................................................
56cdf0e10cSrcweir namespace sdbtools
57cdf0e10cSrcweir {
58cdf0e10cSrcweir //........................................................................
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	/** === begin UNO using === **/
61cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
62cdf0e10cSrcweir     using ::com::sun::star::sdbc::XConnection;
63cdf0e10cSrcweir     using ::com::sun::star::lang::NullPointerException;
64cdf0e10cSrcweir     using ::com::sun::star::lang::IllegalArgumentException;
65cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
66cdf0e10cSrcweir     using ::com::sun::star::sdbc::SQLException;
67cdf0e10cSrcweir     using ::com::sun::star::sdbc::XDatabaseMetaData;
68cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
69cdf0e10cSrcweir     using ::com::sun::star::container::XNameAccess;
70cdf0e10cSrcweir     using ::com::sun::star::sdbc::XDatabaseMetaData;
71cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
72cdf0e10cSrcweir     using ::com::sun::star::sdbcx::XTablesSupplier;
73cdf0e10cSrcweir     using ::com::sun::star::sdb::XQueriesSupplier;
74cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
75cdf0e10cSrcweir     using ::com::sun::star::uno::makeAny;
76cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
77cdf0e10cSrcweir 	/** === end UNO using === **/
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     namespace CommandType = ::com::sun::star::sdb::CommandType;
80cdf0e10cSrcweir     namespace ErrorCondition = ::com::sun::star::sdb::ErrorCondition;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	//====================================================================
83cdf0e10cSrcweir 	//= INameValidation
84cdf0e10cSrcweir 	//====================================================================
85cdf0e10cSrcweir     class INameValidation
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir     public:
88cdf0e10cSrcweir         virtual bool validateName( const ::rtl::OUString& _rName ) = 0;
89cdf0e10cSrcweir         virtual void validateName_throw( const ::rtl::OUString& _rName ) = 0;
90cdf0e10cSrcweir 
~INameValidation()91cdf0e10cSrcweir         virtual ~INameValidation() { }
92cdf0e10cSrcweir     };
93cdf0e10cSrcweir     typedef ::boost::shared_ptr< INameValidation >   PNameValidation;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	//====================================================================
96cdf0e10cSrcweir 	//= PlainExistenceCheck
97cdf0e10cSrcweir 	//====================================================================
98cdf0e10cSrcweir     class PlainExistenceCheck : public INameValidation
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir     private:
101cdf0e10cSrcweir         const ::comphelper::ComponentContext    m_aContext;
102cdf0e10cSrcweir         Reference< XConnection >                m_xConnection;
103cdf0e10cSrcweir         Reference< XNameAccess >                m_xContainer;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     public:
PlainExistenceCheck(const::comphelper::ComponentContext & _rContext,const Reference<XConnection> & _rxConnection,const Reference<XNameAccess> & _rxContainer)106cdf0e10cSrcweir         PlainExistenceCheck( const ::comphelper::ComponentContext& _rContext, const Reference< XConnection >& _rxConnection, const Reference< XNameAccess >& _rxContainer )
107cdf0e10cSrcweir             :m_aContext( _rContext )
108cdf0e10cSrcweir             ,m_xConnection( _rxConnection )
109cdf0e10cSrcweir             ,m_xContainer( _rxContainer )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             OSL_ENSURE( m_xContainer.is(), "PlainExistenceCheck::PlainExistenceCheck: this will crash!" );
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         // INameValidation
validateName(const::rtl::OUString & _rName)115cdf0e10cSrcweir         virtual bool validateName( const ::rtl::OUString& _rName )
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             return !m_xContainer->hasByName( _rName );
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir 
validateName_throw(const::rtl::OUString & _rName)120cdf0e10cSrcweir         virtual void validateName_throw( const ::rtl::OUString& _rName )
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir             if ( validateName( _rName ) )
123cdf0e10cSrcweir                 return;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             ::connectivity::SQLError aErrors( m_aContext );
126cdf0e10cSrcweir             SQLException aError( aErrors.getSQLException( ErrorCondition::DB_OBJECT_NAME_IS_USED, m_xConnection, _rName ) );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir             ::dbtools::DatabaseMetaData aMeta( m_xConnection );
129cdf0e10cSrcweir             if ( aMeta.supportsSubqueriesInFrom() )
130cdf0e10cSrcweir             {
131cdf0e10cSrcweir                 String sNeedDistinctNames( SdbtRes( STR_QUERY_AND_TABLE_DISTINCT_NAMES ) );
132cdf0e10cSrcweir                 aError.NextException <<= SQLException( sNeedDistinctNames, m_xConnection, ::rtl::OUString(), 0, Any() );
133cdf0e10cSrcweir             }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir             throw aError;
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir     };
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	//====================================================================
140cdf0e10cSrcweir 	//= TableValidityCheck
141cdf0e10cSrcweir 	//====================================================================
142cdf0e10cSrcweir     class TableValidityCheck : public INameValidation
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         const ::comphelper::ComponentContext  m_aContext;
145cdf0e10cSrcweir         const Reference< XConnection >        m_xConnection;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     public:
TableValidityCheck(const::comphelper::ComponentContext & _rContext,const Reference<XConnection> & _rxConnection)148cdf0e10cSrcweir         TableValidityCheck( const ::comphelper::ComponentContext& _rContext, const Reference< XConnection >& _rxConnection )
149cdf0e10cSrcweir             :m_aContext( _rContext )
150cdf0e10cSrcweir             ,m_xConnection( _rxConnection )
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir 
validateName(const::rtl::OUString & _rName)154cdf0e10cSrcweir         virtual bool validateName( const ::rtl::OUString& _rName )
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             ::dbtools::DatabaseMetaData aMeta( m_xConnection );
157cdf0e10cSrcweir             if  ( !aMeta.restrictIdentifiersToSQL92() )
158cdf0e10cSrcweir                 return true;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             ::rtl::OUString sCatalog, sSchema, sName;
161cdf0e10cSrcweir             ::dbtools::qualifiedNameComponents(
162cdf0e10cSrcweir                 m_xConnection->getMetaData(), _rName, sCatalog, sSchema, sName, ::dbtools::eInTableDefinitions );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir             ::rtl::OUString sExtraNameCharacters( m_xConnection->getMetaData()->getExtraNameCharacters() );
165cdf0e10cSrcweir             if  (   ( sCatalog.getLength() && !::dbtools::isValidSQLName( sCatalog, sExtraNameCharacters ) )
166cdf0e10cSrcweir                 ||  ( sSchema.getLength() && !::dbtools::isValidSQLName( sSchema, sExtraNameCharacters ) )
167cdf0e10cSrcweir                 ||  ( sName.getLength() && !::dbtools::isValidSQLName( sName, sExtraNameCharacters ) )
168cdf0e10cSrcweir                 )
169cdf0e10cSrcweir                 return false;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             return true;
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
validateName_throw(const::rtl::OUString & _rName)174cdf0e10cSrcweir         virtual void validateName_throw( const ::rtl::OUString& _rName )
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             if ( validateName( _rName ) )
177cdf0e10cSrcweir                 return;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             ::connectivity::SQLError aErrors( m_aContext );
180cdf0e10cSrcweir             aErrors.raiseException( ErrorCondition::DB_INVALID_SQL_NAME, m_xConnection, _rName );
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir     };
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	//====================================================================
185cdf0e10cSrcweir 	//= QueryValidityCheck
186cdf0e10cSrcweir 	//====================================================================
187cdf0e10cSrcweir     class QueryValidityCheck : public INameValidation
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         const ::comphelper::ComponentContext    m_aContext;
190cdf0e10cSrcweir         const Reference< XConnection >          m_xConnection;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     public:
QueryValidityCheck(const::comphelper::ComponentContext & _rContext,const Reference<XConnection> & _rxConnection)193cdf0e10cSrcweir         QueryValidityCheck( const ::comphelper::ComponentContext& _rContext, const Reference< XConnection >& _rxConnection )
194cdf0e10cSrcweir             :m_aContext( _rContext )
195cdf0e10cSrcweir             ,m_xConnection( _rxConnection )
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir 
validateName_getErrorCondition(const::rtl::OUString & _rName)199cdf0e10cSrcweir         inline ::connectivity::ErrorCondition validateName_getErrorCondition( const ::rtl::OUString& _rName )
200cdf0e10cSrcweir         {
201cdf0e10cSrcweir             if  (   ( _rName.indexOf( (sal_Unicode)34  ) >= 0 )  // "
202cdf0e10cSrcweir                 ||  ( _rName.indexOf( (sal_Unicode)39  ) >= 0 )  // '
203cdf0e10cSrcweir                 ||  ( _rName.indexOf( (sal_Unicode)96  ) >= 0 )  //
204cdf0e10cSrcweir                 ||  ( _rName.indexOf( (sal_Unicode)145 ) >= 0 )  //
205cdf0e10cSrcweir                 ||  ( _rName.indexOf( (sal_Unicode)146 ) >= 0 )  //
206cdf0e10cSrcweir                 ||  ( _rName.indexOf( (sal_Unicode)180 ) >= 0 )  // #86621# removed unparsable chars
207cdf0e10cSrcweir                 )
208cdf0e10cSrcweir                 return ErrorCondition::DB_QUERY_NAME_WITH_QUOTES;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir             if ( _rName.indexOf( '/') >= 0 )
211cdf0e10cSrcweir                 return ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             return 0;
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir 
validateName(const::rtl::OUString & _rName)216cdf0e10cSrcweir         virtual bool validateName( const ::rtl::OUString& _rName )
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             if ( validateName_getErrorCondition( _rName ) != 0 )
219cdf0e10cSrcweir                 return false;
220cdf0e10cSrcweir             return true;
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir 
validateName_throw(const::rtl::OUString & _rName)223cdf0e10cSrcweir         virtual void validateName_throw( const ::rtl::OUString& _rName )
224cdf0e10cSrcweir         {
225cdf0e10cSrcweir             ::connectivity::ErrorCondition nErrorCondition = validateName_getErrorCondition( _rName );
226cdf0e10cSrcweir             if ( nErrorCondition != 0 )
227cdf0e10cSrcweir             {
228cdf0e10cSrcweir                 ::connectivity::SQLError aErrors( m_aContext );
229cdf0e10cSrcweir                 aErrors.raiseException( nErrorCondition, m_xConnection );
230cdf0e10cSrcweir             }
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     };
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 	//====================================================================
235cdf0e10cSrcweir 	//= CombinedNameCheck
236cdf0e10cSrcweir 	//====================================================================
237cdf0e10cSrcweir     class CombinedNameCheck : public INameValidation
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir     private:
240cdf0e10cSrcweir         PNameValidation  m_pPrimary;
241cdf0e10cSrcweir         PNameValidation  m_pSecondary;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     public:
CombinedNameCheck(PNameValidation _pPrimary,PNameValidation _pSecondary)244cdf0e10cSrcweir         CombinedNameCheck( PNameValidation _pPrimary, PNameValidation _pSecondary )
245cdf0e10cSrcweir             :m_pPrimary( _pPrimary )
246cdf0e10cSrcweir             ,m_pSecondary( _pSecondary )
247cdf0e10cSrcweir         {
248cdf0e10cSrcweir             OSL_ENSURE( m_pPrimary.get() && m_pSecondary.get(), "CombinedNameCheck::CombinedNameCheck: this will crash!" );
249cdf0e10cSrcweir         }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir         // INameValidation
validateName(const::rtl::OUString & _rName)252cdf0e10cSrcweir         virtual bool validateName( const ::rtl::OUString& _rName )
253cdf0e10cSrcweir         {
254cdf0e10cSrcweir             return m_pPrimary->validateName( _rName ) && m_pSecondary->validateName( _rName );
255cdf0e10cSrcweir         }
256cdf0e10cSrcweir 
validateName_throw(const::rtl::OUString & _rName)257cdf0e10cSrcweir         virtual void validateName_throw( const ::rtl::OUString& _rName )
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             m_pPrimary->validateName_throw( _rName );
260cdf0e10cSrcweir             m_pSecondary->validateName_throw( _rName );
261cdf0e10cSrcweir         }
262cdf0e10cSrcweir     };
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 	//====================================================================
265cdf0e10cSrcweir 	//= NameCheckFactory
266cdf0e10cSrcweir 	//====================================================================
267cdf0e10cSrcweir     class NameCheckFactory
268cdf0e10cSrcweir     {
269cdf0e10cSrcweir     public:
270cdf0e10cSrcweir         /** creates an INameValidation instance which can be used to check the existence of query or table names
271cdf0e10cSrcweir 
272cdf0e10cSrcweir             @param _rContext
273cdf0e10cSrcweir                 the component's context
274cdf0e10cSrcweir 
275cdf0e10cSrcweir             @param  _nCommandType
276cdf0e10cSrcweir                 the type of objects (CommandType::TABLE or CommandType::QUERY) of which names shall be checked for existence
277cdf0e10cSrcweir 
278cdf0e10cSrcweir             @param  _rxConnection
279cdf0e10cSrcweir                 the connection relative to which the names are to be checked. Must be an SDB-level connection
280cdf0e10cSrcweir 
281cdf0e10cSrcweir             @throws IllegalArgumentException
282cdf0e10cSrcweir                 if the given connection is no SDB-level connection
283cdf0e10cSrcweir 
284cdf0e10cSrcweir             @throws IllegalArgumentException
285cdf0e10cSrcweir                 if the given command type is neither CommandType::TABLE or CommandType::QUERY
286cdf0e10cSrcweir         */
287cdf0e10cSrcweir         static  PNameValidation  createExistenceCheck(
288cdf0e10cSrcweir                     const ::comphelper::ComponentContext& _rContext,
289cdf0e10cSrcweir                     sal_Int32 _nCommandType,
290cdf0e10cSrcweir                     const Reference< XConnection >& _rxConnection
291cdf0e10cSrcweir                 );
292cdf0e10cSrcweir 
293cdf0e10cSrcweir         /** creates an INameValidation instance which can be used to check the validity of a query or table name
294cdf0e10cSrcweir 
295cdf0e10cSrcweir             @param _rContext
296cdf0e10cSrcweir                 the component's context
297cdf0e10cSrcweir 
298cdf0e10cSrcweir             @param  _nCommandType
299cdf0e10cSrcweir                 the type of objects (CommandType::TABLE or CommandType::QUERY) of which names shall be validated
300cdf0e10cSrcweir 
301cdf0e10cSrcweir             @param  _rxConnection
302cdf0e10cSrcweir                 the connection relative to which the names are to be checked. Must be an SDB-level connection
303cdf0e10cSrcweir 
304cdf0e10cSrcweir             @throws IllegalArgumentException
305cdf0e10cSrcweir                 if the given connection is no SDB-level connection
306cdf0e10cSrcweir 
307cdf0e10cSrcweir             @throws IllegalArgumentException
308cdf0e10cSrcweir                 if the given command type is neither CommandType::TABLE or CommandType::QUERY
309cdf0e10cSrcweir         */
310cdf0e10cSrcweir         static  PNameValidation  createValidityCheck(
311cdf0e10cSrcweir                     const ::comphelper::ComponentContext& _rContext,
312cdf0e10cSrcweir                     const sal_Int32 _nCommandType,
313cdf0e10cSrcweir                     const Reference< XConnection >& _rxConnection
314cdf0e10cSrcweir                 );
315cdf0e10cSrcweir 
316cdf0e10cSrcweir     private:
317cdf0e10cSrcweir         NameCheckFactory();                                     // never implemented
318cdf0e10cSrcweir 
319cdf0e10cSrcweir     private:
320cdf0e10cSrcweir         static  void    verifyCommandType( sal_Int32 _nCommandType );
321cdf0e10cSrcweir     };
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 	//--------------------------------------------------------------------
verifyCommandType(sal_Int32 _nCommandType)324cdf0e10cSrcweir     void NameCheckFactory::verifyCommandType( sal_Int32 _nCommandType )
325cdf0e10cSrcweir     {
326cdf0e10cSrcweir         if  (   ( _nCommandType != CommandType::TABLE )
327cdf0e10cSrcweir             &&  ( _nCommandType != CommandType::QUERY )
328cdf0e10cSrcweir             )
329cdf0e10cSrcweir             throw IllegalArgumentException(
330cdf0e10cSrcweir                 String( SdbtRes( STR_INVALID_COMMAND_TYPE ) ),
331cdf0e10cSrcweir                 NULL,
332cdf0e10cSrcweir                 0
333cdf0e10cSrcweir             );
334cdf0e10cSrcweir     }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 	//--------------------------------------------------------------------
createExistenceCheck(const::comphelper::ComponentContext & _rContext,sal_Int32 _nCommandType,const Reference<XConnection> & _rxConnection)337cdf0e10cSrcweir     PNameValidation  NameCheckFactory::createExistenceCheck( const ::comphelper::ComponentContext& _rContext, sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection )
338cdf0e10cSrcweir     {
339cdf0e10cSrcweir         verifyCommandType( _nCommandType );
340cdf0e10cSrcweir 
341cdf0e10cSrcweir         ::dbtools::DatabaseMetaData aMeta( _rxConnection );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir         Reference< XNameAccess > xTables, xQueries;
344cdf0e10cSrcweir         try
345cdf0e10cSrcweir         {
346cdf0e10cSrcweir             Reference< XTablesSupplier > xSuppTables( _rxConnection, UNO_QUERY_THROW );
347cdf0e10cSrcweir             Reference< XQueriesSupplier > xQueriesSupplier( _rxConnection, UNO_QUERY_THROW );
348cdf0e10cSrcweir             xTables.set( xSuppTables->getTables(), UNO_QUERY_THROW );
349cdf0e10cSrcweir             xQueries.set( xQueriesSupplier->getQueries(), UNO_QUERY_THROW );
350cdf0e10cSrcweir         }
351cdf0e10cSrcweir         catch( const Exception& )
352cdf0e10cSrcweir         {
353cdf0e10cSrcweir         	throw IllegalArgumentException(
354cdf0e10cSrcweir                 String( SdbtRes( STR_CONN_WITHOUT_QUERIES_OR_TABLES ) ),
355cdf0e10cSrcweir                 NULL,
356cdf0e10cSrcweir                 0
357cdf0e10cSrcweir             );
358cdf0e10cSrcweir         }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir         PNameValidation pTableCheck( new PlainExistenceCheck( _rContext, _rxConnection, xTables ) );
361cdf0e10cSrcweir         PNameValidation pQueryCheck( new PlainExistenceCheck( _rContext, _rxConnection, xQueries ) );
362cdf0e10cSrcweir         PNameValidation pReturn;
363cdf0e10cSrcweir 
364cdf0e10cSrcweir         if ( aMeta.supportsSubqueriesInFrom() )
365cdf0e10cSrcweir             pReturn.reset( new CombinedNameCheck( pTableCheck, pQueryCheck ) );
366cdf0e10cSrcweir         else if ( _nCommandType == CommandType::TABLE )
367cdf0e10cSrcweir             pReturn = pTableCheck;
368cdf0e10cSrcweir         else
369cdf0e10cSrcweir             pReturn = pQueryCheck;
370cdf0e10cSrcweir         return pReturn;
371cdf0e10cSrcweir     }
372cdf0e10cSrcweir 
373cdf0e10cSrcweir 	//--------------------------------------------------------------------
createValidityCheck(const::comphelper::ComponentContext & _rContext,sal_Int32 _nCommandType,const Reference<XConnection> & _rxConnection)374cdf0e10cSrcweir     PNameValidation  NameCheckFactory::createValidityCheck( const ::comphelper::ComponentContext& _rContext, sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection )
375cdf0e10cSrcweir     {
376cdf0e10cSrcweir         verifyCommandType( _nCommandType );
377cdf0e10cSrcweir 
378cdf0e10cSrcweir         Reference< XDatabaseMetaData > xMeta;
379cdf0e10cSrcweir         try
380cdf0e10cSrcweir         {
381cdf0e10cSrcweir             xMeta.set( _rxConnection->getMetaData(), UNO_QUERY_THROW );
382cdf0e10cSrcweir         }
383cdf0e10cSrcweir         catch( const Exception& )
384cdf0e10cSrcweir         {
385cdf0e10cSrcweir         	throw IllegalArgumentException(
386cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The connection could not provide its database's meta data." ) ),
387cdf0e10cSrcweir                 NULL,
388cdf0e10cSrcweir                 0
389cdf0e10cSrcweir             );
390cdf0e10cSrcweir         }
391cdf0e10cSrcweir 
392cdf0e10cSrcweir         if ( _nCommandType == CommandType::TABLE )
393cdf0e10cSrcweir             return PNameValidation( new TableValidityCheck( _rContext, _rxConnection ) );
394cdf0e10cSrcweir         return PNameValidation( new QueryValidityCheck( _rContext, _rxConnection ) );
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 	//====================================================================
398cdf0e10cSrcweir 	//= ObjectNames_Impl
399cdf0e10cSrcweir 	//====================================================================
400cdf0e10cSrcweir     struct ObjectNames_Impl
401cdf0e10cSrcweir     {
402cdf0e10cSrcweir         SdbtClient  m_aModuleClient;    // keep the module alive as long as this instance lives
403cdf0e10cSrcweir     };
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 	//====================================================================
406cdf0e10cSrcweir 	//= ObjectNames
407cdf0e10cSrcweir 	//====================================================================
408cdf0e10cSrcweir 	//--------------------------------------------------------------------
ObjectNames(const::comphelper::ComponentContext & _rContext,const Reference<XConnection> & _rxConnection)409cdf0e10cSrcweir     ObjectNames::ObjectNames( const ::comphelper::ComponentContext& _rContext, const Reference< XConnection >& _rxConnection )
410cdf0e10cSrcweir         :ConnectionDependentComponent( _rContext )
411cdf0e10cSrcweir         ,m_pImpl( new ObjectNames_Impl )
412cdf0e10cSrcweir     {
413cdf0e10cSrcweir         if ( !_rxConnection.is() )
414cdf0e10cSrcweir             throw NullPointerException();
415cdf0e10cSrcweir         setWeakConnection( _rxConnection );
416cdf0e10cSrcweir     }
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 	//--------------------------------------------------------------------
~ObjectNames()419cdf0e10cSrcweir     ObjectNames::~ObjectNames()
420cdf0e10cSrcweir     {
421cdf0e10cSrcweir     }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     //--------------------------------------------------------------------
suggestName(::sal_Int32 _CommandType,const::rtl::OUString & _BaseName)424cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ObjectNames::suggestName( ::sal_Int32 _CommandType, const ::rtl::OUString& _BaseName ) throw (IllegalArgumentException, RuntimeException)
425cdf0e10cSrcweir     {
426cdf0e10cSrcweir         EntryGuard aGuard( *this );
427cdf0e10cSrcweir 
428cdf0e10cSrcweir         PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( getContext(), _CommandType, getConnection() ) );
429cdf0e10cSrcweir 
430cdf0e10cSrcweir         String sBaseName( _BaseName );
431cdf0e10cSrcweir         if ( sBaseName.Len() == 0 )
432cdf0e10cSrcweir         {
433cdf0e10cSrcweir             if ( _CommandType == CommandType::TABLE )
434cdf0e10cSrcweir                 sBaseName = String( SdbtRes( STR_BASENAME_TABLE ) );
435cdf0e10cSrcweir             else
436cdf0e10cSrcweir                 sBaseName = String( SdbtRes( STR_BASENAME_QUERY ) );
437cdf0e10cSrcweir         }
438cdf0e10cSrcweir 
439cdf0e10cSrcweir         ::rtl::OUString sName( sBaseName );
440cdf0e10cSrcweir         sal_Int32 i = 1;
441cdf0e10cSrcweir         while ( !pNameCheck->validateName( sName ) )
442cdf0e10cSrcweir         {
443cdf0e10cSrcweir             ::rtl::OUStringBuffer aNameBuffer;
444cdf0e10cSrcweir             aNameBuffer.append( sBaseName );
445cdf0e10cSrcweir             aNameBuffer.appendAscii( " " );
446cdf0e10cSrcweir             aNameBuffer.append( (sal_Int32)++i );
447cdf0e10cSrcweir             sName = aNameBuffer.makeStringAndClear();
448cdf0e10cSrcweir         }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir         return sName;
451cdf0e10cSrcweir     }
452cdf0e10cSrcweir 
453cdf0e10cSrcweir     //--------------------------------------------------------------------
convertToSQLName(const::rtl::OUString & Name)454cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ObjectNames::convertToSQLName( const ::rtl::OUString& Name ) throw (RuntimeException)
455cdf0e10cSrcweir     {
456cdf0e10cSrcweir         EntryGuard aGuard( *this );
457cdf0e10cSrcweir         Reference< XDatabaseMetaData > xMeta( getConnection()->getMetaData(), UNO_QUERY_THROW );
458cdf0e10cSrcweir         return ::dbtools::convertName2SQLName( Name, xMeta->getExtraNameCharacters() );
459cdf0e10cSrcweir     }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir     //--------------------------------------------------------------------
isNameUsed(::sal_Int32 _CommandType,const::rtl::OUString & _Name)462cdf0e10cSrcweir     ::sal_Bool SAL_CALL ObjectNames::isNameUsed( ::sal_Int32 _CommandType, const ::rtl::OUString& _Name ) throw (IllegalArgumentException, RuntimeException)
463cdf0e10cSrcweir     {
464cdf0e10cSrcweir         EntryGuard aGuard( *this );
465cdf0e10cSrcweir 
466cdf0e10cSrcweir         PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( getContext(), _CommandType, getConnection()) );
467cdf0e10cSrcweir         return !pNameCheck->validateName( _Name );
468cdf0e10cSrcweir     }
469cdf0e10cSrcweir 
470cdf0e10cSrcweir     //--------------------------------------------------------------------
isNameValid(::sal_Int32 _CommandType,const::rtl::OUString & _Name)471cdf0e10cSrcweir     ::sal_Bool SAL_CALL ObjectNames::isNameValid( ::sal_Int32 _CommandType, const ::rtl::OUString& _Name ) throw (IllegalArgumentException, RuntimeException)
472cdf0e10cSrcweir     {
473cdf0e10cSrcweir         EntryGuard aGuard( *this );
474cdf0e10cSrcweir 
475cdf0e10cSrcweir         PNameValidation pNameCheck( NameCheckFactory::createValidityCheck( getContext(), _CommandType, getConnection()) );
476cdf0e10cSrcweir         return pNameCheck->validateName( _Name );
477cdf0e10cSrcweir     }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir     //--------------------------------------------------------------------
checkNameForCreate(::sal_Int32 _CommandType,const::rtl::OUString & _Name)480cdf0e10cSrcweir     void SAL_CALL ObjectNames::checkNameForCreate( ::sal_Int32 _CommandType, const ::rtl::OUString& _Name ) throw (SQLException, RuntimeException)
481cdf0e10cSrcweir     {
482cdf0e10cSrcweir         EntryGuard aGuard( *this );
483cdf0e10cSrcweir 
484cdf0e10cSrcweir         PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( getContext(), _CommandType, getConnection() ) );
485cdf0e10cSrcweir         pNameCheck->validateName_throw( _Name );
486cdf0e10cSrcweir 
487cdf0e10cSrcweir         pNameCheck = NameCheckFactory::createValidityCheck( getContext(), _CommandType, getConnection() );
488cdf0e10cSrcweir         pNameCheck->validateName_throw( _Name );
489cdf0e10cSrcweir     }
490cdf0e10cSrcweir 
491cdf0e10cSrcweir //........................................................................
492cdf0e10cSrcweir } // namespace sdbtools
493cdf0e10cSrcweir //........................................................................
494cdf0e10cSrcweir 
495