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