Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/connectivity/source/commontools/dbmetadata.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
21
#include <connectivity/dbmetadata.hxx>
22
#include <connectivity/dbexception.hxx>
23
#include <connectivity/DriversConfig.hxx>
24
#include <connectivity/standardsqlstate.hxx>
25
#include <strings.hrc>
26
#include <resource/sharedresources.hxx>
27
28
#include <com/sun/star/lang/IllegalArgumentException.hpp>
29
#include <com/sun/star/container/XChild.hpp>
30
#include <com/sun/star/beans/XPropertySet.hpp>
31
#include <com/sun/star/sdb/BooleanComparisonMode.hpp>
32
#include <com/sun/star/sdbc/XDatabaseMetaData2.hpp>
33
#include <com/sun/star/sdbcx/XUsersSupplier.hpp>
34
#include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
35
#include <com/sun/star/sdbc/DriverManager.hpp>
36
37
#include <comphelper/diagnose_ex.hxx>
38
#include <comphelper/namedvaluecollection.hxx>
39
#include <comphelper/processfactory.hxx>
40
#include <sal/log.hxx>
41
42
#include <optional>
43
44
45
namespace dbtools
46
{
47
48
49
    using ::com::sun::star::uno::Reference;
50
    using ::com::sun::star::sdbc::XConnection;
51
    using ::com::sun::star::sdbc::XDatabaseMetaData;
52
    using ::com::sun::star::sdbc::XDatabaseMetaData2;
53
    using ::com::sun::star::lang::IllegalArgumentException;
54
    using ::com::sun::star::uno::Exception;
55
    using ::com::sun::star::uno::Any;
56
    using ::com::sun::star::uno::XComponentContext;
57
    using ::com::sun::star::container::XChild;
58
    using ::com::sun::star::uno::UNO_QUERY_THROW;
59
    using ::com::sun::star::beans::XPropertySet;
60
    using ::com::sun::star::uno::UNO_QUERY;
61
    using ::com::sun::star::sdbcx::XUsersSupplier;
62
    using ::com::sun::star::sdbcx::XDataDefinitionSupplier;
63
    using ::com::sun::star::sdbc::DriverManager;
64
    using ::com::sun::star::sdbc::XDriverManager2;
65
    using ::com::sun::star::uno::UNO_SET_THROW;
66
67
    namespace BooleanComparisonMode = ::com::sun::star::sdb::BooleanComparisonMode;
68
69
    struct DatabaseMetaData_Impl
70
    {
71
        Reference< XConnection >        xConnection;
72
        Reference< XDatabaseMetaData >  xConnectionMetaData;
73
        ::connectivity::DriversConfig   aDriverConfig;
74
75
        ::std::optional< OUString >    sCachedIdentifierQuoteString;
76
        ::std::optional< OUString >    sCachedCatalogSeparator;
77
78
        DatabaseMetaData_Impl()
79
192k
            : aDriverConfig( ::comphelper::getProcessComponentContext() )
80
192k
        {
81
192k
        }
82
    };
83
84
85
    namespace
86
    {
87
88
        void lcl_construct( DatabaseMetaData_Impl& _metaDataImpl, const Reference< XConnection >& _connection )
89
192k
        {
90
192k
            _metaDataImpl.xConnection = _connection;
91
192k
            if ( !_metaDataImpl.xConnection.is() )
92
0
                return;
93
94
192k
            _metaDataImpl.xConnectionMetaData = _connection->getMetaData();
95
192k
            if ( !_metaDataImpl.xConnectionMetaData.is() )
96
0
                throw IllegalArgumentException();
97
192k
        }
98
99
100
        void lcl_checkConnected( const DatabaseMetaData_Impl& _metaDataImpl )
101
220k
        {
102
220k
            if ( !_metaDataImpl.xConnection.is() || !_metaDataImpl.xConnectionMetaData.is() )
103
0
            {
104
0
                ::connectivity::SharedResources aResources;
105
0
                const OUString sError( aResources.getResourceString(STR_NO_CONNECTION_GIVEN));
106
0
                throwSQLException( sError, StandardSQLState::CONNECTION_DOES_NOT_EXIST, nullptr );
107
0
            }
108
220k
        }
109
110
111
        bool lcl_getDriverSetting( const OUString& _asciiName, const DatabaseMetaData_Impl& _metaData, Any& _out_setting )
112
0
        {
113
0
            lcl_checkConnected( _metaData );
114
0
            const ::comphelper::NamedValueCollection& rDriverMetaData = _metaData.aDriverConfig.getMetaData( _metaData.xConnectionMetaData->getURL() );
115
0
            if ( !rDriverMetaData.has( _asciiName ) )
116
0
                return false;
117
0
            _out_setting = rDriverMetaData.get( _asciiName );
118
0
            return true;
119
0
        }
120
121
122
        bool lcl_getConnectionSetting(const OUString& _asciiName, const DatabaseMetaData_Impl& _metaData, Any& _out_setting )
123
0
        {
124
0
            try
125
0
            {
126
0
                Reference< XChild > xConnectionAsChild( _metaData.xConnection, UNO_QUERY );
127
0
                if ( xConnectionAsChild.is() )
128
0
                {
129
0
                    Reference< XPropertySet > xDataSource( xConnectionAsChild->getParent(), UNO_QUERY_THROW );
130
0
                    Reference< XPropertySet > xDataSourceSettings(
131
0
                        xDataSource->getPropertyValue(u"Settings"_ustr),
132
0
                        UNO_QUERY_THROW );
133
134
0
                    _out_setting = xDataSourceSettings->getPropertyValue( _asciiName );
135
0
                }
136
0
                else
137
0
                {
138
0
                    Reference< XDatabaseMetaData2 > xExtendedMetaData( _metaData.xConnectionMetaData, UNO_QUERY_THROW );
139
0
                    _out_setting = ::comphelper::NamedValueCollection::get( xExtendedMetaData->getConnectionInfo(), _asciiName );
140
0
                    return _out_setting.hasValue();
141
0
                }
142
0
                return true;
143
0
            }
144
0
            catch( const Exception& )
145
0
            {
146
0
                DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
147
0
            }
148
0
            return false;
149
0
        }
150
151
152
        const OUString& lcl_getConnectionStringSetting(
153
            const DatabaseMetaData_Impl& _metaData, ::std::optional< OUString >& _cachedSetting,
154
            OUString (SAL_CALL XDatabaseMetaData::*_getter)() )
155
201k
        {
156
201k
            if ( !_cachedSetting )
157
153k
            {
158
153k
                lcl_checkConnected( _metaData );
159
153k
                try
160
153k
                {
161
153k
                    _cachedSetting = (_metaData.xConnectionMetaData.get()->*_getter)();
162
153k
                }
163
153k
                catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION("connectivity.commontools"); }
164
153k
            }
165
201k
            return *_cachedSetting;
166
201k
        }
167
    }
168
169
    DatabaseMetaData::DatabaseMetaData()
170
0
        :m_pImpl( new DatabaseMetaData_Impl )
171
0
    {
172
0
    }
173
174
    DatabaseMetaData::DatabaseMetaData( const Reference< XConnection >& _connection )
175
192k
        :m_pImpl( new DatabaseMetaData_Impl )
176
192k
    {
177
192k
        lcl_construct( *m_pImpl, _connection );
178
192k
    }
179
180
181
    DatabaseMetaData::DatabaseMetaData( const DatabaseMetaData& _copyFrom )
182
795k
        :m_pImpl( new DatabaseMetaData_Impl( *_copyFrom.m_pImpl ) )
183
795k
    {
184
795k
    }
185
186
    DatabaseMetaData::DatabaseMetaData(DatabaseMetaData&& _copyFrom) noexcept
187
0
        :m_pImpl(std::move(_copyFrom.m_pImpl))
188
0
    {
189
0
    }
190
191
    DatabaseMetaData& DatabaseMetaData::operator=( const DatabaseMetaData& _copyFrom )
192
0
    {
193
0
        if ( this == &_copyFrom )
194
0
            return *this;
195
196
0
        m_pImpl.reset( new DatabaseMetaData_Impl( *_copyFrom.m_pImpl ) );
197
0
        return *this;
198
0
    }
199
200
    DatabaseMetaData& DatabaseMetaData::operator=(DatabaseMetaData&& _copyFrom) noexcept
201
0
    {
202
0
        m_pImpl = std::move(_copyFrom.m_pImpl);
203
0
        return *this;
204
0
    }
205
206
    DatabaseMetaData::~DatabaseMetaData()
207
988k
    {
208
988k
    }
209
210
    bool DatabaseMetaData::isConnected() const
211
0
    {
212
0
        return m_pImpl->xConnection.is();
213
0
    }
214
215
216
    bool DatabaseMetaData::supportsSubqueriesInFrom() const
217
67.4k
    {
218
67.4k
        lcl_checkConnected( *m_pImpl );
219
220
67.4k
        bool bSupportsSubQueries = false;
221
67.4k
        try
222
67.4k
        {
223
67.4k
            sal_Int32 maxTablesInselect = m_pImpl->xConnectionMetaData->getMaxTablesInSelect();
224
67.4k
            bSupportsSubQueries = ( maxTablesInselect > 1 ) || ( maxTablesInselect == 0 );
225
            // TODO: is there a better way to determine this? The above is not really true. More precise,
226
            // it's a *very* generous heuristics ...
227
67.4k
        }
228
67.4k
        catch( const Exception& )
229
67.4k
        {
230
0
            DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
231
0
        }
232
67.4k
        return bSupportsSubQueries;
233
67.4k
    }
234
235
236
    bool DatabaseMetaData::supportsPrimaryKeys() const
237
0
    {
238
0
        lcl_checkConnected( *m_pImpl );
239
240
0
        bool bDoesSupportPrimaryKeys = false;
241
0
        try
242
0
        {
243
0
            Any setting;
244
0
            if  (   !( lcl_getConnectionSetting( u"PrimaryKeySupport"_ustr, *m_pImpl, setting ) )
245
0
                ||  !( setting >>= bDoesSupportPrimaryKeys )
246
0
                )
247
0
                bDoesSupportPrimaryKeys = m_pImpl->xConnectionMetaData->supportsCoreSQLGrammar()
248
0
                    || m_pImpl->xConnectionMetaData->supportsANSI92EntryLevelSQL();
249
0
        }
250
0
        catch( const Exception& )
251
0
        {
252
0
            DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
253
0
        }
254
0
        return bDoesSupportPrimaryKeys;
255
0
    }
256
257
258
    const OUString&  DatabaseMetaData::getIdentifierQuoteString() const
259
95.9k
    {
260
95.9k
        return lcl_getConnectionStringSetting( *m_pImpl, m_pImpl->sCachedIdentifierQuoteString, &XDatabaseMetaData::getIdentifierQuoteString );
261
95.9k
    }
262
263
264
    const OUString&  DatabaseMetaData::getCatalogSeparator() const
265
105k
    {
266
105k
        return lcl_getConnectionStringSetting( *m_pImpl, m_pImpl->sCachedCatalogSeparator, &XDatabaseMetaData::getCatalogSeparator );
267
105k
    }
268
269
270
    bool DatabaseMetaData::restrictIdentifiersToSQL92() const
271
0
    {
272
0
        lcl_checkConnected( *m_pImpl );
273
274
0
        bool restrict( false );
275
0
        Any setting;
276
0
        if ( lcl_getConnectionSetting( u"EnableSQL92Check"_ustr, *m_pImpl, setting ) )
277
0
            if( ! (setting >>= restrict) )
278
0
                SAL_WARN("connectivity.commontools", "restrictIdentifiersToSQL92: unable to assign EnableSQL92Check");
279
0
        return restrict;
280
0
    }
281
282
283
    bool DatabaseMetaData::generateASBeforeCorrelationName() const
284
0
    {
285
0
        bool doGenerate( false );
286
0
        Any setting;
287
0
        if ( lcl_getConnectionSetting( u"GenerateASBeforeCorrelationName"_ustr, *m_pImpl, setting ) )
288
0
            if( ! (setting >>= doGenerate) )
289
0
                SAL_WARN("connectivity.commontools", "generateASBeforeCorrelationName: unable to assign GenerateASBeforeCorrelationName");
290
0
        return doGenerate;
291
0
    }
292
293
    bool DatabaseMetaData::shouldEscapeDateTime() const
294
0
    {
295
0
        bool doGenerate( true );
296
0
        Any setting;
297
0
        if ( lcl_getConnectionSetting( u"EscapeDateTime"_ustr, *m_pImpl, setting ) )
298
0
            if( ! (setting >>= doGenerate) )
299
0
                SAL_WARN("connectivity.commontools", "shouldEscapeDateTime: unable to assign EscapeDateTime");
300
0
        return doGenerate;
301
0
    }
302
303
    bool DatabaseMetaData::shouldSubstituteParameterNames() const
304
0
    {
305
0
        bool doSubstitute( true );
306
0
        Any setting;
307
0
        if ( lcl_getConnectionSetting( u"ParameterNameSubstitution"_ustr, *m_pImpl, setting ) )
308
0
            if( ! (setting >>= doSubstitute) )
309
0
                SAL_WARN("connectivity.commontools", "shouldSubstituteParameterNames: unable to assign ParameterNameSubstitution");
310
0
        return doSubstitute;
311
0
    }
312
313
    bool DatabaseMetaData::isAutoIncrementPrimaryKey() const
314
0
    {
315
0
        bool is( true );
316
0
        Any setting;
317
0
        if ( lcl_getDriverSetting( u"AutoIncrementIsPrimaryKey"_ustr, *m_pImpl, setting ) )
318
0
            if( ! (setting >>= is) )
319
0
                SAL_WARN("connectivity.commontools", "isAutoIncrementPrimaryKey: unable to assign AutoIncrementIsPrimaryKey");
320
0
        return is;
321
0
    }
322
323
    sal_Int32 DatabaseMetaData::getBooleanComparisonMode() const
324
0
    {
325
0
        sal_Int32 mode( BooleanComparisonMode::EQUAL_INTEGER );
326
0
        Any setting;
327
0
        if ( lcl_getConnectionSetting( u"BooleanComparisonMode"_ustr, *m_pImpl, setting ) )
328
0
            if( ! (setting >>= mode) )
329
0
                SAL_WARN("connectivity.commontools", "getBooleanComparisonMode: unable to assign BooleanComparisonMode");
330
0
        return mode;
331
0
    }
332
333
    bool DatabaseMetaData::supportsRelations() const
334
0
    {
335
0
        lcl_checkConnected( *m_pImpl );
336
0
        bool bSupport = false;
337
0
        try
338
0
        {
339
0
            bSupport = m_pImpl->xConnectionMetaData->supportsIntegrityEnhancementFacility();
340
0
        }
341
0
        catch( const Exception& )
342
0
        {
343
0
            DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
344
0
        }
345
0
        try
346
0
        {
347
0
            if ( !bSupport )
348
0
            {
349
0
                const OUString url = m_pImpl->xConnectionMetaData->getURL();
350
0
                bSupport = url.startsWith("sdbc:mysql");
351
0
            }
352
0
        }
353
0
        catch( const Exception& )
354
0
        {
355
0
            DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
356
0
        }
357
0
        return bSupport;
358
0
    }
359
360
361
    bool DatabaseMetaData::supportsColumnAliasInOrderBy() const
362
0
    {
363
0
        bool doGenerate( true );
364
0
        Any setting;
365
0
        if ( lcl_getConnectionSetting( u"ColumnAliasInOrderBy"_ustr, *m_pImpl, setting ) )
366
0
            if( ! (setting >>= doGenerate) )
367
0
                SAL_WARN("connectivity.commontools", "supportsColumnAliasInOrderBy: unable to assign ColumnAliasInOrderBy");
368
0
        return doGenerate;
369
0
    }
370
371
372
    bool DatabaseMetaData::supportsUserAdministration( const Reference<XComponentContext>& _rContext ) const
373
0
    {
374
0
        lcl_checkConnected( *m_pImpl  );
375
376
0
        bool isSupported( false );
377
0
        try
378
0
        {
379
            // find the XUsersSupplier interface
380
            // - either directly at the connection
381
0
            Reference< XUsersSupplier > xUsersSupp( m_pImpl->xConnection, UNO_QUERY );
382
0
            if ( !xUsersSupp.is() )
383
0
            {
384
                // - or at the driver manager
385
0
                Reference< XDriverManager2 > xDriverManager = DriverManager::create( _rContext );
386
0
                Reference< XDataDefinitionSupplier > xDriver( xDriverManager->getDriverByURL( m_pImpl->xConnectionMetaData->getURL() ), UNO_QUERY );
387
0
                if ( xDriver.is() )
388
0
                    xUsersSupp.set( xDriver->getDataDefinitionByConnection( m_pImpl->xConnection ), UNO_QUERY );
389
0
            }
390
391
0
            isSupported = ( xUsersSupp.is() && xUsersSupp->getUsers().is() );
392
0
        }
393
0
        catch( const Exception& )
394
0
        {
395
0
            DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
396
0
        }
397
0
        return isSupported;
398
0
    }
399
400
401
    bool DatabaseMetaData::displayEmptyTableFolders() const
402
0
    {
403
0
        bool doDisplay( true );
404
#ifdef IMPLEMENTED_LATER
405
        Any setting;
406
        if ( lcl_getConnectionSetting( "DisplayEmptyTableFolders", *m_pImpl, setting ) )
407
            if( ! (setting >>= doDisplay) )
408
                SAL_WARN("connectivity.commontools", "displayEmptyTableFolders: unable to assign DisplayEmptyTableFolders");
409
#else
410
0
        try
411
0
        {
412
0
            Reference< XDatabaseMetaData > xMeta( m_pImpl->xConnectionMetaData, UNO_SET_THROW );
413
0
            OUString sConnectionURL( xMeta->getURL() );
414
0
            doDisplay = sConnectionURL.startsWith( "sdbc:mysql:mysqlc" );
415
0
        }
416
0
        catch( const Exception& )
417
0
        {
418
0
            DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
419
0
        }
420
0
#endif
421
0
        return doDisplay;
422
0
    }
423
424
    bool DatabaseMetaData::supportsThreads() const
425
0
    {
426
0
        bool bSupported( true );
427
0
        try
428
0
        {
429
0
            Reference< XDatabaseMetaData > xMeta( m_pImpl->xConnectionMetaData, UNO_SET_THROW );
430
0
            OUString sConnectionURL( xMeta->getURL() );
431
0
            bSupported = !sConnectionURL.startsWith( "sdbc:mysql:mysqlc" );
432
0
        }
433
0
        catch( const Exception& )
434
0
        {
435
0
            DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
436
0
        }
437
0
        return bSupported;
438
0
    }
439
440
441
} // namespace dbtools
442
443
444
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */