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 <TDatabaseMetaDataBase.hxx>
21 #include <RowFunctionParser.hxx>
23 #include <comphelper/sequenceashashmap.hxx>
24 #include <comphelper/evtlistenerhlp.hxx>
25 #include <comphelper/types.hxx>
26 #include <com/sun/star/lang/XComponent.hpp>
27 #include <resource/sharedresources.hxx>
28 #include <strings.hrc>
29 #include <connectivity/dbexception.hxx>
30 #include <sal/macros.h>
32 using namespace com::sun::star::uno
;
33 using namespace com::sun::star::lang
;
34 using namespace com::sun::star::sdbc
;
35 using namespace com::sun::star::beans
;
36 using namespace comphelper
;
37 using namespace connectivity
;
40 ODatabaseMetaDataBase::ODatabaseMetaDataBase(const Reference
< XConnection
>& _rxConnection
,const Sequence
< PropertyValue
>& _rInfo
)
41 : m_aConnectionInfo(_rInfo
)
42 ,m_isCatalogAtStart(false,false)
43 ,m_sCatalogSeparator(false,OUString())
44 ,m_sIdentifierQuoteString(false,OUString())
45 ,m_supportsCatalogsInTableDefinitions(false,false)
46 ,m_supportsSchemasInTableDefinitions(false,false)
47 ,m_supportsCatalogsInDataManipulation(false,false)
48 ,m_supportsSchemasInDataManipulation(false,false)
49 ,m_supportsMixedCaseQuotedIdentifiers(false,false)
50 ,m_supportsAlterTableWithAddColumn(false,false)
51 ,m_supportsAlterTableWithDropColumn(false,false)
52 ,m_MaxStatements(false,0)
53 ,m_MaxTablesInSelect(false,0)
54 ,m_storesMixedCaseQuotedIdentifiers(false,false)
55 , m_xConnection(_rxConnection
)
57 osl_atomic_increment( &m_refCount
);
59 m_xListenerHelper
= new OEventListenerHelper(this);
60 Reference
<XComponent
> xCom(m_xConnection
,UNO_QUERY
);
62 xCom
->addEventListener(m_xListenerHelper
);
64 osl_atomic_decrement( &m_refCount
);
67 ODatabaseMetaDataBase::~ODatabaseMetaDataBase()
72 Sequence
< PropertyValue
> SAL_CALL
ODatabaseMetaDataBase::getConnectionInfo( )
74 return m_aConnectionInfo
;
78 void SAL_CALL
ODatabaseMetaDataBase::disposing( const EventObject
& /*Source*/ )
80 // cut off all references to the connection
81 m_xConnection
.clear();
82 m_xListenerHelper
.clear();
85 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getTypeInfo( )
87 ::osl::MutexGuard
aGuard( m_aMutex
);
88 if ( m_aTypeInfoRows
.empty() )
90 Reference
< XResultSet
> xRet
= impl_getTypeInfo_throw();
91 Reference
< XRow
> xRow(xRet
,UNO_QUERY
);
92 ::comphelper::SequenceAsHashMap
aMap(m_aConnectionInfo
);
93 Sequence
< Any
> aTypeInfoSettings
;
94 aTypeInfoSettings
= aMap
.getUnpackedValueOrDefault("TypeInfoSettings",aTypeInfoSettings
);
98 static const sal_Int32 pTypes
[] = {
118 std::vector
<std::shared_ptr
<ExpressionNode
>> aConditions
;
119 if ( aTypeInfoSettings
.getLength() > 1 && ((aTypeInfoSettings
.getLength() % 2) == 0) )
121 const Any
* pIter
= aTypeInfoSettings
.getConstArray();
122 const Any
* pEnd
= pIter
+ aTypeInfoSettings
.getLength();
125 for(;pIter
!= pEnd
;++pIter
)
126 aConditions
.push_back(FunctionParser::parseFunction(::comphelper::getString(*pIter
)));
130 ::connectivity::SharedResources aResources
;
131 const OUString
sError( aResources
.getResourceString(STR_FORMULA_WRONG
));
132 ::dbtools::throwGenericSQLException(sError
,*this);
136 ::connectivity::ODatabaseMetaDataResultSet::ORows aTypeInfoRows
;
137 while( xRet
->next() )
139 ::connectivity::ODatabaseMetaDataResultSet::ORow aRow
;
140 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
141 const sal_Int32
* pType
= pTypes
;
142 for (sal_Int32 i
= 1; i
<= sal_Int32(SAL_N_ELEMENTS(pTypes
)); ++i
,++pType
)
145 aValue
.fill(i
,*pType
,xRow
);
146 aRow
.push_back(new ORowSetValueDecorator(aValue
));
149 std::vector
<std::shared_ptr
<ExpressionNode
>>::iterator aIter
= aConditions
.begin();
150 std::vector
<std::shared_ptr
<ExpressionNode
>>::const_iterator aEnd
= aConditions
.end();
151 for (; aIter
!= aEnd
; ++aIter
)
153 if ( (*aIter
)->evaluate(aRow
)->getValue().getBool() )
156 (*aIter
)->fill(aRow
);
161 aTypeInfoRows
.push_back(aRow
);
163 m_aTypeInfoRows
= aTypeInfoRows
;
166 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo
);
167 Reference
< XResultSet
> xRet
= pResult
;
168 pResult
->setRows(m_aTypeInfoRows
);
172 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getExportedKeys(
173 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/ )
175 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eExportedKeys
);
178 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getImportedKeys(
179 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/ )
181 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eImportedKeys
);
184 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getPrimaryKeys(
185 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/ )
187 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::ePrimaryKeys
);
190 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getIndexInfo(
191 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/,
192 sal_Bool
/*unique*/, sal_Bool
/*approximate*/ )
194 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eIndexInfo
);
197 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getBestRowIdentifier(
198 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/, sal_Int32
/*scope*/,
199 sal_Bool
/*nullable*/ )
201 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eBestRowIdentifier
);
204 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getCrossReference(
205 const Any
& /*primaryCatalog*/, const OUString
& /*primarySchema*/,
206 const OUString
& /*primaryTable*/, const Any
& /*foreignCatalog*/,
207 const OUString
& /*foreignSchema*/, const OUString
& /*foreignTable*/ )
209 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eCrossReference
);
212 Reference
< XConnection
> SAL_CALL
ODatabaseMetaDataBase::getConnection( )
214 return m_xConnection
;
217 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getProcedureColumns(
218 const Any
& /*catalog*/, const OUString
& /*schemaPattern*/,
219 const OUString
& /*procedureNamePattern*/, const OUString
& /*columnNamePattern*/ )
221 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eProcedureColumns
);
224 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getProcedures(
225 const Any
& /*catalog*/, const OUString
& /*schemaPattern*/,
226 const OUString
& /*procedureNamePattern*/ )
228 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eProcedures
);
231 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getVersionColumns(
232 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/ )
234 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eVersionColumns
);
237 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getSchemas( )
239 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eSchemas
);
242 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getColumnPrivileges(
243 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/,
244 const OUString
& /*columnNamePattern*/ )
246 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eColumnPrivileges
);
249 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getTablePrivileges(
250 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& /*table*/)
252 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eTablePrivileges
);
255 Reference
< XResultSet
> SAL_CALL
ODatabaseMetaDataBase::getCatalogs( )
257 return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eCatalogs
);
260 OUString SAL_CALL
ODatabaseMetaDataBase::getIdentifierQuoteString( )
262 return callImplMethod(m_sIdentifierQuoteString
,std::function
<OUString(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_getIdentifierQuoteString_throw
));
265 sal_Bool SAL_CALL
ODatabaseMetaDataBase::isCatalogAtStart( )
267 return callImplMethod(m_isCatalogAtStart
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_isCatalogAtStart_throw
));
270 OUString SAL_CALL
ODatabaseMetaDataBase::getCatalogSeparator( )
272 return callImplMethod(m_sCatalogSeparator
,std::function
<OUString(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_getCatalogSeparator_throw
));
275 sal_Bool SAL_CALL
ODatabaseMetaDataBase::supportsCatalogsInTableDefinitions( )
277 return callImplMethod(m_supportsCatalogsInTableDefinitions
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_supportsCatalogsInTableDefinitions_throw
));
280 sal_Bool SAL_CALL
ODatabaseMetaDataBase::supportsSchemasInTableDefinitions( )
282 return callImplMethod(m_supportsSchemasInTableDefinitions
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_supportsSchemasInTableDefinitions_throw
));
285 sal_Bool SAL_CALL
ODatabaseMetaDataBase::supportsCatalogsInDataManipulation( )
287 return callImplMethod(m_supportsCatalogsInDataManipulation
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_supportsCatalogsInDataManipulation_throw
));
290 sal_Bool SAL_CALL
ODatabaseMetaDataBase::supportsSchemasInDataManipulation( )
292 return callImplMethod(m_supportsSchemasInDataManipulation
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_supportsSchemasInDataManipulation_throw
));
295 sal_Bool SAL_CALL
ODatabaseMetaDataBase::supportsMixedCaseQuotedIdentifiers( )
297 return callImplMethod(m_supportsMixedCaseQuotedIdentifiers
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_supportsMixedCaseQuotedIdentifiers_throw
));
300 sal_Bool SAL_CALL
ODatabaseMetaDataBase::supportsAlterTableWithAddColumn( )
302 return callImplMethod(m_supportsAlterTableWithAddColumn
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_supportsAlterTableWithAddColumn_throw
));
305 sal_Bool SAL_CALL
ODatabaseMetaDataBase::supportsAlterTableWithDropColumn( )
307 return callImplMethod(m_supportsAlterTableWithDropColumn
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_supportsAlterTableWithDropColumn_throw
));
310 sal_Int32 SAL_CALL
ODatabaseMetaDataBase::getMaxStatements( )
312 return callImplMethod(m_MaxStatements
,std::function
<sal_Int32(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_getMaxStatements_throw
));
315 sal_Int32 SAL_CALL
ODatabaseMetaDataBase::getMaxTablesInSelect( )
317 return callImplMethod(m_MaxTablesInSelect
,std::function
<sal_Int32(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_getMaxTablesInSelect_throw
));
320 sal_Bool SAL_CALL
ODatabaseMetaDataBase::storesMixedCaseQuotedIdentifiers( )
322 return callImplMethod(m_storesMixedCaseQuotedIdentifiers
,std::function
<bool(ODatabaseMetaDataBase
*)>(&ODatabaseMetaDataBase::impl_storesMixedCaseQuotedIdentifiers_throw
));
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */