2010-10-12 15:59:03 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-14 17:39:53 +01:00
|
|
|
/*
|
|
|
|
* 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 .
|
|
|
|
*/
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
#include "objectnames.hxx"
|
|
|
|
|
|
|
|
#include "module_sdbt.hxx"
|
|
|
|
#include "sdbt_resource.hrc"
|
|
|
|
|
|
|
|
#include <com/sun/star/lang/NullPointerException.hpp>
|
|
|
|
#include <com/sun/star/sdb/CommandType.hpp>
|
|
|
|
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
|
|
|
|
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
|
2007-11-21 14:46:42 +00:00
|
|
|
#include <com/sun/star/sdb/ErrorCondition.hpp>
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
#include <connectivity/dbmetadata.hxx>
|
|
|
|
#include <connectivity/dbtools.hxx>
|
2007-11-21 14:46:42 +00:00
|
|
|
#include <connectivity/sqlerror.hxx>
|
|
|
|
#include <cppuhelper/exc_hlp.hxx>
|
2006-07-10 14:19:57 +00:00
|
|
|
#include <rtl/ustrbuf.hxx>
|
|
|
|
|
2015-09-17 11:30:13 +01:00
|
|
|
#include <memory>
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
namespace sdbtools
|
|
|
|
{
|
|
|
|
|
|
|
|
using ::com::sun::star::uno::Reference;
|
|
|
|
using ::com::sun::star::sdbc::XConnection;
|
|
|
|
using ::com::sun::star::lang::NullPointerException;
|
|
|
|
using ::com::sun::star::lang::IllegalArgumentException;
|
|
|
|
using ::com::sun::star::uno::RuntimeException;
|
|
|
|
using ::com::sun::star::sdbc::SQLException;
|
|
|
|
using ::com::sun::star::sdbc::XDatabaseMetaData;
|
|
|
|
using ::com::sun::star::uno::XInterface;
|
|
|
|
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::makeAny;
|
|
|
|
using ::com::sun::star::uno::Any;
|
2013-03-19 13:08:47 +02:00
|
|
|
using ::com::sun::star::uno::XComponentContext;
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
namespace CommandType = ::com::sun::star::sdb::CommandType;
|
2007-11-21 14:46:42 +00:00
|
|
|
namespace ErrorCondition = ::com::sun::star::sdb::ErrorCondition;
|
2006-07-26 06:47:19 +00:00
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// INameValidation
|
2006-07-26 06:47:19 +00:00
|
|
|
class INameValidation
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-02-23 19:28:39 +01:00
|
|
|
virtual bool validateName( const OUString& _rName ) = 0;
|
|
|
|
virtual void validateName_throw( const OUString& _rName ) = 0;
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2006-07-26 06:47:19 +00:00
|
|
|
virtual ~INameValidation() { }
|
2006-07-10 14:19:57 +00:00
|
|
|
};
|
2015-09-17 11:30:13 +01:00
|
|
|
typedef std::shared_ptr< INameValidation > PNameValidation;
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// PlainExistenceCheck
|
2006-07-26 06:47:19 +00:00
|
|
|
class PlainExistenceCheck : public INameValidation
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
private:
|
2013-03-19 13:08:47 +02:00
|
|
|
const Reference<XComponentContext> m_aContext;
|
2007-11-21 14:46:42 +00:00
|
|
|
Reference< XConnection > m_xConnection;
|
|
|
|
Reference< XNameAccess > m_xContainer;
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
public:
|
2013-03-19 13:08:47 +02:00
|
|
|
PlainExistenceCheck( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection, const Reference< XNameAccess >& _rxContainer )
|
2007-11-21 14:46:42 +00:00
|
|
|
:m_aContext( _rContext )
|
|
|
|
,m_xConnection( _rxConnection )
|
2006-07-26 06:47:19 +00:00
|
|
|
,m_xContainer( _rxContainer )
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
OSL_ENSURE( m_xContainer.is(), "PlainExistenceCheck::PlainExistenceCheck: this will crash!" );
|
|
|
|
}
|
|
|
|
|
2006-07-26 06:47:19 +00:00
|
|
|
// INameValidation
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual bool validateName( const OUString& _rName ) override
|
2006-07-26 06:47:19 +00:00
|
|
|
{
|
|
|
|
return !m_xContainer->hasByName( _rName );
|
|
|
|
}
|
|
|
|
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void validateName_throw( const OUString& _rName ) override
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
2007-11-21 14:46:42 +00:00
|
|
|
if ( validateName( _rName ) )
|
|
|
|
return;
|
|
|
|
|
2013-03-19 13:08:47 +02:00
|
|
|
::connectivity::SQLError aErrors( m_aContext );
|
2007-11-21 14:46:42 +00:00
|
|
|
SQLException aError( aErrors.getSQLException( ErrorCondition::DB_OBJECT_NAME_IS_USED, m_xConnection, _rName ) );
|
|
|
|
|
|
|
|
::dbtools::DatabaseMetaData aMeta( m_xConnection );
|
|
|
|
if ( aMeta.supportsSubqueriesInFrom() )
|
|
|
|
{
|
2013-02-22 01:54:27 +01:00
|
|
|
OUString sNeedDistinctNames( SdbtRes( STR_QUERY_AND_TABLE_DISTINCT_NAMES ) );
|
2013-02-23 19:28:39 +01:00
|
|
|
aError.NextException <<= SQLException( sNeedDistinctNames, m_xConnection, OUString(), 0, Any() );
|
2007-11-21 14:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw aError;
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// TableValidityCheck
|
2006-07-26 06:47:19 +00:00
|
|
|
class TableValidityCheck : public INameValidation
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
2013-03-19 13:08:47 +02:00
|
|
|
const Reference<XComponentContext> m_aContext;
|
2007-11-21 14:46:42 +00:00
|
|
|
const Reference< XConnection > m_xConnection;
|
2006-07-26 06:47:19 +00:00
|
|
|
|
2006-07-10 14:19:57 +00:00
|
|
|
public:
|
2013-03-19 13:08:47 +02:00
|
|
|
TableValidityCheck( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection )
|
2007-11-21 14:46:42 +00:00
|
|
|
:m_aContext( _rContext )
|
|
|
|
,m_xConnection( _rxConnection )
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual bool validateName( const OUString& _rName ) override
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
2006-07-26 06:47:19 +00:00
|
|
|
::dbtools::DatabaseMetaData aMeta( m_xConnection );
|
|
|
|
if ( !aMeta.restrictIdentifiersToSQL92() )
|
|
|
|
return true;
|
|
|
|
|
2013-02-23 19:28:39 +01:00
|
|
|
OUString sCatalog, sSchema, sName;
|
2006-07-26 06:47:19 +00:00
|
|
|
::dbtools::qualifiedNameComponents(
|
2016-02-25 09:38:09 +02:00
|
|
|
m_xConnection->getMetaData(), _rName, sCatalog, sSchema, sName, ::dbtools::EComposeRule::InTableDefinitions );
|
2006-07-26 06:47:19 +00:00
|
|
|
|
2013-02-23 19:28:39 +01:00
|
|
|
OUString sExtraNameCharacters( m_xConnection->getMetaData()->getExtraNameCharacters() );
|
2011-12-19 18:10:37 -02:00
|
|
|
if ( ( !sCatalog.isEmpty() && !::dbtools::isValidSQLName( sCatalog, sExtraNameCharacters ) )
|
|
|
|
|| ( !sSchema.isEmpty() && !::dbtools::isValidSQLName( sSchema, sExtraNameCharacters ) )
|
|
|
|
|| ( !sName.isEmpty() && !::dbtools::isValidSQLName( sName, sExtraNameCharacters ) )
|
2006-07-26 06:47:19 +00:00
|
|
|
)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void validateName_throw( const OUString& _rName ) override
|
2006-07-26 06:47:19 +00:00
|
|
|
{
|
2007-11-21 14:46:42 +00:00
|
|
|
if ( validateName( _rName ) )
|
|
|
|
return;
|
|
|
|
|
2013-03-19 13:08:47 +02:00
|
|
|
::connectivity::SQLError aErrors( m_aContext );
|
2007-11-21 14:46:42 +00:00
|
|
|
aErrors.raiseException( ErrorCondition::DB_INVALID_SQL_NAME, m_xConnection, _rName );
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// QueryValidityCheck
|
2006-07-26 06:47:19 +00:00
|
|
|
class QueryValidityCheck : public INameValidation
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
2013-03-19 13:08:47 +02:00
|
|
|
const Reference<XComponentContext> m_aContext;
|
2007-11-21 14:46:42 +00:00
|
|
|
const Reference< XConnection > m_xConnection;
|
2006-07-26 06:47:19 +00:00
|
|
|
|
2006-07-10 14:19:57 +00:00
|
|
|
public:
|
2013-03-19 13:08:47 +02:00
|
|
|
QueryValidityCheck( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection )
|
2007-11-21 14:46:42 +00:00
|
|
|
:m_aContext( _rContext )
|
|
|
|
,m_xConnection( _rxConnection )
|
2006-07-26 06:47:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-14 14:00:46 +02:00
|
|
|
static inline ::connectivity::ErrorCondition validateName_getErrorCondition( const OUString& _rName )
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
if ( ( _rName.indexOf( (sal_Unicode)34 ) >= 0 ) // "
|
|
|
|
|| ( _rName.indexOf( (sal_Unicode)39 ) >= 0 ) // '
|
2014-02-25 18:11:51 +01:00
|
|
|
|| ( _rName.indexOf( (sal_Unicode)96 ) >= 0 )
|
|
|
|
|| ( _rName.indexOf( (sal_Unicode)145 ) >= 0 )
|
|
|
|
|| ( _rName.indexOf( (sal_Unicode)146 ) >= 0 )
|
2011-02-07 20:20:28 +01:00
|
|
|
|| ( _rName.indexOf( (sal_Unicode)180 ) >= 0 ) // removed unparsable chars
|
2006-07-10 14:19:57 +00:00
|
|
|
)
|
2007-11-21 14:46:42 +00:00
|
|
|
return ErrorCondition::DB_QUERY_NAME_WITH_QUOTES;
|
2007-05-10 09:15:44 +00:00
|
|
|
|
|
|
|
if ( _rName.indexOf( '/') >= 0 )
|
2007-11-21 14:46:42 +00:00
|
|
|
return ErrorCondition::DB_OBJECT_NAME_WITH_SLASHES;
|
2007-05-10 09:15:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual bool validateName( const OUString& _rName ) override
|
2007-05-10 09:15:44 +00:00
|
|
|
{
|
2007-11-21 14:46:42 +00:00
|
|
|
if ( validateName_getErrorCondition( _rName ) != 0 )
|
2006-07-10 14:19:57 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2006-07-26 06:47:19 +00:00
|
|
|
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void validateName_throw( const OUString& _rName ) override
|
2006-07-26 06:47:19 +00:00
|
|
|
{
|
2007-11-21 14:46:42 +00:00
|
|
|
::connectivity::ErrorCondition nErrorCondition = validateName_getErrorCondition( _rName );
|
|
|
|
if ( nErrorCondition != 0 )
|
2006-07-26 06:47:19 +00:00
|
|
|
{
|
2013-03-19 13:08:47 +02:00
|
|
|
::connectivity::SQLError aErrors( m_aContext );
|
2007-11-21 14:46:42 +00:00
|
|
|
aErrors.raiseException( nErrorCondition, m_xConnection );
|
2006-07-26 06:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-10 14:19:57 +00:00
|
|
|
};
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// CombinedNameCheck
|
2006-07-26 06:47:19 +00:00
|
|
|
class CombinedNameCheck : public INameValidation
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
private:
|
2006-07-26 06:47:19 +00:00
|
|
|
PNameValidation m_pPrimary;
|
|
|
|
PNameValidation m_pSecondary;
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
public:
|
2006-07-26 06:47:19 +00:00
|
|
|
CombinedNameCheck( PNameValidation _pPrimary, PNameValidation _pSecondary )
|
2006-07-10 14:19:57 +00:00
|
|
|
:m_pPrimary( _pPrimary )
|
|
|
|
,m_pSecondary( _pSecondary )
|
|
|
|
{
|
|
|
|
OSL_ENSURE( m_pPrimary.get() && m_pSecondary.get(), "CombinedNameCheck::CombinedNameCheck: this will crash!" );
|
|
|
|
}
|
|
|
|
|
2006-07-26 06:47:19 +00:00
|
|
|
// INameValidation
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual bool validateName( const OUString& _rName ) override
|
2006-07-26 06:47:19 +00:00
|
|
|
{
|
|
|
|
return m_pPrimary->validateName( _rName ) && m_pSecondary->validateName( _rName );
|
|
|
|
}
|
|
|
|
|
2015-10-12 16:04:04 +02:00
|
|
|
virtual void validateName_throw( const OUString& _rName ) override
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
2006-07-26 06:47:19 +00:00
|
|
|
m_pPrimary->validateName_throw( _rName );
|
|
|
|
m_pSecondary->validateName_throw( _rName );
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// NameCheckFactory
|
2016-04-08 14:58:51 +02:00
|
|
|
class NameCheckFactory
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-04-08 14:58:51 +02:00
|
|
|
NameCheckFactory(const NameCheckFactory&) = delete;
|
|
|
|
const NameCheckFactory& operator=(const NameCheckFactory&) = delete;
|
2006-07-26 06:47:19 +00:00
|
|
|
/** creates an INameValidation instance which can be used to check the existence of query or table names
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
@param _rContext
|
|
|
|
the component's context
|
|
|
|
|
2006-07-10 14:19:57 +00:00
|
|
|
@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
|
|
|
|
*/
|
2007-11-21 14:46:42 +00:00
|
|
|
static PNameValidation createExistenceCheck(
|
2013-03-19 13:08:47 +02:00
|
|
|
const Reference<XComponentContext>& _rContext,
|
2007-11-21 14:46:42 +00:00
|
|
|
sal_Int32 _nCommandType,
|
|
|
|
const Reference< XConnection >& _rxConnection
|
|
|
|
);
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2006-07-26 06:47:19 +00:00
|
|
|
/** creates an INameValidation instance which can be used to check the validity of a query or table name
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
@param _rContext
|
|
|
|
the component's context
|
|
|
|
|
2006-07-10 14:19:57 +00:00
|
|
|
@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
|
|
|
|
*/
|
2007-11-21 14:46:42 +00:00
|
|
|
static PNameValidation createValidityCheck(
|
2013-03-19 13:08:47 +02:00
|
|
|
const Reference<XComponentContext>& _rContext,
|
2007-11-21 14:46:42 +00:00
|
|
|
const sal_Int32 _nCommandType,
|
|
|
|
const Reference< XConnection >& _rxConnection
|
|
|
|
);
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void verifyCommandType( sal_Int32 _nCommandType );
|
|
|
|
};
|
|
|
|
|
|
|
|
void NameCheckFactory::verifyCommandType( sal_Int32 _nCommandType )
|
|
|
|
{
|
|
|
|
if ( ( _nCommandType != CommandType::TABLE )
|
|
|
|
&& ( _nCommandType != CommandType::QUERY )
|
|
|
|
)
|
|
|
|
throw IllegalArgumentException(
|
2013-02-22 01:54:27 +01:00
|
|
|
OUString( SdbtRes( STR_INVALID_COMMAND_TYPE ) ),
|
2015-11-10 10:13:39 +01:00
|
|
|
nullptr,
|
2006-07-10 14:19:57 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-19 13:08:47 +02:00
|
|
|
PNameValidation NameCheckFactory::createExistenceCheck( const Reference<XComponentContext>& _rContext, sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection )
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
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(), UNO_QUERY_THROW );
|
|
|
|
xQueries.set( xQueriesSupplier->getQueries(), UNO_QUERY_THROW );
|
|
|
|
}
|
|
|
|
catch( const Exception& )
|
|
|
|
{
|
|
|
|
throw IllegalArgumentException(
|
2013-02-22 01:54:27 +01:00
|
|
|
OUString( SdbtRes( STR_CONN_WITHOUT_QUERIES_OR_TABLES ) ),
|
2015-11-10 10:13:39 +01:00
|
|
|
nullptr,
|
2006-07-10 14:19:57 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
PNameValidation pTableCheck( new PlainExistenceCheck( _rContext, _rxConnection, xTables ) );
|
|
|
|
PNameValidation pQueryCheck( new PlainExistenceCheck( _rContext, _rxConnection, xQueries ) );
|
2006-07-26 06:47:19 +00:00
|
|
|
PNameValidation pReturn;
|
2006-07-10 14:19:57 +00:00
|
|
|
|
|
|
|
if ( aMeta.supportsSubqueriesInFrom() )
|
|
|
|
pReturn.reset( new CombinedNameCheck( pTableCheck, pQueryCheck ) );
|
|
|
|
else if ( _nCommandType == CommandType::TABLE )
|
|
|
|
pReturn = pTableCheck;
|
|
|
|
else
|
|
|
|
pReturn = pQueryCheck;
|
|
|
|
return pReturn;
|
|
|
|
}
|
|
|
|
|
2013-03-19 13:08:47 +02:00
|
|
|
PNameValidation NameCheckFactory::createValidityCheck( const Reference<XComponentContext>& _rContext, sal_Int32 _nCommandType, const Reference< XConnection >& _rxConnection )
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
verifyCommandType( _nCommandType );
|
|
|
|
|
|
|
|
Reference< XDatabaseMetaData > xMeta;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
xMeta.set( _rxConnection->getMetaData(), UNO_QUERY_THROW );
|
|
|
|
}
|
|
|
|
catch( const Exception& )
|
|
|
|
{
|
|
|
|
throw IllegalArgumentException(
|
2014-05-28 12:50:02 +02:00
|
|
|
"The connection could not provide its database's meta data.",
|
2015-11-10 10:13:39 +01:00
|
|
|
nullptr,
|
2006-07-10 14:19:57 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( _nCommandType == CommandType::TABLE )
|
2007-11-21 14:46:42 +00:00
|
|
|
return PNameValidation( new TableValidityCheck( _rContext, _rxConnection ) );
|
|
|
|
return PNameValidation( new QueryValidityCheck( _rContext, _rxConnection ) );
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// ObjectNames_Impl
|
2006-07-10 14:19:57 +00:00
|
|
|
struct ObjectNames_Impl
|
|
|
|
{
|
2007-11-21 14:46:42 +00:00
|
|
|
SdbtClient m_aModuleClient; // keep the module alive as long as this instance lives
|
2006-07-10 14:19:57 +00:00
|
|
|
};
|
|
|
|
|
2013-08-17 23:43:14 +02:00
|
|
|
// ObjectNames
|
2013-03-19 13:08:47 +02:00
|
|
|
ObjectNames::ObjectNames( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection )
|
2007-11-21 14:46:42 +00:00
|
|
|
:ConnectionDependentComponent( _rContext )
|
|
|
|
,m_pImpl( new ObjectNames_Impl )
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
setWeakConnection( _rxConnection );
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectNames::~ObjectNames()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-17 12:17:17 +01:00
|
|
|
OUString SAL_CALL ObjectNames::suggestName( ::sal_Int32 _CommandType, const OUString& _BaseName ) throw (IllegalArgumentException, SQLException, RuntimeException, std::exception)
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
EntryGuard aGuard( *this );
|
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( getContext(), _CommandType, getConnection() ) );
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2013-02-22 01:54:27 +01:00
|
|
|
OUString sBaseName( _BaseName );
|
|
|
|
if ( sBaseName.isEmpty() )
|
2008-12-11 07:05:03 +00:00
|
|
|
{
|
2006-07-10 14:19:57 +00:00
|
|
|
if ( _CommandType == CommandType::TABLE )
|
2013-02-22 01:54:27 +01:00
|
|
|
sBaseName = OUString( SdbtRes( STR_BASENAME_TABLE ) );
|
2006-07-10 14:19:57 +00:00
|
|
|
else
|
2013-02-22 01:54:27 +01:00
|
|
|
sBaseName = OUString( SdbtRes( STR_BASENAME_QUERY ) );
|
2008-12-11 07:05:03 +00:00
|
|
|
}
|
2013-07-03 18:10:38 +02:00
|
|
|
else if( _CommandType == CommandType::QUERY )
|
|
|
|
{
|
|
|
|
sBaseName=sBaseName.replace('/', '_');
|
|
|
|
}
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2013-02-23 19:28:39 +01:00
|
|
|
OUString sName( sBaseName );
|
2006-07-10 14:19:57 +00:00
|
|
|
sal_Int32 i = 1;
|
2006-07-26 06:47:19 +00:00
|
|
|
while ( !pNameCheck->validateName( sName ) )
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
2013-08-17 22:14:22 +02:00
|
|
|
sName = sBaseName + " " + OUString::number(++i);
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sName;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
OUString SAL_CALL ObjectNames::convertToSQLName( const OUString& Name ) throw (RuntimeException, std::exception)
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
EntryGuard aGuard( *this );
|
|
|
|
Reference< XDatabaseMetaData > xMeta( getConnection()->getMetaData(), UNO_QUERY_THROW );
|
|
|
|
return ::dbtools::convertName2SQLName( Name, xMeta->getExtraNameCharacters() );
|
|
|
|
}
|
|
|
|
|
2014-08-17 12:18:52 +01:00
|
|
|
sal_Bool SAL_CALL ObjectNames::isNameUsed( ::sal_Int32 _CommandType, const OUString& _Name ) throw (IllegalArgumentException, SQLException, RuntimeException, std::exception)
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
EntryGuard aGuard( *this );
|
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( getContext(), _CommandType, getConnection()) );
|
2006-07-26 06:47:19 +00:00
|
|
|
return !pNameCheck->validateName( _Name );
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 13:52:06 +02:00
|
|
|
sal_Bool SAL_CALL ObjectNames::isNameValid( ::sal_Int32 _CommandType, const OUString& _Name ) throw (IllegalArgumentException, RuntimeException, std::exception)
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
EntryGuard aGuard( *this );
|
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
PNameValidation pNameCheck( NameCheckFactory::createValidityCheck( getContext(), _CommandType, getConnection()) );
|
2006-07-26 06:47:19 +00:00
|
|
|
return pNameCheck->validateName( _Name );
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void SAL_CALL ObjectNames::checkNameForCreate( ::sal_Int32 _CommandType, const OUString& _Name ) throw (SQLException, RuntimeException, std::exception)
|
2006-07-10 14:19:57 +00:00
|
|
|
{
|
|
|
|
EntryGuard aGuard( *this );
|
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
PNameValidation pNameCheck( NameCheckFactory::createExistenceCheck( getContext(), _CommandType, getConnection() ) );
|
2006-07-26 06:47:19 +00:00
|
|
|
pNameCheck->validateName_throw( _Name );
|
2006-07-10 14:19:57 +00:00
|
|
|
|
2007-11-21 14:46:42 +00:00
|
|
|
pNameCheck = NameCheckFactory::createValidityCheck( getContext(), _CommandType, getConnection() );
|
2006-07-26 06:47:19 +00:00
|
|
|
pNameCheck->validateName_throw( _Name );
|
2006-07-10 14:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sdbtools
|
|
|
|
|
2010-10-12 15:59:03 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|