Line data Source code
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 "calc/CDatabaseMetaData.hxx"
21 : #include "calc/CConnection.hxx"
22 : #include <com/sun/star/sdbc/DataType.hpp>
23 : #include <com/sun/star/sdbc/ResultSetType.hpp>
24 : #include <com/sun/star/sdbc/ColumnValue.hpp>
25 : #include <com/sun/star/beans/XPropertySet.hpp>
26 : #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
27 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
28 : #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
29 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
30 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
31 : #include <com/sun/star/sheet/XCellRangesQuery.hpp>
32 : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
33 : #include <com/sun/star/sheet/XDatabaseRanges.hpp>
34 : #include <com/sun/star/sheet/XDatabaseRange.hpp>
35 : #include "FDatabaseMetaDataResultSet.hxx"
36 : #include <com/sun/star/lang/XUnoTunnel.hpp>
37 : #include <comphelper/types.hxx>
38 :
39 : using namespace connectivity::calc;
40 : using namespace connectivity::file;
41 : using namespace ::com::sun::star::uno;
42 : using namespace ::com::sun::star::beans;
43 : using namespace ::com::sun::star::sdbcx;
44 : using namespace ::com::sun::star::sdbc;
45 : using namespace ::com::sun::star::container;
46 : using namespace ::com::sun::star::table;
47 : using namespace ::com::sun::star::sheet;
48 :
49 :
50 :
51 0 : OCalcDatabaseMetaData::OCalcDatabaseMetaData(OConnection* _pCon) :ODatabaseMetaData(_pCon)
52 : {
53 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::OCalcDatabaseMetaData" );
54 0 : }
55 :
56 :
57 :
58 0 : OCalcDatabaseMetaData::~OCalcDatabaseMetaData()
59 : {
60 0 : }
61 :
62 :
63 0 : Reference< XResultSet > OCalcDatabaseMetaData::impl_getTypeInfo_throw( )
64 : {
65 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::impl_getTypeInfo_throw" );
66 0 : ::osl::MutexGuard aGuard( m_aMutex );
67 :
68 0 : ODatabaseMetaDataResultSet* pResult = new ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo);
69 0 : Reference< XResultSet > xRef = pResult;
70 :
71 0 : static ODatabaseMetaDataResultSet::ORows aRows;
72 0 : if(aRows.empty())
73 : {
74 0 : ODatabaseMetaDataResultSet::ORow aRow;
75 :
76 0 : aRows.reserve(6);
77 0 : aRow.reserve(18);
78 :
79 0 : aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
80 0 : aRow.push_back(new ORowSetValueDecorator(OUString("VARCHAR")));
81 0 : aRow.push_back(new ORowSetValueDecorator(DataType::VARCHAR));
82 0 : aRow.push_back(new ORowSetValueDecorator((sal_Int32)65535));
83 0 : aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
84 0 : aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
85 0 : aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
86 0 : aRow.push_back(ODatabaseMetaDataResultSet::get1Value()); // ORowSetValue((sal_Int32)ColumnValue::NULLABLE)
87 0 : aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
88 0 : aRow.push_back(new ORowSetValueDecorator((sal_Int32)ColumnSearch::CHAR));
89 0 : aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
90 0 : aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
91 0 : aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
92 0 : aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
93 0 : aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
94 0 : aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
95 0 : aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
96 0 : aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
97 0 : aRow.push_back(new ORowSetValueDecorator((sal_Int32)10));
98 :
99 :
100 0 : aRows.push_back(aRow);
101 :
102 0 : aRow[1] = new ORowSetValueDecorator(OUString("DECIMAL"));
103 0 : aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
104 0 : aRow[3] = ODatabaseMetaDataResultSet::get0Value();
105 0 : aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
106 0 : aRow[15] = ODatabaseMetaDataResultSet::get0Value();
107 0 : aRows.push_back(aRow);
108 :
109 0 : aRow[1] = new ORowSetValueDecorator(OUString("BOOL"));
110 0 : aRow[2] = new ORowSetValueDecorator(DataType::BIT);
111 0 : aRow[3] = new ORowSetValueDecorator((sal_Int32)20);
112 0 : aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
113 0 : aRow[15] = new ORowSetValueDecorator((sal_Int32)15);
114 0 : aRows.push_back(aRow);
115 :
116 0 : aRow[1] = new ORowSetValueDecorator(OUString("DATE"));
117 0 : aRow[2] = new ORowSetValueDecorator(DataType::DATE);
118 0 : aRow[3] = ODatabaseMetaDataResultSet::get0Value();
119 0 : aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
120 0 : aRow[15] = ODatabaseMetaDataResultSet::get0Value();
121 0 : aRows.push_back(aRow);
122 :
123 0 : aRow[1] = new ORowSetValueDecorator(OUString("TIME"));
124 0 : aRow[2] = new ORowSetValueDecorator(DataType::TIME);
125 0 : aRow[3] = ODatabaseMetaDataResultSet::get0Value();
126 0 : aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
127 0 : aRow[15] = ODatabaseMetaDataResultSet::get0Value();
128 0 : aRows.push_back(aRow);
129 :
130 0 : aRow[1] = new ORowSetValueDecorator(OUString("TIMESTAMP"));
131 0 : aRow[2] = new ORowSetValueDecorator(DataType::TIMESTAMP);
132 0 : aRow[3] = ODatabaseMetaDataResultSet::get0Value();
133 0 : aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
134 0 : aRow[15] = ODatabaseMetaDataResultSet::get0Value();
135 0 : aRows.push_back(aRow);
136 : }
137 :
138 0 : pResult->setRows(aRows);
139 0 : return xRef;
140 : }
141 :
142 :
143 :
144 0 : Reference< XResultSet > SAL_CALL OCalcDatabaseMetaData::getColumns(
145 : const Any& /*catalog*/, const OUString& /*schemaPattern*/, const OUString& tableNamePattern,
146 : const OUString& columnNamePattern ) throw(SQLException, RuntimeException, std::exception)
147 : {
148 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getColumns" );
149 0 : ::osl::MutexGuard aGuard( m_aMutex );
150 :
151 :
152 0 : Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
153 0 : if(!xTables.is())
154 0 : throw SQLException();
155 :
156 0 : Reference< XNameAccess> xNames = xTables->getTables();
157 0 : if(!xNames.is())
158 0 : throw SQLException();
159 :
160 0 : ODatabaseMetaDataResultSet::ORows aRows;
161 0 : ODatabaseMetaDataResultSet::ORow aRow(19);
162 :
163 0 : aRow[10] = new ORowSetValueDecorator((sal_Int32)10);
164 :
165 0 : Sequence< OUString> aTabNames(xNames->getElementNames());
166 0 : const OUString* pTabIter = aTabNames.getConstArray();
167 0 : const OUString* pTabEnd = pTabIter + aTabNames.getLength();
168 0 : for(;pTabIter != pTabEnd;++pTabIter)
169 : {
170 0 : if(match(tableNamePattern,*pTabIter,'\0'))
171 : {
172 0 : const Reference< XColumnsSupplier> xTable(xNames->getByName(*pTabIter),UNO_QUERY_THROW);
173 : OSL_ENSURE(xTable.is(),"Table not found! Normallya exception had to be thrown here!");
174 0 : aRow[3] = new ORowSetValueDecorator(*pTabIter);
175 :
176 0 : const Reference< XNameAccess> xColumns = xTable->getColumns();
177 0 : if(!xColumns.is())
178 0 : throw SQLException();
179 :
180 0 : const Sequence< OUString> aColNames(xColumns->getElementNames());
181 :
182 0 : const OUString* pColumnIter = aColNames.getConstArray();
183 0 : const OUString* pEnd = pColumnIter + aColNames.getLength();
184 0 : Reference< XPropertySet> xColumn;
185 0 : for(sal_Int32 i=1;pColumnIter != pEnd;++pColumnIter,++i)
186 : {
187 0 : if(match(columnNamePattern,*pColumnIter,'\0'))
188 : {
189 0 : aRow[4] = new ORowSetValueDecorator( *pColumnIter);
190 :
191 0 : xColumns->getByName(*pColumnIter) >>= xColumn;
192 : OSL_ENSURE(xColumn.is(),"Columns contains a column who isn't a fastpropertyset!");
193 0 : aRow[5] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))));
194 0 : aRow[6] = new ORowSetValueDecorator(::comphelper::getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))));
195 0 : aRow[7] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))));
196 : // aRow[8] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
197 0 : aRow[9] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
198 0 : aRow[11] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))));
199 : // aRow[12] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
200 0 : aRow[13] = new ORowSetValueDecorator(::comphelper::getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))));
201 : // aRow[14] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
202 : // aRow[15] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
203 0 : switch(sal_Int32(aRow[5]->getValue()))
204 : {
205 : case DataType::CHAR:
206 : case DataType::VARCHAR:
207 0 : aRow[16] = new ORowSetValueDecorator((sal_Int32)254);
208 0 : break;
209 : case DataType::LONGVARCHAR:
210 0 : aRow[16] = new ORowSetValueDecorator((sal_Int32)65535);
211 0 : break;
212 : default:
213 0 : aRow[16] = new ORowSetValueDecorator((sal_Int32)0);
214 : }
215 0 : aRow[17] = new ORowSetValueDecorator(i);
216 0 : switch(sal_Int32(aRow[11]->getValue()))
217 : {
218 : case ColumnValue::NO_NULLS:
219 0 : aRow[18] = new ORowSetValueDecorator(OUString("NO"));
220 0 : break;
221 : case ColumnValue::NULLABLE:
222 0 : aRow[18] = new ORowSetValueDecorator(OUString("YES"));
223 0 : break;
224 : default:
225 0 : aRow[18] = new ORowSetValueDecorator(OUString());
226 : }
227 0 : aRows.push_back(aRow);
228 : }
229 0 : }
230 : }
231 : }
232 :
233 0 : ODatabaseMetaDataResultSet* pResult = new ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns);
234 0 : Reference< XResultSet > xRef = pResult;
235 0 : pResult->setRows(aRows);
236 :
237 0 : return xRef;
238 : }
239 :
240 :
241 :
242 0 : OUString SAL_CALL OCalcDatabaseMetaData::getURL( ) throw(SQLException, RuntimeException, std::exception)
243 : {
244 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getURL" );
245 0 : ::osl::MutexGuard aGuard( m_aMutex );
246 :
247 0 : return OUString("sdbc:calc:") + m_pConnection->getURL();
248 : }
249 :
250 :
251 :
252 0 : sal_Int32 SAL_CALL OCalcDatabaseMetaData::getMaxBinaryLiteralLength( ) throw(SQLException, RuntimeException, std::exception)
253 : {
254 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getMaxBinaryLiteralLength" );
255 0 : return SAL_MAX_INT32;
256 : }
257 :
258 :
259 :
260 0 : sal_Int32 SAL_CALL OCalcDatabaseMetaData::getMaxCharLiteralLength( ) throw(SQLException, RuntimeException, std::exception)
261 : {
262 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getMaxCharLiteralLength" );
263 0 : return SAL_MAX_INT32;
264 : }
265 :
266 0 : sal_Int32 SAL_CALL OCalcDatabaseMetaData::getMaxColumnNameLength( ) throw(SQLException, RuntimeException, std::exception)
267 : {
268 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getMaxColumnNameLength" );
269 0 : return SAL_MAX_INT32;
270 : }
271 :
272 0 : sal_Int32 SAL_CALL OCalcDatabaseMetaData::getMaxColumnsInIndex( ) throw(SQLException, RuntimeException, std::exception)
273 : {
274 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getMaxColumnsInIndex" );
275 0 : return 1;
276 : }
277 :
278 0 : sal_Int32 SAL_CALL OCalcDatabaseMetaData::getMaxColumnsInTable( ) throw(SQLException, RuntimeException, std::exception)
279 : {
280 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getMaxColumnsInTable" );
281 0 : return 256;
282 : }
283 :
284 :
285 :
286 0 : static sal_Bool lcl_IsEmptyOrHidden( const Reference<XSpreadsheets>& xSheets, const OUString& rName )
287 : {
288 0 : Any aAny = xSheets->getByName( rName );
289 0 : Reference<XSpreadsheet> xSheet;
290 0 : if ( aAny >>= xSheet )
291 : {
292 : // test if sheet is hidden
293 :
294 0 : Reference<XPropertySet> xProp( xSheet, UNO_QUERY );
295 0 : if (xProp.is())
296 : {
297 0 : sal_Bool bVisible = sal_Bool();
298 0 : Any aVisAny = xProp->getPropertyValue("IsVisible");
299 0 : if ( aVisAny >>= bVisible )
300 0 : if (!bVisible)
301 0 : return sal_True; // hidden
302 : }
303 :
304 : // use the same data area as in OCalcTable to test for empty table
305 :
306 0 : Reference<XSheetCellCursor> xCursor = xSheet->createCursor();
307 0 : Reference<XCellRangeAddressable> xRange( xCursor, UNO_QUERY );
308 0 : if ( xRange.is() )
309 : {
310 0 : xCursor->collapseToSize( 1, 1 ); // single (first) cell
311 0 : xCursor->collapseToCurrentRegion(); // contiguous data area
312 :
313 0 : CellRangeAddress aRangeAddr = xRange->getRangeAddress();
314 0 : if ( aRangeAddr.StartColumn == aRangeAddr.EndColumn &&
315 0 : aRangeAddr.StartRow == aRangeAddr.EndRow )
316 : {
317 : // single cell -> check content
318 0 : Reference<XCell> xCell = xCursor->getCellByPosition( 0, 0 );
319 0 : if ( xCell.is() && xCell->getType() == CellContentType_EMPTY )
320 0 : return sal_True;
321 : }
322 0 : }
323 : }
324 :
325 0 : return sal_False;
326 : }
327 :
328 0 : static sal_Bool lcl_IsUnnamed( const Reference<XDatabaseRanges>& xRanges, const OUString& rName )
329 : {
330 0 : sal_Bool bUnnamed = sal_False;
331 :
332 0 : Any aAny = xRanges->getByName( rName );
333 0 : Reference<XDatabaseRange> xRange;
334 0 : if ( aAny >>= xRange )
335 : {
336 0 : Reference<XPropertySet> xRangeProp( xRange, UNO_QUERY );
337 0 : if ( xRangeProp.is() )
338 : {
339 : try
340 : {
341 0 : Any aUserAny = xRangeProp->getPropertyValue("IsUserDefined");
342 0 : sal_Bool bUserDefined = sal_Bool();
343 0 : if ( aUserAny >>= bUserDefined )
344 0 : bUnnamed = !bUserDefined;
345 : }
346 0 : catch ( UnknownPropertyException& )
347 : {
348 : // optional property
349 : }
350 0 : }
351 : }
352 :
353 0 : return bUnnamed;
354 : }
355 :
356 :
357 :
358 0 : Reference< XResultSet > SAL_CALL OCalcDatabaseMetaData::getTables(
359 : const Any& /*catalog*/, const OUString& /*schemaPattern*/,
360 : const OUString& tableNamePattern, const Sequence< OUString >& types )
361 : throw(SQLException, RuntimeException, std::exception)
362 : {
363 : SAL_INFO( "connectivity.drivers", "calc Ocke.Janssen@sun.com OCalcDatabaseMetaData::getTables" );
364 0 : ::osl::MutexGuard aGuard( m_aMutex );
365 :
366 0 : ODatabaseMetaDataResultSet* pResult = new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables);
367 0 : Reference< XResultSet > xRef = pResult;
368 :
369 : // check if ORowSetValue type is given
370 : // when no types are given then we have to return all tables e.g. TABLE
371 :
372 0 : OUString aTable("TABLE");
373 :
374 0 : sal_Bool bTableFound = sal_True;
375 0 : sal_Int32 nLength = types.getLength();
376 0 : if(nLength)
377 : {
378 0 : bTableFound = sal_False;
379 :
380 0 : const OUString* pIter = types.getConstArray();
381 0 : const OUString* pEnd = pIter + nLength;
382 0 : for(;pIter != pEnd;++pIter)
383 : {
384 0 : if(*pIter == aTable)
385 : {
386 0 : bTableFound = sal_True;
387 0 : break;
388 : }
389 : }
390 : }
391 0 : if(!bTableFound)
392 0 : return xRef;
393 :
394 : // get the sheet names from the document
395 :
396 0 : OCalcConnection::ODocHolder aDocHodler(((OCalcConnection*)m_pConnection));
397 0 : Reference<XSpreadsheetDocument> xDoc = aDocHodler.getDoc();
398 0 : if ( !xDoc.is() )
399 0 : throw SQLException();
400 0 : Reference<XSpreadsheets> xSheets = xDoc->getSheets();
401 0 : if ( !xSheets.is() )
402 0 : throw SQLException();
403 0 : Sequence< OUString > aSheetNames = xSheets->getElementNames();
404 :
405 0 : ODatabaseMetaDataResultSet::ORows aRows;
406 0 : sal_Int32 nSheetCount = aSheetNames.getLength();
407 0 : for (sal_Int32 nSheet=0; nSheet<nSheetCount; nSheet++)
408 : {
409 0 : OUString aName = aSheetNames[nSheet];
410 0 : if ( !lcl_IsEmptyOrHidden( xSheets, aName ) && match(tableNamePattern,aName,'\0') )
411 : {
412 0 : ODatabaseMetaDataResultSet::ORow aRow(3);
413 0 : aRow.reserve(6);
414 0 : aRow.push_back(new ORowSetValueDecorator(aName));
415 0 : aRow.push_back(new ORowSetValueDecorator(aTable));
416 0 : aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
417 0 : aRows.push_back(aRow);
418 : }
419 0 : }
420 :
421 : // also use database ranges
422 :
423 0 : Reference<XPropertySet> xDocProp( xDoc, UNO_QUERY );
424 0 : if ( xDocProp.is() )
425 : {
426 0 : Any aRangesAny = xDocProp->getPropertyValue("DatabaseRanges");
427 0 : Reference<XDatabaseRanges> xRanges;
428 0 : if ( aRangesAny >>= xRanges )
429 : {
430 0 : Sequence< OUString > aDBNames = xRanges->getElementNames();
431 0 : sal_Int32 nDBCount = aDBNames.getLength();
432 0 : for (sal_Int32 nRange=0; nRange<nDBCount; nRange++)
433 : {
434 0 : OUString aName = aDBNames[nRange];
435 0 : if ( !lcl_IsUnnamed( xRanges, aName ) && match(tableNamePattern,aName,'\0') )
436 : {
437 0 : ODatabaseMetaDataResultSet::ORow aRow(3);
438 0 : aRow.reserve(6);
439 0 : aRow.push_back(new ORowSetValueDecorator(aName));
440 0 : aRow.push_back(new ORowSetValueDecorator(aTable));
441 0 : aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
442 0 : aRows.push_back(aRow);
443 : }
444 0 : }
445 0 : }
446 : }
447 :
448 0 : pResult->setRows(aRows);
449 :
450 0 : return xRef;
451 : }
452 :
453 :
454 :
455 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|