LibreOffice Module connectivity (master) 1
DDatabaseMetaData.cxx
Go to the documentation of this file.
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
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>
29#include <dbase/DIndex.hxx>
33#include <comphelper/types.hxx>
34#include <ucbhelper/content.hxx>
35
36using namespace ::comphelper;
37using namespace connectivity::dbase;
38using namespace connectivity;
39using namespace ::com::sun::star::uno;
40using namespace ::com::sun::star::beans;
41using namespace ::com::sun::star::sdbcx;
42using namespace ::com::sun::star::sdbc;
43using namespace ::com::sun::star::container;
44using namespace ::com::sun::star::ucb;
45using namespace ::com::sun::star::lang;
46
47ODbaseDatabaseMetaData::ODbaseDatabaseMetaData(::connectivity::file::OConnection* _pCon) :ODatabaseMetaData(_pCon)
48{
49}
50
52{
53}
54
56{
58
59 static ODatabaseMetaDataResultSet::ORows aRows = []()
60 {
62 aTmp.reserve(10);
64 {
66 new ORowSetValueDecorator(OUString("VARCHAR")),
67 new ORowSetValueDecorator(DataType::VARCHAR),
68 new ORowSetValueDecorator(sal_Int32(254)),
71 new ORowSetValueDecorator(OUString("length")),
72 new ORowSetValueDecorator(sal_Int32(ColumnValue::NULLABLE)),
74 new ORowSetValueDecorator(sal_Int32(ColumnSearch::FULL)),
78 new ORowSetValueDecorator(OUString("C")),
83 new ORowSetValueDecorator(sal_Int32(10))
84 };
85
86 aTmp.push_back(aRow);
87
88 aRow[1] = new ORowSetValueDecorator(OUString("LONGVARCHAR"));
89 aRow[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR);
90 aRow[3] = new ORowSetValueDecorator(sal_Int32(2147483647));
91 aRow[6] = new ORowSetValueDecorator();
92 aRow[13] = new ORowSetValueDecorator(OUString("M"));
93 aTmp.push_back(aRow);
94
95 aRow[1] = new ORowSetValueDecorator(OUString("DATE"));
96 aRow[2] = new ORowSetValueDecorator(DataType::DATE);
97 aRow[3] = new ORowSetValueDecorator(sal_Int32(10));
98 aRow[13] = new ORowSetValueDecorator(OUString("D"));
99 aTmp.push_back(aRow);
100
101 aRow[1] = new ORowSetValueDecorator(OUString("BOOLEAN"));
102 aRow[2] = new ORowSetValueDecorator(DataType::BIT);
106 aRow[6] = new ORowSetValueDecorator(OUString());
108 aRow[13] = new ORowSetValueDecorator(OUString("L"));
109 aTmp.push_back(aRow);
110
111 aRow[1] = new ORowSetValueDecorator(OUString("DOUBLE"));
112 aRow[2] = new ORowSetValueDecorator(DataType::DOUBLE);
113 aRow[3] = new ORowSetValueDecorator(sal_Int32(8));
114 aRow[13] = new ORowSetValueDecorator(OUString("B"));
115 aTmp.push_back(aRow);
116
117 aRow[11] = new ORowSetValueDecorator(ORowSetValue(true));
118 aRow[13] = new ORowSetValueDecorator(OUString("Y"));
119 aTmp.push_back(aRow);
120
121 aRow[1] = new ORowSetValueDecorator(OUString("TIMESTAMP"));
122 aRow[2] = new ORowSetValueDecorator(DataType::TIMESTAMP);
123 aRow[11] = new ORowSetValueDecorator(ORowSetValue(false));
124 aRow[13] = new ORowSetValueDecorator(OUString("T"));
125 aTmp.push_back(aRow);
126
127 aRow[1] = new ORowSetValueDecorator(OUString("INTEGER"));
128 aRow[2] = new ORowSetValueDecorator(DataType::INTEGER);
129 aRow[3] = new ORowSetValueDecorator(sal_Int32(10));
130 aRow[13] = new ORowSetValueDecorator(OUString("I"));
131 aTmp.push_back(aRow);
132
133 aRow[1] = new ORowSetValueDecorator(OUString("DECIMAL"));
134 aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
135 aRow[3] = new ORowSetValueDecorator(sal_Int32(20));
136 aRow[6] = new ORowSetValueDecorator(OUString("length,scale"));
137 aRow[13] = new ORowSetValueDecorator(OUString("F"));
138 aTmp.push_back(aRow);
139
140 aRow[1] = new ORowSetValueDecorator(OUString("NUMERIC"));
141 aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
142 aRow[3] = new ORowSetValueDecorator(sal_Int32(16));
143 aRow[13] = new ORowSetValueDecorator(OUString("N"));
144 aRow[15] = new ORowSetValueDecorator(sal_Int32(16));
145 aTmp.push_back(aRow);
146
147 return aTmp;
148 }();
149
150 pResult->setRows(std::move(aRows));
151 return pResult;
152}
153
154Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getColumns(
155 const Any& /*catalog*/, const OUString& /*schemaPattern*/, const OUString& tableNamePattern,
156 const OUString& columnNamePattern )
157{
158 ::osl::MutexGuard aGuard( m_aMutex );
159
160 Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
161 if(!xTables.is())
162 throw SQLException();
163
164 Reference< XNameAccess> xNames = xTables->getTables();
165 if(!xNames.is())
166 throw SQLException();
167
170
171 try
172 {
173 aRow[10] = new ORowSetValueDecorator(sal_Int32(10));
174 Sequence< OUString> aTabNames(xNames->getElementNames());
175 const OUString* pTabBegin = aTabNames.getConstArray();
176 const OUString* pTabEnd = pTabBegin + aTabNames.getLength();
177 for(;pTabBegin != pTabEnd;++pTabBegin)
178 {
179 if(match(tableNamePattern,*pTabBegin,'\0'))
180 {
181 Reference< XColumnsSupplier> xTable(
182 xNames->getByName(*pTabBegin), css::uno::UNO_QUERY);
183 OSL_ENSURE(xTable.is(),"Table not found! Normally an exception had to be thrown here!");
184 aRow[3] = new ORowSetValueDecorator(*pTabBegin);
185
186 Reference< XNameAccess> xColumns = xTable->getColumns();
187 if(!xColumns.is())
188 throw SQLException();
189
190 Sequence< OUString> aColNames(xColumns->getElementNames());
191
192 const OUString* pBegin = aColNames.getConstArray();
193 const OUString* pEnd = pBegin + aColNames.getLength();
194 Reference< XPropertySet> xColumn;
195 for(sal_Int32 i=1;pBegin != pEnd;++pBegin,++i)
196 {
197 if(match(columnNamePattern,*pBegin,'\0'))
198 {
199 aRow[4] = new ORowSetValueDecorator(*pBegin);
200
201 xColumn.set(
202 xColumns->getByName(*pBegin), css::uno::UNO_QUERY);
203 OSL_ENSURE(xColumn.is(),"Columns contains a column who isn't a fastpropertyset!");
210 switch(aRow[5]->getValue().getInt32())
211 {
212 case DataType::CHAR:
213 case DataType::VARCHAR:
214 aRow[16] = new ORowSetValueDecorator(sal_Int32(254));
215 break;
216 case DataType::LONGVARCHAR:
217 aRow[16] = new ORowSetValueDecorator(sal_Int32(65535));
218 break;
219 default:
220 aRow[16] = new ORowSetValueDecorator(sal_Int32(0));
221 }
222 aRow[17] = new ORowSetValueDecorator(i);
223 switch(aRow[11]->getValue().getInt32())
224 {
225 case ColumnValue::NO_NULLS:
226 aRow[18] = new ORowSetValueDecorator(OUString("NO"));
227 break;
228 case ColumnValue::NULLABLE:
229 aRow[18] = new ORowSetValueDecorator(OUString("YES"));
230 break;
231 default:
232 aRow[18] = new ORowSetValueDecorator(OUString());
233 }
234 aRows.push_back(aRow);
235 }
236 }
237 }
238 }
239 }
240 catch (const WrappedTargetException& e)
241 {
242 SQLException aSql;
243 if (e.TargetException >>= aSql)
244 throw aSql;
245 throw WrappedTargetRuntimeException(e.Message, e.Context, e.TargetException);
246 }
248 pResult->setRows(std::move(aRows));
249
250 return pResult;
251}
252
253Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getIndexInfo(
254 const Any& /*catalog*/, const OUString& /*schema*/, const OUString& table,
255 sal_Bool unique, sal_Bool /*approximate*/ )
256{
257 ::osl::MutexGuard aGuard( m_aMutex );
258
259 Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
260 if(!xTables.is())
261 throw SQLException();
262
263 Reference< XNameAccess> xNames = xTables->getTables();
264 if(!xNames.is())
265 throw SQLException();
266
269
270 aRow[5] = new ORowSetValueDecorator(OUString());
271 aRow[10] = new ORowSetValueDecorator(OUString("A"));
272
273 Reference< XIndexesSupplier> xTable(
274 xNames->getByName(table), css::uno::UNO_QUERY);
275 aRow[3] = new ORowSetValueDecorator(table);
276 aRow[7] = new ORowSetValueDecorator(sal_Int32(3));
277
278 Reference< XNameAccess> xIndexes = xTable->getIndexes();
279 if(!xIndexes.is())
280 throw SQLException();
281
282 Sequence< OUString> aIdxNames(xIndexes->getElementNames());
283
284 const OUString* pBegin = aIdxNames.getConstArray();
285 const OUString* pEnd = pBegin + aIdxNames.getLength();
286 Reference< XPropertySet> xIndex;
287 for(;pBegin != pEnd;++pBegin)
288 {
289 xIndex.set(xIndexes->getByName(*pBegin), css::uno::UNO_QUERY);
290 OSL_ENSURE(xIndex.is(),"Indexes contains a column who isn't a fastpropertyset!");
291
292 if(unique && !getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))))
293 continue;
295 aRow[6] = new ORowSetValueDecorator(*pBegin);
296
297 auto pIndex = dynamic_cast<ODbaseIndex*>(xIndex.get());
298 if(pIndex)
299 {
300 aRow[11] = new ORowSetValueDecorator(static_cast<sal_Int32>(pIndex->getHeader().db_maxkeys));
301 aRow[12] = new ORowSetValueDecorator(static_cast<sal_Int32>(pIndex->getHeader().db_pagecount));
302 }
303
304 Reference<XColumnsSupplier> xColumnsSup(xIndex,UNO_QUERY);
305 Reference< XNameAccess> xColumns = xColumnsSup->getColumns();
306 Sequence< OUString> aColNames(xColumns->getElementNames());
307
308 const OUString* pColBegin = aColNames.getConstArray();
309 const OUString* pColEnd = pColBegin + aColNames.getLength();
310 for(sal_Int32 j=1;pColBegin != pColEnd;++pColBegin,++j)
311 {
312 aRow[8] = new ORowSetValueDecorator(j);
313 aRow[9] = new ORowSetValueDecorator(*pColBegin);
314 aRows.push_back(aRow);
315 }
316 }
317
319 pResult->setRows(std::move(aRows));
320 return pResult;
321}
322
324{
325 ::osl::MutexGuard aGuard( m_aMutex );
326 return "sdbc:dbase:" + m_pConnection->getURL();
327}
328
330{
331 return SAL_MAX_INT32;
332}
333
335{
336 return 254;
337}
338
340{
341 return 10;
342}
343
345{
346 return 1;
347}
348
350{
351 return 128;
352}
353
355{
356 return true;
357}
358
360{
361 return false;
362}
363
365{
366 ::osl::MutexGuard aGuard( m_aMutex );
367
368 bool bReadOnly = false;
369 ::ucbhelper::Content aFile(m_pConnection->getContent(),Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
370 aFile.getPropertyValue("IsReadOnly") >>= bReadOnly;
371
372 return bReadOnly;
373}
374
376{
377 return true;
378}
379
381{
382 return true;
383}
384
385
386/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static ORowSetValueDecoratorRef const & getEmptyValue()
return an empty ORowSetValueDecorator
static ORowSetValueDecoratorRef const & getBasicValue()
return an ORowSetValueDecorator with ColumnSearch::BASIC as value
std::vector< ORowSetValueDecoratorRef > ORow
@ eIndexInfo
describes a result set as expected by XDatabaseMetaData::getIndexInfo
@ eTypeInfo
describes a result set as expected by XDatabaseMetaData::getTypeInfo
@ eColumns
describes a result set as expected by XDatabaseMetaData::getColumns
static ORowSetValueDecoratorRef const & get1Value()
return an ORowSetValueDecorator with 1 as value
static ORowSetValueDecoratorRef const & get0Value()
return an ORowSetValueDecorator with 0 as value
static ORowSetValueDecoratorRef const & getQuoteValue()
return an ORowSetValueDecorator with string ' as value
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
const OUString & getURL() const
Definition: TConnection.hxx:62
ORowSetValueDecorator decorates an ORowSetValue so the value is "refcounted".
Definition: FValue.hxx:402
virtual sal_Bool SAL_CALL isReadOnly() override
virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL getColumns(const css::uno::Any &catalog, const OUString &schemaPattern, const OUString &tableNamePattern, const OUString &columnNamePattern) override
virtual OUString SAL_CALL getURL() override
virtual bool impl_supportsMixedCaseQuotedIdentifiers_throw() override
virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL getIndexInfo(const css::uno::Any &catalog, const OUString &schema, const OUString &table, sal_Bool unique, sal_Bool approximate) override
virtual sal_Int32 SAL_CALL getMaxColumnsInIndex() override
virtual sal_Int32 SAL_CALL getMaxBinaryLiteralLength() override
virtual css::uno::Reference< css::sdbc::XResultSet > impl_getTypeInfo_throw() override
virtual sal_Int32 SAL_CALL getMaxColumnNameLength() override
virtual sal_Bool SAL_CALL supportsAlterTableWithAddColumn() override
virtual bool impl_storesMixedCaseQuotedIdentifiers_throw() override
virtual sal_Int32 SAL_CALL getMaxColumnsInTable() override
virtual sal_Bool SAL_CALL supportsAlterTableWithDropColumn() override
virtual sal_Int32 SAL_CALL getMaxCharLiteralLength() override
virtual css::uno::Reference< css::sdbcx::XTablesSupplier > createCatalog()
const css::uno::Reference< css::ucb::XContent > & getContent() const
mutable::osl::Mutex m_aMutex
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
css::uno::Any getPropertyValue(const OUString &rPropertyName)
Reference< XColumn > xColumn
bool bReadOnly
@ table
bool getBOOL(const Any &_rAny)
sal_Int32 getINT32(const Any &_rAny)
Reference< XComponentContext > getProcessComponentContext()
OUString getString(const Any &_rAny)
static bool getValue(EContact *pContact, sal_Int32 nColumnNum, GType nType, GValue *pStackValue, bool &_out_rWasNull)
Definition: NResultSet.cxx:243
bool match(const sal_Unicode *pWild, const sal_Unicode *pStr, const sal_Unicode cEscape)
Definition: CommonTools.cxx:51
int i
#define PROPERTY_ID_TYPE
Definition: propertyids.hxx:51
#define PROPERTY_ID_ISUNIQUE
Definition: propertyids.hxx:65
#define PROPERTY_ID_ISNULLABLE
Definition: propertyids.hxx:55
#define PROPERTY_ID_PRECISION
Definition: propertyids.hxx:53
#define PROPERTY_ID_TYPENAME
Definition: propertyids.hxx:52
#define PROPERTY_ID_DEFAULTVALUE
Definition: propertyids.hxx:59
#define PROPERTY_ID_SCALE
Definition: propertyids.hxx:54
const Color aColNames[SC_RANGECOLORS]
#define SAL_MAX_INT32
unsigned char sal_Bool