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