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