diff options
Diffstat (limited to 'dbaccess/source/sdbtools/connection')
-rw-r--r-- | dbaccess/source/sdbtools/connection/connectiontools.cxx | 132 | ||||
-rw-r--r-- | dbaccess/source/sdbtools/connection/datasourcemetadata.cxx | 51 | ||||
-rw-r--r-- | dbaccess/source/sdbtools/connection/datasourcemetadata.hxx | 69 | ||||
-rw-r--r-- | dbaccess/source/sdbtools/connection/objectnames.cxx | 421 | ||||
-rw-r--r-- | dbaccess/source/sdbtools/connection/objectnames.hxx | 74 | ||||
-rw-r--r-- | dbaccess/source/sdbtools/connection/tablename.cxx | 220 | ||||
-rw-r--r-- | dbaccess/source/sdbtools/connection/tablename.hxx | 84 |
7 files changed, 1051 insertions, 0 deletions
diff --git a/dbaccess/source/sdbtools/connection/connectiontools.cxx b/dbaccess/source/sdbtools/connection/connectiontools.cxx new file mode 100644 index 0000000000..5dd408bac8 --- /dev/null +++ b/dbaccess/source/sdbtools/connection/connectiontools.cxx @@ -0,0 +1,132 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <connectiontools.hxx> +#include "tablename.hxx" +#include "objectnames.hxx" +#include "datasourcemetadata.hxx" + +#include <comphelper/namedvaluecollection.hxx> +#include <cppuhelper/supportsservice.hxx> +#include <connectivity/dbtools.hxx> +#include <connectivity/statementcomposer.hxx> + +namespace sdbtools +{ + + using namespace ::com::sun::star; + using namespace ::com::sun::star::uno; + using ::com::sun::star::uno::Reference; + using ::com::sun::star::sdb::tools::XTableName; + using ::com::sun::star::sdb::tools::XObjectNames; + using ::com::sun::star::sdb::tools::XDataSourceMetaData; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::Any; + using ::com::sun::star::sdbc::XConnection; + using ::com::sun::star::lang::IllegalArgumentException; + using ::com::sun::star::uno::XComponentContext; + + // ConnectionTools + ConnectionTools::ConnectionTools( const Reference<XComponentContext>& _rContext ) + :ConnectionDependentComponent( _rContext ) + { + } + + ConnectionTools::~ConnectionTools() + { + } + + Reference< XTableName > SAL_CALL ConnectionTools::createTableName() + { + EntryGuard aGuard( *this ); + return new TableName( getContext(), getConnection() ); + } + + Reference< XObjectNames > SAL_CALL ConnectionTools::getObjectNames() + { + EntryGuard aGuard( *this ); + return new ObjectNames( getContext(), getConnection() ); + } + + Reference< XDataSourceMetaData > SAL_CALL ConnectionTools::getDataSourceMetaData() + { + EntryGuard aGuard( *this ); + return new DataSourceMetaData( getContext(), getConnection() ); + } + Reference< container::XNameAccess > SAL_CALL ConnectionTools::getFieldsByCommandDescriptor( ::sal_Int32 commandType, const OUString& command, Reference< lang::XComponent >& keepFieldsAlive ) + { + EntryGuard aGuard( *this ); + ::dbtools::SQLExceptionInfo aErrorInfo; + Reference< container::XNameAccess > xRet = ::dbtools::getFieldsByCommandDescriptor(getConnection(),commandType,command,keepFieldsAlive,&aErrorInfo); + if ( aErrorInfo.isValid() ) + aErrorInfo.doThrow(); + return xRet; + } + Reference< sdb::XSingleSelectQueryComposer > SAL_CALL ConnectionTools::getComposer( ::sal_Int32 commandType, const OUString& command ) + { + EntryGuard aGuard( *this ); + dbtools::StatementComposer aComposer(getConnection(), command, commandType, true ); + aComposer.setDisposeComposer(false); + return aComposer.getComposer(); + } + + OUString SAL_CALL ConnectionTools::getImplementationName() + { + return "com.sun.star.comp.dbaccess.ConnectionTools"; + } + + sal_Bool SAL_CALL ConnectionTools::supportsService(const OUString & ServiceName) + { + return cppu::supportsService(this, ServiceName); + } + + Sequence< OUString > SAL_CALL ConnectionTools::getSupportedServiceNames() + { + return { "com.sun.star.sdb.tools.ConnectionTools" }; + } + + void SAL_CALL ConnectionTools::initialize(const Sequence< Any > & _rArguments) + { + ::osl::MutexGuard aGuard( getMutex() ); + + Reference< XConnection > xConnection; + if (_rArguments.getLength()==1 && (_rArguments[0] >>= xConnection)) + { + } + else + { + ::comphelper::NamedValueCollection aArguments( _rArguments ); + aArguments.get( "Connection" ) >>= xConnection; + } + if ( !xConnection.is() ) + throw IllegalArgumentException(); + + setWeakConnection( xConnection ); + } + +} // namespace sdbtools + +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* +com_sun_star_comp_dbaccess_ConnectionTools_get_implementation( + css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& ) +{ + return cppu::acquire(new sdbtools::ConnectionTools(context)); +} +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/sdbtools/connection/datasourcemetadata.cxx b/dbaccess/source/sdbtools/connection/datasourcemetadata.cxx new file mode 100644 index 0000000000..a446885b8b --- /dev/null +++ b/dbaccess/source/sdbtools/connection/datasourcemetadata.cxx @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "datasourcemetadata.hxx" + +#include <connectivity/dbmetadata.hxx> + +namespace sdbtools +{ + + using ::com::sun::star::uno::Reference; + using ::com::sun::star::sdbc::XConnection; + using ::com::sun::star::uno::XComponentContext; + + // DataSourceMetaData + DataSourceMetaData::DataSourceMetaData( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection ) + :ConnectionDependentComponent( _rContext ) + { + setWeakConnection( _rxConnection ); + } + + DataSourceMetaData::~DataSourceMetaData() + { + } + + sal_Bool SAL_CALL DataSourceMetaData::supportsQueriesInFrom( ) + { + EntryGuard aGuard( *this ); + ::dbtools::DatabaseMetaData aMeta( getConnection() ); + return aMeta.supportsSubqueriesInFrom(); + } + +} // namespace sdbtools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/sdbtools/connection/datasourcemetadata.hxx b/dbaccess/source/sdbtools/connection/datasourcemetadata.hxx new file mode 100644 index 0000000000..232f97f601 --- /dev/null +++ b/dbaccess/source/sdbtools/connection/datasourcemetadata.hxx @@ -0,0 +1,69 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <connectiondependent.hxx> + +#include <com/sun/star/sdb/tools/XDataSourceMetaData.hpp> + +#include <cppuhelper/implbase.hxx> + +#include <memory> + +namespace sdbtools +{ + + // DataSourceMetaData + typedef ::cppu::WeakImplHelper< css::sdb::tools::XDataSourceMetaData + > DataSourceMetaData_Base; + /** default implementation for XDataSourceMetaData + */ + class DataSourceMetaData :public DataSourceMetaData_Base + ,public ConnectionDependentComponent + { + + public: + /** constructs the instance + @param _rContext + the component's context + @param _rxConnection + the connection to work with. Will be held weak. Must not be <NULL/>. + @throws css::lang::NullPointerException + if _rxConnection is <NULL/> + */ + DataSourceMetaData( + const css::uno::Reference< css::uno::XComponentContext >& _rContext, + const css::uno::Reference< css::sdbc::XConnection >& _rxConnection + ); + + // XDataSourceMetaData + virtual sal_Bool SAL_CALL supportsQueriesInFrom() override; + + protected: + virtual ~DataSourceMetaData() override; + + private: + DataSourceMetaData( const DataSourceMetaData& ) = delete; + DataSourceMetaData& operator=( const DataSourceMetaData& ) = delete; + }; + +} // namespace sdbtools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/sdbtools/connection/objectnames.cxx b/dbaccess/source/sdbtools/connection/objectnames.cxx new file mode 100644 index 0000000000..e36092e30b --- /dev/null +++ b/dbaccess/source/sdbtools/connection/objectnames.cxx @@ -0,0 +1,421 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "objectnames.hxx" +#include <core_resource.hxx> + +#include <strings.hrc> + +#include <com/sun/star/sdb/CommandType.hpp> +#include <com/sun/star/sdbcx/XTablesSupplier.hpp> +#include <com/sun/star/sdb/XQueriesSupplier.hpp> +#include <com/sun/star/sdb/ErrorCondition.hpp> + +#include <connectivity/dbmetadata.hxx> +#include <connectivity/dbtools.hxx> +#include <connectivity/sqlerror.hxx> +#include <osl/diagnose.h> + +#include <memory> +#include <utility> + +namespace sdbtools +{ + + using ::com::sun::star::uno::Reference; + using ::com::sun::star::sdbc::XConnection; + using ::com::sun::star::lang::IllegalArgumentException; + using ::com::sun::star::sdbc::SQLException; + using ::com::sun::star::sdbc::XDatabaseMetaData; + using ::com::sun::star::container::XNameAccess; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::sdbcx::XTablesSupplier; + using ::com::sun::star::sdb::XQueriesSupplier; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::XComponentContext; + + namespace CommandType = ::com::sun::star::sdb::CommandType; + namespace ErrorCondition = ::com::sun::star::sdb::ErrorCondition; + + namespace { + + // INameValidation + class INameValidation + { + public: + virtual bool validateName( const OUString& _rName ) = 0; + virtual void validateName_throw( const OUString& _rName ) = 0; + + virtual ~INameValidation() { } + }; + + } + + typedef std::shared_ptr< INameValidation > PNameValidation; + + namespace { + + // PlainExistenceCheck + class PlainExistenceCheck : public INameValidation + { + private: + Reference< XConnection > m_xConnection; + Reference< XNameAccess > m_xContainer; + + public: + PlainExistenceCheck( const Reference< XConnection >& _rxConnection, const Reference< XNameAccess >& _rxContainer ) + :m_xConnection( _rxConnection ) + ,m_xContainer( _rxContainer ) + { + OSL_ENSURE( m_xContainer.is(), "PlainExistenceCheck::PlainExistenceCheck: this will crash!" ); + } + + // INameValidation + virtual bool validateName( const OUString& _rName ) override + { + return !m_xContainer->hasByName( _rName ); + } + + virtual void validateName_throw( const OUString& _rName ) override + { + if ( validateName( _rName ) ) + return; + + ::connectivity::SQLError aErrors; + SQLException aError( aErrors.getSQLException( ErrorCondition::DB_OBJECT_NAME_IS_USED, m_xConnection, _rName ) ); + + ::dbtools::DatabaseMetaData aMeta( m_xConnection ); + if ( aMeta.supportsSubqueriesInFrom() ) + { + OUString sNeedDistinctNames( DBA_RES( STR_QUERY_AND_TABLE_DISTINCT_NAMES ) ); + aError.NextException <<= SQLException( sNeedDistinctNames, m_xConnection, OUString(), 0, Any() ); + } + + throw aError; + } + }; + + // TableValidityCheck + class TableValidityCheck : public INameValidation + { + const Reference< XConnection > m_xConnection; + + public: + TableValidityCheck( const Reference< XConnection >& _rxConnection ) + :m_xConnection( _rxConnection ) + { + } + + virtual bool validateName( const OUString& _rName ) override + { + ::dbtools::DatabaseMetaData aMeta( m_xConnection ); + if ( !aMeta.restrictIdentifiersToSQL92() ) + return true; + + OUString sCatalog, sSchema, sName; + ::dbtools::qualifiedNameComponents( + m_xConnection->getMetaData(), _rName, sCatalog, sSchema, sName, ::dbtools::EComposeRule::InTableDefinitions ); + + OUString sExtraNameCharacters( m_xConnection->getMetaData()->getExtraNameCharacters() ); + return !( ( !sCatalog.isEmpty() && !::dbtools::isValidSQLName( sCatalog, sExtraNameCharacters ) ) + || ( !sSchema.isEmpty() && !::dbtools::isValidSQLName( sSchema, sExtraNameCharacters ) ) + || ( !sName.isEmpty() && !::dbtools::isValidSQLName( sName, sExtraNameCharacters ) )); + } + + virtual void validateName_throw( const OUString& _rName ) override + { + if ( validateName( _rName ) ) + return; + + ::connectivity::SQLError aErrors; + aErrors.raiseException( ErrorCondition::DB_INVALID_SQL_NAME, m_xConnection, _rName ); + } + }; + + // QueryValidityCheck + class QueryValidityCheck : public INameValidation + { + const Reference< XConnection > m_xConnection; + + public: + QueryValidityCheck( const Reference< XConnection >& _rxConnection ) + :m_xConnection( _rxConnection ) + { + } + + static ::connectivity::ErrorCondition validateName_getErrorCondition( std::u16string_view _rName ) + { + if ( ( _rName.find( u'"' ) != std::u16string_view::npos ) + || ( _rName.find( u'\'' ) != std::u16string_view::npos ) + || ( _rName.find( u'`' ) != std::u16string_view::npos ) + || ( _rName.find( u'\x0091' ) != std::u16string_view::npos ) + || ( _rName.find( u'\x0092' ) != std::u16string_view::npos ) + || ( _rName.find( u'\x00B4' ) != std::u16string_view::npos ) // removed unparsable chars + ) + return ErrorCondition::DB_QUERY_NAME_WITH_QUOTES; + + if ( _rName.find( '/') != std::u16string_view::npos ) + return ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES; + + return 0; + } + + virtual bool validateName( const OUString& _rName ) override + { + return validateName_getErrorCondition( _rName ) == 0; + } + + virtual void validateName_throw( const OUString& _rName ) override + { + ::connectivity::ErrorCondition nErrorCondition = validateName_getErrorCondition( _rName ); + if ( nErrorCondition != 0 ) + { + ::connectivity::SQLError aErrors; + aErrors.raiseException( nErrorCondition, m_xConnection ); + } + } + }; + + // CombinedNameCheck + class CombinedNameCheck : public INameValidation + { + private: + PNameValidation m_pPrimary; + PNameValidation m_pSecondary; + + public: + CombinedNameCheck(PNameValidation _pPrimary, PNameValidation _pSecondary) + :m_pPrimary(std::move( _pPrimary )) + ,m_pSecondary(std::move( _pSecondary )) + { + OSL_ENSURE( m_pPrimary && m_pSecondary, "CombinedNameCheck::CombinedNameCheck: this will crash!" ); + } + + // INameValidation + virtual bool validateName( const OUString& _rName ) override + { + return m_pPrimary->validateName( _rName ) && m_pSecondary->validateName( _rName ); + } + + virtual void validateName_throw( const OUString& _rName ) override + { + m_pPrimary->validateName_throw( _rName ); + m_pSecondary->validateName_throw( _rName ); + } + }; + + // NameCheckFactory + class NameCheckFactory + { + public: + NameCheckFactory(const NameCheckFactory&) = delete; + const NameCheckFactory& operator=(const NameCheckFactory&) = delete; + /** creates an INameValidation 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 PNameValidation createExistenceCheck( + sal_Int32 _nCommandType, + const Reference< XConnection >& _rxConnection + ); + + /** creates an INameValidation 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 + + @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 PNameValidation createValidityCheck( + const sal_Int32 _nCommandType, + const Reference< XConnection >& _rxConnection + ); + + private: + static void verifyCommandType( sal_Int32 _nCommandType ); + }; + + } + + void NameCheckFactory::verifyCommandType( sal_Int32 _nCommandType ) + { + if ( ( _nCommandType != CommandType::TABLE ) + && ( _nCommandType != CommandType::QUERY ) + ) + throw IllegalArgumentException( + DBA_RES( STR_INVALID_COMMAND_TYPE ), + nullptr, + 0 + ); + } + + PNameValidation NameCheckFactory::createExistenceCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ) + { + verifyCommandType( _nCommandType ); + + ::dbtools::DatabaseMetaData aMeta( _rxConnection ); + + Reference< XNameAccess > xTables, xQueries; + try + { + Reference< XTablesSupplier > xSuppTables( _rxConnection, UNO_QUERY_THROW ); + Reference< XQueriesSupplier > xQueriesSupplier( _rxConnection, UNO_QUERY_THROW ); + xTables.set( xSuppTables->getTables(), css::uno::UNO_SET_THROW ); + xQueries.set( xQueriesSupplier->getQueries(), css::uno::UNO_SET_THROW ); + } + catch( const Exception& ) + { + throw IllegalArgumentException( + DBA_RES( STR_CONN_WITHOUT_QUERIES_OR_TABLES ), + nullptr, + 0 + ); + } + + PNameValidation pTableCheck = std::make_shared<PlainExistenceCheck>( _rxConnection, xTables ); + PNameValidation pQueryCheck = std::make_shared<PlainExistenceCheck>( _rxConnection, xQueries ); + PNameValidation pReturn; + + if ( aMeta.supportsSubqueriesInFrom() ) + pReturn = std::make_shared<CombinedNameCheck>( pTableCheck, pQueryCheck ); + else if ( _nCommandType == CommandType::TABLE ) + pReturn = pTableCheck; + else + pReturn = pQueryCheck; + return pReturn; + } + + PNameValidation NameCheckFactory::createValidityCheck( sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection ) + { + verifyCommandType( _nCommandType ); + + Reference< XDatabaseMetaData > xMeta; + try + { + xMeta.set( _rxConnection->getMetaData(), css::uno::UNO_SET_THROW ); + } + catch( const Exception& ) + { + throw IllegalArgumentException( + "The connection could not provide its database's meta data.", + nullptr, + 0 + ); + } + + if ( _nCommandType == CommandType::TABLE ) + return std::make_shared<TableValidityCheck>( _rxConnection ); + return std::make_shared<QueryValidityCheck>( _rxConnection ); + } + + // ObjectNames + ObjectNames::ObjectNames( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection ) + :ConnectionDependentComponent( _rContext ) + { + setWeakConnection( _rxConnection ); + } + + ObjectNames::~ObjectNames() + { + } + + OUString SAL_CALL ObjectNames::suggestName( ::sal_Int32 CommandType, const OUString& BaseName ) + { + EntryGuard aGuard( *this ); + + PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( CommandType, getConnection() ) ); + + OUString sBaseName( BaseName ); + if ( sBaseName.isEmpty() ) + { + if ( CommandType == CommandType::TABLE ) + sBaseName = DBA_RES(STR_BASENAME_TABLE); + else + sBaseName = DBA_RES(STR_BASENAME_QUERY); + } + else if( CommandType == CommandType::QUERY ) + { + sBaseName=sBaseName.replace('/', '_'); + } + + OUString sName( sBaseName ); + sal_Int32 i = 1; + while ( !pNameCheck->validateName( sName ) ) + { + sName = sBaseName + " " + OUString::number(++i); + } + + return sName; + } + + OUString SAL_CALL ObjectNames::convertToSQLName( const OUString& Name ) + { + EntryGuard aGuard( *this ); + Reference< XDatabaseMetaData > xMeta( getConnection()->getMetaData(), css::uno::UNO_SET_THROW ); + return ::dbtools::convertName2SQLName( Name, xMeta->getExtraNameCharacters() ); + } + + sal_Bool SAL_CALL ObjectNames::isNameUsed( ::sal_Int32 CommandType, const OUString& Name ) + { + EntryGuard aGuard( *this ); + + PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( CommandType, getConnection()) ); + return !pNameCheck->validateName( Name ); + } + + sal_Bool SAL_CALL ObjectNames::isNameValid( ::sal_Int32 CommandType, const OUString& Name ) + { + EntryGuard aGuard( *this ); + + PNameValidation pNameCheck( NameCheckFactory::createValidityCheck( CommandType, getConnection()) ); + return pNameCheck->validateName( Name ); + } + + void SAL_CALL ObjectNames::checkNameForCreate( ::sal_Int32 CommandType, const OUString& Name ) + { + EntryGuard aGuard( *this ); + + PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( CommandType, getConnection() ) ); + pNameCheck->validateName_throw( Name ); + + pNameCheck = NameCheckFactory::createValidityCheck( CommandType, getConnection() ); + pNameCheck->validateName_throw( Name ); + } + +} // namespace sdbtools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/sdbtools/connection/objectnames.hxx b/dbaccess/source/sdbtools/connection/objectnames.hxx new file mode 100644 index 0000000000..439db09671 --- /dev/null +++ b/dbaccess/source/sdbtools/connection/objectnames.hxx @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <connectiondependent.hxx> + +#include <com/sun/star/sdb/tools/XObjectNames.hpp> + +#include <cppuhelper/implbase.hxx> + +#include <memory> + +namespace sdbtools +{ + + // ObjectNames + typedef ::cppu::WeakImplHelper< css::sdb::tools::XObjectNames + > ObjectNames_Base; + /** default implementation for XObjectNames + */ + class ObjectNames :public ObjectNames_Base + ,public ConnectionDependentComponent + { + public: + /** constructs the instance + + @param _rContext + the component's context + @param _rxConnection + the connection to work with. Will be held weak. Must not be <NULL/>. + + @throws css::lang::NullPointerException + if _rxConnection is <NULL/> + */ + ObjectNames( + const css::uno::Reference< css::uno::XComponentContext >& _rContext, + const css::uno::Reference< css::sdbc::XConnection >& _rxConnection + ); + + // XObjectNames + virtual OUString SAL_CALL suggestName( ::sal_Int32 CommandType, const OUString& BaseName ) override; + virtual OUString SAL_CALL convertToSQLName( const OUString& Name ) override; + virtual sal_Bool SAL_CALL isNameUsed( ::sal_Int32 CommandType, const OUString& Name ) override; + virtual sal_Bool SAL_CALL isNameValid( ::sal_Int32 CommandType, const OUString& Name ) override; + virtual void SAL_CALL checkNameForCreate( ::sal_Int32 CommandType, const OUString& Name ) override; + + protected: + virtual ~ObjectNames() override; + + private: + ObjectNames( const ObjectNames& ) = delete; + ObjectNames& operator=( const ObjectNames& ) = delete; + }; + +} // namespace sdbtools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/sdbtools/connection/tablename.cxx b/dbaccess/source/sdbtools/connection/tablename.cxx new file mode 100644 index 0000000000..f6a77dd3e9 --- /dev/null +++ b/dbaccess/source/sdbtools/connection/tablename.cxx @@ -0,0 +1,220 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "tablename.hxx" +#include <core_resource.hxx> +#include <strings.hrc> +#include <strings.hxx> + +#include <com/sun/star/sdb/tools/CompositionType.hpp> +#include <com/sun/star/sdbcx/XTablesSupplier.hpp> + +#include <connectivity/dbtools.hxx> +#include <comphelper/diagnose_ex.hxx> + +namespace sdbtools +{ + + using ::com::sun::star::uno::Reference; + using ::com::sun::star::sdbc::XConnection; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::lang::IllegalArgumentException; + using ::com::sun::star::beans::XPropertySet; + using ::com::sun::star::container::NoSuchElementException; + using ::com::sun::star::sdbcx::XTablesSupplier; + using ::com::sun::star::container::XNameAccess; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::lang::WrappedTargetException; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::beans::XPropertySetInfo; + using ::com::sun::star::uno::XComponentContext; + + namespace CompositionType = ::com::sun::star::sdb::tools::CompositionType; + + using namespace ::dbtools; + + // TableName + TableName::TableName( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection ) + :ConnectionDependentComponent( _rContext ) + { + setWeakConnection( _rxConnection ); + } + + TableName::~TableName() + { + } + + OUString SAL_CALL TableName::getCatalogName() + { + EntryGuard aGuard( *this ); + return msCatalog; + } + + void SAL_CALL TableName::setCatalogName( const OUString& _catalogName ) + { + EntryGuard aGuard( *this ); + msCatalog = _catalogName; + } + + OUString SAL_CALL TableName::getSchemaName() + { + EntryGuard aGuard( *this ); + return msSchema; + } + + void SAL_CALL TableName::setSchemaName( const OUString& _schemaName ) + { + EntryGuard aGuard( *this ); + msSchema = _schemaName; + } + + OUString SAL_CALL TableName::getTableName() + { + EntryGuard aGuard( *this ); + return msName; + } + + void SAL_CALL TableName::setTableName( const OUString& _tableName ) + { + EntryGuard aGuard( *this ); + msName = _tableName; + } + + OUString SAL_CALL TableName::getNameForSelect() + { + EntryGuard aGuard( *this ); + return composeTableNameForSelect( getConnection(), msCatalog, msSchema, msName ); + } + + Reference< XPropertySet > SAL_CALL TableName::getTable() + { + EntryGuard aGuard( *this ); + + Reference< XTablesSupplier > xSuppTables( getConnection(), UNO_QUERY_THROW ); + Reference< XNameAccess > xTables( xSuppTables->getTables(), css::uno::UNO_SET_THROW ); + + Reference< XPropertySet > xTable; + try + { + xTable.set( xTables->getByName( getComposedName( CompositionType::Complete, false ) ), UNO_QUERY_THROW ); + } + catch( const WrappedTargetException& ) + { + throw NoSuchElementException(); + } + catch( const RuntimeException& ) { throw; } + catch( const NoSuchElementException& ) { throw; } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("dbaccess"); + throw NoSuchElementException(); + } + + return xTable; + } + + void SAL_CALL TableName::setTable( const Reference< XPropertySet >& _table ) + { + EntryGuard aGuard( *this ); + + Reference< XPropertySetInfo > xPSI( _table, UNO_QUERY ); + if ( !xPSI.is() + || !xPSI->hasPropertyByName( PROPERTY_CATALOGNAME ) + || !xPSI->hasPropertyByName( PROPERTY_SCHEMANAME ) + || !xPSI->hasPropertyByName( PROPERTY_NAME ) + ) + throw IllegalArgumentException( + DBA_RES( STR_NO_TABLE_OBJECT ), + *this, + 0 + ); + + try + { + OSL_VERIFY( _table->getPropertyValue( PROPERTY_CATALOGNAME ) >>= msCatalog ); + OSL_VERIFY( _table->getPropertyValue( PROPERTY_SCHEMANAME ) >>= msSchema ); + OSL_VERIFY( _table->getPropertyValue( PROPERTY_NAME ) >>= msName ); + } + catch( const RuntimeException& ) { throw; } + catch( const Exception& e ) + { + throw IllegalArgumentException( e.Message, e.Context, 0 ); + } + } + + namespace + { + /** translates a CompositionType into an EComposeRule + @throws IllegalArgumentException + if the given value does not denote a valid CompositionType + */ + EComposeRule lcl_translateCompositionType_throw( sal_Int32 _nType ) + { + static const struct + { + sal_Int32 nCompositionType; + EComposeRule eComposeRule; + } TypeTable[] = + { + { CompositionType::ForTableDefinitions, EComposeRule::InTableDefinitions }, + { CompositionType::ForIndexDefinitions, EComposeRule::InIndexDefinitions }, + { CompositionType::ForDataManipulation, EComposeRule::InDataManipulation }, + { CompositionType::ForProcedureCalls, EComposeRule::InProcedureCalls }, + { CompositionType::ForPrivilegeDefinitions, EComposeRule::InPrivilegeDefinitions }, + { CompositionType::Complete, EComposeRule::Complete } + }; + + auto const found = std::find_if(std::begin(TypeTable), std::end(TypeTable) + , [_nType](auto const & type){ return type.nCompositionType == _nType; }); + if (found == std::end(TypeTable)) + throw IllegalArgumentException( + DBA_RES( STR_INVALID_COMPOSITION_TYPE ), + nullptr, + 0 + ); + + return found->eComposeRule; + } + } + + OUString SAL_CALL TableName::getComposedName( ::sal_Int32 Type, sal_Bool Quote ) + { + EntryGuard aGuard( *this ); + + return composeTableName( + getConnection()->getMetaData(), + msCatalog, msSchema, msName, Quote, + lcl_translateCompositionType_throw( Type ) ); + } + + void SAL_CALL TableName::setComposedName( const OUString& ComposedName, ::sal_Int32 Type ) + { + EntryGuard aGuard( *this ); + + qualifiedNameComponents( + getConnection()->getMetaData(), + ComposedName, + msCatalog, msSchema, msName, + lcl_translateCompositionType_throw( Type ) ); + } + +} // namespace sdbtools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dbaccess/source/sdbtools/connection/tablename.hxx b/dbaccess/source/sdbtools/connection/tablename.hxx new file mode 100644 index 0000000000..74abc3025f --- /dev/null +++ b/dbaccess/source/sdbtools/connection/tablename.hxx @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <connectiondependent.hxx> + +#include <com/sun/star/sdb/tools/XTableName.hpp> + +#include <cppuhelper/implbase.hxx> + +#include <memory> + +namespace sdbtools +{ + + // TableName + typedef ::cppu::WeakImplHelper< css::sdb::tools::XTableName + > TableName_Base; + /** default implementation for XTableName + */ + class TableName :public TableName_Base + ,public ConnectionDependentComponent + { + public: + /** constructs the instance + + @param _rContext + the component's context + @param _rxConnection + the connection to work with. Will be held weak. Must not be <NULL/>. + + @throws css::lang::NullPointerException + if _rxConnection is <NULL/> + */ + TableName( + const css::uno::Reference< css::uno::XComponentContext >& _rContext, + const css::uno::Reference< css::sdbc::XConnection >& _rxConnection + ); + + // XTableName + virtual OUString SAL_CALL getCatalogName() override; + virtual void SAL_CALL setCatalogName( const OUString& _catalogname ) override; + virtual OUString SAL_CALL getSchemaName() override; + virtual void SAL_CALL setSchemaName( const OUString& _schemaname ) override; + virtual OUString SAL_CALL getTableName() override; + virtual void SAL_CALL setTableName( const OUString& _tablename ) override; + virtual OUString SAL_CALL getNameForSelect() override; + virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL getTable() override; + virtual void SAL_CALL setTable( const css::uno::Reference< css::beans::XPropertySet >& _table ) override; + virtual OUString SAL_CALL getComposedName( ::sal_Int32 Type, sal_Bool Quote ) override; + virtual void SAL_CALL setComposedName( const OUString& ComposedName, ::sal_Int32 Type ) override; + + protected: + virtual ~TableName() override; + + private: + TableName( const TableName& ) = delete; + TableName& operator=( const TableName& ) = delete; + + OUString msCatalog; + OUString msSchema; + OUString msName; + }; + +} // namespace sdbtools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |