LibreOffice Module dbaccess (master) 1
tablename.cxx
Go to the documentation of this file.
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#include "tablename.hxx"
21#include <core_resource.hxx>
22#include <strings.hrc>
23#include <strings.hxx>
24
25#include <com/sun/star/sdb/tools/CompositionType.hpp>
26#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
27
30
31namespace sdbtools
32{
33
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::sdbc::XConnection;
36 using ::com::sun::star::uno::RuntimeException;
37 using ::com::sun::star::lang::IllegalArgumentException;
38 using ::com::sun::star::beans::XPropertySet;
39 using ::com::sun::star::container::NoSuchElementException;
40 using ::com::sun::star::sdbcx::XTablesSupplier;
41 using ::com::sun::star::container::XNameAccess;
42 using ::com::sun::star::uno::UNO_QUERY_THROW;
43 using ::com::sun::star::lang::WrappedTargetException;
44 using ::com::sun::star::uno::Exception;
45 using ::com::sun::star::uno::UNO_QUERY;
46 using ::com::sun::star::beans::XPropertySetInfo;
47 using ::com::sun::star::uno::XComponentContext;
48
49 namespace CompositionType = ::com::sun::star::sdb::tools::CompositionType;
50
51 using namespace ::dbtools;
52
53 // TableName
54 TableName::TableName( const Reference<XComponentContext>& _rContext, const Reference< XConnection >& _rxConnection )
55 :ConnectionDependentComponent( _rContext )
56 {
57 setWeakConnection( _rxConnection );
58 }
59
60 TableName::~TableName()
61 {
62 }
63
64 OUString SAL_CALL TableName::getCatalogName()
65 {
66 EntryGuard aGuard( *this );
67 return msCatalog;
68 }
69
70 void SAL_CALL TableName::setCatalogName( const OUString& _catalogName )
71 {
72 EntryGuard aGuard( *this );
73 msCatalog = _catalogName;
74 }
75
76 OUString SAL_CALL TableName::getSchemaName()
77 {
78 EntryGuard aGuard( *this );
79 return msSchema;
80 }
81
82 void SAL_CALL TableName::setSchemaName( const OUString& _schemaName )
83 {
84 EntryGuard aGuard( *this );
85 msSchema = _schemaName;
86 }
87
88 OUString SAL_CALL TableName::getTableName()
89 {
90 EntryGuard aGuard( *this );
91 return msName;
92 }
93
94 void SAL_CALL TableName::setTableName( const OUString& _tableName )
95 {
96 EntryGuard aGuard( *this );
97 msName = _tableName;
98 }
99
100 OUString SAL_CALL TableName::getNameForSelect()
101 {
102 EntryGuard aGuard( *this );
103 return composeTableNameForSelect( getConnection(), msCatalog, msSchema, msName );
104 }
105
106 Reference< XPropertySet > SAL_CALL TableName::getTable()
107 {
108 EntryGuard aGuard( *this );
109
110 Reference< XTablesSupplier > xSuppTables( getConnection(), UNO_QUERY_THROW );
111 Reference< XNameAccess > xTables( xSuppTables->getTables(), css::uno::UNO_SET_THROW );
112
113 Reference< XPropertySet > xTable;
114 try
115 {
116 xTable.set( xTables->getByName( getComposedName( CompositionType::Complete, false ) ), UNO_QUERY_THROW );
117 }
118 catch( const WrappedTargetException& )
119 {
120 throw NoSuchElementException();
121 }
122 catch( const RuntimeException& ) { throw; }
123 catch( const NoSuchElementException& ) { throw; }
124 catch( const Exception& )
125 {
126 DBG_UNHANDLED_EXCEPTION("dbaccess");
127 throw NoSuchElementException();
128 }
129
130 return xTable;
131 }
132
133 void SAL_CALL TableName::setTable( const Reference< XPropertySet >& _table )
134 {
135 EntryGuard aGuard( *this );
136
137 Reference< XPropertySetInfo > xPSI( _table, UNO_QUERY );
138 if ( !xPSI.is()
139 || !xPSI->hasPropertyByName( PROPERTY_CATALOGNAME )
140 || !xPSI->hasPropertyByName( PROPERTY_SCHEMANAME )
141 || !xPSI->hasPropertyByName( PROPERTY_NAME )
142 )
143 throw IllegalArgumentException(
144 DBA_RES( STR_NO_TABLE_OBJECT ),
145 *this,
146 0
147 );
148
149 try
150 {
151 OSL_VERIFY( _table->getPropertyValue( PROPERTY_CATALOGNAME ) >>= msCatalog );
152 OSL_VERIFY( _table->getPropertyValue( PROPERTY_SCHEMANAME ) >>= msSchema );
153 OSL_VERIFY( _table->getPropertyValue( PROPERTY_NAME ) >>= msName );
154 }
155 catch( const RuntimeException& ) { throw; }
156 catch( const Exception& e )
157 {
158 throw IllegalArgumentException( e.Message, e.Context, 0 );
159 }
160 }
161
162 namespace
163 {
168 EComposeRule lcl_translateCompositionType_throw( sal_Int32 _nType )
169 {
170 static const struct
171 {
172 sal_Int32 nCompositionType;
173 EComposeRule eComposeRule;
174 } TypeTable[] =
175 {
176 { CompositionType::ForTableDefinitions, EComposeRule::InTableDefinitions },
177 { CompositionType::ForIndexDefinitions, EComposeRule::InIndexDefinitions },
178 { CompositionType::ForDataManipulation, EComposeRule::InDataManipulation },
179 { CompositionType::ForProcedureCalls, EComposeRule::InProcedureCalls },
180 { CompositionType::ForPrivilegeDefinitions, EComposeRule::InPrivilegeDefinitions },
181 { CompositionType::Complete, EComposeRule::Complete }
182 };
183
184 auto const found = std::find_if(std::begin(TypeTable), std::end(TypeTable)
185 , [_nType](auto const & type){ return type.nCompositionType == _nType; });
186 if (found == std::end(TypeTable))
187 throw IllegalArgumentException(
188 DBA_RES( STR_INVALID_COMPOSITION_TYPE ),
189 nullptr,
190 0
191 );
192
193 return found->eComposeRule;
194 }
195 }
196
197 OUString SAL_CALL TableName::getComposedName( ::sal_Int32 Type, sal_Bool Quote )
198 {
199 EntryGuard aGuard( *this );
200
201 return composeTableName(
202 getConnection()->getMetaData(),
203 msCatalog, msSchema, msName, Quote,
204 lcl_translateCompositionType_throw( Type ) );
205 }
206
207 void SAL_CALL TableName::setComposedName( const OUString& ComposedName, ::sal_Int32 Type )
208 {
209 EntryGuard aGuard( *this );
210
212 getConnection()->getMetaData(),
213 ComposedName,
214 msCatalog, msSchema, msName,
215 lcl_translateCompositionType_throw( Type ) );
216 }
217
218} // namespace sdbtools
219
220/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
a class for guarding methods of a connection-dependent component
TableName(const css::uno::Reference< css::uno::XComponentContext > &_rContext, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection)
constructs the instance
#define DBA_RES(id)
#define DBG_UNHANDLED_EXCEPTION(...)
@ Exception
Type
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Reference< XConnection > getConnection(const Reference< XRowSet > &_rxRowSet)
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
OUString composeTableNameForSelect(const Reference< XConnection > &_rxConnection, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName)
EComposeRule
constexpr OUStringLiteral PROPERTY_SCHEMANAME(u"SchemaName")
constexpr OUStringLiteral PROPERTY_CATALOGNAME(u"CatalogName")
constexpr OUStringLiteral PROPERTY_NAME(u"Name")
OUString msName
unsigned char sal_Bool
ResultType type