bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / commontools / dbmetadata.cxx
blobf5a987b51b07c8aac84ca086c93351f1a8ba64ed
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <connectivity/dbmetadata.hxx>
22 #include <connectivity/dbexception.hxx>
23 #include <connectivity/DriversConfig.hxx>
24 #include "resource/common_res.hrc"
25 #include "resource/sharedresources.hxx"
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/container/XChild.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <com/sun/star/beans/XPropertySetInfo.hpp>
32 #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
33 #include <com/sun/star/sdbc/XDatabaseMetaData2.hpp>
34 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
35 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
36 #include <com/sun/star/sdbc/XDriverAccess.hpp>
37 #include <com/sun/star/sdbc/DriverManager.hpp>
39 #include <tools/diagnose_ex.h>
40 #include <comphelper/namedvaluecollection.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <sal/macros.h>
44 #include <boost/optional.hpp>
47 namespace dbtools
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::sdbc::XConnection;
53 using ::com::sun::star::sdbc::XDatabaseMetaData;
54 using ::com::sun::star::sdbc::XDatabaseMetaData2;
55 using ::com::sun::star::lang::IllegalArgumentException;
56 using ::com::sun::star::uno::Exception;
57 using ::com::sun::star::uno::Any;
58 using ::com::sun::star::uno::XComponentContext;
59 using ::com::sun::star::container::XChild;
60 using ::com::sun::star::uno::UNO_QUERY_THROW;
61 using ::com::sun::star::beans::XPropertySet;
62 using ::com::sun::star::uno::Sequence;
63 using ::com::sun::star::beans::PropertyValue;
64 using ::com::sun::star::beans::XPropertySetInfo;
65 using ::com::sun::star::uno::UNO_QUERY;
66 using ::com::sun::star::sdbcx::XUsersSupplier;
67 using ::com::sun::star::sdbcx::XDataDefinitionSupplier;
68 using ::com::sun::star::sdbc::XDriverAccess;
69 using ::com::sun::star::sdbc::DriverManager;
70 using ::com::sun::star::sdbc::XDriverManager2;
71 using ::com::sun::star::uno::UNO_SET_THROW;
73 namespace BooleanComparisonMode = ::com::sun::star::sdb::BooleanComparisonMode;
75 struct DatabaseMetaData_Impl
77 Reference< XConnection > xConnection;
78 Reference< XDatabaseMetaData > xConnectionMetaData;
79 ::connectivity::DriversConfig aDriverConfig;
81 ::boost::optional< OUString > sCachedIdentifierQuoteString;
82 ::boost::optional< OUString > sCachedCatalogSeparator;
84 DatabaseMetaData_Impl()
85 :xConnection()
86 ,xConnectionMetaData()
87 ,aDriverConfig( ::comphelper::getProcessComponentContext() )
88 ,sCachedIdentifierQuoteString()
89 ,sCachedCatalogSeparator()
95 namespace
98 static void lcl_construct( DatabaseMetaData_Impl& _metaDataImpl, const Reference< XConnection >& _connection )
100 _metaDataImpl.xConnection = _connection;
101 if ( !_metaDataImpl.xConnection.is() )
102 return;
104 _metaDataImpl.xConnectionMetaData = _connection->getMetaData();
105 if ( !_metaDataImpl.xConnectionMetaData.is() )
106 throw IllegalArgumentException();
110 static void lcl_checkConnected( const DatabaseMetaData_Impl& _metaDataImpl )
112 if ( !_metaDataImpl.xConnection.is() || !_metaDataImpl.xConnectionMetaData.is() )
114 ::connectivity::SharedResources aResources;
115 const OUString sError( aResources.getResourceString(STR_NO_CONNECTION_GIVEN));
116 throwSQLException( sError, SQL_CONNECTION_DOES_NOT_EXIST, NULL );
121 static bool lcl_getDriverSetting( const sal_Char* _asciiName, const DatabaseMetaData_Impl& _metaData, Any& _out_setting )
123 lcl_checkConnected( _metaData );
124 const ::comphelper::NamedValueCollection& rDriverMetaData = _metaData.aDriverConfig.getMetaData( _metaData.xConnectionMetaData->getURL() );
125 if ( !rDriverMetaData.has( _asciiName ) )
126 return false;
127 _out_setting = rDriverMetaData.get( _asciiName );
128 return true;
132 static bool lcl_getConnectionSetting( const sal_Char* _asciiName, const DatabaseMetaData_Impl& _metaData, Any& _out_setting )
136 Reference< XChild > xConnectionAsChild( _metaData.xConnection, UNO_QUERY );
137 if ( xConnectionAsChild.is() )
139 Reference< XPropertySet > xDataSource( xConnectionAsChild->getParent(), UNO_QUERY_THROW );
140 Reference< XPropertySet > xDataSourceSettings(
141 xDataSource->getPropertyValue("Settings"),
142 UNO_QUERY_THROW );
144 _out_setting = xDataSourceSettings->getPropertyValue( OUString::createFromAscii( _asciiName ) );
146 else
148 Reference< XDatabaseMetaData2 > xExtendedMetaData( _metaData.xConnectionMetaData, UNO_QUERY_THROW );
149 ::comphelper::NamedValueCollection aSettings( xExtendedMetaData->getConnectionInfo() );
150 _out_setting = aSettings.get( _asciiName );
151 return _out_setting.hasValue();
153 return true;
155 catch( const Exception& )
157 DBG_UNHANDLED_EXCEPTION();
159 return false;
163 static const OUString& lcl_getConnectionStringSetting(
164 const DatabaseMetaData_Impl& _metaData, ::boost::optional< OUString >& _cachedSetting,
165 OUString (SAL_CALL XDatabaseMetaData::*_getter)() )
167 if ( !_cachedSetting )
169 lcl_checkConnected( _metaData );
172 _cachedSetting.reset( (_metaData.xConnectionMetaData.get()->*_getter)() );
174 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
176 return *_cachedSetting;
180 DatabaseMetaData::DatabaseMetaData()
181 :m_pImpl( new DatabaseMetaData_Impl )
186 DatabaseMetaData::DatabaseMetaData( const Reference< XConnection >& _connection )
187 :m_pImpl( new DatabaseMetaData_Impl )
189 lcl_construct( *m_pImpl, _connection );
193 DatabaseMetaData::DatabaseMetaData( const DatabaseMetaData& _copyFrom )
194 :m_pImpl( new DatabaseMetaData_Impl( *_copyFrom.m_pImpl ) )
199 DatabaseMetaData& DatabaseMetaData::operator=( const DatabaseMetaData& _copyFrom )
201 if ( this == &_copyFrom )
202 return *this;
204 m_pImpl.reset( new DatabaseMetaData_Impl( *_copyFrom.m_pImpl ) );
205 return *this;
209 DatabaseMetaData::~DatabaseMetaData()
214 bool DatabaseMetaData::isConnected() const
216 return m_pImpl->xConnection.is();
220 bool DatabaseMetaData::supportsSubqueriesInFrom() const
222 lcl_checkConnected( *m_pImpl );
224 bool supportsSubQueries = false;
227 sal_Int32 maxTablesInselect = m_pImpl->xConnectionMetaData->getMaxTablesInSelect();
228 supportsSubQueries = ( maxTablesInselect > 1 ) || ( maxTablesInselect == 0 );
229 // TODO: is there a better way to determine this? The above is not really true. More precise,
230 // it's a *very* generous heuristics ...
232 catch( const Exception& )
234 DBG_UNHANDLED_EXCEPTION();
236 return supportsSubQueries;
240 bool DatabaseMetaData::supportsPrimaryKeys() const
242 lcl_checkConnected( *m_pImpl );
244 bool doesSupportPrimaryKeys = false;
247 Any setting;
248 if ( !( lcl_getConnectionSetting( "PrimaryKeySupport", *m_pImpl, setting ) )
249 || !( setting >>= doesSupportPrimaryKeys )
251 doesSupportPrimaryKeys = m_pImpl->xConnectionMetaData->supportsCoreSQLGrammar()
252 || m_pImpl->xConnectionMetaData->supportsANSI92EntryLevelSQL();
254 catch( const Exception& )
256 DBG_UNHANDLED_EXCEPTION();
258 return doesSupportPrimaryKeys;
262 const OUString& DatabaseMetaData::getIdentifierQuoteString() const
264 return lcl_getConnectionStringSetting( *m_pImpl, m_pImpl->sCachedIdentifierQuoteString, &XDatabaseMetaData::getIdentifierQuoteString );
268 const OUString& DatabaseMetaData::getCatalogSeparator() const
270 return lcl_getConnectionStringSetting( *m_pImpl, m_pImpl->sCachedCatalogSeparator, &XDatabaseMetaData::getCatalogSeparator );
274 bool DatabaseMetaData::restrictIdentifiersToSQL92() const
276 lcl_checkConnected( *m_pImpl );
278 bool restrict( false );
279 Any setting;
280 if ( lcl_getConnectionSetting( "EnableSQL92Check", *m_pImpl, setting ) )
281 OSL_VERIFY( setting >>= restrict );
282 return restrict;
286 bool DatabaseMetaData::generateASBeforeCorrelationName() const
288 bool doGenerate( false );
289 Any setting;
290 if ( lcl_getConnectionSetting( "GenerateASBeforeCorrelationName", *m_pImpl, setting ) )
291 OSL_VERIFY( setting >>= doGenerate );
292 return doGenerate;
295 bool DatabaseMetaData::shouldEscapeDateTime() const
297 bool doGenerate( true );
298 Any setting;
299 if ( lcl_getConnectionSetting( "EscapeDateTime", *m_pImpl, setting ) )
300 OSL_VERIFY( setting >>= doGenerate );
301 return doGenerate;
304 bool DatabaseMetaData::isAutoIncrementPrimaryKey() const
306 bool is( true );
307 Any setting;
308 if ( lcl_getDriverSetting( "AutoIncrementIsPrimaryKey", *m_pImpl, setting ) )
309 OSL_VERIFY( setting >>= is );
310 return is;
313 sal_Int32 DatabaseMetaData::getBooleanComparisonMode() const
315 sal_Int32 mode( BooleanComparisonMode::EQUAL_INTEGER );
316 Any setting;
317 if ( lcl_getConnectionSetting( "BooleanComparisonMode", *m_pImpl, setting ) )
318 OSL_VERIFY( setting >>= mode );
319 return mode;
322 bool DatabaseMetaData::supportsRelations() const
324 lcl_checkConnected( *m_pImpl );
325 bool bSupport = false;
328 bSupport = m_pImpl->xConnectionMetaData->supportsIntegrityEnhancementFacility();
330 catch( const Exception& )
332 DBG_UNHANDLED_EXCEPTION();
336 if ( !bSupport )
338 const OUString url = m_pImpl->xConnectionMetaData->getURL();
339 char pMySQL[] = "sdbc:mysql";
340 bSupport = url.matchAsciiL(pMySQL,(sizeof(pMySQL)/sizeof(pMySQL[0]))-1);
343 catch( const Exception& )
345 DBG_UNHANDLED_EXCEPTION();
347 return bSupport;
351 bool DatabaseMetaData::supportsColumnAliasInOrderBy() const
353 bool doGenerate( true );
354 Any setting;
355 if ( lcl_getConnectionSetting( "ColumnAliasInOrderBy", *m_pImpl, setting ) )
356 OSL_VERIFY( setting >>= doGenerate );
357 return doGenerate;
361 bool DatabaseMetaData::supportsUserAdministration( const Reference<XComponentContext>& _rContext ) const
363 lcl_checkConnected( *m_pImpl );
365 bool isSupported( false );
368 // find the XUsersSupplier interface
369 // - either directly at the connection
370 Reference< XUsersSupplier > xUsersSupp( m_pImpl->xConnection, UNO_QUERY );
371 if ( !xUsersSupp.is() )
373 // - or at the driver manager
374 Reference< XDriverManager2 > xDriverManager = DriverManager::create( _rContext );
375 Reference< XDataDefinitionSupplier > xDriver( xDriverManager->getDriverByURL( m_pImpl->xConnectionMetaData->getURL() ), UNO_QUERY );
376 if ( xDriver.is() )
377 xUsersSupp.set( xDriver->getDataDefinitionByConnection( m_pImpl->xConnection ), UNO_QUERY );
380 isSupported = ( xUsersSupp.is() && xUsersSupp->getUsers().is() );
382 catch( const Exception& )
384 DBG_UNHANDLED_EXCEPTION();
386 return isSupported;
390 bool DatabaseMetaData::displayEmptyTableFolders() const
392 bool doDisplay( true );
393 #ifdef IMPLEMENTED_LATER
394 Any setting;
395 if ( lcl_getConnectionSetting( "DisplayEmptyTableFolders", *m_pImpl, setting ) )
396 OSL_VERIFY( setting >>= doDisplay );
397 #else
400 Reference< XDatabaseMetaData > xMeta( m_pImpl->xConnectionMetaData, UNO_SET_THROW );
401 OUString sConnectionURL( xMeta->getURL() );
402 doDisplay = sConnectionURL.startsWith( "sdbc:mysql:mysqlc" );
404 catch( const Exception& )
406 DBG_UNHANDLED_EXCEPTION();
408 #endif
409 return doDisplay;
412 bool DatabaseMetaData::supportsThreads() const
414 bool bSupported( true );
417 Reference< XDatabaseMetaData > xMeta( m_pImpl->xConnectionMetaData, UNO_SET_THROW );
418 OUString sConnectionURL( xMeta->getURL() );
419 bSupported = !sConnectionURL.startsWith( "sdbc:mysql:mysqlc" );
421 catch( const Exception& )
423 DBG_UNHANDLED_EXCEPTION();
425 return bSupported;
429 } // namespace dbtools
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */