bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / dbmetadata.cxx
blob4f4aa1e0935317e716e2c876cb073883b09c4739
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 <strings.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/sdb/BooleanComparisonMode.hpp>
31 #include <com/sun/star/sdbc/XDatabaseMetaData2.hpp>
32 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
33 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
34 #include <com/sun/star/sdbc/DriverManager.hpp>
36 #include <tools/diagnose_ex.h>
37 #include <comphelper/namedvaluecollection.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <sal/macros.h>
41 #include <boost/optional.hpp>
44 namespace dbtools
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::sdbc::XConnection;
50 using ::com::sun::star::sdbc::XDatabaseMetaData;
51 using ::com::sun::star::sdbc::XDatabaseMetaData2;
52 using ::com::sun::star::lang::IllegalArgumentException;
53 using ::com::sun::star::uno::Exception;
54 using ::com::sun::star::uno::Any;
55 using ::com::sun::star::uno::XComponentContext;
56 using ::com::sun::star::container::XChild;
57 using ::com::sun::star::uno::UNO_QUERY_THROW;
58 using ::com::sun::star::beans::XPropertySet;
59 using ::com::sun::star::uno::UNO_QUERY;
60 using ::com::sun::star::sdbcx::XUsersSupplier;
61 using ::com::sun::star::sdbcx::XDataDefinitionSupplier;
62 using ::com::sun::star::sdbc::DriverManager;
63 using ::com::sun::star::sdbc::XDriverManager2;
64 using ::com::sun::star::uno::UNO_SET_THROW;
66 namespace BooleanComparisonMode = ::com::sun::star::sdb::BooleanComparisonMode;
68 struct DatabaseMetaData_Impl
70 Reference< XConnection > xConnection;
71 Reference< XDatabaseMetaData > xConnectionMetaData;
72 ::connectivity::DriversConfig aDriverConfig;
74 ::boost::optional< OUString > sCachedIdentifierQuoteString;
75 ::boost::optional< OUString > sCachedCatalogSeparator;
77 DatabaseMetaData_Impl()
78 :xConnection()
79 ,xConnectionMetaData()
80 ,aDriverConfig( ::comphelper::getProcessComponentContext() )
81 ,sCachedIdentifierQuoteString()
82 ,sCachedCatalogSeparator()
88 namespace
91 void lcl_construct( DatabaseMetaData_Impl& _metaDataImpl, const Reference< XConnection >& _connection )
93 _metaDataImpl.xConnection = _connection;
94 if ( !_metaDataImpl.xConnection.is() )
95 return;
97 _metaDataImpl.xConnectionMetaData = _connection->getMetaData();
98 if ( !_metaDataImpl.xConnectionMetaData.is() )
99 throw IllegalArgumentException();
103 void lcl_checkConnected( const DatabaseMetaData_Impl& _metaDataImpl )
105 if ( !_metaDataImpl.xConnection.is() || !_metaDataImpl.xConnectionMetaData.is() )
107 ::connectivity::SharedResources aResources;
108 const OUString sError( aResources.getResourceString(STR_NO_CONNECTION_GIVEN));
109 throwSQLException( sError, StandardSQLState::CONNECTION_DOES_NOT_EXIST, nullptr );
114 bool lcl_getDriverSetting( const sal_Char* _asciiName, const DatabaseMetaData_Impl& _metaData, Any& _out_setting )
116 lcl_checkConnected( _metaData );
117 const ::comphelper::NamedValueCollection& rDriverMetaData = _metaData.aDriverConfig.getMetaData( _metaData.xConnectionMetaData->getURL() );
118 if ( !rDriverMetaData.has( _asciiName ) )
119 return false;
120 _out_setting = rDriverMetaData.get( _asciiName );
121 return true;
125 bool lcl_getConnectionSetting( const sal_Char* _asciiName, const DatabaseMetaData_Impl& _metaData, Any& _out_setting )
129 Reference< XChild > xConnectionAsChild( _metaData.xConnection, UNO_QUERY );
130 if ( xConnectionAsChild.is() )
132 Reference< XPropertySet > xDataSource( xConnectionAsChild->getParent(), UNO_QUERY_THROW );
133 Reference< XPropertySet > xDataSourceSettings(
134 xDataSource->getPropertyValue("Settings"),
135 UNO_QUERY_THROW );
137 _out_setting = xDataSourceSettings->getPropertyValue( OUString::createFromAscii( _asciiName ) );
139 else
141 Reference< XDatabaseMetaData2 > xExtendedMetaData( _metaData.xConnectionMetaData, UNO_QUERY_THROW );
142 ::comphelper::NamedValueCollection aSettings( xExtendedMetaData->getConnectionInfo() );
143 _out_setting = aSettings.get( _asciiName );
144 return _out_setting.hasValue();
146 return true;
148 catch( const Exception& )
150 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
152 return false;
156 const OUString& lcl_getConnectionStringSetting(
157 const DatabaseMetaData_Impl& _metaData, ::boost::optional< OUString >& _cachedSetting,
158 OUString (SAL_CALL XDatabaseMetaData::*_getter)() )
160 if ( !_cachedSetting )
162 lcl_checkConnected( _metaData );
165 _cachedSetting = (_metaData.xConnectionMetaData.get()->*_getter)();
167 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION("connectivity.commontools"); }
169 return *_cachedSetting;
173 DatabaseMetaData::DatabaseMetaData()
174 :m_pImpl( new DatabaseMetaData_Impl )
178 DatabaseMetaData::DatabaseMetaData( const Reference< XConnection >& _connection )
179 :m_pImpl( new DatabaseMetaData_Impl )
181 lcl_construct( *m_pImpl, _connection );
185 DatabaseMetaData::DatabaseMetaData( const DatabaseMetaData& _copyFrom )
186 :m_pImpl( new DatabaseMetaData_Impl( *_copyFrom.m_pImpl ) )
190 DatabaseMetaData::DatabaseMetaData( DatabaseMetaData&& _copyFrom )
191 :m_pImpl(std::move(_copyFrom.m_pImpl))
195 DatabaseMetaData& DatabaseMetaData::operator=( const DatabaseMetaData& _copyFrom )
197 if ( this == &_copyFrom )
198 return *this;
200 m_pImpl.reset( new DatabaseMetaData_Impl( *_copyFrom.m_pImpl ) );
201 return *this;
204 DatabaseMetaData& DatabaseMetaData::operator=( DatabaseMetaData&& _copyFrom )
206 m_pImpl = std::move(_copyFrom.m_pImpl);
207 return *this;
210 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 bSupportsSubQueries = false;
227 sal_Int32 maxTablesInselect = m_pImpl->xConnectionMetaData->getMaxTablesInSelect();
228 bSupportsSubQueries = ( 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("connectivity.commontools");
236 return bSupportsSubQueries;
240 bool DatabaseMetaData::supportsPrimaryKeys() const
242 lcl_checkConnected( *m_pImpl );
244 bool bDoesSupportPrimaryKeys = false;
247 Any setting;
248 if ( !( lcl_getConnectionSetting( "PrimaryKeySupport", *m_pImpl, setting ) )
249 || !( setting >>= bDoesSupportPrimaryKeys )
251 bDoesSupportPrimaryKeys = m_pImpl->xConnectionMetaData->supportsCoreSQLGrammar()
252 || m_pImpl->xConnectionMetaData->supportsANSI92EntryLevelSQL();
254 catch( const Exception& )
256 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
258 return bDoesSupportPrimaryKeys;
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::shouldSubstituteParameterNames() const
306 bool doSubstitute( true );
307 Any setting;
308 if ( lcl_getConnectionSetting( "ParameterNameSubstitution", *m_pImpl, setting ) )
309 OSL_VERIFY( setting >>= doSubstitute );
310 return doSubstitute;
313 bool DatabaseMetaData::isAutoIncrementPrimaryKey() const
315 bool is( true );
316 Any setting;
317 if ( lcl_getDriverSetting( "AutoIncrementIsPrimaryKey", *m_pImpl, setting ) )
318 OSL_VERIFY( setting >>= is );
319 return is;
322 sal_Int32 DatabaseMetaData::getBooleanComparisonMode() const
324 sal_Int32 mode( BooleanComparisonMode::EQUAL_INTEGER );
325 Any setting;
326 if ( lcl_getConnectionSetting( "BooleanComparisonMode", *m_pImpl, setting ) )
327 OSL_VERIFY( setting >>= mode );
328 return mode;
331 bool DatabaseMetaData::supportsRelations() const
333 lcl_checkConnected( *m_pImpl );
334 bool bSupport = false;
337 bSupport = m_pImpl->xConnectionMetaData->supportsIntegrityEnhancementFacility();
339 catch( const Exception& )
341 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
345 if ( !bSupport )
347 const OUString url = m_pImpl->xConnectionMetaData->getURL();
348 bSupport = url.startsWith("sdbc:mysql");
351 catch( const Exception& )
353 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
355 return bSupport;
359 bool DatabaseMetaData::supportsColumnAliasInOrderBy() const
361 bool doGenerate( true );
362 Any setting;
363 if ( lcl_getConnectionSetting( "ColumnAliasInOrderBy", *m_pImpl, setting ) )
364 OSL_VERIFY( setting >>= doGenerate );
365 return doGenerate;
369 bool DatabaseMetaData::supportsUserAdministration( const Reference<XComponentContext>& _rContext ) const
371 lcl_checkConnected( *m_pImpl );
373 bool isSupported( false );
376 // find the XUsersSupplier interface
377 // - either directly at the connection
378 Reference< XUsersSupplier > xUsersSupp( m_pImpl->xConnection, UNO_QUERY );
379 if ( !xUsersSupp.is() )
381 // - or at the driver manager
382 Reference< XDriverManager2 > xDriverManager = DriverManager::create( _rContext );
383 Reference< XDataDefinitionSupplier > xDriver( xDriverManager->getDriverByURL( m_pImpl->xConnectionMetaData->getURL() ), UNO_QUERY );
384 if ( xDriver.is() )
385 xUsersSupp.set( xDriver->getDataDefinitionByConnection( m_pImpl->xConnection ), UNO_QUERY );
388 isSupported = ( xUsersSupp.is() && xUsersSupp->getUsers().is() );
390 catch( const Exception& )
392 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
394 return isSupported;
398 bool DatabaseMetaData::displayEmptyTableFolders() const
400 bool doDisplay( true );
401 #ifdef IMPLEMENTED_LATER
402 Any setting;
403 if ( lcl_getConnectionSetting( "DisplayEmptyTableFolders", *m_pImpl, setting ) )
404 OSL_VERIFY( setting >>= doDisplay );
405 #else
408 Reference< XDatabaseMetaData > xMeta( m_pImpl->xConnectionMetaData, UNO_SET_THROW );
409 OUString sConnectionURL( xMeta->getURL() );
410 doDisplay = sConnectionURL.startsWith( "sdbc:mysql:mysqlc" );
412 catch( const Exception& )
414 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
416 #endif
417 return doDisplay;
420 bool DatabaseMetaData::supportsThreads() const
422 bool bSupported( true );
425 Reference< XDatabaseMetaData > xMeta( m_pImpl->xConnectionMetaData, UNO_SET_THROW );
426 OUString sConnectionURL( xMeta->getURL() );
427 bSupported = !sConnectionURL.startsWith( "sdbc:mysql:mysqlc" );
429 catch( const Exception& )
431 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
433 return bSupported;
437 } // namespace dbtools
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */