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
;
62 TableName::TableName( const Reference
<XComponentContext
>& _rContext
, const Reference
< XConnection
>& _rxConnection
)
63 :ConnectionDependentComponent( _rContext
)
64 ,m_pImpl( new TableName_Impl
)
66 setWeakConnection( _rxConnection
);
69 TableName::~TableName()
73 OUString SAL_CALL
TableName::getCatalogName()
75 EntryGuard
aGuard( *this );
76 return m_pImpl
->sCatalog
;
79 void SAL_CALL
TableName::setCatalogName( const OUString
& _catalogName
)
81 EntryGuard
aGuard( *this );
82 m_pImpl
->sCatalog
= _catalogName
;
85 OUString SAL_CALL
TableName::getSchemaName()
87 EntryGuard
aGuard( *this );
88 return m_pImpl
->sSchema
;
91 void SAL_CALL
TableName::setSchemaName( const OUString
& _schemaName
)
93 EntryGuard
aGuard( *this );
94 m_pImpl
->sSchema
= _schemaName
;
97 OUString SAL_CALL
TableName::getTableName()
99 EntryGuard
aGuard( *this );
100 return m_pImpl
->sName
;
103 void SAL_CALL
TableName::setTableName( const OUString
& _tableName
)
105 EntryGuard
aGuard( *this );
106 m_pImpl
->sName
= _tableName
;
109 OUString SAL_CALL
TableName::getNameForSelect()
111 EntryGuard
aGuard( *this );
112 return composeTableNameForSelect( getConnection(), m_pImpl
->sCatalog
, m_pImpl
->sSchema
, m_pImpl
->sName
);
115 Reference
< XPropertySet
> SAL_CALL
TableName::getTable()
117 EntryGuard
aGuard( *this );
119 Reference
< XTablesSupplier
> xSuppTables( getConnection(), UNO_QUERY_THROW
);
120 Reference
< XNameAccess
> xTables( xSuppTables
->getTables(), css::uno::UNO_SET_THROW
);
122 Reference
< XPropertySet
> xTable
;
125 xTable
.set( xTables
->getByName( getComposedName( CompositionType::Complete
, false ) ), UNO_QUERY_THROW
);
127 catch( const WrappedTargetException
& )
129 throw NoSuchElementException();
131 catch( const RuntimeException
& ) { throw; }
132 catch( const NoSuchElementException
& ) { throw; }
133 catch( const Exception
& )
135 DBG_UNHANDLED_EXCEPTION("dbaccess");
136 throw NoSuchElementException();
142 void SAL_CALL
TableName::setTable( const Reference
< XPropertySet
>& _table
)
144 EntryGuard
aGuard( *this );
146 Reference
< XPropertySetInfo
> xPSI( _table
, UNO_QUERY
);
148 || !xPSI
->hasPropertyByName( PROPERTY_CATALOGNAME
)
149 || !xPSI
->hasPropertyByName( PROPERTY_SCHEMANAME
)
150 || !xPSI
->hasPropertyByName( PROPERTY_NAME
)
152 throw IllegalArgumentException(
153 DBA_RES( STR_NO_TABLE_OBJECT
),
160 OSL_VERIFY( _table
->getPropertyValue( PROPERTY_CATALOGNAME
) >>= m_pImpl
->sCatalog
);
161 OSL_VERIFY( _table
->getPropertyValue( PROPERTY_SCHEMANAME
) >>= m_pImpl
->sSchema
);
162 OSL_VERIFY( _table
->getPropertyValue( PROPERTY_NAME
) >>= m_pImpl
->sName
);
164 catch( const RuntimeException
& ) { throw; }
165 catch( const Exception
& e
)
167 throw IllegalArgumentException( e
.Message
, e
.Context
, 0 );
173 /** translates a CompositionType into an EComposeRule
174 @throws IllegalArgumentException
175 if the given value does not denote a valid CompositionType
177 EComposeRule
lcl_translateCompositionType_throw( sal_Int32 _nType
)
181 sal_Int32 nCompositionType
;
182 EComposeRule eComposeRule
;
185 { CompositionType::ForTableDefinitions
, EComposeRule::InTableDefinitions
},
186 { CompositionType::ForIndexDefinitions
, EComposeRule::InIndexDefinitions
},
187 { CompositionType::ForDataManipulation
, EComposeRule::InDataManipulation
},
188 { CompositionType::ForProcedureCalls
, EComposeRule::InProcedureCalls
},
189 { CompositionType::ForPrivilegeDefinitions
, EComposeRule::InPrivilegeDefinitions
},
190 { CompositionType::Complete
, EComposeRule::Complete
}
193 auto const found
= std::find_if(std::begin(TypeTable
), std::end(TypeTable
)
194 , [_nType
](auto const & type
){ return type
.nCompositionType
== _nType
; });
195 if (found
== std::end(TypeTable
))
196 throw IllegalArgumentException(
197 DBA_RES( STR_INVALID_COMPOSITION_TYPE
),
202 return found
->eComposeRule
;
206 OUString SAL_CALL
TableName::getComposedName( ::sal_Int32 Type
, sal_Bool Quote
)
208 EntryGuard
aGuard( *this );
210 return composeTableName(
211 getConnection()->getMetaData(),
212 m_pImpl
->sCatalog
, m_pImpl
->sSchema
, m_pImpl
->sName
, Quote
,
213 lcl_translateCompositionType_throw( Type
) );
216 void SAL_CALL
TableName::setComposedName( const OUString
& ComposedName
, ::sal_Int32 Type
)
218 EntryGuard
aGuard( *this );
220 qualifiedNameComponents(
221 getConnection()->getMetaData(),
223 m_pImpl
->sCatalog
, m_pImpl
->sSchema
, m_pImpl
->sName
,
224 lcl_translateCompositionType_throw( Type
) );
227 } // namespace sdbtools
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */