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