Use COMReference to handle COM pointers in CreateShortcut
[LibreOffice.git] / connectivity / source / drivers / dbase / DDatabaseMetaData.cxx
blob7e17e1fc0d79deb8aee7aafacc45df1d7c4d8864
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
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/servicehelper.hxx>
33 #include <comphelper/types.hxx>
34 #include <ucbhelper/content.hxx>
36 using namespace ::comphelper;
37 using namespace connectivity::dbase;
38 using namespace connectivity;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::sdbcx;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::container;
44 using namespace ::com::sun::star::ucb;
45 using namespace ::com::sun::star::lang;
47 ODbaseDatabaseMetaData::ODbaseDatabaseMetaData(::connectivity::file::OConnection* _pCon) :ODatabaseMetaData(_pCon)
51 ODbaseDatabaseMetaData::~ODbaseDatabaseMetaData()
55 Reference< XResultSet > ODbaseDatabaseMetaData::impl_getTypeInfo_throw( )
57 rtl::Reference<::connectivity::ODatabaseMetaDataResultSet> pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo);
59 ODatabaseMetaDataResultSet::ORows aRows;
60 aRows.reserve(10);
61 ODatabaseMetaDataResultSet::ORow aRow
63 ODatabaseMetaDataResultSet::getEmptyValue(),
64 new ORowSetValueDecorator(u"VARCHAR"_ustr),
65 new ORowSetValueDecorator(DataType::VARCHAR),
66 new ORowSetValueDecorator(sal_Int32(254)),
67 ODatabaseMetaDataResultSet::getQuoteValue(),
68 ODatabaseMetaDataResultSet::getQuoteValue(),
69 new ORowSetValueDecorator(u"length"_ustr),
70 new ORowSetValueDecorator(sal_Int32(ColumnValue::NULLABLE)),
71 ODatabaseMetaDataResultSet::get1Value(),
72 new ORowSetValueDecorator(sal_Int32(ColumnSearch::FULL)),
73 ODatabaseMetaDataResultSet::get1Value(),
74 ODatabaseMetaDataResultSet::get0Value(),
75 ODatabaseMetaDataResultSet::get0Value(),
76 new ORowSetValueDecorator(u"C"_ustr),
77 ODatabaseMetaDataResultSet::get0Value(),
78 ODatabaseMetaDataResultSet::get0Value(),
79 ODatabaseMetaDataResultSet::getEmptyValue(),
80 ODatabaseMetaDataResultSet::getEmptyValue(),
81 new ORowSetValueDecorator(sal_Int32(10))
84 aRows.push_back(aRow);
86 aRow[1] = new ORowSetValueDecorator(u"LONGVARCHAR"_ustr);
87 aRow[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR);
88 aRow[3] = new ORowSetValueDecorator(sal_Int32(2147483647));
89 aRow[6] = new ORowSetValueDecorator();
90 aRow[13] = new ORowSetValueDecorator(u"M"_ustr);
91 aRows.push_back(aRow);
93 aRow[1] = new ORowSetValueDecorator(u"DATE"_ustr);
94 aRow[2] = new ORowSetValueDecorator(DataType::DATE);
95 aRow[3] = new ORowSetValueDecorator(sal_Int32(10));
96 aRow[13] = new ORowSetValueDecorator(u"D"_ustr);
97 aRows.push_back(aRow);
99 aRow[1] = new ORowSetValueDecorator(u"BOOLEAN"_ustr);
100 aRow[2] = new ORowSetValueDecorator(DataType::BIT);
101 aRow[3] = ODatabaseMetaDataResultSet::get1Value();
102 aRow[4] = ODatabaseMetaDataResultSet::getEmptyValue();
103 aRow[5] = ODatabaseMetaDataResultSet::getEmptyValue();
104 aRow[6] = new ORowSetValueDecorator(OUString());
105 aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
106 aRow[13] = new ORowSetValueDecorator(u"L"_ustr);
107 aRows.push_back(aRow);
109 aRow[1] = new ORowSetValueDecorator(u"DOUBLE"_ustr);
110 aRow[2] = new ORowSetValueDecorator(DataType::DOUBLE);
111 aRow[3] = new ORowSetValueDecorator(sal_Int32(8));
112 aRow[13] = new ORowSetValueDecorator(u"B"_ustr);
113 aRows.push_back(aRow);
115 aRow[11] = new ORowSetValueDecorator(ORowSetValue(true));
116 aRow[13] = new ORowSetValueDecorator(u"Y"_ustr);
117 aRows.push_back(aRow);
119 aRow[1] = new ORowSetValueDecorator(u"TIMESTAMP"_ustr);
120 aRow[2] = new ORowSetValueDecorator(DataType::TIMESTAMP);
121 aRow[11] = new ORowSetValueDecorator(ORowSetValue(false));
122 aRow[13] = new ORowSetValueDecorator(u"T"_ustr);
123 aRows.push_back(aRow);
125 aRow[1] = new ORowSetValueDecorator(u"INTEGER"_ustr);
126 aRow[2] = new ORowSetValueDecorator(DataType::INTEGER);
127 aRow[3] = new ORowSetValueDecorator(sal_Int32(10));
128 aRow[13] = new ORowSetValueDecorator(u"I"_ustr);
129 aRows.push_back(aRow);
131 aRow[1] = new ORowSetValueDecorator(u"DECIMAL"_ustr);
132 aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
133 aRow[3] = new ORowSetValueDecorator(sal_Int32(20));
134 aRow[6] = new ORowSetValueDecorator(u"length,scale"_ustr);
135 aRow[13] = new ORowSetValueDecorator(u"F"_ustr);
136 aRows.push_back(aRow);
138 aRow[1] = new ORowSetValueDecorator(u"NUMERIC"_ustr);
139 aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
140 aRow[3] = new ORowSetValueDecorator(sal_Int32(16));
141 aRow[13] = new ORowSetValueDecorator(u"N"_ustr);
142 aRow[15] = new ORowSetValueDecorator(sal_Int32(16));
143 aRows.push_back(aRow);
145 pResult->setRows(std::move(aRows));
146 return pResult;
149 Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getColumns(
150 const Any& /*catalog*/, const OUString& /*schemaPattern*/, const OUString& tableNamePattern,
151 const OUString& columnNamePattern )
153 ::osl::MutexGuard aGuard( m_aMutex );
155 Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
156 if(!xTables.is())
157 throw SQLException();
159 Reference< XNameAccess> xNames = xTables->getTables();
160 if(!xNames.is())
161 throw SQLException();
163 ODatabaseMetaDataResultSet::ORows aRows;
164 ODatabaseMetaDataResultSet::ORow aRow(19);
168 aRow[10] = new ORowSetValueDecorator(sal_Int32(10));
169 for (auto& tabName : xNames->getElementNames())
171 if (match(tableNamePattern, tabName, '\0'))
173 Reference<XColumnsSupplier> xTable(xNames->getByName(tabName), css::uno::UNO_QUERY);
174 OSL_ENSURE(xTable.is(),"Table not found! Normally an exception had to be thrown here!");
175 aRow[3] = new ORowSetValueDecorator(tabName);
177 Reference< XNameAccess> xColumns = xTable->getColumns();
178 if(!xColumns.is())
179 throw SQLException();
181 Reference< XPropertySet> xColumn;
182 sal_Int32 i = 0;
183 for (auto& colName : xColumns->getElementNames())
185 ++i;
186 if (match(columnNamePattern, colName, '\0'))
188 aRow[4] = new ORowSetValueDecorator(colName);
190 xColumn.set(xColumns->getByName(colName), css::uno::UNO_QUERY);
191 OSL_ENSURE(xColumn.is(),"Columns contains a column who isn't a fastpropertyset!");
192 aRow[5] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))));
193 aRow[6] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))));
194 aRow[7] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))));
195 aRow[9] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
196 aRow[11] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))));
197 aRow[13] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))));
198 switch(aRow[5]->getValue().getInt32())
200 case DataType::CHAR:
201 case DataType::VARCHAR:
202 aRow[16] = new ORowSetValueDecorator(sal_Int32(254));
203 break;
204 case DataType::LONGVARCHAR:
205 aRow[16] = new ORowSetValueDecorator(sal_Int32(65535));
206 break;
207 default:
208 aRow[16] = new ORowSetValueDecorator(sal_Int32(0));
210 aRow[17] = new ORowSetValueDecorator(i);
211 switch(aRow[11]->getValue().getInt32())
213 case ColumnValue::NO_NULLS:
214 aRow[18] = new ORowSetValueDecorator(u"NO"_ustr);
215 break;
216 case ColumnValue::NULLABLE:
217 aRow[18] = new ORowSetValueDecorator(u"YES"_ustr);
218 break;
219 default:
220 aRow[18] = new ORowSetValueDecorator(OUString());
222 aRows.push_back(aRow);
228 catch (const WrappedTargetException& e)
230 SQLException aSql;
231 if (e.TargetException >>= aSql)
232 throw aSql;
233 throw WrappedTargetRuntimeException(e.Message, e.Context, e.TargetException);
235 rtl::Reference<::connectivity::ODatabaseMetaDataResultSet> pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns);
236 pResult->setRows(std::move(aRows));
238 return pResult;
241 Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getIndexInfo(
242 const Any& /*catalog*/, const OUString& /*schema*/, const OUString& table,
243 sal_Bool unique, sal_Bool /*approximate*/ )
245 ::osl::MutexGuard aGuard( m_aMutex );
247 Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
248 if(!xTables.is())
249 throw SQLException();
251 Reference< XNameAccess> xNames = xTables->getTables();
252 if(!xNames.is())
253 throw SQLException();
255 ODatabaseMetaDataResultSet::ORows aRows;
256 ODatabaseMetaDataResultSet::ORow aRow(14);
258 aRow[5] = new ORowSetValueDecorator(OUString());
259 aRow[10] = new ORowSetValueDecorator(u"A"_ustr);
261 Reference< XIndexesSupplier> xTable(
262 xNames->getByName(table), css::uno::UNO_QUERY);
263 aRow[3] = new ORowSetValueDecorator(table);
264 aRow[7] = new ORowSetValueDecorator(sal_Int32(3));
266 Reference< XNameAccess> xIndexes = xTable->getIndexes();
267 if(!xIndexes.is())
268 throw SQLException();
270 Reference< XPropertySet> xIndex;
271 for (auto& idxName : xIndexes->getElementNames())
273 xIndex.set(xIndexes->getByName(idxName), css::uno::UNO_QUERY);
274 OSL_ENSURE(xIndex.is(),"Indexes contains a column who isn't a fastpropertyset!");
276 if(unique && !getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))))
277 continue;
278 aRow[4] = new ORowSetValueDecorator(ORowSetValue(getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE)))));
279 aRow[6] = new ORowSetValueDecorator(idxName);
281 auto pIndex = dynamic_cast<ODbaseIndex*>(xIndex.get());
282 if(pIndex)
284 aRow[11] = new ORowSetValueDecorator(static_cast<sal_Int32>(pIndex->getHeader().db_maxkeys));
285 aRow[12] = new ORowSetValueDecorator(static_cast<sal_Int32>(pIndex->getHeader().db_pagecount));
288 Reference<XColumnsSupplier> xColumnsSup(xIndex,UNO_QUERY);
289 Reference< XNameAccess> xColumns = xColumnsSup->getColumns();
291 sal_Int32 j = 0;
292 for (auto& colName : xColumns->getElementNames())
294 aRow[8] = new ORowSetValueDecorator(++j);
295 aRow[9] = new ORowSetValueDecorator(colName);
296 aRows.push_back(aRow);
300 rtl::Reference<::connectivity::ODatabaseMetaDataResultSet> pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eIndexInfo);
301 pResult->setRows(std::move(aRows));
302 return pResult;
305 OUString SAL_CALL ODbaseDatabaseMetaData::getURL( )
307 ::osl::MutexGuard aGuard( m_aMutex );
308 return "sdbc:dbase:" + m_pConnection->getURL();
311 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxBinaryLiteralLength( )
313 return SAL_MAX_INT32;
316 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxCharLiteralLength( )
318 return 254;
321 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnNameLength( )
323 return 10;
326 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnsInIndex( )
328 return 1;
331 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnsInTable( )
333 return 128;
336 sal_Bool SAL_CALL ODbaseDatabaseMetaData::supportsAlterTableWithAddColumn( )
338 return true;
341 sal_Bool SAL_CALL ODbaseDatabaseMetaData::supportsAlterTableWithDropColumn( )
343 return false;
346 sal_Bool SAL_CALL ODbaseDatabaseMetaData::isReadOnly( )
348 ::osl::MutexGuard aGuard( m_aMutex );
350 bool bReadOnly = false;
351 ::ucbhelper::Content aFile(m_pConnection->getContent(),Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
352 aFile.getPropertyValue(u"IsReadOnly"_ustr) >>= bReadOnly;
354 return bReadOnly;
357 bool ODbaseDatabaseMetaData::impl_storesMixedCaseQuotedIdentifiers_throw( )
359 return true;
362 bool ODbaseDatabaseMetaData::impl_supportsMixedCaseQuotedIdentifiers_throw( )
364 return true;
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */