1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <core_resource.hxx>
22 #include <strings.hrc>
23 #include <strings.hxx>
25 #include <com/sun/star/sdb/tools/CompositionType.hpp>
26 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
28 #include <connectivity/dbtools.hxx>
29 #include <comphelper/diagnose_ex.hxx>
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
;
49 namespace CompositionType
= ::com::sun::star::sdb::tools::CompositionType
;
51 using namespace ::dbtools
;
54 TableName::TableName( const Reference
<XComponentContext
>& _rContext
, const Reference
< XConnection
>& _rxConnection
)
55 :ConnectionDependentComponent( _rContext
)
57 setWeakConnection( _rxConnection
);
60 TableName::~TableName()
64 OUString SAL_CALL
TableName::getCatalogName()
66 EntryGuard
aGuard( *this );
70 void SAL_CALL
TableName::setCatalogName( const OUString
& _catalogName
)
72 EntryGuard
aGuard( *this );
73 msCatalog
= _catalogName
;
76 OUString SAL_CALL
TableName::getSchemaName()
78 EntryGuard
aGuard( *this );
82 void SAL_CALL
TableName::setSchemaName( const OUString
& _schemaName
)
84 EntryGuard
aGuard( *this );
85 msSchema
= _schemaName
;
88 OUString SAL_CALL
TableName::getTableName()
90 EntryGuard
aGuard( *this );
94 void SAL_CALL
TableName::setTableName( const OUString
& _tableName
)
96 EntryGuard
aGuard( *this );
100 OUString SAL_CALL
TableName::getNameForSelect()
102 EntryGuard
aGuard( *this );
103 return composeTableNameForSelect( getConnection(), msCatalog
, msSchema
, msName
);
106 Reference
< XPropertySet
> SAL_CALL
TableName::getTable()
108 EntryGuard
aGuard( *this );
110 Reference
< XTablesSupplier
> xSuppTables( getConnection(), UNO_QUERY_THROW
);
111 Reference
< XNameAccess
> xTables( xSuppTables
->getTables(), css::uno::UNO_SET_THROW
);
113 Reference
< XPropertySet
> xTable
;
116 xTable
.set( xTables
->getByName( getComposedName( CompositionType::Complete
, false ) ), UNO_QUERY_THROW
);
118 catch( const WrappedTargetException
& )
120 throw NoSuchElementException();
122 catch( const RuntimeException
& ) { throw; }
123 catch( const NoSuchElementException
& ) { throw; }
124 catch( const Exception
& )
126 DBG_UNHANDLED_EXCEPTION("dbaccess");
127 throw NoSuchElementException();
133 void SAL_CALL
TableName::setTable( const Reference
< XPropertySet
>& _table
)
135 EntryGuard
aGuard( *this );
137 Reference
< XPropertySetInfo
> xPSI( _table
, UNO_QUERY
);
139 || !xPSI
->hasPropertyByName( PROPERTY_CATALOGNAME
)
140 || !xPSI
->hasPropertyByName( PROPERTY_SCHEMANAME
)
141 || !xPSI
->hasPropertyByName( PROPERTY_NAME
)
143 throw IllegalArgumentException(
144 DBA_RES( STR_NO_TABLE_OBJECT
),
151 OSL_VERIFY( _table
->getPropertyValue( PROPERTY_CATALOGNAME
) >>= msCatalog
);
152 OSL_VERIFY( _table
->getPropertyValue( PROPERTY_SCHEMANAME
) >>= msSchema
);
153 OSL_VERIFY( _table
->getPropertyValue( PROPERTY_NAME
) >>= msName
);
155 catch( const RuntimeException
& ) { throw; }
156 catch( const Exception
& e
)
158 throw IllegalArgumentException( e
.Message
, e
.Context
, 0 );
164 /** translates a CompositionType into an EComposeRule
165 @throws IllegalArgumentException
166 if the given value does not denote a valid CompositionType
168 EComposeRule
lcl_translateCompositionType_throw( sal_Int32 _nType
)
172 sal_Int32 nCompositionType
;
173 EComposeRule eComposeRule
;
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
}
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
),
193 return found
->eComposeRule
;
197 OUString SAL_CALL
TableName::getComposedName( ::sal_Int32 Type
, sal_Bool Quote
)
199 EntryGuard
aGuard( *this );
201 return composeTableName(
202 getConnection()->getMetaData(),
203 msCatalog
, msSchema
, msName
, Quote
,
204 lcl_translateCompositionType_throw( Type
) );
207 void SAL_CALL
TableName::setComposedName( const OUString
& ComposedName
, ::sal_Int32 Type
)
209 EntryGuard
aGuard( *this );
211 qualifiedNameComponents(
212 getConnection()->getMetaData(),
214 msCatalog
, msSchema
, msName
,
215 lcl_translateCompositionType_throw( Type
) );
218 } // namespace sdbtools
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */