1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #include "tablename.hxx"
28 #include "sdbt_resource.hrc"
29 #include "module_sdbt.hxx"
30 #include "sdbtstrings.hrc"
31 
32 /** === begin UNO includes === **/
33 #include <com/sun/star/lang/NullPointerException.hpp>
34 #include <com/sun/star/sdb/tools/CompositionType.hpp>
35 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
36 /** === end UNO includes === **/
37 
38 #include <connectivity/dbtools.hxx>
39 #include <tools/diagnose_ex.h>
40 #include <tools/string.hxx>
41 
42 //........................................................................
43 namespace sdbtools
44 {
45 //........................................................................
46 
47 	/** === begin UNO using === **/
48     using ::com::sun::star::uno::Reference;
49     using ::com::sun::star::sdbc::XConnection;
50     using ::com::sun::star::lang::NullPointerException;
51     using ::com::sun::star::uno::RuntimeException;
52     using ::com::sun::star::lang::IllegalArgumentException;
53     using ::com::sun::star::beans::XPropertySet;
54     using ::com::sun::star::container::NoSuchElementException;
55     using ::com::sun::star::sdbcx::XTablesSupplier;
56     using ::com::sun::star::container::XNameAccess;
57     using ::com::sun::star::uno::UNO_QUERY_THROW;
58     using ::com::sun::star::lang::WrappedTargetException;
59     using ::com::sun::star::uno::Exception;
60     using ::com::sun::star::uno::UNO_QUERY;
61     using ::com::sun::star::beans::XPropertySetInfo;
62 	/** === end UNO using === **/
63 
64     namespace CompositionType = ::com::sun::star::sdb::tools::CompositionType;
65 
66     using namespace ::dbtools;
67 
68 	//====================================================================
69 	//= TableName
70 	//====================================================================
71     struct TableName_Impl
72     {
73         SdbtClient      m_aModuleClient;    // keep the module alive as long as this instance lives
74 
75         ::rtl::OUString sCatalog;
76         ::rtl::OUString sSchema;
77         ::rtl::OUString sName;
78     };
79 
80     //====================================================================
81 	//= TableName
82 	//====================================================================
83 	//--------------------------------------------------------------------
TableName(const::comphelper::ComponentContext & _rContext,const Reference<XConnection> & _rxConnection)84     TableName::TableName( const ::comphelper::ComponentContext& _rContext, const Reference< XConnection >& _rxConnection )
85         :ConnectionDependentComponent( _rContext )
86         ,m_pImpl( new TableName_Impl )
87     {
88         if ( !_rxConnection.is() )
89             throw NullPointerException();
90 
91         setWeakConnection( _rxConnection );
92     }
93 
94 	//--------------------------------------------------------------------
~TableName()95     TableName::~TableName()
96     {
97     }
98 
99     //--------------------------------------------------------------------
getCatalogName()100     ::rtl::OUString SAL_CALL TableName::getCatalogName() throw (RuntimeException)
101     {
102         EntryGuard aGuard( *this );
103         return m_pImpl->sCatalog;
104     }
105 
106     //--------------------------------------------------------------------
setCatalogName(const::rtl::OUString & _catalogName)107     void SAL_CALL TableName::setCatalogName( const ::rtl::OUString& _catalogName ) throw (RuntimeException)
108     {
109         EntryGuard aGuard( *this );
110         m_pImpl->sCatalog = _catalogName;
111     }
112 
113     //--------------------------------------------------------------------
getSchemaName()114     ::rtl::OUString SAL_CALL TableName::getSchemaName() throw (RuntimeException)
115     {
116         EntryGuard aGuard( *this );
117         return m_pImpl->sSchema;
118     }
119 
120     //--------------------------------------------------------------------
setSchemaName(const::rtl::OUString & _schemaName)121     void SAL_CALL TableName::setSchemaName( const ::rtl::OUString& _schemaName ) throw (RuntimeException)
122     {
123         EntryGuard aGuard( *this );
124         m_pImpl->sSchema = _schemaName;
125     }
126 
127     //--------------------------------------------------------------------
getTableName()128     ::rtl::OUString SAL_CALL TableName::getTableName() throw (RuntimeException)
129     {
130         EntryGuard aGuard( *this );
131         return m_pImpl->sName;
132     }
133 
134     //--------------------------------------------------------------------
setTableName(const::rtl::OUString & _tableName)135     void SAL_CALL TableName::setTableName( const ::rtl::OUString& _tableName ) throw (RuntimeException)
136     {
137         EntryGuard aGuard( *this );
138         m_pImpl->sName = _tableName;
139     }
140 
141     //--------------------------------------------------------------------
getNameForSelect()142     ::rtl::OUString SAL_CALL TableName::getNameForSelect() throw (RuntimeException)
143     {
144         EntryGuard aGuard( *this );
145         return composeTableNameForSelect( getConnection(), m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName );
146     }
147 
148     //--------------------------------------------------------------------
getTable()149     Reference< XPropertySet > SAL_CALL TableName::getTable() throw (NoSuchElementException, RuntimeException)
150     {
151         EntryGuard aGuard( *this );
152 
153         Reference< XTablesSupplier > xSuppTables( getConnection(), UNO_QUERY_THROW );
154         Reference< XNameAccess > xTables( xSuppTables->getTables(), UNO_QUERY_THROW );
155 
156         Reference< XPropertySet > xTable;
157         try
158         {
159             xTable.set( xTables->getByName( getComposedName( CompositionType::Complete, sal_False ) ), UNO_QUERY_THROW );
160         }
161         catch( const WrappedTargetException& )
162         {
163             throw NoSuchElementException();
164         }
165         catch( const RuntimeException& ) { throw; }
166         catch( const NoSuchElementException& ) { throw; }
167         catch( const Exception& )
168         {
169             DBG_UNHANDLED_EXCEPTION();
170             throw NoSuchElementException();
171         }
172 
173         return xTable;
174     }
175 
176     //--------------------------------------------------------------------
setTable(const Reference<XPropertySet> & _table)177     void SAL_CALL TableName::setTable( const Reference< XPropertySet >& _table ) throw (IllegalArgumentException, RuntimeException)
178     {
179         EntryGuard aGuard( *this );
180 
181         Reference< XPropertySetInfo > xPSI( _table, UNO_QUERY );
182         if  (   !xPSI.is()
183             ||  !xPSI->hasPropertyByName( PROPERTY_CATALOGNAME )
184             ||  !xPSI->hasPropertyByName( PROPERTY_SCHEMANAME )
185             ||  !xPSI->hasPropertyByName( PROPERTY_NAME )
186             )
187             throw IllegalArgumentException(
188                 String( SdbtRes( STR_NO_TABLE_OBJECT ) ),
189                 *this,
190                 0
191             );
192 
193         try
194         {
195             OSL_VERIFY( _table->getPropertyValue( PROPERTY_CATALOGNAME ) >>= m_pImpl->sCatalog );
196             OSL_VERIFY( _table->getPropertyValue( PROPERTY_SCHEMANAME ) >>= m_pImpl->sSchema );
197             OSL_VERIFY( _table->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->sName );
198         }
199         catch( const RuntimeException& ) { throw; }
200         catch( const Exception& e )
201         {
202             throw IllegalArgumentException( e.Message, e.Context, 0 );
203         }
204     }
205 
206     //--------------------------------------------------------------------
207     namespace
208     {
209         /** translates a CopmositionType into a EComposeRule
210             @throws IllegalArgumentException
211                 if the given value does not denote a valid CompositionType
212         */
lcl_translateCompositionType_throw(sal_Int32 _nType)213         EComposeRule lcl_translateCompositionType_throw( sal_Int32 _nType )
214         {
215             struct
216             {
217                 sal_Int32       nCompositionType;
218                 EComposeRule    eComposeRule;
219             }   TypeTable[] =
220             {
221                 { CompositionType::ForTableDefinitions,      eInTableDefinitions },
222                 { CompositionType::ForIndexDefinitions,      eInIndexDefinitions },
223                 { CompositionType::ForDataManipulation,      eInDataManipulation },
224                 { CompositionType::ForProcedureCalls,        eInProcedureCalls },
225                 { CompositionType::ForPrivilegeDefinitions,  eInPrivilegeDefinitions },
226                 { CompositionType::ForPrivilegeDefinitions,  eComplete }
227             };
228 
229             bool found = false;
230             size_t i = 0;
231             for ( ; ( i < sizeof( TypeTable ) / sizeof( TypeTable[0] ) ) && !found; ++i )
232                 if ( TypeTable[i].nCompositionType == _nType )
233                     found = true;
234             if ( !found )
235                 throw IllegalArgumentException(
236                     String( SdbtRes( STR_INVALID_COMPOSITION_TYPE ) ),
237                     NULL,
238                     0
239                 );
240 
241             return TypeTable[i].eComposeRule;
242         }
243     }
244 
245     //--------------------------------------------------------------------
getComposedName(::sal_Int32 _Type,::sal_Bool _Quote)246     ::rtl::OUString SAL_CALL TableName::getComposedName( ::sal_Int32 _Type, ::sal_Bool _Quote ) throw (IllegalArgumentException, RuntimeException)
247     {
248         EntryGuard aGuard( *this );
249 
250         return composeTableName(
251             getConnection()->getMetaData(),
252             m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName, _Quote,
253             lcl_translateCompositionType_throw( _Type ) );
254     }
255 
256     //--------------------------------------------------------------------
setComposedName(const::rtl::OUString & _ComposedName,::sal_Int32 _Type)257     void SAL_CALL TableName::setComposedName( const ::rtl::OUString& _ComposedName, ::sal_Int32 _Type ) throw (RuntimeException)
258     {
259         EntryGuard aGuard( *this );
260 
261         qualifiedNameComponents(
262             getConnection()->getMetaData(),
263             _ComposedName,
264             m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName,
265             lcl_translateCompositionType_throw( _Type ) );
266     }
267 
268 //........................................................................
269 } // namespace sdbtools
270 //........................................................................
271 
272