Use correct object
[LibreOffice.git] / connectivity / source / drivers / calc / CDatabaseMetaData.cxx
blob4c642e66f2e7257e1a6a06eb6ba5e517e5619fc1
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 <calc/CDatabaseMetaData.hxx>
21 #include <calc/CConnection.hxx>
22 #include <com/sun/star/sdbc/SQLException.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
25 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
26 #include <com/sun/star/sheet/XSpreadsheet.hpp>
27 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
28 #include <com/sun/star/sheet/XDatabaseRanges.hpp>
29 #include <com/sun/star/sheet/XDatabaseRange.hpp>
30 #include <FDatabaseMetaDataResultSet.hxx>
32 using namespace connectivity::calc;
33 using namespace connectivity::file;
34 using namespace connectivity::component;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::sdbc;
38 using namespace ::com::sun::star::container;
39 using namespace ::com::sun::star::table;
40 using namespace ::com::sun::star::sheet;
42 OCalcDatabaseMetaData::OCalcDatabaseMetaData(OConnection* _pCon) :OComponentDatabaseMetaData(_pCon)
46 OCalcDatabaseMetaData::~OCalcDatabaseMetaData()
50 OUString SAL_CALL OCalcDatabaseMetaData::getURL( )
52 ::osl::MutexGuard aGuard( m_aMutex );
54 return "sdbc:calc:" + m_pConnection->getURL();
57 static bool lcl_IsEmptyOrHidden( const Reference<XSpreadsheets>& xSheets, const OUString& rName )
59 Any aAny = xSheets->getByName( rName );
60 Reference<XSpreadsheet> xSheet;
61 if ( !(aAny >>= xSheet) )
62 return false;
64 // test if sheet is hidden
66 Reference<XPropertySet> xProp( xSheet, UNO_QUERY );
67 if (xProp.is())
69 bool bVisible;
70 Any aVisAny = xProp->getPropertyValue(u"IsVisible"_ustr);
71 if ( (aVisAny >>= bVisible) && !bVisible)
72 return true; // hidden
75 // use the same data area as in OCalcTable to test for empty table
77 Reference<XSheetCellCursor> xCursor = xSheet->createCursor();
78 Reference<XCellRangeAddressable> xRange( xCursor, UNO_QUERY );
79 if ( xRange.is() )
81 xCursor->collapseToSize( 1, 1 ); // single (first) cell
82 xCursor->collapseToCurrentRegion(); // contiguous data area
84 CellRangeAddress aRangeAddr = xRange->getRangeAddress();
85 if ( aRangeAddr.StartColumn == aRangeAddr.EndColumn &&
86 aRangeAddr.StartRow == aRangeAddr.EndRow )
88 // single cell -> check content
89 Reference<XCell> xCell = xCursor->getCellByPosition( 0, 0 );
90 if ( xCell.is() && xCell->getType() == CellContentType_EMPTY )
91 return true;
95 return false;
98 static bool lcl_IsUnnamed( const Reference<XDatabaseRanges>& xRanges, const OUString& rName )
100 bool bUnnamed = false;
102 Any aAny = xRanges->getByName( rName );
103 Reference<XDatabaseRange> xRange;
104 if ( aAny >>= xRange )
106 Reference<XPropertySet> xRangeProp( xRange, UNO_QUERY );
107 if ( xRangeProp.is() )
111 Any aUserAny = xRangeProp->getPropertyValue(u"IsUserDefined"_ustr);
112 bool bUserDefined;
113 if ( aUserAny >>= bUserDefined )
114 bUnnamed = !bUserDefined;
116 catch ( UnknownPropertyException& )
118 // optional property
123 return bUnnamed;
126 Reference< XResultSet > SAL_CALL OCalcDatabaseMetaData::getTables(
127 const Any& /*catalog*/, const OUString& /*schemaPattern*/,
128 const OUString& tableNamePattern, const Sequence< OUString >& types )
130 ::osl::MutexGuard aGuard( m_aMutex );
132 rtl::Reference<ODatabaseMetaDataResultSet> pResult = new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables);
134 // check if ORowSetValue type is given
135 // when no types are given then we have to return all tables e.g. TABLE
137 static constexpr OUString aTable(u"TABLE"_ustr);
139 if (types.hasElements())
141 bool bTableFound = false;
143 for (auto& type : types)
145 if (type == aTable)
147 bTableFound = true;
148 break;
151 if (!bTableFound)
152 return pResult;
155 // get the sheet names from the document
157 OCalcConnection::ODocHolder aDocHolder(static_cast<OCalcConnection*>(m_pConnection));
158 const Reference<XSpreadsheetDocument>& xDoc = aDocHolder.getDoc();
159 if ( !xDoc.is() )
160 throw SQLException();
161 Reference<XSpreadsheets> xSheets = xDoc->getSheets();
162 if ( !xSheets.is() )
163 throw SQLException();
164 Sequence< OUString > aSheetNames = xSheets->getElementNames();
166 ODatabaseMetaDataResultSet::ORows aRows;
167 sal_Int32 nSheetCount = aSheetNames.getLength();
168 for (sal_Int32 nSheet=0; nSheet<nSheetCount; nSheet++)
170 const OUString& aName = aSheetNames[nSheet];
171 if ( !lcl_IsEmptyOrHidden( xSheets, aName ) && match(tableNamePattern,aName,'\0') )
173 aRows.push_back( { nullptr, nullptr, nullptr,
174 new ORowSetValueDecorator(aName),
175 new ORowSetValueDecorator(aTable),
176 ODatabaseMetaDataResultSet::getEmptyValue()
177 } );
181 // also use database ranges
183 Reference<XPropertySet> xDocProp( xDoc, UNO_QUERY );
184 if ( xDocProp.is() )
186 Any aRangesAny = xDocProp->getPropertyValue(u"DatabaseRanges"_ustr);
187 Reference<XDatabaseRanges> xRanges;
188 if ( aRangesAny >>= xRanges )
190 Sequence< OUString > aDBNames = xRanges->getElementNames();
191 sal_Int32 nDBCount = aDBNames.getLength();
192 for (sal_Int32 nRange=0; nRange<nDBCount; nRange++)
194 const OUString& aName = aDBNames[nRange];
195 if ( !lcl_IsUnnamed( xRanges, aName ) && match(tableNamePattern,aName,'\0') )
197 aRows.push_back( { nullptr, nullptr, nullptr,
198 new ORowSetValueDecorator(aName),
199 new ORowSetValueDecorator(aTable),
200 ODatabaseMetaDataResultSet::getEmptyValue()
201 } );
207 pResult->setRows(std::move(aRows));
209 return pResult;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */