1*96de5490SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*96de5490SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*96de5490SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*96de5490SAndrew Rist * distributed with this work for additional information 6*96de5490SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*96de5490SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*96de5490SAndrew Rist * "License"); you may not use this file except in compliance 9*96de5490SAndrew Rist * with the License. You may obtain a copy of the License at 10*96de5490SAndrew Rist * 11*96de5490SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*96de5490SAndrew Rist * 13*96de5490SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*96de5490SAndrew Rist * software distributed under the License is distributed on an 15*96de5490SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*96de5490SAndrew Rist * KIND, either express or implied. See the License for the 17*96de5490SAndrew Rist * specific language governing permissions and limitations 18*96de5490SAndrew Rist * under the License. 19*96de5490SAndrew Rist * 20*96de5490SAndrew Rist *************************************************************/ 21*96de5490SAndrew Rist 22*96de5490SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX 28cdf0e10cSrcweir #include "defaultobjectnamecheck.hxx" 29cdf0e10cSrcweir #endif 30cdf0e10cSrcweir 31cdf0e10cSrcweir #ifndef _DBU_MISC_HRC_ 32cdf0e10cSrcweir #include "dbu_misc.hrc" 33cdf0e10cSrcweir #endif 34cdf0e10cSrcweir 35cdf0e10cSrcweir #ifndef _DBAUI_MODULE_DBU_HXX_ 36cdf0e10cSrcweir #include "moduledbu.hxx" 37cdf0e10cSrcweir #endif 38cdf0e10cSrcweir 39cdf0e10cSrcweir /** === begin UNO includes === **/ 40cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_ 41cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 42cdf0e10cSrcweir #endif 43cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_ 44cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp> 45cdf0e10cSrcweir #endif 46cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_ 47cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp> 48cdf0e10cSrcweir #endif 49cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_ 50cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp> 51cdf0e10cSrcweir #endif 52cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_TOOLS_XCONNECTIONTOOLS_HPP_ 53cdf0e10cSrcweir #include <com/sun/star/sdb/tools/XConnectionTools.hpp> 54cdf0e10cSrcweir #endif 55cdf0e10cSrcweir /** === end UNO includes === **/ 56cdf0e10cSrcweir 57cdf0e10cSrcweir #ifndef _DBHELPER_DBEXCEPTION_HXX_ 58cdf0e10cSrcweir #include <connectivity/dbexception.hxx> 59cdf0e10cSrcweir #endif 60cdf0e10cSrcweir #ifndef CONNECTIVITY_INC_CONNECTIVITY_DBMETADATA_HXX 61cdf0e10cSrcweir #include <connectivity/dbmetadata.hxx> 62cdf0e10cSrcweir #endif 63cdf0e10cSrcweir 64cdf0e10cSrcweir #ifndef _RTL_USTRBUF_HXX_ 65cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 66cdf0e10cSrcweir #endif 67cdf0e10cSrcweir 68cdf0e10cSrcweir #ifndef TOOLS_DIAGNOSE_EX_H 69cdf0e10cSrcweir #include <tools/diagnose_ex.h> 70cdf0e10cSrcweir #endif 71cdf0e10cSrcweir #ifndef _STRING_HXX 72cdf0e10cSrcweir #include <tools/string.hxx> 73cdf0e10cSrcweir #endif 74cdf0e10cSrcweir #ifndef _CPPUHELPER_EXC_HLP_HXX_ 75cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx> 76cdf0e10cSrcweir #endif 77cdf0e10cSrcweir 78cdf0e10cSrcweir #include <vector> 79cdf0e10cSrcweir #include <boost/shared_ptr.hpp> 80cdf0e10cSrcweir 81cdf0e10cSrcweir //........................................................................ 82cdf0e10cSrcweir namespace dbaui 83cdf0e10cSrcweir { 84cdf0e10cSrcweir //........................................................................ 85cdf0e10cSrcweir 86cdf0e10cSrcweir /** === begin UNO using === **/ 87cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 88cdf0e10cSrcweir using ::com::sun::star::container::XNameAccess; 89cdf0e10cSrcweir using ::com::sun::star::lang::IllegalArgumentException; 90cdf0e10cSrcweir using ::com::sun::star::container::XHierarchicalNameAccess; 91cdf0e10cSrcweir using ::com::sun::star::sdbc::SQLException; 92cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 93cdf0e10cSrcweir using ::com::sun::star::sdbc::XConnection; 94cdf0e10cSrcweir using ::com::sun::star::sdbcx::XTablesSupplier; 95cdf0e10cSrcweir using ::com::sun::star::sdb::XQueriesSupplier; 96cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 97cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 98cdf0e10cSrcweir using ::com::sun::star::uno::Any; 99cdf0e10cSrcweir using ::com::sun::star::sdb::tools::XObjectNames; 100cdf0e10cSrcweir using ::com::sun::star::sdb::tools::XConnectionTools; 101cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 102cdf0e10cSrcweir /** === end UNO using === **/ 103cdf0e10cSrcweir 104cdf0e10cSrcweir using namespace dbtools; 105cdf0e10cSrcweir 106cdf0e10cSrcweir namespace CommandType = ::com::sun::star::sdb::CommandType; 107cdf0e10cSrcweir 108cdf0e10cSrcweir //==================================================================== 109cdf0e10cSrcweir //= helper 110cdf0e10cSrcweir //==================================================================== 111cdf0e10cSrcweir namespace 112cdf0e10cSrcweir { lcl_fillNameExistsError(const::rtl::OUString & _rObjectName,SQLExceptionInfo & _out_rErrorToDisplay)113cdf0e10cSrcweir void lcl_fillNameExistsError( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir String sErrorMessage = String( ModuleRes( STR_NAMED_OBJECT_ALREADY_EXISTS ) ); 116cdf0e10cSrcweir sErrorMessage.SearchAndReplaceAllAscii( "$#$", _rObjectName ); 117cdf0e10cSrcweir SQLException aError; 118cdf0e10cSrcweir aError.Message = sErrorMessage; 119cdf0e10cSrcweir _out_rErrorToDisplay = aError; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir //==================================================================== 125cdf0e10cSrcweir //= HierarchicalNameCheck_Impl 126cdf0e10cSrcweir //==================================================================== 127cdf0e10cSrcweir struct HierarchicalNameCheck_Impl 128cdf0e10cSrcweir { 129cdf0e10cSrcweir Reference< XHierarchicalNameAccess > xHierarchicalNames; 130cdf0e10cSrcweir ::rtl::OUString sRelativeRoot; 131cdf0e10cSrcweir }; 132cdf0e10cSrcweir 133cdf0e10cSrcweir //==================================================================== 134cdf0e10cSrcweir //= HierarchicalNameCheck 135cdf0e10cSrcweir //==================================================================== 136cdf0e10cSrcweir //-------------------------------------------------------------------- HierarchicalNameCheck(const Reference<XHierarchicalNameAccess> & _rxNames,const::rtl::OUString & _rRelativeRoot)137cdf0e10cSrcweir HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const ::rtl::OUString& _rRelativeRoot ) 138cdf0e10cSrcweir :m_pImpl( new HierarchicalNameCheck_Impl ) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir m_pImpl->xHierarchicalNames = _rxNames; 141cdf0e10cSrcweir m_pImpl->sRelativeRoot = _rRelativeRoot; 142cdf0e10cSrcweir 143cdf0e10cSrcweir if ( !m_pImpl->xHierarchicalNames.is() ) 144cdf0e10cSrcweir throw IllegalArgumentException(); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir //-------------------------------------------------------------------- ~HierarchicalNameCheck()148cdf0e10cSrcweir HierarchicalNameCheck::~HierarchicalNameCheck() 149cdf0e10cSrcweir { 150cdf0e10cSrcweir } 151cdf0e10cSrcweir 152cdf0e10cSrcweir //-------------------------------------------------------------------- isNameValid(const::rtl::OUString & _rObjectName,SQLExceptionInfo & _out_rErrorToDisplay) const153cdf0e10cSrcweir bool HierarchicalNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const 154cdf0e10cSrcweir { 155cdf0e10cSrcweir try 156cdf0e10cSrcweir { 157cdf0e10cSrcweir ::rtl::OUStringBuffer aCompleteName; 158cdf0e10cSrcweir if ( m_pImpl->sRelativeRoot.getLength() ) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir aCompleteName.append( m_pImpl->sRelativeRoot ); 161cdf0e10cSrcweir aCompleteName.appendAscii( "/" ); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir aCompleteName.append( _rObjectName ); 164cdf0e10cSrcweir 165cdf0e10cSrcweir ::rtl::OUString sCompleteName( aCompleteName.makeStringAndClear() ); 166cdf0e10cSrcweir if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) ) 167cdf0e10cSrcweir return true; 168cdf0e10cSrcweir } 169cdf0e10cSrcweir catch( const Exception& ) 170cdf0e10cSrcweir { 171cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir 174cdf0e10cSrcweir lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay ); 175cdf0e10cSrcweir return false; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir //==================================================================== 179cdf0e10cSrcweir //= DynamicTableOrQueryNameCheck_Impl 180cdf0e10cSrcweir //==================================================================== 181cdf0e10cSrcweir struct DynamicTableOrQueryNameCheck_Impl 182cdf0e10cSrcweir { 183cdf0e10cSrcweir sal_Int32 nCommandType; 184cdf0e10cSrcweir Reference< XObjectNames > xObjectNames; 185cdf0e10cSrcweir }; 186cdf0e10cSrcweir 187cdf0e10cSrcweir //==================================================================== 188cdf0e10cSrcweir //= DynamicTableOrQueryNameCheck 189cdf0e10cSrcweir //==================================================================== 190cdf0e10cSrcweir //-------------------------------------------------------------------- DynamicTableOrQueryNameCheck(const Reference<XConnection> & _rxSdbLevelConnection,sal_Int32 _nCommandType)191cdf0e10cSrcweir DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType ) 192cdf0e10cSrcweir :m_pImpl( new DynamicTableOrQueryNameCheck_Impl ) 193cdf0e10cSrcweir { 194cdf0e10cSrcweir Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY ); 195cdf0e10cSrcweir if ( xConnTools.is() ) 196cdf0e10cSrcweir m_pImpl->xObjectNames.set( xConnTools->getObjectNames() ); 197cdf0e10cSrcweir if ( !m_pImpl->xObjectNames.is() ) 198cdf0e10cSrcweir throw IllegalArgumentException(); 199cdf0e10cSrcweir 200cdf0e10cSrcweir if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) ) 201cdf0e10cSrcweir throw IllegalArgumentException(); 202cdf0e10cSrcweir m_pImpl->nCommandType = _nCommandType; 203cdf0e10cSrcweir } 204cdf0e10cSrcweir 205cdf0e10cSrcweir //-------------------------------------------------------------------- ~DynamicTableOrQueryNameCheck()206cdf0e10cSrcweir DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck() 207cdf0e10cSrcweir { 208cdf0e10cSrcweir } 209cdf0e10cSrcweir 210cdf0e10cSrcweir //-------------------------------------------------------------------- isNameValid(const::rtl::OUString & _rObjectName,::dbtools::SQLExceptionInfo & _out_rErrorToDisplay) const211cdf0e10cSrcweir bool DynamicTableOrQueryNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const 212cdf0e10cSrcweir { 213cdf0e10cSrcweir try 214cdf0e10cSrcweir { 215cdf0e10cSrcweir m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName ); 216cdf0e10cSrcweir return true; 217cdf0e10cSrcweir } 218cdf0e10cSrcweir catch( const SQLException& ) 219cdf0e10cSrcweir { 220cdf0e10cSrcweir _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ); 221cdf0e10cSrcweir } 222cdf0e10cSrcweir return false; 223cdf0e10cSrcweir } 224cdf0e10cSrcweir 225cdf0e10cSrcweir //........................................................................ 226cdf0e10cSrcweir } // namespace dbaui 227cdf0e10cSrcweir //........................................................................ 228cdf0e10cSrcweir 229