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