1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "TDatabaseMetaDataBase.hxx"
27 #include "RowFunctionParser.hxx"
28 
29 #include <comphelper/sequenceashashmap.hxx>
30 #include <comphelper/evtlistenerhlp.hxx>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include "resource/sharedresources.hxx"
33 #include "resource/common_res.hrc"
34 #include <connectivity/dbexception.hxx>
35 
36 using namespace com::sun::star::uno;
37 using namespace com::sun::star::lang;
38 using namespace com::sun::star::sdbc;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::beans;
41 using namespace comphelper;
42 using namespace connectivity;
43 
44 
ODatabaseMetaDataBase(const Reference<XConnection> & _rxConnection,const Sequence<PropertyValue> & _rInfo)45 ODatabaseMetaDataBase::ODatabaseMetaDataBase(const Reference< XConnection >& _rxConnection,const Sequence< PropertyValue >& _rInfo)
46     : m_aConnectionInfo(_rInfo)
47     ,m_isCatalogAtStart(false,sal_False)
48     ,m_sCatalogSeparator(false,::rtl::OUString())
49     ,m_sIdentifierQuoteString(false,::rtl::OUString())
50     ,m_supportsCatalogsInTableDefinitions(false,sal_False)
51     ,m_supportsSchemasInTableDefinitions(false,sal_False)
52     ,m_supportsCatalogsInDataManipulation(false,sal_False)
53     ,m_supportsSchemasInDataManipulation(false,sal_False)
54     ,m_supportsMixedCaseQuotedIdentifiers(false,sal_False)
55     ,m_supportsAlterTableWithAddColumn(false,sal_False)
56     ,m_supportsAlterTableWithDropColumn(false,sal_False)
57     ,m_MaxStatements(false,0)
58     ,m_MaxTablesInSelect(false,0)
59     ,m_storesMixedCaseQuotedIdentifiers(false,sal_False)
60 	, m_xConnection(_rxConnection)
61 {
62 	osl_incrementInterlockedCount( &m_refCount );
63 	{
64 		m_xListenerHelper = new OEventListenerHelper(this);
65 		Reference<XComponent> xCom(m_xConnection,UNO_QUERY);
66 		if(xCom.is())
67 			xCom->addEventListener(m_xListenerHelper);
68 	}
69 	osl_decrementInterlockedCount( &m_refCount );
70 }
71 // -------------------------------------------------------------------------
~ODatabaseMetaDataBase()72 ODatabaseMetaDataBase::~ODatabaseMetaDataBase()
73 {
74 }
75 
76 // -----------------------------------------------------------------------------
getConnectionInfo()77 Sequence< PropertyValue > SAL_CALL ODatabaseMetaDataBase::getConnectionInfo(  ) throw (RuntimeException)
78 {
79     return m_aConnectionInfo;
80 }
81 
82 // -----------------------------------------------------------------------------
disposing(const EventObject &)83 void SAL_CALL ODatabaseMetaDataBase::disposing( const EventObject& /*Source*/ ) throw(RuntimeException)
84 {
85 	// cut off all references to the connection
86 m_xConnection.clear();
87 m_xListenerHelper.clear();
88 }
89 // -----------------------------------------------------------------------------
getTypeInfo()90 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getTypeInfo(  ) throw(SQLException, RuntimeException)
91 {
92     ::osl::MutexGuard aGuard( m_aMutex );
93     if ( m_aTypeInfoRows.empty() )
94     {
95         Reference< XResultSet > xRet = impl_getTypeInfo_throw();
96         Reference< XRow > xRow(xRet,UNO_QUERY);
97         ::comphelper::SequenceAsHashMap aMap(m_aConnectionInfo);
98         Sequence< Any > aTypeInfoSettings;
99         aTypeInfoSettings = aMap.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TypeInfoSettings")),aTypeInfoSettings);
100 
101         if ( xRow.is() )
102         {
103             static sal_Int32 pTypes[] = {
104                                         DataType::VARCHAR
105                                         ,DataType::INTEGER
106                                         ,DataType::INTEGER
107                                         ,DataType::VARCHAR
108                                         ,DataType::VARCHAR
109                                         ,DataType::VARCHAR
110                                         ,DataType::INTEGER
111                                         ,DataType::BOOLEAN
112                                         ,DataType::INTEGER
113                                         ,DataType::BOOLEAN
114                                         ,DataType::BOOLEAN
115                                         ,DataType::BOOLEAN
116                                         ,DataType::VARCHAR
117                                         ,DataType::INTEGER
118                                         ,DataType::INTEGER
119                                         ,DataType::INTEGER
120                                         ,DataType::INTEGER
121                                         ,DataType::INTEGER
122                                     };
123             ::std::vector<ExpressionNodeSharedPtr> aConditions;
124             if ( aTypeInfoSettings.getLength() > 1 && ((aTypeInfoSettings.getLength() % 2) == 0) )
125             {
126                 const Any* pIter = aTypeInfoSettings.getConstArray();
127                 const Any* pEnd	 = pIter + aTypeInfoSettings.getLength();
128                 try
129                 {
130                     for(;pIter != pEnd;++pIter)
131                         aConditions.push_back(FunctionParser::parseFunction(::comphelper::getString(*pIter)));
132                 }
133                 catch(ParseError&)
134                 {
135                     ::connectivity::SharedResources aResources;
136                     const ::rtl::OUString sError( aResources.getResourceString(STR_FORMULA_WRONG));
137                     ::dbtools::throwGenericSQLException(sError,*this);
138                 }
139             }
140 
141             ::connectivity::ODatabaseMetaDataResultSet::ORows aTypeInfoRows;
142             while( xRet->next() )
143             {
144                 ::connectivity::ODatabaseMetaDataResultSet::ORow aRow;
145                 aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
146                 sal_Int32* pType = pTypes;
147                 for (sal_Int32 i = 1; i <= sal_Int32(sizeof(pTypes)/sizeof(pTypes[0])); ++i,++pType)
148                 {
149                     ORowSetValue aValue;
150                     aValue.fill(i,*pType,xRow);
151                     aRow.push_back(new ORowSetValueDecorator(aValue));
152                 }
153 
154                 ::std::vector<ExpressionNodeSharedPtr>::iterator aIter = aConditions.begin();
155                 ::std::vector<ExpressionNodeSharedPtr>::iterator aEnd = aConditions.end();
156                 for (; aIter != aEnd; ++aIter)
157                 {
158                     if ( (*aIter)->evaluate(aRow)->getValue().getBool() )
159                     {
160                         ++aIter;
161                         (*aIter)->fill(aRow);
162                     }
163                     else
164                         ++aIter;
165                 }
166                 aTypeInfoRows.push_back(aRow);
167             }
168             m_aTypeInfoRows = aTypeInfoRows;
169         }
170     }
171     ::connectivity::ODatabaseMetaDataResultSet* pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo);
172 	Reference< XResultSet > xRet = pResult;
173 	pResult->setRows(m_aTypeInfoRows);
174     return xRet;
175 }
176 // -------------------------------------------------------------------------
getExportedKeys(const Any &,const::rtl::OUString &,const::rtl::OUString &)177 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getExportedKeys(
178         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/ ) throw(SQLException, RuntimeException)
179 {
180     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eExportedKeys );
181 }
182 // -------------------------------------------------------------------------
getImportedKeys(const Any &,const::rtl::OUString &,const::rtl::OUString &)183 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getImportedKeys(
184         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/ ) throw(SQLException, RuntimeException)
185 {
186     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eImportedKeys );
187 }
188 // -------------------------------------------------------------------------
getPrimaryKeys(const Any &,const::rtl::OUString &,const::rtl::OUString &)189 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getPrimaryKeys(
190         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/ ) throw(SQLException, RuntimeException)
191 {
192     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::ePrimaryKeys );
193 }
194 // -------------------------------------------------------------------------
getIndexInfo(const Any &,const::rtl::OUString &,const::rtl::OUString &,sal_Bool,sal_Bool)195 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getIndexInfo(
196         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/,
197         sal_Bool /*unique*/, sal_Bool /*approximate*/ ) throw(SQLException, RuntimeException)
198 {
199     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eIndexInfo );
200 }
201 // -------------------------------------------------------------------------
getBestRowIdentifier(const Any &,const::rtl::OUString &,const::rtl::OUString &,sal_Int32,sal_Bool)202 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getBestRowIdentifier(
203         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/, sal_Int32 /*scope*/,
204         sal_Bool /*nullable*/ ) throw(SQLException, RuntimeException)
205 {
206     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eBestRowIdentifier );
207 }
208 // -------------------------------------------------------------------------
getCrossReference(const Any &,const::rtl::OUString &,const::rtl::OUString &,const Any &,const::rtl::OUString &,const::rtl::OUString &)209 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getCrossReference(
210         const Any& /*primaryCatalog*/, const ::rtl::OUString& /*primarySchema*/,
211         const ::rtl::OUString& /*primaryTable*/, const Any& /*foreignCatalog*/,
212         const ::rtl::OUString& /*foreignSchema*/, const ::rtl::OUString& /*foreignTable*/ ) throw(SQLException, RuntimeException)
213 {
214     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eCrossReference );
215 }
216 // -------------------------------------------------------------------------
getConnection()217 Reference< XConnection > SAL_CALL ODatabaseMetaDataBase::getConnection(  ) throw(SQLException, RuntimeException)
218 {
219     return m_xConnection;
220 }
221 // -------------------------------------------------------------------------
getProcedureColumns(const Any &,const::rtl::OUString &,const::rtl::OUString &,const::rtl::OUString &)222 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getProcedureColumns(
223         const Any& /*catalog*/, const ::rtl::OUString& /*schemaPattern*/,
224         const ::rtl::OUString& /*procedureNamePattern*/, const ::rtl::OUString& /*columnNamePattern*/ ) throw(SQLException, RuntimeException)
225 {
226     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eProcedureColumns );
227 }
228 // -------------------------------------------------------------------------
getProcedures(const Any &,const::rtl::OUString &,const::rtl::OUString &)229 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getProcedures(
230         const Any& /*catalog*/, const ::rtl::OUString& /*schemaPattern*/,
231         const ::rtl::OUString& /*procedureNamePattern*/ ) throw(SQLException, RuntimeException)
232 {
233     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eProcedures );
234 }
235 // -------------------------------------------------------------------------
getVersionColumns(const Any &,const::rtl::OUString &,const::rtl::OUString &)236 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getVersionColumns(
237         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/ ) throw(SQLException, RuntimeException)
238 {
239     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eVersionColumns );
240 }
241 // -------------------------------------------------------------------------
getSchemas()242 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getSchemas(  ) throw(SQLException, RuntimeException)
243 {
244     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eSchemas );
245 }
246 // -------------------------------------------------------------------------
getColumnPrivileges(const Any &,const::rtl::OUString &,const::rtl::OUString &,const::rtl::OUString &)247 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getColumnPrivileges(
248         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/,
249         const ::rtl::OUString& /*columnNamePattern*/ ) throw(SQLException, RuntimeException)
250 {
251     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eColumnPrivileges );
252 }
253 // -------------------------------------------------------------------------
getTablePrivileges(const Any &,const::rtl::OUString &,const::rtl::OUString &)254 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getTablePrivileges(
255         const Any& /*catalog*/, const ::rtl::OUString& /*schema*/, const ::rtl::OUString& /*table*/) throw(SQLException, RuntimeException)
256 {
257     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eTablePrivileges );
258 }
259 // -------------------------------------------------------------------------
getCatalogs()260 Reference< XResultSet > SAL_CALL ODatabaseMetaDataBase::getCatalogs(  ) throw(SQLException, RuntimeException)
261 {
262     return new ODatabaseMetaDataResultSet( ODatabaseMetaDataResultSet::eCatalogs );
263 }
264 // -----------------------------------------------------------------------------
getIdentifierQuoteString()265 ::rtl::OUString SAL_CALL ODatabaseMetaDataBase::getIdentifierQuoteString(  ) throw(SQLException, RuntimeException)
266 {
267     return callImplMethod(m_sIdentifierQuoteString,::std::mem_fun_t< ::rtl::OUString ,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_getIdentifierQuoteString_throw));
268 }
269 // -----------------------------------------------------------------------------
isCatalogAtStart()270 sal_Bool SAL_CALL ODatabaseMetaDataBase::isCatalogAtStart(  ) throw(SQLException, RuntimeException)
271 {
272     return callImplMethod(m_isCatalogAtStart,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_isCatalogAtStart_throw));
273 }
274 // -----------------------------------------------------------------------------
getCatalogSeparator()275 ::rtl::OUString SAL_CALL ODatabaseMetaDataBase::getCatalogSeparator(  ) throw(SQLException, RuntimeException)
276 {
277     return callImplMethod(m_sCatalogSeparator,::std::mem_fun_t< ::rtl::OUString,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_getCatalogSeparator_throw));
278 }
279 // -----------------------------------------------------------------------------
supportsCatalogsInTableDefinitions()280 sal_Bool SAL_CALL ODatabaseMetaDataBase::supportsCatalogsInTableDefinitions(  ) throw(SQLException, RuntimeException)
281 {
282     return callImplMethod(m_supportsCatalogsInTableDefinitions,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_supportsCatalogsInTableDefinitions_throw));
283 }
284 // -----------------------------------------------------------------------------
supportsSchemasInTableDefinitions()285 sal_Bool SAL_CALL ODatabaseMetaDataBase::supportsSchemasInTableDefinitions(  ) throw(SQLException, RuntimeException)
286 {
287     return callImplMethod(m_supportsSchemasInTableDefinitions,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_supportsSchemasInTableDefinitions_throw));
288 }
289 // -----------------------------------------------------------------------------
supportsCatalogsInDataManipulation()290 sal_Bool SAL_CALL ODatabaseMetaDataBase::supportsCatalogsInDataManipulation(  ) throw(SQLException, RuntimeException)
291 {
292     return callImplMethod(m_supportsCatalogsInDataManipulation,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_supportsCatalogsInDataManipulation_throw));
293 }
294 // -----------------------------------------------------------------------------
supportsSchemasInDataManipulation()295 sal_Bool SAL_CALL ODatabaseMetaDataBase::supportsSchemasInDataManipulation(  ) throw(SQLException, RuntimeException)
296 {
297     return callImplMethod(m_supportsSchemasInDataManipulation,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_supportsSchemasInDataManipulation_throw));
298 }
299 // -----------------------------------------------------------------------------
supportsMixedCaseQuotedIdentifiers()300 sal_Bool SAL_CALL ODatabaseMetaDataBase::supportsMixedCaseQuotedIdentifiers(  ) throw(SQLException, RuntimeException)
301 {
302     return callImplMethod(m_supportsMixedCaseQuotedIdentifiers,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_supportsMixedCaseQuotedIdentifiers_throw));
303 }
304 // -----------------------------------------------------------------------------
supportsAlterTableWithAddColumn()305 sal_Bool SAL_CALL ODatabaseMetaDataBase::supportsAlterTableWithAddColumn(  ) throw(SQLException, RuntimeException)
306 {
307     return callImplMethod(m_supportsAlterTableWithAddColumn,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_supportsAlterTableWithAddColumn_throw));
308 }
309 // -----------------------------------------------------------------------------
supportsAlterTableWithDropColumn()310 sal_Bool SAL_CALL ODatabaseMetaDataBase::supportsAlterTableWithDropColumn(  ) throw(SQLException, RuntimeException)
311 {
312     return callImplMethod(m_supportsAlterTableWithDropColumn,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_supportsAlterTableWithDropColumn_throw));
313 }
314 // -----------------------------------------------------------------------------
getMaxStatements()315 sal_Int32 SAL_CALL ODatabaseMetaDataBase::getMaxStatements(  ) throw(SQLException, RuntimeException)
316 {
317     return callImplMethod(m_MaxStatements,::std::mem_fun_t< sal_Int32,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_getMaxStatements_throw));
318 }
319 // -----------------------------------------------------------------------------
getMaxTablesInSelect()320 sal_Int32 SAL_CALL ODatabaseMetaDataBase::getMaxTablesInSelect(  ) throw(SQLException, RuntimeException)
321 {
322     return callImplMethod(m_MaxTablesInSelect,::std::mem_fun_t< sal_Int32,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_getMaxTablesInSelect_throw));
323 }
324 // -----------------------------------------------------------------------------
storesMixedCaseQuotedIdentifiers()325 sal_Bool SAL_CALL ODatabaseMetaDataBase::storesMixedCaseQuotedIdentifiers(  ) throw(SQLException, RuntimeException)
326 {
327     return callImplMethod(m_storesMixedCaseQuotedIdentifiers,::std::mem_fun_t< sal_Bool,ODatabaseMetaDataBase>(&ODatabaseMetaDataBase::impl_storesMixedCaseQuotedIdentifiers_throw));
328 }
329 // -----------------------------------------------------------------------------
330