1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "connectivity/dbmetadata.hxx"
31 #include "connectivity/dbexception.hxx"
32 #include "connectivity/DriversConfig.hxx"
33 #include "resource/common_res.hrc"
34 #include "resource/sharedresources.hxx"
36 /** === begin UNO includes === **/
37 #include <com/sun/star/lang/IllegalArgumentException.hpp>
38 #include <com/sun/star/container/XChild.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 #include <com/sun/star/beans/XPropertySetInfo.hpp>
42 #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
43 #include <com/sun/star/sdbc/XDatabaseMetaData2.hpp>
44 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
45 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
46 #include <com/sun/star/sdbc/XDriverAccess.hpp>
47 /** === end UNO includes === **/
49 #include <tools/diagnose_ex.h>
50 #include <comphelper/namedvaluecollection.hxx>
51 #include <comphelper/componentcontext.hxx>
52 #include <comphelper/processfactory.hxx>
53 #include <sal/macros.h>
55 #include <boost/optional.hpp>
57 //........................................................................
60 //........................................................................
62 /** === begin UNO using === **/
63 using ::com::sun::star::uno::Reference
;
64 using ::com::sun::star::sdbc::XConnection
;
65 using ::com::sun::star::sdbc::XConnection
;
66 using ::com::sun::star::sdbc::XDatabaseMetaData
;
67 using ::com::sun::star::sdbc::XDatabaseMetaData2
;
68 using ::com::sun::star::lang::IllegalArgumentException
;
69 using ::com::sun::star::uno::Exception
;
70 using ::com::sun::star::uno::Any
;
71 using ::com::sun::star::container::XChild
;
72 using ::com::sun::star::uno::UNO_QUERY_THROW
;
73 using ::com::sun::star::beans::XPropertySet
;
74 using ::com::sun::star::uno::Sequence
;
75 using ::com::sun::star::beans::PropertyValue
;
76 using ::com::sun::star::beans::XPropertySetInfo
;
77 using ::com::sun::star::uno::UNO_QUERY
;
78 using ::com::sun::star::sdbcx::XUsersSupplier
;
79 using ::com::sun::star::sdbcx::XDataDefinitionSupplier
;
80 using ::com::sun::star::sdbc::XDriverAccess
;
81 using ::com::sun::star::uno::UNO_SET_THROW
;
82 /** === end UNO using === **/
83 namespace BooleanComparisonMode
= ::com::sun::star::sdb::BooleanComparisonMode
;
85 //====================================================================
86 //= DatabaseMetaData_Impl
87 //====================================================================
88 struct DatabaseMetaData_Impl
90 Reference
< XConnection
> xConnection
;
91 Reference
< XDatabaseMetaData
> xConnectionMetaData
;
92 ::connectivity::DriversConfig aDriverConfig
;
94 ::boost::optional
< ::rtl::OUString
> sCachedIdentifierQuoteString
;
95 ::boost::optional
< ::rtl::OUString
> sCachedCatalogSeparator
;
97 DatabaseMetaData_Impl()
99 ,xConnectionMetaData()
100 ,aDriverConfig( ::comphelper::getProcessServiceFactory() )
101 ,sCachedIdentifierQuoteString()
102 ,sCachedCatalogSeparator()
107 //--------------------------------------------------------------------
110 //................................................................
111 static void lcl_construct( DatabaseMetaData_Impl
& _metaDataImpl
, const Reference
< XConnection
>& _connection
)
113 _metaDataImpl
.xConnection
= _connection
;
114 if ( !_metaDataImpl
.xConnection
.is() )
117 _metaDataImpl
.xConnectionMetaData
= _connection
->getMetaData();
118 if ( !_metaDataImpl
.xConnectionMetaData
.is() )
119 throw IllegalArgumentException();
122 //................................................................
123 static void lcl_checkConnected( const DatabaseMetaData_Impl
& _metaDataImpl
)
125 if ( !_metaDataImpl
.xConnection
.is() || !_metaDataImpl
.xConnectionMetaData
.is() )
127 ::connectivity::SharedResources aResources
;
128 const ::rtl::OUString
sError( aResources
.getResourceString(STR_NO_CONNECTION_GIVEN
));
129 throwSQLException( sError
, SQL_CONNECTION_DOES_NOT_EXIST
, NULL
);
133 //................................................................
134 static bool lcl_getDriverSetting( const sal_Char
* _asciiName
, const DatabaseMetaData_Impl
& _metaData
, Any
& _out_setting
)
136 lcl_checkConnected( _metaData
);
137 const ::comphelper::NamedValueCollection
& rDriverMetaData
= _metaData
.aDriverConfig
.getMetaData( _metaData
.xConnectionMetaData
->getURL() );
138 if ( !rDriverMetaData
.has( _asciiName
) )
140 _out_setting
= rDriverMetaData
.get( _asciiName
);
144 //................................................................
145 static bool lcl_getConnectionSetting( const sal_Char
* _asciiName
, const DatabaseMetaData_Impl
& _metaData
, Any
& _out_setting
)
149 Reference
< XChild
> xConnectionAsChild( _metaData
.xConnection
, UNO_QUERY
);
150 if ( xConnectionAsChild
.is() )
152 Reference
< XPropertySet
> xDataSource( xConnectionAsChild
->getParent(), UNO_QUERY_THROW
);
153 Reference
< XPropertySet
> xDataSourceSettings(
154 xDataSource
->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Settings" ) ) ),
157 _out_setting
= xDataSourceSettings
->getPropertyValue( ::rtl::OUString::createFromAscii( _asciiName
) );
161 Reference
< XDatabaseMetaData2
> xExtendedMetaData( _metaData
.xConnectionMetaData
, UNO_QUERY_THROW
);
162 ::comphelper::NamedValueCollection
aSettings( xExtendedMetaData
->getConnectionInfo() );
163 _out_setting
= aSettings
.get( _asciiName
);
164 return _out_setting
.hasValue();
168 catch( const Exception
& )
170 DBG_UNHANDLED_EXCEPTION();
175 //................................................................
176 static const ::rtl::OUString
& lcl_getConnectionStringSetting(
177 const DatabaseMetaData_Impl
& _metaData
, ::boost::optional
< ::rtl::OUString
>& _cachedSetting
,
178 ::rtl::OUString (SAL_CALL
XDatabaseMetaData::*_getter
)() )
180 if ( !_cachedSetting
)
182 lcl_checkConnected( _metaData
);
185 _cachedSetting
.reset( (_metaData
.xConnectionMetaData
.get()->*_getter
)() );
187 catch( const Exception
& ) { DBG_UNHANDLED_EXCEPTION(); }
189 return *_cachedSetting
;
193 //====================================================================
195 //====================================================================
196 //--------------------------------------------------------------------
197 DatabaseMetaData::DatabaseMetaData()
198 :m_pImpl( new DatabaseMetaData_Impl
)
202 //--------------------------------------------------------------------
203 DatabaseMetaData::DatabaseMetaData( const Reference
< XConnection
>& _connection
)
204 :m_pImpl( new DatabaseMetaData_Impl
)
206 lcl_construct( *m_pImpl
, _connection
);
209 //--------------------------------------------------------------------
210 DatabaseMetaData::DatabaseMetaData( const DatabaseMetaData
& _copyFrom
)
211 :m_pImpl( new DatabaseMetaData_Impl( *_copyFrom
.m_pImpl
) )
215 //--------------------------------------------------------------------
216 DatabaseMetaData
& DatabaseMetaData::operator=( const DatabaseMetaData
& _copyFrom
)
218 if ( this == &_copyFrom
)
221 m_pImpl
.reset( new DatabaseMetaData_Impl( *_copyFrom
.m_pImpl
) );
225 //--------------------------------------------------------------------
226 DatabaseMetaData::~DatabaseMetaData()
230 //--------------------------------------------------------------------
231 bool DatabaseMetaData::isConnected() const
233 return m_pImpl
->xConnection
.is();
236 //--------------------------------------------------------------------
237 bool DatabaseMetaData::supportsSubqueriesInFrom() const
239 lcl_checkConnected( *m_pImpl
);
241 bool supportsSubQueries
= false;
244 sal_Int32 maxTablesInselect
= m_pImpl
->xConnectionMetaData
->getMaxTablesInSelect();
245 supportsSubQueries
= ( maxTablesInselect
> 1 ) || ( maxTablesInselect
== 0 );
246 // TODO: is there a better way to determine this? The above is not really true. More precise,
247 // it's a *very* generous heuristics ...
249 catch( const Exception
& )
251 DBG_UNHANDLED_EXCEPTION();
253 return supportsSubQueries
;
256 //--------------------------------------------------------------------
257 bool DatabaseMetaData::supportsPrimaryKeys() const
259 lcl_checkConnected( *m_pImpl
);
261 bool doesSupportPrimaryKeys
= false;
265 if ( !( lcl_getConnectionSetting( "PrimaryKeySupport", *m_pImpl
, setting
) )
266 || !( setting
>>= doesSupportPrimaryKeys
)
268 doesSupportPrimaryKeys
= m_pImpl
->xConnectionMetaData
->supportsCoreSQLGrammar();
270 catch( const Exception
& )
272 DBG_UNHANDLED_EXCEPTION();
274 return doesSupportPrimaryKeys
;
277 //--------------------------------------------------------------------
278 const ::rtl::OUString
& DatabaseMetaData::getIdentifierQuoteString() const
280 return lcl_getConnectionStringSetting( *m_pImpl
, m_pImpl
->sCachedIdentifierQuoteString
, &XDatabaseMetaData::getIdentifierQuoteString
);
283 //--------------------------------------------------------------------
284 const ::rtl::OUString
& DatabaseMetaData::getCatalogSeparator() const
286 return lcl_getConnectionStringSetting( *m_pImpl
, m_pImpl
->sCachedCatalogSeparator
, &XDatabaseMetaData::getCatalogSeparator
);
289 //--------------------------------------------------------------------
290 bool DatabaseMetaData::restrictIdentifiersToSQL92() const
292 lcl_checkConnected( *m_pImpl
);
294 bool restrict( false );
296 if ( lcl_getConnectionSetting( "EnableSQL92Check", *m_pImpl
, setting
) )
297 OSL_VERIFY( setting
>>= restrict
);
301 //--------------------------------------------------------------------
302 bool DatabaseMetaData::generateASBeforeCorrelationName() const
304 bool doGenerate( true );
306 if ( lcl_getConnectionSetting( "GenerateASBeforeCorrelationName", *m_pImpl
, setting
) )
307 OSL_VERIFY( setting
>>= doGenerate
);
310 //--------------------------------------------------------------------
311 bool DatabaseMetaData::shouldEscapeDateTime() const
313 bool doGenerate( true );
315 if ( lcl_getConnectionSetting( "EscapeDateTime", *m_pImpl
, setting
) )
316 OSL_VERIFY( setting
>>= doGenerate
);
319 //--------------------------------------------------------------------
320 bool DatabaseMetaData::isAutoIncrementPrimaryKey() const
324 if ( lcl_getDriverSetting( "AutoIncrementIsPrimaryKey", *m_pImpl
, setting
) )
325 OSL_VERIFY( setting
>>= is
);
328 //--------------------------------------------------------------------
329 sal_Int32
DatabaseMetaData::getBooleanComparisonMode() const
331 sal_Int32
mode( BooleanComparisonMode::EQUAL_INTEGER
);
333 if ( lcl_getConnectionSetting( "BooleanComparisonMode", *m_pImpl
, setting
) )
334 OSL_VERIFY( setting
>>= mode
);
337 //--------------------------------------------------------------------
338 bool DatabaseMetaData::supportsRelations() const
340 lcl_checkConnected( *m_pImpl
);
341 bool bSupport
= false;
344 bSupport
= m_pImpl
->xConnectionMetaData
->supportsIntegrityEnhancementFacility();
346 catch( const Exception
& )
348 DBG_UNHANDLED_EXCEPTION();
354 const ::rtl::OUString url
= m_pImpl
->xConnectionMetaData
->getURL();
355 char pMySQL
[] = "sdbc:mysql";
356 bSupport
= url
.matchAsciiL(pMySQL
,(sizeof(pMySQL
)/sizeof(pMySQL
[0]))-1);
359 catch( const Exception
& )
361 DBG_UNHANDLED_EXCEPTION();
366 //--------------------------------------------------------------------
367 bool DatabaseMetaData::supportsColumnAliasInOrderBy() const
369 bool doGenerate( true );
371 if ( lcl_getConnectionSetting( "ColumnAliasInOrderBy", *m_pImpl
, setting
) )
372 OSL_VERIFY( setting
>>= doGenerate
);
376 //--------------------------------------------------------------------
377 bool DatabaseMetaData::supportsUserAdministration( const ::comphelper::ComponentContext
& _rContext
) const
379 lcl_checkConnected( *m_pImpl
);
381 bool isSupported( false );
384 // find the XUsersSupplier interface
385 // - either directly at the connection
386 Reference
< XUsersSupplier
> xUsersSupp( m_pImpl
->xConnection
, UNO_QUERY
);
387 if ( !xUsersSupp
.is() )
389 // - or at the driver manager
390 Reference
< XDriverAccess
> xDriverManager(
391 _rContext
.createComponent( "com.sun.star.sdbc.DriverManager" ), UNO_QUERY_THROW
);
392 Reference
< XDataDefinitionSupplier
> xDriver( xDriverManager
->getDriverByURL( m_pImpl
->xConnectionMetaData
->getURL() ), UNO_QUERY
);
394 xUsersSupp
.set( xDriver
->getDataDefinitionByConnection( m_pImpl
->xConnection
), UNO_QUERY
);
397 isSupported
= ( xUsersSupp
.is() && xUsersSupp
->getUsers().is() );
399 catch( const Exception
& )
401 DBG_UNHANDLED_EXCEPTION();
406 //--------------------------------------------------------------------
407 bool DatabaseMetaData::displayEmptyTableFolders() const
409 bool doDisplay( true );
410 #ifdef IMPLEMENTED_LATER
412 if ( lcl_getConnectionSetting( "DisplayEmptyTableFolders", *m_pImpl
, setting
) )
413 OSL_VERIFY( setting
>>= doDisplay
);
417 Reference
< XDatabaseMetaData
> xMeta( m_pImpl
->xConnectionMetaData
, UNO_SET_THROW
);
418 ::rtl::OUString
sConnectionURL( xMeta
->getURL() );
419 doDisplay
= sConnectionURL
.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "sdbc:mysql:mysqlc" ) ) == 0;
421 catch( const Exception
& )
423 DBG_UNHANDLED_EXCEPTION();
428 //--------------------------------------------------------------------
429 bool DatabaseMetaData::supportsThreads() const
431 bool bSupported( true );
434 Reference
< XDatabaseMetaData
> xMeta( m_pImpl
->xConnectionMetaData
, UNO_SET_THROW
);
435 ::rtl::OUString
sConnectionURL( xMeta
->getURL() );
436 bSupported
= sConnectionURL
.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "sdbc:mysql:mysqlc" ) ) != 0;
438 catch( const Exception
& )
440 DBG_UNHANDLED_EXCEPTION();
445 //........................................................................
446 } // namespace dbtools
447 //........................................................................
449 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */