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