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 #include "tablename.hxx"
28cdf0e10cSrcweir #include "sdbt_resource.hrc"
29cdf0e10cSrcweir #include "module_sdbt.hxx"
30cdf0e10cSrcweir #include "sdbtstrings.hrc"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /** === begin UNO includes === **/
33cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp>
34cdf0e10cSrcweir #include <com/sun/star/sdb/tools/CompositionType.hpp>
35cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
36cdf0e10cSrcweir /** === end UNO includes === **/
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
39cdf0e10cSrcweir #include <tools/diagnose_ex.h>
40cdf0e10cSrcweir #include <tools/string.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //........................................................................
43cdf0e10cSrcweir namespace sdbtools
44cdf0e10cSrcweir {
45cdf0e10cSrcweir //........................................................................
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	/** === begin UNO using === **/
48cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
49cdf0e10cSrcweir     using ::com::sun::star::sdbc::XConnection;
50cdf0e10cSrcweir     using ::com::sun::star::lang::NullPointerException;
51cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
52cdf0e10cSrcweir     using ::com::sun::star::lang::IllegalArgumentException;
53cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
54cdf0e10cSrcweir     using ::com::sun::star::container::NoSuchElementException;
55cdf0e10cSrcweir     using ::com::sun::star::sdbcx::XTablesSupplier;
56cdf0e10cSrcweir     using ::com::sun::star::container::XNameAccess;
57cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
58cdf0e10cSrcweir     using ::com::sun::star::lang::WrappedTargetException;
59cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
60cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
61cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySetInfo;
62cdf0e10cSrcweir 	/** === end UNO using === **/
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     namespace CompositionType = ::com::sun::star::sdb::tools::CompositionType;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     using namespace ::dbtools;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	//====================================================================
69cdf0e10cSrcweir 	//= TableName
70cdf0e10cSrcweir 	//====================================================================
71cdf0e10cSrcweir     struct TableName_Impl
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         SdbtClient      m_aModuleClient;    // keep the module alive as long as this instance lives
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         ::rtl::OUString sCatalog;
76cdf0e10cSrcweir         ::rtl::OUString sSchema;
77cdf0e10cSrcweir         ::rtl::OUString sName;
78cdf0e10cSrcweir     };
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     //====================================================================
81cdf0e10cSrcweir 	//= TableName
82cdf0e10cSrcweir 	//====================================================================
83cdf0e10cSrcweir 	//--------------------------------------------------------------------
TableName(const::comphelper::ComponentContext & _rContext,const Reference<XConnection> & _rxConnection)84cdf0e10cSrcweir     TableName::TableName( const ::comphelper::ComponentContext& _rContext, const Reference< XConnection >& _rxConnection )
85cdf0e10cSrcweir         :ConnectionDependentComponent( _rContext )
86cdf0e10cSrcweir         ,m_pImpl( new TableName_Impl )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         if ( !_rxConnection.is() )
89cdf0e10cSrcweir             throw NullPointerException();
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         setWeakConnection( _rxConnection );
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	//--------------------------------------------------------------------
~TableName()95cdf0e10cSrcweir     TableName::~TableName()
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     //--------------------------------------------------------------------
getCatalogName()100cdf0e10cSrcweir     ::rtl::OUString SAL_CALL TableName::getCatalogName() throw (RuntimeException)
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         EntryGuard aGuard( *this );
103cdf0e10cSrcweir         return m_pImpl->sCatalog;
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     //--------------------------------------------------------------------
setCatalogName(const::rtl::OUString & _catalogName)107cdf0e10cSrcweir     void SAL_CALL TableName::setCatalogName( const ::rtl::OUString& _catalogName ) throw (RuntimeException)
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         EntryGuard aGuard( *this );
110cdf0e10cSrcweir         m_pImpl->sCatalog = _catalogName;
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     //--------------------------------------------------------------------
getSchemaName()114cdf0e10cSrcweir     ::rtl::OUString SAL_CALL TableName::getSchemaName() throw (RuntimeException)
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         EntryGuard aGuard( *this );
117cdf0e10cSrcweir         return m_pImpl->sSchema;
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     //--------------------------------------------------------------------
setSchemaName(const::rtl::OUString & _schemaName)121cdf0e10cSrcweir     void SAL_CALL TableName::setSchemaName( const ::rtl::OUString& _schemaName ) throw (RuntimeException)
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         EntryGuard aGuard( *this );
124cdf0e10cSrcweir         m_pImpl->sSchema = _schemaName;
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     //--------------------------------------------------------------------
getTableName()128cdf0e10cSrcweir     ::rtl::OUString SAL_CALL TableName::getTableName() throw (RuntimeException)
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         EntryGuard aGuard( *this );
131cdf0e10cSrcweir         return m_pImpl->sName;
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     //--------------------------------------------------------------------
setTableName(const::rtl::OUString & _tableName)135cdf0e10cSrcweir     void SAL_CALL TableName::setTableName( const ::rtl::OUString& _tableName ) throw (RuntimeException)
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         EntryGuard aGuard( *this );
138cdf0e10cSrcweir         m_pImpl->sName = _tableName;
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     //--------------------------------------------------------------------
getNameForSelect()142cdf0e10cSrcweir     ::rtl::OUString SAL_CALL TableName::getNameForSelect() throw (RuntimeException)
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         EntryGuard aGuard( *this );
145cdf0e10cSrcweir         return composeTableNameForSelect( getConnection(), m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName );
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     //--------------------------------------------------------------------
getTable()149cdf0e10cSrcweir     Reference< XPropertySet > SAL_CALL TableName::getTable() throw (NoSuchElementException, RuntimeException)
150cdf0e10cSrcweir     {
151cdf0e10cSrcweir         EntryGuard aGuard( *this );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         Reference< XTablesSupplier > xSuppTables( getConnection(), UNO_QUERY_THROW );
154cdf0e10cSrcweir         Reference< XNameAccess > xTables( xSuppTables->getTables(), UNO_QUERY_THROW );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         Reference< XPropertySet > xTable;
157cdf0e10cSrcweir         try
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             xTable.set( xTables->getByName( getComposedName( CompositionType::Complete, sal_False ) ), UNO_QUERY_THROW );
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir         catch( const WrappedTargetException& )
162cdf0e10cSrcweir         {
163cdf0e10cSrcweir             throw NoSuchElementException();
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir         catch( const RuntimeException& ) { throw; }
166cdf0e10cSrcweir         catch( const NoSuchElementException& ) { throw; }
167cdf0e10cSrcweir         catch( const Exception& )
168cdf0e10cSrcweir         {
169cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
170cdf0e10cSrcweir             throw NoSuchElementException();
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         return xTable;
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     //--------------------------------------------------------------------
setTable(const Reference<XPropertySet> & _table)177cdf0e10cSrcweir     void SAL_CALL TableName::setTable( const Reference< XPropertySet >& _table ) throw (IllegalArgumentException, RuntimeException)
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         EntryGuard aGuard( *this );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir         Reference< XPropertySetInfo > xPSI( _table, UNO_QUERY );
182cdf0e10cSrcweir         if  (   !xPSI.is()
183cdf0e10cSrcweir             ||  !xPSI->hasPropertyByName( PROPERTY_CATALOGNAME )
184cdf0e10cSrcweir             ||  !xPSI->hasPropertyByName( PROPERTY_SCHEMANAME )
185cdf0e10cSrcweir             ||  !xPSI->hasPropertyByName( PROPERTY_NAME )
186cdf0e10cSrcweir             )
187cdf0e10cSrcweir             throw IllegalArgumentException(
188cdf0e10cSrcweir                 String( SdbtRes( STR_NO_TABLE_OBJECT ) ),
189cdf0e10cSrcweir                 *this,
190cdf0e10cSrcweir                 0
191cdf0e10cSrcweir             );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         try
194cdf0e10cSrcweir         {
195cdf0e10cSrcweir             OSL_VERIFY( _table->getPropertyValue( PROPERTY_CATALOGNAME ) >>= m_pImpl->sCatalog );
196cdf0e10cSrcweir             OSL_VERIFY( _table->getPropertyValue( PROPERTY_SCHEMANAME ) >>= m_pImpl->sSchema );
197cdf0e10cSrcweir             OSL_VERIFY( _table->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->sName );
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir         catch( const RuntimeException& ) { throw; }
200cdf0e10cSrcweir         catch( const Exception& e )
201cdf0e10cSrcweir         {
202cdf0e10cSrcweir             throw IllegalArgumentException( e.Message, e.Context, 0 );
203cdf0e10cSrcweir         }
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     //--------------------------------------------------------------------
207cdf0e10cSrcweir     namespace
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         /** translates a CopmositionType into a EComposeRule
210cdf0e10cSrcweir             @throws IllegalArgumentException
211cdf0e10cSrcweir                 if the given value does not denote a valid CompositionType
212cdf0e10cSrcweir         */
lcl_translateCompositionType_throw(sal_Int32 _nType)213cdf0e10cSrcweir         EComposeRule lcl_translateCompositionType_throw( sal_Int32 _nType )
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir             struct
216cdf0e10cSrcweir             {
217cdf0e10cSrcweir                 sal_Int32       nCompositionType;
218cdf0e10cSrcweir                 EComposeRule    eComposeRule;
219cdf0e10cSrcweir             }   TypeTable[] =
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir                 { CompositionType::ForTableDefinitions,      eInTableDefinitions },
222cdf0e10cSrcweir                 { CompositionType::ForIndexDefinitions,      eInIndexDefinitions },
223cdf0e10cSrcweir                 { CompositionType::ForDataManipulation,      eInDataManipulation },
224cdf0e10cSrcweir                 { CompositionType::ForProcedureCalls,        eInProcedureCalls },
225cdf0e10cSrcweir                 { CompositionType::ForPrivilegeDefinitions,  eInPrivilegeDefinitions },
226cdf0e10cSrcweir                 { CompositionType::ForPrivilegeDefinitions,  eComplete }
227cdf0e10cSrcweir             };
228cdf0e10cSrcweir 
229cdf0e10cSrcweir             bool found = false;
230cdf0e10cSrcweir             size_t i = 0;
231cdf0e10cSrcweir             for ( ; ( i < sizeof( TypeTable ) / sizeof( TypeTable[0] ) ) && !found; ++i )
232cdf0e10cSrcweir                 if ( TypeTable[i].nCompositionType == _nType )
233cdf0e10cSrcweir                     found = true;
234cdf0e10cSrcweir             if ( !found )
235cdf0e10cSrcweir                 throw IllegalArgumentException(
236cdf0e10cSrcweir                     String( SdbtRes( STR_INVALID_COMPOSITION_TYPE ) ),
237cdf0e10cSrcweir                     NULL,
238cdf0e10cSrcweir                     0
239cdf0e10cSrcweir                 );
240cdf0e10cSrcweir 
241cdf0e10cSrcweir             return TypeTable[i].eComposeRule;
242cdf0e10cSrcweir         }
243cdf0e10cSrcweir     }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     //--------------------------------------------------------------------
getComposedName(::sal_Int32 _Type,::sal_Bool _Quote)246cdf0e10cSrcweir     ::rtl::OUString SAL_CALL TableName::getComposedName( ::sal_Int32 _Type, ::sal_Bool _Quote ) throw (IllegalArgumentException, RuntimeException)
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir         EntryGuard aGuard( *this );
249cdf0e10cSrcweir 
250cdf0e10cSrcweir         return composeTableName(
251cdf0e10cSrcweir             getConnection()->getMetaData(),
252cdf0e10cSrcweir             m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName, _Quote,
253cdf0e10cSrcweir             lcl_translateCompositionType_throw( _Type ) );
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     //--------------------------------------------------------------------
setComposedName(const::rtl::OUString & _ComposedName,::sal_Int32 _Type)257cdf0e10cSrcweir     void SAL_CALL TableName::setComposedName( const ::rtl::OUString& _ComposedName, ::sal_Int32 _Type ) throw (RuntimeException)
258cdf0e10cSrcweir     {
259cdf0e10cSrcweir         EntryGuard aGuard( *this );
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         qualifiedNameComponents(
262cdf0e10cSrcweir             getConnection()->getMetaData(),
263cdf0e10cSrcweir             _ComposedName,
264cdf0e10cSrcweir             m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName,
265cdf0e10cSrcweir             lcl_translateCompositionType_throw( _Type ) );
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir //........................................................................
269cdf0e10cSrcweir } // namespace sdbtools
270cdf0e10cSrcweir //........................................................................
271cdf0e10cSrcweir 
272