Update ooo320-m1
[ooovba.git] / dbaccess / source / sdbtools / connection / tablename.cxx
blob56bd5c5a2efdfd98deffa4b156a52c58396292b6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tablename.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "tablename.hxx"
35 #include "sdbt_resource.hrc"
36 #include "module_sdbt.hxx"
37 #include "sdbtstrings.hrc"
39 /** === begin UNO includes === **/
40 #include <com/sun/star/lang/NullPointerException.hpp>
41 #include <com/sun/star/sdb/tools/CompositionType.hpp>
42 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
43 /** === end UNO includes === **/
45 #include <connectivity/dbtools.hxx>
46 #include <tools/diagnose_ex.h>
47 #include <tools/string.hxx>
49 //........................................................................
50 namespace sdbtools
52 //........................................................................
54 /** === begin UNO using === **/
55 using ::com::sun::star::uno::Reference;
56 using ::com::sun::star::sdbc::XConnection;
57 using ::com::sun::star::lang::NullPointerException;
58 using ::com::sun::star::uno::RuntimeException;
59 using ::com::sun::star::lang::IllegalArgumentException;
60 using ::com::sun::star::beans::XPropertySet;
61 using ::com::sun::star::container::NoSuchElementException;
62 using ::com::sun::star::sdbcx::XTablesSupplier;
63 using ::com::sun::star::container::XNameAccess;
64 using ::com::sun::star::uno::UNO_QUERY_THROW;
65 using ::com::sun::star::lang::WrappedTargetException;
66 using ::com::sun::star::uno::Exception;
67 using ::com::sun::star::uno::UNO_QUERY;
68 using ::com::sun::star::beans::XPropertySetInfo;
69 /** === end UNO using === **/
71 namespace CompositionType = ::com::sun::star::sdb::tools::CompositionType;
73 using namespace ::dbtools;
75 //====================================================================
76 //= TableName
77 //====================================================================
78 struct TableName_Impl
80 SdbtClient m_aModuleClient; // keep the module alive as long as this instance lives
82 ::rtl::OUString sCatalog;
83 ::rtl::OUString sSchema;
84 ::rtl::OUString sName;
87 //====================================================================
88 //= TableName
89 //====================================================================
90 //--------------------------------------------------------------------
91 TableName::TableName( const ::comphelper::ComponentContext& _rContext, const Reference< XConnection >& _rxConnection )
92 :ConnectionDependentComponent( _rContext )
93 ,m_pImpl( new TableName_Impl )
95 if ( !_rxConnection.is() )
96 throw NullPointerException();
98 setWeakConnection( _rxConnection );
101 //--------------------------------------------------------------------
102 TableName::~TableName()
106 //--------------------------------------------------------------------
107 ::rtl::OUString SAL_CALL TableName::getCatalogName() throw (RuntimeException)
109 EntryGuard aGuard( *this );
110 return m_pImpl->sCatalog;
113 //--------------------------------------------------------------------
114 void SAL_CALL TableName::setCatalogName( const ::rtl::OUString& _catalogName ) throw (RuntimeException)
116 EntryGuard aGuard( *this );
117 m_pImpl->sCatalog = _catalogName;
120 //--------------------------------------------------------------------
121 ::rtl::OUString SAL_CALL TableName::getSchemaName() throw (RuntimeException)
123 EntryGuard aGuard( *this );
124 return m_pImpl->sSchema;
127 //--------------------------------------------------------------------
128 void SAL_CALL TableName::setSchemaName( const ::rtl::OUString& _schemaName ) throw (RuntimeException)
130 EntryGuard aGuard( *this );
131 m_pImpl->sSchema = _schemaName;
134 //--------------------------------------------------------------------
135 ::rtl::OUString SAL_CALL TableName::getTableName() throw (RuntimeException)
137 EntryGuard aGuard( *this );
138 return m_pImpl->sName;
141 //--------------------------------------------------------------------
142 void SAL_CALL TableName::setTableName( const ::rtl::OUString& _tableName ) throw (RuntimeException)
144 EntryGuard aGuard( *this );
145 m_pImpl->sName = _tableName;
148 //--------------------------------------------------------------------
149 ::rtl::OUString SAL_CALL TableName::getNameForSelect() throw (RuntimeException)
151 EntryGuard aGuard( *this );
152 return composeTableNameForSelect( getConnection(), m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName );
155 //--------------------------------------------------------------------
156 Reference< XPropertySet > SAL_CALL TableName::getTable() throw (NoSuchElementException, RuntimeException)
158 EntryGuard aGuard( *this );
160 Reference< XTablesSupplier > xSuppTables( getConnection(), UNO_QUERY_THROW );
161 Reference< XNameAccess > xTables( xSuppTables->getTables(), UNO_QUERY_THROW );
163 Reference< XPropertySet > xTable;
166 xTable.set( xTables->getByName( getComposedName( CompositionType::Complete, sal_False ) ), UNO_QUERY_THROW );
168 catch( const WrappedTargetException& )
170 throw NoSuchElementException();
172 catch( const RuntimeException& ) { throw; }
173 catch( const NoSuchElementException& ) { throw; }
174 catch( const Exception& )
176 DBG_UNHANDLED_EXCEPTION();
177 throw NoSuchElementException();
180 return xTable;
183 //--------------------------------------------------------------------
184 void SAL_CALL TableName::setTable( const Reference< XPropertySet >& _table ) throw (IllegalArgumentException, RuntimeException)
186 EntryGuard aGuard( *this );
188 Reference< XPropertySetInfo > xPSI( _table, UNO_QUERY );
189 if ( !xPSI.is()
190 || !xPSI->hasPropertyByName( PROPERTY_CATALOGNAME )
191 || !xPSI->hasPropertyByName( PROPERTY_SCHEMANAME )
192 || !xPSI->hasPropertyByName( PROPERTY_NAME )
194 throw IllegalArgumentException(
195 String( SdbtRes( STR_NO_TABLE_OBJECT ) ),
196 *this,
202 OSL_VERIFY( _table->getPropertyValue( PROPERTY_CATALOGNAME ) >>= m_pImpl->sCatalog );
203 OSL_VERIFY( _table->getPropertyValue( PROPERTY_SCHEMANAME ) >>= m_pImpl->sSchema );
204 OSL_VERIFY( _table->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->sName );
206 catch( const RuntimeException& ) { throw; }
207 catch( const Exception& e )
209 throw IllegalArgumentException( e.Message, e.Context, 0 );
213 //--------------------------------------------------------------------
214 namespace
216 /** translates a CopmositionType into a EComposeRule
217 @throws IllegalArgumentException
218 if the given value does not denote a valid CompositionType
220 EComposeRule lcl_translateCompositionType_throw( sal_Int32 _nType )
222 struct
224 sal_Int32 nCompositionType;
225 EComposeRule eComposeRule;
226 } TypeTable[] =
228 { CompositionType::ForTableDefinitions, eInTableDefinitions },
229 { CompositionType::ForIndexDefinitions, eInIndexDefinitions },
230 { CompositionType::ForDataManipulation, eInDataManipulation },
231 { CompositionType::ForProcedureCalls, eInProcedureCalls },
232 { CompositionType::ForPrivilegeDefinitions, eInPrivilegeDefinitions },
233 { CompositionType::ForPrivilegeDefinitions, eComplete }
236 bool found = false;
237 size_t i = 0;
238 for ( ; ( i < sizeof( TypeTable ) / sizeof( TypeTable[0] ) ) && !found; ++i )
239 if ( TypeTable[i].nCompositionType == _nType )
240 found = true;
241 if ( !found )
242 throw IllegalArgumentException(
243 String( SdbtRes( STR_INVALID_COMPOSITION_TYPE ) ),
244 NULL,
248 return TypeTable[i].eComposeRule;
252 //--------------------------------------------------------------------
253 ::rtl::OUString SAL_CALL TableName::getComposedName( ::sal_Int32 _Type, ::sal_Bool _Quote ) throw (IllegalArgumentException, RuntimeException)
255 EntryGuard aGuard( *this );
257 return composeTableName(
258 getConnection()->getMetaData(),
259 m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName, _Quote,
260 lcl_translateCompositionType_throw( _Type ) );
263 //--------------------------------------------------------------------
264 void SAL_CALL TableName::setComposedName( const ::rtl::OUString& _ComposedName, ::sal_Int32 _Type ) throw (RuntimeException)
266 EntryGuard aGuard( *this );
268 qualifiedNameComponents(
269 getConnection()->getMetaData(),
270 _ComposedName,
271 m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName,
272 lcl_translateCompositionType_throw( _Type ) );
275 //........................................................................
276 } // namespace sdbtools
277 //........................................................................