Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/connectivity/source/drivers/dbase/DDatabaseMetaData.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
#include <dbase/DDatabaseMetaData.hxx>
21
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
22
#include <com/sun/star/sdbc/ColumnSearch.hpp>
23
#include <com/sun/star/sdbc/DataType.hpp>
24
#include <com/sun/star/sdbc/ColumnValue.hpp>
25
#include <com/sun/star/beans/XPropertySet.hpp>
26
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
27
#include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
28
#include <FDatabaseMetaDataResultSet.hxx>
29
#include <dbase/DIndex.hxx>
30
#include <connectivity/FValue.hxx>
31
#include <comphelper/processfactory.hxx>
32
#include <comphelper/types.hxx>
33
#include <ucbhelper/content.hxx>
34
35
using namespace ::comphelper;
36
using namespace connectivity::dbase;
37
using namespace connectivity;
38
using namespace ::com::sun::star::uno;
39
using namespace ::com::sun::star::beans;
40
using namespace ::com::sun::star::sdbcx;
41
using namespace ::com::sun::star::sdbc;
42
using namespace ::com::sun::star::container;
43
using namespace ::com::sun::star::ucb;
44
using namespace ::com::sun::star::lang;
45
46
21.4k
ODbaseDatabaseMetaData::ODbaseDatabaseMetaData(::connectivity::file::OConnection* _pCon)    :ODatabaseMetaData(_pCon)
47
21.4k
{
48
21.4k
}
49
50
ODbaseDatabaseMetaData::~ODbaseDatabaseMetaData()
51
21.4k
{
52
21.4k
}
53
54
Reference< XResultSet > ODbaseDatabaseMetaData::impl_getTypeInfo_throw(  )
55
0
{
56
0
    rtl::Reference<::connectivity::ODatabaseMetaDataResultSet> pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo);
57
58
0
    ODatabaseMetaDataResultSet::ORows aRows;
59
0
    aRows.reserve(10);
60
0
    ODatabaseMetaDataResultSet::ORow aRow
61
0
    {
62
0
        ODatabaseMetaDataResultSet::getEmptyValue(),
63
0
        new ORowSetValueDecorator(u"VARCHAR"_ustr),
64
0
        new ORowSetValueDecorator(DataType::VARCHAR),
65
0
        new ORowSetValueDecorator(sal_Int32(254)),
66
0
        ODatabaseMetaDataResultSet::getQuoteValue(),
67
0
        ODatabaseMetaDataResultSet::getQuoteValue(),
68
0
        new ORowSetValueDecorator(u"length"_ustr),
69
0
        new ORowSetValueDecorator(sal_Int32(ColumnValue::NULLABLE)),
70
0
        ODatabaseMetaDataResultSet::get1Value(),
71
0
        new ORowSetValueDecorator(sal_Int32(ColumnSearch::FULL)),
72
0
        ODatabaseMetaDataResultSet::get1Value(),
73
0
        ODatabaseMetaDataResultSet::get0Value(),
74
0
        ODatabaseMetaDataResultSet::get0Value(),
75
0
        new ORowSetValueDecorator(u"C"_ustr),
76
0
        ODatabaseMetaDataResultSet::get0Value(),
77
0
        ODatabaseMetaDataResultSet::get0Value(),
78
0
        ODatabaseMetaDataResultSet::getEmptyValue(),
79
0
        ODatabaseMetaDataResultSet::getEmptyValue(),
80
0
        new ORowSetValueDecorator(sal_Int32(10))
81
0
    };
82
83
0
    aRows.push_back(aRow);
84
85
0
    aRow[1] = new ORowSetValueDecorator(u"LONGVARCHAR"_ustr);
86
0
    aRow[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR);
87
0
    aRow[3] = new ORowSetValueDecorator(sal_Int32(2147483647));
88
0
    aRow[6] = new ORowSetValueDecorator();
89
0
    aRow[13] = new ORowSetValueDecorator(u"M"_ustr);
90
0
    aRows.push_back(aRow);
91
92
0
    aRow[1] = new ORowSetValueDecorator(u"DATE"_ustr);
93
0
    aRow[2] = new ORowSetValueDecorator(DataType::DATE);
94
0
    aRow[3] = new ORowSetValueDecorator(sal_Int32(10));
95
0
    aRow[13] = new ORowSetValueDecorator(u"D"_ustr);
96
0
    aRows.push_back(aRow);
97
98
0
    aRow[1] = new ORowSetValueDecorator(u"BOOLEAN"_ustr);
99
0
    aRow[2] = new ORowSetValueDecorator(DataType::BIT);
100
0
    aRow[3] = ODatabaseMetaDataResultSet::get1Value();
101
0
    aRow[4] = ODatabaseMetaDataResultSet::getEmptyValue();
102
0
    aRow[5] = ODatabaseMetaDataResultSet::getEmptyValue();
103
0
    aRow[6] = new ORowSetValueDecorator(OUString());
104
0
    aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
105
0
    aRow[13] = new ORowSetValueDecorator(u"L"_ustr);
106
0
    aRows.push_back(aRow);
107
108
0
    aRow[1] = new ORowSetValueDecorator(u"DOUBLE"_ustr);
109
0
    aRow[2] = new ORowSetValueDecorator(DataType::DOUBLE);
110
0
    aRow[3] = new ORowSetValueDecorator(sal_Int32(8));
111
0
    aRow[13] = new ORowSetValueDecorator(u"B"_ustr);
112
0
    aRows.push_back(aRow);
113
114
0
    aRow[11] = new ORowSetValueDecorator(ORowSetValue(true));
115
0
    aRow[13] = new ORowSetValueDecorator(u"Y"_ustr);
116
0
    aRows.push_back(aRow);
117
118
0
    aRow[1] = new ORowSetValueDecorator(u"TIMESTAMP"_ustr);
119
0
    aRow[2] = new ORowSetValueDecorator(DataType::TIMESTAMP);
120
0
    aRow[11] = new ORowSetValueDecorator(ORowSetValue(false));
121
0
    aRow[13] = new ORowSetValueDecorator(u"T"_ustr);
122
0
    aRows.push_back(aRow);
123
124
0
    aRow[1] = new ORowSetValueDecorator(u"INTEGER"_ustr);
125
0
    aRow[2] = new ORowSetValueDecorator(DataType::INTEGER);
126
0
    aRow[3] = new ORowSetValueDecorator(sal_Int32(10));
127
0
    aRow[13] = new ORowSetValueDecorator(u"I"_ustr);
128
0
    aRows.push_back(aRow);
129
130
0
    aRow[1] = new ORowSetValueDecorator(u"DECIMAL"_ustr);
131
0
    aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
132
0
    aRow[3] = new ORowSetValueDecorator(sal_Int32(20));
133
0
    aRow[6] = new ORowSetValueDecorator(u"length,scale"_ustr);
134
0
    aRow[13] = new ORowSetValueDecorator(u"F"_ustr);
135
0
    aRows.push_back(aRow);
136
137
0
    aRow[1] = new ORowSetValueDecorator(u"NUMERIC"_ustr);
138
0
    aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
139
0
    aRow[3] = new ORowSetValueDecorator(sal_Int32(16));
140
0
    aRow[13] = new ORowSetValueDecorator(u"N"_ustr);
141
0
    aRow[15] = new ORowSetValueDecorator(sal_Int32(16));
142
0
    aRows.push_back(std::move(aRow));
143
144
0
    pResult->setRows(std::move(aRows));
145
0
    return pResult;
146
0
}
147
148
Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getColumns(
149
    const Any& /*catalog*/, const OUString& /*schemaPattern*/, const OUString& tableNamePattern,
150
        const OUString& columnNamePattern )
151
10.7k
{
152
10.7k
    ::osl::MutexGuard aGuard( m_aMutex );
153
154
10.7k
    Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
155
10.7k
    if(!xTables.is())
156
0
        throw SQLException();
157
158
10.7k
    Reference< XNameAccess> xNames = xTables->getTables();
159
10.7k
    if(!xNames.is())
160
0
        throw SQLException();
161
162
10.7k
    ODatabaseMetaDataResultSet::ORows aRows;
163
10.7k
    ODatabaseMetaDataResultSet::ORow aRow(19);
164
165
10.7k
    try
166
10.7k
    {
167
10.7k
        aRow[10] = new ORowSetValueDecorator(sal_Int32(10));
168
10.7k
        for (auto& tabName : xNames->getElementNames())
169
10.7k
        {
170
10.7k
            if (match(tableNamePattern, tabName, '\0'))
171
10.7k
            {
172
10.7k
                Reference<XColumnsSupplier> xTable(xNames->getByName(tabName), css::uno::UNO_QUERY);
173
10.7k
                OSL_ENSURE(xTable.is(),"Table not found! Normally an exception had to be thrown here!");
174
10.7k
                aRow[3] = new ORowSetValueDecorator(tabName);
175
176
10.7k
                Reference< XNameAccess> xColumns = xTable->getColumns();
177
10.7k
                if(!xColumns.is())
178
0
                    throw SQLException();
179
180
10.7k
                Reference< XPropertySet> xColumn;
181
10.7k
                sal_Int32 i = 0;
182
10.7k
                for (auto& colName : xColumns->getElementNames())
183
318k
                {
184
318k
                    ++i;
185
318k
                    if (match(columnNamePattern, colName, '\0'))
186
318k
                    {
187
318k
                        aRow[4] = new ORowSetValueDecorator(colName);
188
189
318k
                        xColumn.set(xColumns->getByName(colName), css::uno::UNO_QUERY);
190
318k
                        OSL_ENSURE(xColumn.is(),"Columns contains a column who isn't a fastpropertyset!");
191
318k
                        aRow[5] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))));
192
318k
                        aRow[6] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))));
193
318k
                        aRow[7] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))));
194
318k
                        aRow[9] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
195
318k
                        aRow[11] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))));
196
318k
                        aRow[13] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))));
197
318k
                        switch(aRow[5]->getValue().getInt32())
198
318k
                        {
199
0
                        case DataType::CHAR:
200
11.5k
                        case DataType::VARCHAR:
201
11.5k
                            aRow[16] = new ORowSetValueDecorator(sal_Int32(254));
202
11.5k
                            break;
203
1.95k
                        case DataType::LONGVARCHAR:
204
1.95k
                            aRow[16] = new ORowSetValueDecorator(sal_Int32(65535));
205
1.95k
                            break;
206
304k
                        default:
207
304k
                            aRow[16] = new ORowSetValueDecorator(sal_Int32(0));
208
318k
                        }
209
318k
                        aRow[17] = new ORowSetValueDecorator(i);
210
318k
                        switch(aRow[11]->getValue().getInt32())
211
318k
                        {
212
0
                        case ColumnValue::NO_NULLS:
213
0
                            aRow[18] = new ORowSetValueDecorator(u"NO"_ustr);
214
0
                            break;
215
318k
                        case ColumnValue::NULLABLE:
216
318k
                            aRow[18] = new ORowSetValueDecorator(u"YES"_ustr);
217
318k
                            break;
218
0
                        default:
219
0
                            aRow[18] = new ORowSetValueDecorator(OUString());
220
318k
                        }
221
318k
                        aRows.push_back(aRow);
222
318k
                    }
223
318k
                }
224
10.7k
            }
225
10.7k
        }
226
10.7k
    }
227
10.7k
    catch (const WrappedTargetException& e)
228
10.7k
    {
229
218
        SQLException aSql;
230
218
        if (e.TargetException >>= aSql)
231
218
            throw aSql;
232
0
        throw WrappedTargetRuntimeException(e.Message, e.Context, e.TargetException);
233
218
    }
234
10.5k
    rtl::Reference<::connectivity::ODatabaseMetaDataResultSet> pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns);
235
10.5k
    pResult->setRows(std::move(aRows));
236
237
10.5k
    return pResult;
238
10.7k
}
239
240
Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getIndexInfo(
241
    const Any& /*catalog*/, const OUString& /*schema*/, const OUString& table,
242
        sal_Bool unique, sal_Bool /*approximate*/ )
243
0
{
244
0
    ::osl::MutexGuard aGuard( m_aMutex );
245
246
0
    Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
247
0
    if(!xTables.is())
248
0
                throw SQLException();
249
250
0
    Reference< XNameAccess> xNames = xTables->getTables();
251
0
    if(!xNames.is())
252
0
                throw SQLException();
253
254
0
    ODatabaseMetaDataResultSet::ORows aRows;
255
0
    ODatabaseMetaDataResultSet::ORow aRow(14);
256
257
0
    aRow[5]     = new ORowSetValueDecorator(OUString());
258
0
    aRow[10]    = new ORowSetValueDecorator(u"A"_ustr);
259
260
0
    Reference< XIndexesSupplier> xTable(
261
0
        xNames->getByName(table), css::uno::UNO_QUERY);
262
0
    aRow[3] = new ORowSetValueDecorator(table);
263
0
    aRow[7] = new ORowSetValueDecorator(sal_Int32(3));
264
265
0
    Reference< XNameAccess> xIndexes = xTable->getIndexes();
266
0
    if(!xIndexes.is())
267
0
        throw SQLException();
268
269
0
    Reference< XPropertySet> xIndex;
270
0
    for (auto& idxName : xIndexes->getElementNames())
271
0
    {
272
0
        xIndex.set(xIndexes->getByName(idxName), css::uno::UNO_QUERY);
273
0
        OSL_ENSURE(xIndex.is(),"Indexes contains a column who isn't a fastpropertyset!");
274
275
0
        if(unique && !getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))))
276
0
            continue;
277
0
        aRow[4] = new ORowSetValueDecorator(ORowSetValue(getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE)))));
278
0
        aRow[6] = new ORowSetValueDecorator(idxName);
279
280
0
        auto pIndex = dynamic_cast<ODbaseIndex*>(xIndex.get());
281
0
        if(pIndex)
282
0
        {
283
0
            aRow[11] = new ORowSetValueDecorator(static_cast<sal_Int32>(pIndex->getHeader().db_maxkeys));
284
0
            aRow[12] = new ORowSetValueDecorator(static_cast<sal_Int32>(pIndex->getHeader().db_pagecount));
285
0
        }
286
287
0
        Reference<XColumnsSupplier> xColumnsSup(xIndex,UNO_QUERY);
288
0
        Reference< XNameAccess> xColumns = xColumnsSup->getColumns();
289
290
0
        sal_Int32 j = 0;
291
0
        for (auto& colName : xColumns->getElementNames())
292
0
        {
293
0
            aRow[8] = new ORowSetValueDecorator(++j);
294
0
            aRow[9] = new ORowSetValueDecorator(colName);
295
0
            aRows.push_back(aRow);
296
0
        }
297
0
    }
298
299
0
    rtl::Reference<::connectivity::ODatabaseMetaDataResultSet> pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eIndexInfo);
300
0
    pResult->setRows(std::move(aRows));
301
0
    return pResult;
302
0
}
303
304
OUString SAL_CALL ODbaseDatabaseMetaData::getURL(  )
305
0
{
306
0
    ::osl::MutexGuard aGuard( m_aMutex );
307
0
    return "sdbc:dbase:" + m_pConnection->getURL();
308
0
}
309
310
sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxBinaryLiteralLength(  )
311
0
{
312
0
    return SAL_MAX_INT32;
313
0
}
314
315
sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxCharLiteralLength(  )
316
0
{
317
0
    return 254;
318
0
}
319
320
sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnNameLength(  )
321
0
{
322
0
    return 10;
323
0
}
324
325
sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnsInIndex(  )
326
0
{
327
0
    return 1;
328
0
}
329
330
sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnsInTable(  )
331
0
{
332
0
    return 128;
333
0
}
334
335
sal_Bool SAL_CALL ODbaseDatabaseMetaData::supportsAlterTableWithAddColumn(  )
336
10.5k
{
337
10.5k
    return true;
338
10.5k
}
339
340
sal_Bool SAL_CALL ODbaseDatabaseMetaData::supportsAlterTableWithDropColumn(  )
341
10.5k
{
342
10.5k
    return false;
343
10.5k
}
344
345
sal_Bool SAL_CALL ODbaseDatabaseMetaData::isReadOnly(  )
346
0
{
347
0
    ::osl::MutexGuard aGuard( m_aMutex );
348
349
0
    bool bReadOnly = false;
350
0
    ::ucbhelper::Content aFile(m_pConnection->getContent(),Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
351
0
    aFile.getPropertyValue(u"IsReadOnly"_ustr) >>= bReadOnly;
352
353
0
    return bReadOnly;
354
0
}
355
356
bool ODbaseDatabaseMetaData::impl_storesMixedCaseQuotedIdentifiers_throw(  )
357
0
{
358
0
    return true;
359
0
}
360
361
bool ODbaseDatabaseMetaData::impl_supportsMixedCaseQuotedIdentifiers_throw(  )
362
21.4k
{
363
21.4k
    return true;
364
21.4k
}
365
366
367
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */