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