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