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