bump product version to 5.0.4.1
[LibreOffice.git] / dbaccess / source / sdbtools / connection / tablename.cxx
blob64cce68c4fbcd65187813daf204ba1ff8bbce2dc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "tablename.hxx"
21 #include "sdbt_resource.hrc"
22 #include "module_sdbt.hxx"
23 #include "sdbtstrings.hrc"
25 #include <com/sun/star/lang/NullPointerException.hpp>
26 #include <com/sun/star/sdb/tools/CompositionType.hpp>
27 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
29 #include <connectivity/dbtools.hxx>
30 #include <tools/diagnose_ex.h>
32 namespace sdbtools
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::sdbc::XConnection;
37 using ::com::sun::star::lang::NullPointerException;
38 using ::com::sun::star::uno::RuntimeException;
39 using ::com::sun::star::lang::IllegalArgumentException;
40 using ::com::sun::star::beans::XPropertySet;
41 using ::com::sun::star::container::NoSuchElementException;
42 using ::com::sun::star::sdbcx::XTablesSupplier;
43 using ::com::sun::star::container::XNameAccess;
44 using ::com::sun::star::uno::UNO_QUERY_THROW;
45 using ::com::sun::star::lang::WrappedTargetException;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::uno::UNO_QUERY;
48 using ::com::sun::star::beans::XPropertySetInfo;
49 using ::com::sun::star::uno::XComponentContext;
51 namespace CompositionType = ::com::sun::star::sdb::tools::CompositionType;
53 using namespace ::dbtools;
55 // TableName
56 struct TableName_Impl
58 SdbtClient m_aModuleClient; // keep the module alive as long as this instance lives
60 OUString sCatalog;
61 OUString sSchema;
62 OUString sName;
65 // TableName
66 TableName::TableName( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection )
67 :ConnectionDependentComponent( _rContext )
68 ,m_pImpl( new TableName_Impl )
70 setWeakConnection( _rxConnection );
73 TableName::~TableName()
77 OUString SAL_CALL TableName::getCatalogName() throw (RuntimeException, std::exception)
79 EntryGuard aGuard( *this );
80 return m_pImpl->sCatalog;
83 void SAL_CALL TableName::setCatalogName( const OUString& _catalogName ) throw (RuntimeException, std::exception)
85 EntryGuard aGuard( *this );
86 m_pImpl->sCatalog = _catalogName;
89 OUString SAL_CALL TableName::getSchemaName() throw (RuntimeException, std::exception)
91 EntryGuard aGuard( *this );
92 return m_pImpl->sSchema;
95 void SAL_CALL TableName::setSchemaName( const OUString& _schemaName ) throw (RuntimeException, std::exception)
97 EntryGuard aGuard( *this );
98 m_pImpl->sSchema = _schemaName;
101 OUString SAL_CALL TableName::getTableName() throw (RuntimeException, std::exception)
103 EntryGuard aGuard( *this );
104 return m_pImpl->sName;
107 void SAL_CALL TableName::setTableName( const OUString& _tableName ) throw (RuntimeException, std::exception)
109 EntryGuard aGuard( *this );
110 m_pImpl->sName = _tableName;
113 OUString SAL_CALL TableName::getNameForSelect() throw (RuntimeException, std::exception)
115 EntryGuard aGuard( *this );
116 return composeTableNameForSelect( getConnection(), m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName );
119 Reference< XPropertySet > SAL_CALL TableName::getTable() throw (NoSuchElementException, RuntimeException, std::exception)
121 EntryGuard aGuard( *this );
123 Reference< XTablesSupplier > xSuppTables( getConnection(), UNO_QUERY_THROW );
124 Reference< XNameAccess > xTables( xSuppTables->getTables(), UNO_QUERY_THROW );
126 Reference< XPropertySet > xTable;
129 xTable.set( xTables->getByName( getComposedName( CompositionType::Complete, sal_False ) ), UNO_QUERY_THROW );
131 catch( const WrappedTargetException& )
133 throw NoSuchElementException();
135 catch( const RuntimeException& ) { throw; }
136 catch( const NoSuchElementException& ) { throw; }
137 catch( const Exception& )
139 DBG_UNHANDLED_EXCEPTION();
140 throw NoSuchElementException();
143 return xTable;
146 void SAL_CALL TableName::setTable( const Reference< XPropertySet >& _table ) throw (IllegalArgumentException, RuntimeException, std::exception)
148 EntryGuard aGuard( *this );
150 Reference< XPropertySetInfo > xPSI( _table, UNO_QUERY );
151 if ( !xPSI.is()
152 || !xPSI->hasPropertyByName( PROPERTY_CATALOGNAME )
153 || !xPSI->hasPropertyByName( PROPERTY_SCHEMANAME )
154 || !xPSI->hasPropertyByName( PROPERTY_NAME )
156 throw IllegalArgumentException(
157 OUString( SdbtRes( STR_NO_TABLE_OBJECT ) ),
158 *this,
164 OSL_VERIFY( _table->getPropertyValue( PROPERTY_CATALOGNAME ) >>= m_pImpl->sCatalog );
165 OSL_VERIFY( _table->getPropertyValue( PROPERTY_SCHEMANAME ) >>= m_pImpl->sSchema );
166 OSL_VERIFY( _table->getPropertyValue( PROPERTY_NAME ) >>= m_pImpl->sName );
168 catch( const RuntimeException& ) { throw; }
169 catch( const Exception& e )
171 throw IllegalArgumentException( e.Message, e.Context, 0 );
175 namespace
177 /** translates a CompositionType into a EComposeRule
178 @throws IllegalArgumentException
179 if the given value does not denote a valid CompositionType
181 EComposeRule lcl_translateCompositionType_throw( sal_Int32 _nType )
183 struct
185 sal_Int32 nCompositionType;
186 EComposeRule eComposeRule;
187 } TypeTable[] =
189 { CompositionType::ForTableDefinitions, eInTableDefinitions },
190 { CompositionType::ForIndexDefinitions, eInIndexDefinitions },
191 { CompositionType::ForDataManipulation, eInDataManipulation },
192 { CompositionType::ForProcedureCalls, eInProcedureCalls },
193 { CompositionType::ForPrivilegeDefinitions, eInPrivilegeDefinitions },
194 { CompositionType::ForPrivilegeDefinitions, eComplete }
197 bool found = false;
198 size_t i = 0;
199 for ( ; ( i < sizeof( TypeTable ) / sizeof( TypeTable[0] ) ) && !found; ++i )
200 if ( TypeTable[i].nCompositionType == _nType )
201 found = true;
202 if ( !found )
203 throw IllegalArgumentException(
204 OUString( SdbtRes( STR_INVALID_COMPOSITION_TYPE ) ),
205 NULL,
209 return TypeTable[i].eComposeRule;
213 OUString SAL_CALL TableName::getComposedName( ::sal_Int32 _Type, sal_Bool _Quote ) throw (IllegalArgumentException, RuntimeException, std::exception)
215 EntryGuard aGuard( *this );
217 return composeTableName(
218 getConnection()->getMetaData(),
219 m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName, _Quote,
220 lcl_translateCompositionType_throw( _Type ) );
223 void SAL_CALL TableName::setComposedName( const OUString& _ComposedName, ::sal_Int32 _Type ) throw (RuntimeException, std::exception)
225 EntryGuard aGuard( *this );
227 qualifiedNameComponents(
228 getConnection()->getMetaData(),
229 _ComposedName,
230 m_pImpl->sCatalog, m_pImpl->sSchema, m_pImpl->sName,
231 lcl_translateCompositionType_throw( _Type ) );
234 } // namespace sdbtools
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */