1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: defaultobjectnamecheck.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX
35 #include "defaultobjectnamecheck.hxx"
38 #ifndef _DBU_MISC_HRC_
39 #include "dbu_misc.hrc"
42 #ifndef _DBAUI_MODULE_DBU_HXX_
43 #include "moduledbu.hxx"
46 /** === begin UNO includes === **/
47 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_
48 #include <com/sun/star/lang/IllegalArgumentException.hpp>
50 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
51 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
53 #ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_
54 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
56 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
57 #include <com/sun/star/sdb/CommandType.hpp>
59 #ifndef _COM_SUN_STAR_SDB_TOOLS_XCONNECTIONTOOLS_HPP_
60 #include <com/sun/star/sdb/tools/XConnectionTools.hpp>
62 /** === end UNO includes === **/
64 #ifndef _DBHELPER_DBEXCEPTION_HXX_
65 #include <connectivity/dbexception.hxx>
67 #ifndef CONNECTIVITY_INC_CONNECTIVITY_DBMETADATA_HXX
68 #include <connectivity/dbmetadata.hxx>
71 #ifndef _RTL_USTRBUF_HXX_
72 #include <rtl/ustrbuf.hxx>
75 #ifndef TOOLS_DIAGNOSE_EX_H
76 #include <tools/diagnose_ex.h>
79 #include <tools/string.hxx>
81 #ifndef _CPPUHELPER_EXC_HLP_HXX_
82 #include <cppuhelper/exc_hlp.hxx>
86 #include <boost/shared_ptr.hpp>
88 //........................................................................
91 //........................................................................
93 /** === begin UNO using === **/
94 using ::com::sun::star::uno::Reference
;
95 using ::com::sun::star::container::XNameAccess
;
96 using ::com::sun::star::lang::IllegalArgumentException
;
97 using ::com::sun::star::container::XHierarchicalNameAccess
;
98 using ::com::sun::star::sdbc::SQLException
;
99 using ::com::sun::star::uno::Exception
;
100 using ::com::sun::star::sdbc::XConnection
;
101 using ::com::sun::star::sdbcx::XTablesSupplier
;
102 using ::com::sun::star::sdb::XQueriesSupplier
;
103 using ::com::sun::star::uno::UNO_QUERY_THROW
;
104 using ::com::sun::star::uno::makeAny
;
105 using ::com::sun::star::uno::Any
;
106 using ::com::sun::star::sdb::tools::XObjectNames
;
107 using ::com::sun::star::sdb::tools::XConnectionTools
;
108 using ::com::sun::star::uno::UNO_QUERY
;
109 /** === end UNO using === **/
111 using namespace dbtools
;
113 namespace CommandType
= ::com::sun::star::sdb::CommandType
;
115 //====================================================================
117 //====================================================================
120 void lcl_fillNameExistsError( const ::rtl::OUString
& _rObjectName
, SQLExceptionInfo
& _out_rErrorToDisplay
)
122 String sErrorMessage
= String( ModuleRes( STR_NAMED_OBJECT_ALREADY_EXISTS
) );
123 sErrorMessage
.SearchAndReplaceAllAscii( "$#$", _rObjectName
);
125 aError
.Message
= sErrorMessage
;
126 _out_rErrorToDisplay
= aError
;
131 //====================================================================
132 //= HierarchicalNameCheck_Impl
133 //====================================================================
134 struct HierarchicalNameCheck_Impl
136 Reference
< XHierarchicalNameAccess
> xHierarchicalNames
;
137 ::rtl::OUString sRelativeRoot
;
140 //====================================================================
141 //= HierarchicalNameCheck
142 //====================================================================
143 //--------------------------------------------------------------------
144 HierarchicalNameCheck::HierarchicalNameCheck( const Reference
< XHierarchicalNameAccess
>& _rxNames
, const ::rtl::OUString
& _rRelativeRoot
)
145 :m_pImpl( new HierarchicalNameCheck_Impl
)
147 m_pImpl
->xHierarchicalNames
= _rxNames
;
148 m_pImpl
->sRelativeRoot
= _rRelativeRoot
;
150 if ( !m_pImpl
->xHierarchicalNames
.is() )
151 throw IllegalArgumentException();
154 //--------------------------------------------------------------------
155 HierarchicalNameCheck::~HierarchicalNameCheck()
159 //--------------------------------------------------------------------
160 bool HierarchicalNameCheck::isNameValid( const ::rtl::OUString
& _rObjectName
, SQLExceptionInfo
& _out_rErrorToDisplay
) const
164 ::rtl::OUStringBuffer aCompleteName
;
165 if ( m_pImpl
->sRelativeRoot
.getLength() )
167 aCompleteName
.append( m_pImpl
->sRelativeRoot
);
168 aCompleteName
.appendAscii( "/" );
170 aCompleteName
.append( _rObjectName
);
172 ::rtl::OUString
sCompleteName( aCompleteName
.makeStringAndClear() );
173 if ( !m_pImpl
->xHierarchicalNames
->hasByHierarchicalName( sCompleteName
) )
176 catch( const Exception
& )
178 DBG_UNHANDLED_EXCEPTION();
181 lcl_fillNameExistsError( _rObjectName
, _out_rErrorToDisplay
);
185 //====================================================================
186 //= DynamicTableOrQueryNameCheck_Impl
187 //====================================================================
188 struct DynamicTableOrQueryNameCheck_Impl
190 sal_Int32 nCommandType
;
191 Reference
< XObjectNames
> xObjectNames
;
194 //====================================================================
195 //= DynamicTableOrQueryNameCheck
196 //====================================================================
197 //--------------------------------------------------------------------
198 DynamicTableOrQueryNameCheck::DynamicTableOrQueryNameCheck( const Reference
< XConnection
>& _rxSdbLevelConnection
, sal_Int32 _nCommandType
)
199 :m_pImpl( new DynamicTableOrQueryNameCheck_Impl
)
201 Reference
< XConnectionTools
> xConnTools( _rxSdbLevelConnection
, UNO_QUERY
);
202 if ( xConnTools
.is() )
203 m_pImpl
->xObjectNames
.set( xConnTools
->getObjectNames() );
204 if ( !m_pImpl
->xObjectNames
.is() )
205 throw IllegalArgumentException();
207 if ( ( _nCommandType
!= CommandType::QUERY
) && ( _nCommandType
!= CommandType::TABLE
) )
208 throw IllegalArgumentException();
209 m_pImpl
->nCommandType
= _nCommandType
;
212 //--------------------------------------------------------------------
213 DynamicTableOrQueryNameCheck::~DynamicTableOrQueryNameCheck()
217 //--------------------------------------------------------------------
218 bool DynamicTableOrQueryNameCheck::isNameValid( const ::rtl::OUString
& _rObjectName
, ::dbtools::SQLExceptionInfo
& _out_rErrorToDisplay
) const
222 m_pImpl
->xObjectNames
->checkNameForCreate( m_pImpl
->nCommandType
, _rObjectName
);
225 catch( const SQLException
& )
227 _out_rErrorToDisplay
= ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
232 //........................................................................
234 //........................................................................