1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_dbaccess.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX 32*cdf0e10cSrcweir #include "defaultobjectnamecheck.hxx" 33*cdf0e10cSrcweir #endif 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #ifndef _DBU_MISC_HRC_ 36*cdf0e10cSrcweir #include "dbu_misc.hrc" 37*cdf0e10cSrcweir #endif 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #ifndef _DBAUI_MODULE_DBU_HXX_ 40*cdf0e10cSrcweir #include "moduledbu.hxx" 41*cdf0e10cSrcweir #endif 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir /** === begin UNO includes === **/ 44*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_ 45*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 46*cdf0e10cSrcweir #endif 47*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_ 48*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp> 49*cdf0e10cSrcweir #endif 50*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_ 51*cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp> 52*cdf0e10cSrcweir #endif 53*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_ 54*cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp> 55*cdf0e10cSrcweir #endif 56*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_TOOLS_XCONNECTIONTOOLS_HPP_ 57*cdf0e10cSrcweir #include <com/sun/star/sdb/tools/XConnectionTools.hpp> 58*cdf0e10cSrcweir #endif 59*cdf0e10cSrcweir /** === end UNO includes === **/ 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir #ifndef _DBHELPER_DBEXCEPTION_HXX_ 62*cdf0e10cSrcweir #include <connectivity/dbexception.hxx> 63*cdf0e10cSrcweir #endif 64*cdf0e10cSrcweir #ifndef CONNECTIVITY_INC_CONNECTIVITY_DBMETADATA_HXX 65*cdf0e10cSrcweir #include <connectivity/dbmetadata.hxx> 66*cdf0e10cSrcweir #endif 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir #ifndef _RTL_USTRBUF_HXX_ 69*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 70*cdf0e10cSrcweir #endif 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir #ifndef TOOLS_DIAGNOSE_EX_H 73*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 74*cdf0e10cSrcweir #endif 75*cdf0e10cSrcweir #ifndef _STRING_HXX 76*cdf0e10cSrcweir #include <tools/string.hxx> 77*cdf0e10cSrcweir #endif 78*cdf0e10cSrcweir #ifndef _CPPUHELPER_EXC_HLP_HXX_ 79*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx> 80*cdf0e10cSrcweir #endif 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir #include <vector> 83*cdf0e10cSrcweir #include <boost/shared_ptr.hpp> 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir //........................................................................ 86*cdf0e10cSrcweir namespace dbaui 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir //........................................................................ 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir /** === begin UNO using === **/ 91*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 92*cdf0e10cSrcweir using ::com::sun::star::container::XNameAccess; 93*cdf0e10cSrcweir using ::com::sun::star::lang::IllegalArgumentException; 94*cdf0e10cSrcweir using ::com::sun::star::container::XHierarchicalNameAccess; 95*cdf0e10cSrcweir using ::com::sun::star::sdbc::SQLException; 96*cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 97*cdf0e10cSrcweir using ::com::sun::star::sdbc::XConnection; 98*cdf0e10cSrcweir using ::com::sun::star::sdbcx::XTablesSupplier; 99*cdf0e10cSrcweir using ::com::sun::star::sdb::XQueriesSupplier; 100*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 101*cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 102*cdf0e10cSrcweir using ::com::sun::star::uno::Any; 103*cdf0e10cSrcweir using ::com::sun::star::sdb::tools::XObjectNames; 104*cdf0e10cSrcweir using ::com::sun::star::sdb::tools::XConnectionTools; 105*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 106*cdf0e10cSrcweir /** === end UNO using === **/ 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir using namespace dbtools; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir namespace CommandType = ::com::sun::star::sdb::CommandType; 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir //==================================================================== 113*cdf0e10cSrcweir //= helper 114*cdf0e10cSrcweir //==================================================================== 115*cdf0e10cSrcweir namespace 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir void lcl_fillNameExistsError( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir String sErrorMessage = String( ModuleRes( STR_NAMED_OBJECT_ALREADY_EXISTS ) ); 120*cdf0e10cSrcweir sErrorMessage.SearchAndReplaceAllAscii( "$#$", _rObjectName ); 121*cdf0e10cSrcweir SQLException aError; 122*cdf0e10cSrcweir aError.Message = sErrorMessage; 123*cdf0e10cSrcweir _out_rErrorToDisplay = aError; 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir //==================================================================== 129*cdf0e10cSrcweir //= HierarchicalNameCheck_Impl 130*cdf0e10cSrcweir //==================================================================== 131*cdf0e10cSrcweir struct HierarchicalNameCheck_Impl 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir Reference< XHierarchicalNameAccess > xHierarchicalNames; 134*cdf0e10cSrcweir ::rtl::OUString sRelativeRoot; 135*cdf0e10cSrcweir }; 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir //==================================================================== 138*cdf0e10cSrcweir //= HierarchicalNameCheck 139*cdf0e10cSrcweir //==================================================================== 140*cdf0e10cSrcweir //-------------------------------------------------------------------- 141*cdf0e10cSrcweir HierarchicalNameCheck::HierarchicalNameCheck( const Reference< XHierarchicalNameAccess >& _rxNames, const ::rtl::OUString& _rRelativeRoot ) 142*cdf0e10cSrcweir :m_pImpl( new HierarchicalNameCheck_Impl ) 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir m_pImpl->xHierarchicalNames = _rxNames; 145*cdf0e10cSrcweir m_pImpl->sRelativeRoot = _rRelativeRoot; 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir if ( !m_pImpl->xHierarchicalNames.is() ) 148*cdf0e10cSrcweir throw IllegalArgumentException(); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir //-------------------------------------------------------------------- 152*cdf0e10cSrcweir HierarchicalNameCheck::~HierarchicalNameCheck() 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir //-------------------------------------------------------------------- 157*cdf0e10cSrcweir bool HierarchicalNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, SQLExceptionInfo& _out_rErrorToDisplay ) const 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir try 160*cdf0e10cSrcweir { 161*cdf0e10cSrcweir ::rtl::OUStringBuffer aCompleteName; 162*cdf0e10cSrcweir if ( m_pImpl->sRelativeRoot.getLength() ) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir aCompleteName.append( m_pImpl->sRelativeRoot ); 165*cdf0e10cSrcweir aCompleteName.appendAscii( "/" ); 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir aCompleteName.append( _rObjectName ); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir ::rtl::OUString sCompleteName( aCompleteName.makeStringAndClear() ); 170*cdf0e10cSrcweir if ( !m_pImpl->xHierarchicalNames->hasByHierarchicalName( sCompleteName ) ) 171*cdf0e10cSrcweir return true; 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir catch( const Exception& ) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay ); 179*cdf0e10cSrcweir return false; 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir //==================================================================== 183*cdf0e10cSrcweir //= DynamicTableOrQueryNameCheck_Impl 184*cdf0e10cSrcweir //==================================================================== 185*cdf0e10cSrcweir struct DynamicTableOrQueryNameCheck_Impl 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir sal_Int32 nCommandType; 188*cdf0e10cSrcweir Reference< XObjectNames > xObjectNames; 189*cdf0e10cSrcweir }; 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir //==================================================================== 192*cdf0e10cSrcweir //= DynamicTableOrQueryNameCheck 193*cdf0e10cSrcweir //==================================================================== 194*cdf0e10cSrcweir //-------------------------------------------------------------------- 195*cdf0e10cSrcweir DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference< XConnection >& _rxSdbLevelConnection, sal_Int32 _nCommandType ) 196*cdf0e10cSrcweir :m_pImpl( new DynamicTableOrQueryNameCheck_Impl ) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir Reference< XConnectionTools > xConnTools( _rxSdbLevelConnection, UNO_QUERY ); 199*cdf0e10cSrcweir if ( xConnTools.is() ) 200*cdf0e10cSrcweir m_pImpl->xObjectNames.set( xConnTools->getObjectNames() ); 201*cdf0e10cSrcweir if ( !m_pImpl->xObjectNames.is() ) 202*cdf0e10cSrcweir throw IllegalArgumentException(); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir if ( ( _nCommandType != CommandType::QUERY ) && ( _nCommandType != CommandType::TABLE ) ) 205*cdf0e10cSrcweir throw IllegalArgumentException(); 206*cdf0e10cSrcweir m_pImpl->nCommandType = _nCommandType; 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir //-------------------------------------------------------------------- 210*cdf0e10cSrcweir DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck() 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir //-------------------------------------------------------------------- 215*cdf0e10cSrcweir bool DynamicTableOrQueryNameCheck::isNameValid( const ::rtl::OUString& _rObjectName, ::dbtools::SQLExceptionInfo& _out_rErrorToDisplay ) const 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir try 218*cdf0e10cSrcweir { 219*cdf0e10cSrcweir m_pImpl->xObjectNames->checkNameForCreate( m_pImpl->nCommandType, _rObjectName ); 220*cdf0e10cSrcweir return true; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir catch( const SQLException& ) 223*cdf0e10cSrcweir { 224*cdf0e10cSrcweir _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ); 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir return false; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir //........................................................................ 230*cdf0e10cSrcweir } // namespace dbaui 231*cdf0e10cSrcweir //........................................................................ 232*cdf0e10cSrcweir 233