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 .
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>
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
;
76 //= DatabaseMetaData_Impl
78 struct DatabaseMetaData_Impl
80 Reference
< XConnection
> xConnection
;
81 Reference
< XDatabaseMetaData
> xConnectionMetaData
;
82 ::connectivity::DriversConfig aDriverConfig
;
84 ::boost::optional
< OUString
> sCachedIdentifierQuoteString
;
85 ::boost::optional
< OUString
> sCachedCatalogSeparator
;
87 DatabaseMetaData_Impl()
89 ,xConnectionMetaData()
90 ,aDriverConfig( ::comphelper::getProcessComponentContext() )
91 ,sCachedIdentifierQuoteString()
92 ,sCachedCatalogSeparator()
101 static void lcl_construct( DatabaseMetaData_Impl
& _metaDataImpl
, const Reference
< XConnection
>& _connection
)
103 _metaDataImpl
.xConnection
= _connection
;
104 if ( !_metaDataImpl
.xConnection
.is() )
107 _metaDataImpl
.xConnectionMetaData
= _connection
->getMetaData();
108 if ( !_metaDataImpl
.xConnectionMetaData
.is() )
109 throw IllegalArgumentException();
113 static void lcl_checkConnected( const DatabaseMetaData_Impl
& _metaDataImpl
)
115 if ( !_metaDataImpl
.xConnection
.is() || !_metaDataImpl
.xConnectionMetaData
.is() )
117 ::connectivity::SharedResources aResources
;
118 const OUString
sError( aResources
.getResourceString(STR_NO_CONNECTION_GIVEN
));
119 throwSQLException( sError
, SQL_CONNECTION_DOES_NOT_EXIST
, NULL
);
124 static bool lcl_getDriverSetting( const sal_Char
* _asciiName
, const DatabaseMetaData_Impl
& _metaData
, Any
& _out_setting
)
126 lcl_checkConnected( _metaData
);
127 const ::comphelper::NamedValueCollection
& rDriverMetaData
= _metaData
.aDriverConfig
.getMetaData( _metaData
.xConnectionMetaData
->getURL() );
128 if ( !rDriverMetaData
.has( _asciiName
) )
130 _out_setting
= rDriverMetaData
.get( _asciiName
);
135 static bool lcl_getConnectionSetting( const sal_Char
* _asciiName
, const DatabaseMetaData_Impl
& _metaData
, Any
& _out_setting
)
139 Reference
< XChild
> xConnectionAsChild( _metaData
.xConnection
, UNO_QUERY
);
140 if ( xConnectionAsChild
.is() )
142 Reference
< XPropertySet
> xDataSource( xConnectionAsChild
->getParent(), UNO_QUERY_THROW
);
143 Reference
< XPropertySet
> xDataSourceSettings(
144 xDataSource
->getPropertyValue("Settings"),
147 _out_setting
= xDataSourceSettings
->getPropertyValue( OUString::createFromAscii( _asciiName
) );
151 Reference
< XDatabaseMetaData2
> xExtendedMetaData( _metaData
.xConnectionMetaData
, UNO_QUERY_THROW
);
152 ::comphelper::NamedValueCollection
aSettings( xExtendedMetaData
->getConnectionInfo() );
153 _out_setting
= aSettings
.get( _asciiName
);
154 return _out_setting
.hasValue();
158 catch( const Exception
& )
160 DBG_UNHANDLED_EXCEPTION();
166 static const OUString
& lcl_getConnectionStringSetting(
167 const DatabaseMetaData_Impl
& _metaData
, ::boost::optional
< OUString
>& _cachedSetting
,
168 OUString (SAL_CALL
XDatabaseMetaData::*_getter
)() )
170 if ( !_cachedSetting
)
172 lcl_checkConnected( _metaData
);
175 _cachedSetting
.reset( (_metaData
.xConnectionMetaData
.get()->*_getter
)() );
177 catch( const Exception
& ) { DBG_UNHANDLED_EXCEPTION(); }
179 return *_cachedSetting
;
187 DatabaseMetaData::DatabaseMetaData()
188 :m_pImpl( new DatabaseMetaData_Impl
)
193 DatabaseMetaData::DatabaseMetaData( const Reference
< XConnection
>& _connection
)
194 :m_pImpl( new DatabaseMetaData_Impl
)
196 lcl_construct( *m_pImpl
, _connection
);
200 DatabaseMetaData::DatabaseMetaData( const DatabaseMetaData
& _copyFrom
)
201 :m_pImpl( new DatabaseMetaData_Impl( *_copyFrom
.m_pImpl
) )
206 DatabaseMetaData
& DatabaseMetaData::operator=( const DatabaseMetaData
& _copyFrom
)
208 if ( this == &_copyFrom
)
211 m_pImpl
.reset( new DatabaseMetaData_Impl( *_copyFrom
.m_pImpl
) );
216 DatabaseMetaData::~DatabaseMetaData()
221 bool DatabaseMetaData::isConnected() const
223 return m_pImpl
->xConnection
.is();
227 bool DatabaseMetaData::supportsSubqueriesInFrom() const
229 lcl_checkConnected( *m_pImpl
);
231 bool supportsSubQueries
= false;
234 sal_Int32 maxTablesInselect
= m_pImpl
->xConnectionMetaData
->getMaxTablesInSelect();
235 supportsSubQueries
= ( maxTablesInselect
> 1 ) || ( maxTablesInselect
== 0 );
236 // TODO: is there a better way to determine this? The above is not really true. More precise,
237 // it's a *very* generous heuristics ...
239 catch( const Exception
& )
241 DBG_UNHANDLED_EXCEPTION();
243 return supportsSubQueries
;
247 bool DatabaseMetaData::supportsPrimaryKeys() const
249 lcl_checkConnected( *m_pImpl
);
251 bool doesSupportPrimaryKeys
= false;
255 if ( !( lcl_getConnectionSetting( "PrimaryKeySupport", *m_pImpl
, setting
) )
256 || !( setting
>>= doesSupportPrimaryKeys
)
258 doesSupportPrimaryKeys
= m_pImpl
->xConnectionMetaData
->supportsCoreSQLGrammar();
260 catch( const Exception
& )
262 DBG_UNHANDLED_EXCEPTION();
264 return doesSupportPrimaryKeys
;
268 const OUString
& DatabaseMetaData::getIdentifierQuoteString() const
270 return lcl_getConnectionStringSetting( *m_pImpl
, m_pImpl
->sCachedIdentifierQuoteString
, &XDatabaseMetaData::getIdentifierQuoteString
);
274 const OUString
& DatabaseMetaData::getCatalogSeparator() const
276 return lcl_getConnectionStringSetting( *m_pImpl
, m_pImpl
->sCachedCatalogSeparator
, &XDatabaseMetaData::getCatalogSeparator
);
280 bool DatabaseMetaData::restrictIdentifiersToSQL92() const
282 lcl_checkConnected( *m_pImpl
);
284 bool restrict( false );
286 if ( lcl_getConnectionSetting( "EnableSQL92Check", *m_pImpl
, setting
) )
287 OSL_VERIFY( setting
>>= restrict
);
292 bool DatabaseMetaData::generateASBeforeCorrelationName() const
294 bool doGenerate( true );
296 if ( lcl_getConnectionSetting( "GenerateASBeforeCorrelationName", *m_pImpl
, setting
) )
297 OSL_VERIFY( setting
>>= doGenerate
);
301 bool DatabaseMetaData::shouldEscapeDateTime() const
303 bool doGenerate( true );
305 if ( lcl_getConnectionSetting( "EscapeDateTime", *m_pImpl
, setting
) )
306 OSL_VERIFY( setting
>>= doGenerate
);
310 bool DatabaseMetaData::isAutoIncrementPrimaryKey() const
314 if ( lcl_getDriverSetting( "AutoIncrementIsPrimaryKey", *m_pImpl
, setting
) )
315 OSL_VERIFY( setting
>>= is
);
319 sal_Int32
DatabaseMetaData::getBooleanComparisonMode() const
321 sal_Int32
mode( BooleanComparisonMode::EQUAL_INTEGER
);
323 if ( lcl_getConnectionSetting( "BooleanComparisonMode", *m_pImpl
, setting
) )
324 OSL_VERIFY( setting
>>= mode
);
328 bool DatabaseMetaData::supportsRelations() const
330 lcl_checkConnected( *m_pImpl
);
331 bool bSupport
= false;
334 bSupport
= m_pImpl
->xConnectionMetaData
->supportsIntegrityEnhancementFacility();
336 catch( const Exception
& )
338 DBG_UNHANDLED_EXCEPTION();
344 const OUString url
= m_pImpl
->xConnectionMetaData
->getURL();
345 char pMySQL
[] = "sdbc:mysql";
346 bSupport
= url
.matchAsciiL(pMySQL
,(sizeof(pMySQL
)/sizeof(pMySQL
[0]))-1);
349 catch( const Exception
& )
351 DBG_UNHANDLED_EXCEPTION();
357 bool DatabaseMetaData::supportsColumnAliasInOrderBy() const
359 bool doGenerate( true );
361 if ( lcl_getConnectionSetting( "ColumnAliasInOrderBy", *m_pImpl
, setting
) )
362 OSL_VERIFY( setting
>>= doGenerate
);
367 bool DatabaseMetaData::supportsUserAdministration( const Reference
<XComponentContext
>& _rContext
) const
369 lcl_checkConnected( *m_pImpl
);
371 bool isSupported( false );
374 // find the XUsersSupplier interface
375 // - either directly at the connection
376 Reference
< XUsersSupplier
> xUsersSupp( m_pImpl
->xConnection
, UNO_QUERY
);
377 if ( !xUsersSupp
.is() )
379 // - or at the driver manager
380 Reference
< XDriverManager2
> xDriverManager
= DriverManager::create( _rContext
);
381 Reference
< XDataDefinitionSupplier
> xDriver( xDriverManager
->getDriverByURL( m_pImpl
->xConnectionMetaData
->getURL() ), UNO_QUERY
);
383 xUsersSupp
.set( xDriver
->getDataDefinitionByConnection( m_pImpl
->xConnection
), UNO_QUERY
);
386 isSupported
= ( xUsersSupp
.is() && xUsersSupp
->getUsers().is() );
388 catch( const Exception
& )
390 DBG_UNHANDLED_EXCEPTION();
396 bool DatabaseMetaData::displayEmptyTableFolders() const
398 bool doDisplay( true );
399 #ifdef IMPLEMENTED_LATER
401 if ( lcl_getConnectionSetting( "DisplayEmptyTableFolders", *m_pImpl
, setting
) )
402 OSL_VERIFY( setting
>>= doDisplay
);
406 Reference
< XDatabaseMetaData
> xMeta( m_pImpl
->xConnectionMetaData
, UNO_SET_THROW
);
407 OUString
sConnectionURL( xMeta
->getURL() );
408 doDisplay
= sConnectionURL
.startsWith( "sdbc:mysql:mysqlc" );
410 catch( const Exception
& )
412 DBG_UNHANDLED_EXCEPTION();
418 bool DatabaseMetaData::supportsThreads() const
420 bool bSupported( true );
423 Reference
< XDatabaseMetaData
> xMeta( m_pImpl
->xConnectionMetaData
, UNO_SET_THROW
);
424 OUString
sConnectionURL( xMeta
->getURL() );
425 bSupported
= !sConnectionURL
.startsWith( "sdbc:mysql:mysqlc" );
427 catch( const Exception
& )
429 DBG_UNHANDLED_EXCEPTION();
435 } // namespace dbtools
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */