1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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::sdbcx
;
38 using namespace ::com::sun::star::sdbc
;
39 using namespace ::com::sun::star::container
;
40 using namespace ::com::sun::star::table
;
41 using namespace ::com::sun::star::sheet
;
43 OCalcDatabaseMetaData::OCalcDatabaseMetaData(OConnection
* _pCon
) :OComponentDatabaseMetaData(_pCon
)
47 OCalcDatabaseMetaData::~OCalcDatabaseMetaData()
51 OUString SAL_CALL
OCalcDatabaseMetaData::getURL( )
53 ::osl::MutexGuard
aGuard( m_aMutex
);
55 return "sdbc:calc:" + m_pConnection
->getURL();
58 static bool lcl_IsEmptyOrHidden( const Reference
<XSpreadsheets
>& xSheets
, const OUString
& rName
)
60 Any aAny
= xSheets
->getByName( rName
);
61 Reference
<XSpreadsheet
> xSheet
;
62 if ( aAny
>>= xSheet
)
64 // test if sheet is hidden
66 Reference
<XPropertySet
> xProp( xSheet
, UNO_QUERY
);
70 Any aVisAny
= xProp
->getPropertyValue("IsVisible");
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
);
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
)
99 static bool lcl_IsUnnamed( const Reference
<XDatabaseRanges
>& xRanges
, const OUString
& rName
)
101 bool bUnnamed
= false;
103 Any aAny
= xRanges
->getByName( rName
);
104 Reference
<XDatabaseRange
> xRange
;
105 if ( aAny
>>= xRange
)
107 Reference
<XPropertySet
> xRangeProp( xRange
, UNO_QUERY
);
108 if ( xRangeProp
.is() )
112 Any aUserAny
= xRangeProp
->getPropertyValue("IsUserDefined");
114 if ( aUserAny
>>= bUserDefined
)
115 bUnnamed
= !bUserDefined
;
117 catch ( UnknownPropertyException
& )
127 Reference
< XResultSet
> SAL_CALL
OCalcDatabaseMetaData::getTables(
128 const Any
& /*catalog*/, const OUString
& /*schemaPattern*/,
129 const OUString
& tableNamePattern
, const Sequence
< OUString
>& types
)
131 ::osl::MutexGuard
aGuard( m_aMutex
);
133 rtl::Reference
<ODatabaseMetaDataResultSet
> pResult
= new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables
);
135 // check if ORowSetValue type is given
136 // when no types are given then we have to return all tables e.g. TABLE
138 OUString
aTable("TABLE");
140 bool bTableFound
= true;
141 sal_Int32 nLength
= types
.getLength();
146 const OUString
* pIter
= types
.getConstArray();
147 const OUString
* pEnd
= pIter
+ nLength
;
148 for(;pIter
!= pEnd
;++pIter
)
160 // get the sheet names from the document
162 OCalcConnection::ODocHolder
aDocHolder(static_cast<OCalcConnection
*>(m_pConnection
));
163 const Reference
<XSpreadsheetDocument
>& xDoc
= aDocHolder
.getDoc();
165 throw SQLException();
166 Reference
<XSpreadsheets
> xSheets
= xDoc
->getSheets();
168 throw SQLException();
169 Sequence
< OUString
> aSheetNames
= xSheets
->getElementNames();
171 ODatabaseMetaDataResultSet::ORows aRows
;
172 sal_Int32 nSheetCount
= aSheetNames
.getLength();
173 for (sal_Int32 nSheet
=0; nSheet
<nSheetCount
; nSheet
++)
175 OUString aName
= aSheetNames
[nSheet
];
176 if ( !lcl_IsEmptyOrHidden( xSheets
, aName
) && match(tableNamePattern
,aName
,'\0') )
178 aRows
.push_back( { nullptr, nullptr, nullptr,
179 new ORowSetValueDecorator(aName
),
180 new ORowSetValueDecorator(aTable
),
181 ODatabaseMetaDataResultSet::getEmptyValue()
186 // also use database ranges
188 Reference
<XPropertySet
> xDocProp( xDoc
, UNO_QUERY
);
191 Any aRangesAny
= xDocProp
->getPropertyValue("DatabaseRanges");
192 Reference
<XDatabaseRanges
> xRanges
;
193 if ( aRangesAny
>>= xRanges
)
195 Sequence
< OUString
> aDBNames
= xRanges
->getElementNames();
196 sal_Int32 nDBCount
= aDBNames
.getLength();
197 for (sal_Int32 nRange
=0; nRange
<nDBCount
; nRange
++)
199 OUString aName
= aDBNames
[nRange
];
200 if ( !lcl_IsUnnamed( xRanges
, aName
) && match(tableNamePattern
,aName
,'\0') )
202 aRows
.push_back( { nullptr, nullptr, nullptr,
203 new ORowSetValueDecorator(aName
),
204 new ORowSetValueDecorator(aTable
),
205 ODatabaseMetaDataResultSet::getEmptyValue()
212 pResult
->setRows(aRows
);
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */