LibreOffice Module connectivity (master) 1
calc/CDatabaseMetaData.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 <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>
31
32using namespace connectivity::calc;
33using namespace connectivity::file;
34using namespace connectivity::component;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::beans;
37using namespace ::com::sun::star::sdbcx;
38using namespace ::com::sun::star::sdbc;
39using namespace ::com::sun::star::container;
40using namespace ::com::sun::star::table;
41using namespace ::com::sun::star::sheet;
42
43OCalcDatabaseMetaData::OCalcDatabaseMetaData(OConnection* _pCon) :OComponentDatabaseMetaData(_pCon)
44{
45}
46
48{
49}
50
52{
53 ::osl::MutexGuard aGuard( m_aMutex );
54
55 return "sdbc:calc:" + m_pConnection->getURL();
56}
57
58static bool lcl_IsEmptyOrHidden( const Reference<XSpreadsheets>& xSheets, const OUString& rName )
59{
60 Any aAny = xSheets->getByName( rName );
62 if ( !(aAny >>= xSheet) )
63 return false;
64
65 // test if sheet is hidden
66
67 Reference<XPropertySet> xProp( xSheet, UNO_QUERY );
68 if (xProp.is())
69 {
70 bool bVisible;
71 Any aVisAny = xProp->getPropertyValue("IsVisible");
72 if ( (aVisAny >>= bVisible) && !bVisible)
73 return true; // hidden
74 }
75
76 // use the same data area as in OCalcTable to test for empty table
77
78 Reference<XSheetCellCursor> xCursor = xSheet->createCursor();
79 Reference<XCellRangeAddressable> xRange( xCursor, UNO_QUERY );
80 if ( xRange.is() )
81 {
82 xCursor->collapseToSize( 1, 1 ); // single (first) cell
83 xCursor->collapseToCurrentRegion(); // contiguous data area
84
85 CellRangeAddress aRangeAddr = xRange->getRangeAddress();
86 if ( aRangeAddr.StartColumn == aRangeAddr.EndColumn &&
87 aRangeAddr.StartRow == aRangeAddr.EndRow )
88 {
89 // single cell -> check content
90 Reference<XCell> xCell = xCursor->getCellByPosition( 0, 0 );
91 if ( xCell.is() && xCell->getType() == CellContentType_EMPTY )
92 return true;
93 }
94 }
95
96 return false;
97}
98
99static bool lcl_IsUnnamed( const Reference<XDatabaseRanges>& xRanges, const OUString& rName )
100{
101 bool bUnnamed = false;
102
103 Any aAny = xRanges->getByName( rName );
105 if ( aAny >>= xRange )
106 {
107 Reference<XPropertySet> xRangeProp( xRange, UNO_QUERY );
108 if ( xRangeProp.is() )
109 {
110 try
111 {
112 Any aUserAny = xRangeProp->getPropertyValue("IsUserDefined");
113 bool bUserDefined;
114 if ( aUserAny >>= bUserDefined )
115 bUnnamed = !bUserDefined;
116 }
117 catch ( UnknownPropertyException& )
118 {
119 // optional property
120 }
121 }
122 }
123
124 return bUnnamed;
125}
126
128 const Any& /*catalog*/, const OUString& /*schemaPattern*/,
129 const OUString& tableNamePattern, const Sequence< OUString >& types )
130{
131 ::osl::MutexGuard aGuard( m_aMutex );
132
134
135 // check if ORowSetValue type is given
136 // when no types are given then we have to return all tables e.g. TABLE
137
138 OUString aTable("TABLE");
139
140 bool bTableFound = true;
141 sal_Int32 nLength = types.getLength();
142 if(nLength)
143 {
144 bTableFound = false;
145
146 const OUString* pIter = types.getConstArray();
147 const OUString* pEnd = pIter + nLength;
148 for(;pIter != pEnd;++pIter)
149 {
150 if(*pIter == aTable)
151 {
152 bTableFound = true;
153 break;
154 }
155 }
156 }
157 if(!bTableFound)
158 return pResult;
159
160 // get the sheet names from the document
161
163 const Reference<XSpreadsheetDocument>& xDoc = aDocHolder.getDoc();
164 if ( !xDoc.is() )
165 throw SQLException();
166 Reference<XSpreadsheets> xSheets = xDoc->getSheets();
167 if ( !xSheets.is() )
168 throw SQLException();
169 Sequence< OUString > aSheetNames = xSheets->getElementNames();
170
172 sal_Int32 nSheetCount = aSheetNames.getLength();
173 for (sal_Int32 nSheet=0; nSheet<nSheetCount; nSheet++)
174 {
175 OUString aName = aSheetNames[nSheet];
176 if ( !lcl_IsEmptyOrHidden( xSheets, aName ) && match(tableNamePattern,aName,'\0') )
177 {
178 aRows.push_back( { nullptr, nullptr, nullptr,
180 new ORowSetValueDecorator(aTable),
182 } );
183 }
184 }
185
186 // also use database ranges
187
188 Reference<XPropertySet> xDocProp( xDoc, UNO_QUERY );
189 if ( xDocProp.is() )
190 {
191 Any aRangesAny = xDocProp->getPropertyValue("DatabaseRanges");
193 if ( aRangesAny >>= xRanges )
194 {
195 Sequence< OUString > aDBNames = xRanges->getElementNames();
196 sal_Int32 nDBCount = aDBNames.getLength();
197 for (sal_Int32 nRange=0; nRange<nDBCount; nRange++)
198 {
199 OUString aName = aDBNames[nRange];
200 if ( !lcl_IsUnnamed( xRanges, aName ) && match(tableNamePattern,aName,'\0') )
201 {
202 aRows.push_back( { nullptr, nullptr, nullptr,
204 new ORowSetValueDecorator(aTable),
206 } );
207 }
208 }
209 }
210 }
211
212 pResult->setRows(std::move(aRows));
213
214 return pResult;
215}
216
217/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool lcl_IsUnnamed(const Reference< XDatabaseRanges > &xRanges, const OUString &rName)
static bool lcl_IsEmptyOrHidden(const Reference< XSpreadsheets > &xSheets, const OUString &rName)
static ORowSetValueDecoratorRef const & getEmptyValue()
return an empty ORowSetValueDecorator
@ eTables
describes a result set as expected by XDatabaseMetaData::getTables
const OUString & getURL() const
Definition: TConnection.hxx:62
ORowSetValueDecorator decorates an ORowSetValue so the value is "refcounted".
Definition: FValue.hxx:402
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDoc() const
virtual OUString SAL_CALL getURL() override
virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL getTables(const css::uno::Any &catalog, const OUString &schemaPattern, const OUString &tableNamePattern, const css::uno::Sequence< OUString > &types) override
mutable::osl::Mutex m_aMutex
OUString aName
bool match(const sal_Unicode *pWild, const sal_Unicode *pStr, const sal_Unicode cEscape)
Definition: CommonTools.cxx:51
bool bVisible
sal_Int32 nLength