21#include <core_resource.hxx>
25#include <com/sun/star/sdb/CommandType.hpp>
26#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
27#include <com/sun/star/sdb/XQueriesSupplier.hpp>
28#include <com/sun/star/sdb/ErrorCondition.hpp>
33#include <osl/diagnose.h>
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::sdbc::XConnection;
43 using ::com::sun::star::lang::IllegalArgumentException;
44 using ::com::sun::star::sdbc::SQLException;
45 using ::com::sun::star::sdbc::XDatabaseMetaData;
46 using ::com::sun::star::container::XNameAccess;
47 using ::com::sun::star::uno::UNO_QUERY_THROW;
48 using ::com::sun::star::sdbcx::XTablesSupplier;
49 using ::com::sun::star::sdb::XQueriesSupplier;
50 using ::com::sun::star::uno::Exception;
51 using ::com::sun::star::uno::Any;
52 using ::com::sun::star::uno::XComponentContext;
54 namespace CommandType = ::com::sun::star::sdb::CommandType;
63 virtual bool validateName(
const OUString& _rName ) = 0;
64 virtual void validateName_throw(
const OUString& _rName ) = 0;
66 virtual ~INameValidation() { }
76 class PlainExistenceCheck :
public INameValidation
83 PlainExistenceCheck(
const Reference< XConnection >& _rxConnection,
const Reference< XNameAccess >& _rxContainer )
87 OSL_ENSURE(
m_xContainer.is(),
"PlainExistenceCheck::PlainExistenceCheck: this will crash!" );
91 virtual bool validateName(
const OUString& _rName )
override
96 virtual void validateName_throw(
const OUString& _rName )
override
98 if ( validateName( _rName ) )
105 if ( aMeta.supportsSubqueriesInFrom() )
107 OUString sNeedDistinctNames(
DBA_RES( STR_QUERY_AND_TABLE_DISTINCT_NAMES ) );
108 aError.NextException <<= SQLException( sNeedDistinctNames,
m_xConnection, OUString(), 0,
Any() );
116 class TableValidityCheck :
public INameValidation
121 TableValidityCheck(
const Reference< XConnection >& _rxConnection )
126 virtual bool validateName(
const OUString& _rName )
override
129 if ( !aMeta.restrictIdentifiersToSQL92() )
133 ::dbtools::qualifiedNameComponents(
136 OUString sExtraNameCharacters(
m_xConnection->getMetaData()->getExtraNameCharacters() );
137 return !( ( !
sCatalog.isEmpty() && !::dbtools::isValidSQLName(
sCatalog, sExtraNameCharacters ) )
138 || ( !
sSchema.isEmpty() && !::dbtools::isValidSQLName(
sSchema, sExtraNameCharacters ) )
139 || ( !
sName.isEmpty() && !::dbtools::isValidSQLName(
sName, sExtraNameCharacters ) ));
142 virtual void validateName_throw(
const OUString& _rName )
override
144 if ( validateName( _rName ) )
153 class QueryValidityCheck :
public INameValidation
158 QueryValidityCheck(
const Reference< XConnection >& _rxConnection )
163 static ::connectivity::ErrorCondition validateName_getErrorCondition( std::u16string_view _rName )
165 if ( ( _rName.find( u
'"' ) != std::u16string_view::npos )
166 || ( _rName.find( u
'\'' ) != std::u16string_view::npos )
167 || ( _rName.find( u
'`' ) != std::u16string_view::npos )
168 || ( _rName.find( u
'\x0091' ) != std::u16string_view::npos )
169 || ( _rName.find( u
'\x0092' ) != std::u16string_view::npos )
170 || ( _rName.find( u
'\x00B4' ) != std::u16string_view::npos )
172 return ErrorCondition::DB_QUERY_NAME_WITH_QUOTES;
174 if ( _rName.find(
'/') != std::u16string_view::npos )
175 return ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES;
180 virtual bool validateName(
const OUString& _rName )
override
182 return validateName_getErrorCondition( _rName ) == 0;
185 virtual void validateName_throw(
const OUString& _rName )
override
188 if ( nErrorCondition != 0 )
197 class CombinedNameCheck :
public INameValidation
212 virtual bool validateName(
const OUString& _rName )
override
217 virtual void validateName_throw(
const OUString& _rName )
override
225 class NameCheckFactory
228 NameCheckFactory(
const NameCheckFactory&) =
delete;
229 const NameCheckFactory& operator=(
const NameCheckFactory&) =
delete;
245 sal_Int32 _nCommandType,
246 const Reference< XConnection >& _rxConnection
264 const sal_Int32 _nCommandType,
265 const Reference< XConnection >& _rxConnection
269 static void verifyCommandType( sal_Int32 _nCommandType );
274 void NameCheckFactory::verifyCommandType( sal_Int32 _nCommandType )
279 throw IllegalArgumentException(
280 DBA_RES( STR_INVALID_COMMAND_TYPE ),
286 PNameValidation NameCheckFactory::createExistenceCheck( sal_Int32 _nCommandType,
const Reference< XConnection >& _rxConnection )
288 verifyCommandType( _nCommandType );
292 Reference< XNameAccess > xTables, xQueries;
295 Reference< XTablesSupplier > xSuppTables( _rxConnection, UNO_QUERY_THROW );
296 Reference< XQueriesSupplier > xQueriesSupplier( _rxConnection, UNO_QUERY_THROW );
297 xTables.set( xSuppTables->getTables(), css::uno::UNO_SET_THROW );
298 xQueries.set( xQueriesSupplier->getQueries(), css::uno::UNO_SET_THROW );
300 catch(
const Exception& )
302 throw IllegalArgumentException(
303 DBA_RES( STR_CONN_WITHOUT_QUERIES_OR_TABLES ),
309 PNameValidation pTableCheck = std::make_shared<PlainExistenceCheck>( _rxConnection, xTables );
310 PNameValidation pQueryCheck = std::make_shared<PlainExistenceCheck>( _rxConnection, xQueries );
313 if ( aMeta.supportsSubqueriesInFrom() )
314 pReturn = std::make_shared<CombinedNameCheck>( pTableCheck, pQueryCheck );
316 pReturn = pTableCheck;
318 pReturn = pQueryCheck;
322 PNameValidation NameCheckFactory::createValidityCheck( sal_Int32 _nCommandType,
const Reference< XConnection >& _rxConnection )
324 verifyCommandType( _nCommandType );
326 Reference< XDatabaseMetaData > xMeta;
329 xMeta.set( _rxConnection->getMetaData(), css::uno::UNO_SET_THROW );
331 catch(
const Exception& )
333 throw IllegalArgumentException(
334 "The connection could not provide its database's meta data.",
341 return std::make_shared<TableValidityCheck>( _rxConnection );
342 return std::make_shared<QueryValidityCheck>( _rxConnection );
346 ObjectNames::ObjectNames(
const Reference<XComponentContext>& _rContext,
const Reference< XConnection >& _rxConnection )
347 :ConnectionDependentComponent( _rContext )
349 setWeakConnection( _rxConnection );
352 ObjectNames::~ObjectNames()
356 OUString SAL_CALL ObjectNames::suggestName( ::sal_Int32 CommandType,
const OUString& BaseName )
362 OUString sBaseName( BaseName );
363 if ( sBaseName.isEmpty() )
366 sBaseName =
DBA_RES(STR_BASENAME_TABLE);
368 sBaseName =
DBA_RES(STR_BASENAME_QUERY);
372 sBaseName=sBaseName.replace(
'/',
'_');
375 OUString
sName( sBaseName );
377 while ( !pNameCheck->validateName(
sName ) )
379 sName = sBaseName +
" " + OUString::number(++
i);
385 OUString SAL_CALL ObjectNames::convertToSQLName(
const OUString& Name )
388 Reference< XDatabaseMetaData > xMeta(
getConnection()->getMetaData(), css::uno::UNO_SET_THROW );
389 return ::dbtools::convertName2SQLName(
Name, xMeta->getExtraNameCharacters() );
392 sal_Bool SAL_CALL ObjectNames::isNameUsed( ::sal_Int32 CommandType,
const OUString& Name )
397 return !pNameCheck->validateName(
Name );
400 sal_Bool SAL_CALL ObjectNames::isNameValid( ::sal_Int32 CommandType,
const OUString& Name )
405 return pNameCheck->validateName(
Name );
408 void SAL_CALL ObjectNames::checkNameForCreate( ::sal_Int32 CommandType,
const OUString& Name )
413 pNameCheck->validateName_throw(
Name );
416 pNameCheck->validateName_throw(
Name );
void raiseException(const ErrorCondition _eCondition, const css::uno::Reference< css::uno::XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1=std::nullopt, const std::optional< OUString > &_rParamValue2=std::nullopt, const std::optional< OUString > &_rParamValue3=std::nullopt) const
css::sdbc::SQLException getSQLException(const ErrorCondition _eCondition, const css::uno::Reference< css::uno::XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1=std::nullopt, const std::optional< OUString > &_rParamValue2=std::nullopt, const std::optional< OUString > &_rParamValue3=std::nullopt) const
a class for guarding methods of a connection-dependent component
::sal_Int32 ErrorCondition
PNameValidation m_pPrimary
PNameValidation m_pSecondary
Reference< XNameAccess > m_xContainer
Reference< XConnection > m_xConnection