Update ooo320-m1
[ooovba.git] / dbaccess / source / ui / misc / defaultobjectnamecheck.cxx
blob984d25210e35024186e2a2f123d4b19c7ca13355
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: defaultobjectnamecheck.cxx,v $
10 * $Revision: 1.5 $
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"
36 #endif
38 #ifndef _DBU_MISC_HRC_
39 #include "dbu_misc.hrc"
40 #endif
42 #ifndef _DBAUI_MODULE_DBU_HXX_
43 #include "moduledbu.hxx"
44 #endif
46 /** === begin UNO includes === **/
47 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_
48 #include <com/sun/star/lang/IllegalArgumentException.hpp>
49 #endif
50 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
51 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
52 #endif
53 #ifndef _COM_SUN_STAR_SDB_XQUERIESSUPPLIER_HPP_
54 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
55 #endif
56 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
57 #include <com/sun/star/sdb/CommandType.hpp>
58 #endif
59 #ifndef _COM_SUN_STAR_SDB_TOOLS_XCONNECTIONTOOLS_HPP_
60 #include <com/sun/star/sdb/tools/XConnectionTools.hpp>
61 #endif
62 /** === end UNO includes === **/
64 #ifndef _DBHELPER_DBEXCEPTION_HXX_
65 #include <connectivity/dbexception.hxx>
66 #endif
67 #ifndef CONNECTIVITY_INC_CONNECTIVITY_DBMETADATA_HXX
68 #include <connectivity/dbmetadata.hxx>
69 #endif
71 #ifndef _RTL_USTRBUF_HXX_
72 #include <rtl/ustrbuf.hxx>
73 #endif
75 #ifndef TOOLS_DIAGNOSE_EX_H
76 #include <tools/diagnose_ex.h>
77 #endif
78 #ifndef _STRING_HXX
79 #include <tools/string.hxx>
80 #endif
81 #ifndef _CPPUHELPER_EXC_HLP_HXX_
82 #include <cppuhelper/exc_hlp.hxx>
83 #endif
85 #include <vector>
86 #include <boost/shared_ptr.hpp>
88 //........................................................................
89 namespace dbaui
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 //====================================================================
116 //= helper
117 //====================================================================
118 namespace
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 );
124 SQLException aError;
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 ) )
174 return true;
176 catch( const Exception& )
178 DBG_UNHANDLED_EXCEPTION();
181 lcl_fillNameExistsError( _rObjectName, _out_rErrorToDisplay );
182 return false;
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 );
223 return true;
225 catch( const SQLException& )
227 _out_rErrorToDisplay = ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() );
229 return false;
232 //........................................................................
233 } // namespace dbaui
234 //........................................................................