Tag: cws_src680_qiq User: fs Date: 06/06/01 02:52:24 Modified: /dba/dbaccess/source/sdbtools/connection/ objectnames.cxx, objectnames.hxx
Log: #i51143# extension of XObjectNames File Changes: Directory: /dba/dbaccess/source/sdbtools/connection/ ==================================================== File [changed]: objectnames.cxx Url: http://dba.openoffice.org/source/browse/dba/dbaccess/source/sdbtools/connection/objectnames.cxx?r1=1.1.2.2&r2=1.1.2.3 Delta lines: +137 -30 ---------------------- --- objectnames.cxx 30 May 2006 08:24:11 -0000 1.1.2.2 +++ objectnames.cxx 1 Jun 2006 09:52:21 -0000 1.1.2.3 @@ -4,9 +4,9 @@ * * $RCSfile: objectnames.cxx,v $ * - * $Revision: 1.1.2.2 $ + * $Revision: 1.1.2.3 $ * - * last change: $Author: fs $ $Date: 2006/05/30 08:24:11 $ + * last change: $Author: fs $ $Date: 2006/06/01 09:52:21 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -108,35 +108,72 @@ class INameCheck { public: - virtual bool isNameUsed( const ::rtl::OUString& _rName ) = 0; + virtual bool checkName( const ::rtl::OUString& _rName ) = 0; virtual ~INameCheck() { } }; typedef ::boost::shared_ptr< INameCheck > PNameCheck; //==================================================================== - //= PlainNameCheck + //= PlainExistenceCheck //==================================================================== - class PlainNameCheck : public INameCheck + class PlainExistenceCheck : public INameCheck { private: Reference< XNameAccess > m_xContainer; public: - PlainNameCheck( const Reference< XNameAccess >& _rxContainer ) + PlainExistenceCheck( const Reference< XNameAccess >& _rxContainer ) :m_xContainer( _rxContainer ) { - OSL_ENSURE( m_xContainer.is(), "PlainNameCheck::PlainNameCheck: this will crash!" ); + OSL_ENSURE( m_xContainer.is(), "PlainExistenceCheck::PlainExistenceCheck: this will crash!" ); } // INameCheck - virtual bool isNameUsed( const ::rtl::OUString& _rName ) + virtual bool checkName( const ::rtl::OUString& _rName ) { return m_xContainer->hasByName( _rName ); } }; //==================================================================== + //= TableValidityCheck + //==================================================================== + class TableValidityCheck : public INameCheck + { + Reference< XDatabaseMetaData > m_xMeta; + public: + TableValidityCheck( const Reference< XDatabaseMetaData >& _rxMeta ) + :m_xMeta( _rxMeta ) + { + } + + virtual bool checkName( const ::rtl::OUString& _rName ) + { + return ::dbtools::isValidSQLName( _rName, m_xMeta->getExtraNameCharacters() ); + } + }; + + //==================================================================== + //= QueryValidityCheck + //==================================================================== + class QueryValidityCheck : public INameCheck + { + public: + virtual bool checkName( const ::rtl::OUString& _rName ) + { + if ( ( _rName.indexOf( (sal_Unicode)34 ) >= 0 ) // " + || ( _rName.indexOf( (sal_Unicode)39 ) >= 0 ) // ' + || ( _rName.indexOf( (sal_Unicode)96 ) >= 0 ) // ` + || ( _rName.indexOf( (sal_Unicode)145 ) >= 0 ) // + || ( _rName.indexOf( (sal_Unicode)146 ) >= 0 ) // + || ( _rName.indexOf( (sal_Unicode)180 ) >= 0 ) // ´ + ) + return false; + } + }; + + //==================================================================== //= CombinedNameCheck //==================================================================== class CombinedNameCheck : public INameCheck @@ -154,9 +191,9 @@ } // INameCheck - virtual bool isNameUsed( const ::rtl::OUString& _rName ) + virtual bool checkName( const ::rtl::OUString& _rName ) { - return m_pPrimary->isNameUsed( _rName ) || m_pSecondary->isNameUsed( _rName ); + return m_pPrimary->checkName( _rName ) || m_pSecondary->checkName( _rName ); } }; @@ -166,7 +203,23 @@ class NameCheckFactory { public: - /** creates an INameCheck instance which can be used to check the validity of query or table names + /** creates an INameCheck instance which can be used to check the existence of query or table names + + @param _nCommandType + the type of objects (CommandType::TABLE or CommandType::QUERY) of which names shall be checked for existence + + @param _rxConnection + the connection relative to which the names are to be checked. Must be an SDB-level connection + + @throws IllegalArgumentException + if the given connection is no SDB-level connection + + @throws IllegalArgumentException + if the given command type is neither CommandType::TABLE or CommandType::QUERY + */ + static PNameCheck createExistenceCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ); + + /** creates an INameCheck instance which can be used to check the validity of a query or table name @param _nCommandType the type of objects (CommandType::TABLE or CommandType::QUERY) of which names shall be validated @@ -180,14 +233,17 @@ @throws IllegalArgumentException if the given command type is neither CommandType::TABLE or CommandType::QUERY */ - static PNameCheck createNameCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ); + static PNameCheck createValidityCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ); private: NameCheckFactory(); // never implemented + + private: + static void verifyCommandType( sal_Int32 _nCommandType ); }; //-------------------------------------------------------------------- - PNameCheck NameCheckFactory::createNameCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ) + void NameCheckFactory::verifyCommandType( sal_Int32 _nCommandType ) { if ( ( _nCommandType != CommandType::TABLE ) && ( _nCommandType != CommandType::QUERY ) @@ -198,6 +254,12 @@ 0 ); // TODO: resource + } + + //-------------------------------------------------------------------- + PNameCheck NameCheckFactory::createExistenceCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ) + { + verifyCommandType( _nCommandType ); ::dbtools::DatabaseMetaData aMeta( _rxConnection ); @@ -219,8 +281,8 @@ // TODO: resource } - PNameCheck pTableCheck( new PlainNameCheck( xTables ) ); - PNameCheck pQueryCheck( new PlainNameCheck( xQueries ) ); + PNameCheck pTableCheck( new PlainExistenceCheck( xTables ) ); + PNameCheck pQueryCheck( new PlainExistenceCheck( xQueries ) ); PNameCheck pReturn; if ( aMeta.supportsSubqueriesInFrom() ) @@ -232,6 +294,31 @@ return pReturn; } + //-------------------------------------------------------------------- + PNameCheck NameCheckFactory::createValidityCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ) + { + verifyCommandType( _nCommandType ); + + Reference< XDatabaseMetaData > xMeta; + try + { + xMeta.set( _rxConnection->getMetaData(), UNO_QUERY_THROW ); + } + catch( const Exception& ) + { + throw IllegalArgumentException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The connection could not provide its database's meta data." ) ), + NULL, + 0 + ); + // TODO: resource + } + + if ( _nCommandType == CommandType::TABLE ) + return PNameCheck( new TableValidityCheck( xMeta ) ); + return PNameCheck( new QueryValidityCheck ); + } + //==================================================================== //= ObjectNames_Impl //==================================================================== @@ -262,7 +349,7 @@ { EntryGuard aGuard( *this ); - PNameCheck pNameCheck( NameCheckFactory::createNameCheck( _CommandType, getConnection()) ); + PNameCheck pNameCheck( NameCheckFactory::createExistenceCheck( _CommandType, getConnection()) ); String sBaseName( _BaseName ); if ( sBaseName.Len() == 0 ) @@ -273,7 +360,7 @@ ::rtl::OUString sName( sBaseName ); sal_Int32 i = 1; - while ( pNameCheck->isNameUsed( sName ) ) + while ( pNameCheck->checkName( sName ) ) { ::rtl::OUStringBuffer aNameBuffer; aNameBuffer.append( sBaseName ); @@ -298,25 +385,45 @@ { EntryGuard aGuard( *this ); - PNameCheck pNameCheck( NameCheckFactory::createNameCheck( _CommandType, getConnection()) ); - return pNameCheck->isNameUsed( _Name ); + PNameCheck pNameCheck( NameCheckFactory::createExistenceCheck( _CommandType, getConnection()) ); + return pNameCheck->checkName( _Name ); } //-------------------------------------------------------------------- - void SAL_CALL ObjectNames::checkNameIsUsed( ::sal_Int32 _CommandType, const ::rtl::OUString& _Name ) throw (SQLException, RuntimeException) + ::sal_Bool SAL_CALL ObjectNames::isNameValid( ::sal_Int32 _CommandType, const ::rtl::OUString& _Name ) throw (IllegalArgumentException, RuntimeException) { EntryGuard aGuard( *this ); - PNameCheck pNameCheck( NameCheckFactory::createNameCheck( _CommandType, getConnection()) ); - if ( !pNameCheck->isNameUsed( _Name ) ) - return; + PNameCheck pNameCheck( NameCheckFactory::createValidityCheck( _CommandType, getConnection()) ); + return pNameCheck->checkName( _Name ); + } + + //-------------------------------------------------------------------- + void SAL_CALL ObjectNames::checkNameForCreate( ::sal_Int32 _CommandType, const ::rtl::OUString& _Name ) throw (SQLException, RuntimeException) + { + EntryGuard aGuard( *this ); + PNameCheck pNameCheck( NameCheckFactory::createExistenceCheck( _CommandType, getConnection() ) ); + if ( pNameCheck->checkName( _Name ) ) + { String sNameIsUsed( SdbtRes( STR_NAME_ALREADY_USED_IN_DB ) ); sNameIsUsed.SearchAndReplaceAllAscii( "$name$", _Name ); String sNeedDistinctNames( SdbtRes( STR_QUERY_AND_TABLE_DISTINCT_NAMES ) ); - throw SQLException( - sNameIsUsed, NULL, ::rtl::OUString(), 0, makeAny( SQLException( - sNeedDistinctNames, NULL, ::rtl::OUString(), 0, Any() ) ) ); + + Any aDetails; + ::dbtools::DatabaseMetaData aMeta( getConnection() ); + if ( aMeta.supportsSubqueriesInFrom() ) + aDetails <<= SQLException( sNeedDistinctNames, getConnection(), ::rtl::OUString(), 0, Any() ); + + throw SQLException( sNameIsUsed, getConnection(), ::rtl::OUString(), 0, aDetails ); + } + + pNameCheck = NameCheckFactory::createValidityCheck( _CommandType, getConnection() ); + if ( !pNameCheck->checkName( _Name ) ) + { + String sError( SdbtRes( STR_NO_QUOTES_IN_QUERY_NAMES ) ); + throw SQLException( sError, getConnection(), ::rtl::OUString(), 0, Any() ); + } } //........................................................................ File [changed]: objectnames.hxx Url: http://dba.openoffice.org/source/browse/dba/dbaccess/source/sdbtools/connection/objectnames.hxx?r1=1.1.2.2&r2=1.1.2.3 Delta lines: +4 -3 ------------------- --- objectnames.hxx 30 May 2006 08:24:11 -0000 1.1.2.2 +++ objectnames.hxx 1 Jun 2006 09:52:21 -0000 1.1.2.3 @@ -4,9 +4,9 @@ * * $RCSfile: objectnames.hxx,v $ * - * $Revision: 1.1.2.2 $ + * $Revision: 1.1.2.3 $ * - * last change: $Author: fs $ $Date: 2006/05/30 08:24:11 $ + * last change: $Author: fs $ $Date: 2006/06/01 09:52:21 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -84,7 +84,8 @@ virtual ::rtl::OUString SAL_CALL suggestName( ::sal_Int32 CommandType, const ::rtl::OUString& BaseName ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException); virtual ::rtl::OUString SAL_CALL convertToSQLName( const ::rtl::OUString& Name ) throw (::com::sun::star::uno::RuntimeException); virtual ::sal_Bool SAL_CALL isNameUsed( ::sal_Int32 CommandType, const ::rtl::OUString& Name ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException); - virtual void SAL_CALL checkNameIsUsed( ::sal_Int32 CommandType, const ::rtl::OUString& Name ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); + virtual ::sal_Bool SAL_CALL isNameValid( ::sal_Int32 CommandType, const ::rtl::OUString& Name ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL checkNameForCreate( ::sal_Int32 CommandType, const ::rtl::OUString& Name ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); protected: virtual ~ObjectNames(); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
