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 "flat/EDatabaseMetaData.hxx"
33*cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/sdbc/ResultSetType.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/beans/XFastPropertySet.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
40*cdf0e10cSrcweir #include <tools/urlobj.hxx>
41*cdf0e10cSrcweir #include "FDatabaseMetaDataResultSet.hxx"
42*cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
43*cdf0e10cSrcweir #include <comphelper/extract.hxx>
44*cdf0e10cSrcweir #include <comphelper/types.hxx>
45*cdf0e10cSrcweir #include <rtl/logfile.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace ::comphelper;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir using namespace connectivity;
50*cdf0e10cSrcweir using namespace connectivity::flat;
51*cdf0e10cSrcweir //	using namespace connectivity::file;
52*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
53*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
54*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
55*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
56*cdf0e10cSrcweir using namespace ::com::sun::star::container;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir OFlatDatabaseMetaData::OFlatDatabaseMetaData(::connectivity::file::OConnection* _pCon) 	:ODatabaseMetaData(_pCon)
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::OFlatDatabaseMetaData" );
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir // -------------------------------------------------------------------------
65*cdf0e10cSrcweir OFlatDatabaseMetaData::~OFlatDatabaseMetaData()
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::~OFlatDatabaseMetaData" );
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir // -------------------------------------------------------------------------
70*cdf0e10cSrcweir Reference< XResultSet > OFlatDatabaseMetaData::impl_getTypeInfo_throw(  )
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::impl_getTypeInfo_throw" );
73*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     ::connectivity::ODatabaseMetaDataResultSet* pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo);
76*cdf0e10cSrcweir     Reference< XResultSet > xRef = pResult;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	static ODatabaseMetaDataResultSet::ORows aRows;
79*cdf0e10cSrcweir 	if(aRows.empty())
80*cdf0e10cSrcweir 	{
81*cdf0e10cSrcweir 		ODatabaseMetaDataResultSet::ORow aRow;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
84*cdf0e10cSrcweir 		aRow.push_back(new ORowSetValueDecorator(::rtl::OUString::createFromAscii("CHAR")));
85*cdf0e10cSrcweir 		aRow.push_back(new ORowSetValueDecorator(DataType::CHAR));
86*cdf0e10cSrcweir 		aRow.push_back(new ORowSetValueDecorator((sal_Int32)254));
87*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
88*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
89*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
90*cdf0e10cSrcweir 		aRow.push_back(new ORowSetValueDecorator((sal_Int32)ColumnValue::NULLABLE));
91*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
92*cdf0e10cSrcweir 		aRow.push_back(new ORowSetValueDecorator((sal_Int32)ColumnSearch::CHAR));
93*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
94*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
95*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
96*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
97*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
98*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
99*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
100*cdf0e10cSrcweir 		aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
101*cdf0e10cSrcweir 		aRow.push_back(new ORowSetValueDecorator((sal_Int32)10));
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 		aRows.push_back(aRow);
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("VARCHAR"));
106*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::VARCHAR);
107*cdf0e10cSrcweir 		aRow[4] = ODatabaseMetaDataResultSet::getQuoteValue();
108*cdf0e10cSrcweir 		aRow[5] = ODatabaseMetaDataResultSet::getQuoteValue();
109*cdf0e10cSrcweir 		aRows.push_back(aRow);
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("LONGVARCHAR"));
113*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR);
114*cdf0e10cSrcweir 		aRow[3] = new ORowSetValueDecorator((sal_Int32)65535);
115*cdf0e10cSrcweir 		aRow[4] = ODatabaseMetaDataResultSet::getQuoteValue();
116*cdf0e10cSrcweir 		aRow[5] = ODatabaseMetaDataResultSet::getQuoteValue();
117*cdf0e10cSrcweir 		aRows.push_back(aRow);
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DATE"));
120*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::DATE);
121*cdf0e10cSrcweir 		aRow[3] = new ORowSetValueDecorator((sal_Int32)10);
122*cdf0e10cSrcweir 		aRow[4] = ODatabaseMetaDataResultSet::getQuoteValue();
123*cdf0e10cSrcweir 		aRow[5] = ODatabaseMetaDataResultSet::getQuoteValue();
124*cdf0e10cSrcweir 		aRows.push_back(aRow);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("TIME"));
127*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::TIME);
128*cdf0e10cSrcweir 		aRow[3] = new ORowSetValueDecorator((sal_Int32)8);
129*cdf0e10cSrcweir 		aRow[4] = ODatabaseMetaDataResultSet::getQuoteValue();
130*cdf0e10cSrcweir 		aRow[5] = ODatabaseMetaDataResultSet::getQuoteValue();
131*cdf0e10cSrcweir 		aRows.push_back(aRow);
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("TIMESTAMP"));
134*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::TIMESTAMP);
135*cdf0e10cSrcweir 		aRow[3] = new ORowSetValueDecorator((sal_Int32)19);
136*cdf0e10cSrcweir 		aRow[4] = ODatabaseMetaDataResultSet::getQuoteValue();
137*cdf0e10cSrcweir 		aRow[5] = ODatabaseMetaDataResultSet::getQuoteValue();
138*cdf0e10cSrcweir 		aRows.push_back(aRow);
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("BOOL"));
141*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::BIT);
142*cdf0e10cSrcweir 		aRow[3] = ODatabaseMetaDataResultSet::get1Value();
143*cdf0e10cSrcweir 		aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
144*cdf0e10cSrcweir 		aRows.push_back(aRow);
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DECIMAL"));
147*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
148*cdf0e10cSrcweir 		aRow[3] = new ORowSetValueDecorator((sal_Int32)20);
149*cdf0e10cSrcweir 		aRow[15] = new ORowSetValueDecorator((sal_Int32)15);
150*cdf0e10cSrcweir 		aRows.push_back(aRow);
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DOUBLE"));
153*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::DOUBLE);
154*cdf0e10cSrcweir 		aRow[3] = new ORowSetValueDecorator((sal_Int32)20);
155*cdf0e10cSrcweir 		aRow[15] = ODatabaseMetaDataResultSet::get0Value();
156*cdf0e10cSrcweir 		aRows.push_back(aRow);
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 		aRow[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("NUMERIC"));
159*cdf0e10cSrcweir 		aRow[2] = new ORowSetValueDecorator(DataType::NUMERIC);
160*cdf0e10cSrcweir 		aRow[3] = new ORowSetValueDecorator((sal_Int32)20);
161*cdf0e10cSrcweir 		aRow[15] = new ORowSetValueDecorator((sal_Int32)20);
162*cdf0e10cSrcweir 		aRows.push_back(aRow);
163*cdf0e10cSrcweir 	}
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	pResult->setRows(aRows);
166*cdf0e10cSrcweir 	return xRef;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir // -------------------------------------------------------------------------
169*cdf0e10cSrcweir Reference< XResultSet > SAL_CALL OFlatDatabaseMetaData::getColumns(
170*cdf0e10cSrcweir 	const Any& /*catalog*/, const ::rtl::OUString& /*schemaPattern*/, const ::rtl::OUString& tableNamePattern,
171*cdf0e10cSrcweir         const ::rtl::OUString& columnNamePattern ) throw(SQLException, RuntimeException)
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::getColumns" );
174*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
177*cdf0e10cSrcweir 	if(!xTables.is())
178*cdf0e10cSrcweir         throw SQLException();
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	Reference< XNameAccess> xNames = xTables->getTables();
181*cdf0e10cSrcweir 	if(!xNames.is())
182*cdf0e10cSrcweir 		throw SQLException();
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	ODatabaseMetaDataResultSet::ORows aRows;
185*cdf0e10cSrcweir 	ODatabaseMetaDataResultSet::ORow aRow(19);
186*cdf0e10cSrcweir 	aRow[10] = new ORowSetValueDecorator((sal_Int32)10);
187*cdf0e10cSrcweir 	Sequence< ::rtl::OUString> aTabNames(xNames->getElementNames());
188*cdf0e10cSrcweir 	const ::rtl::OUString* pTabBegin	= aTabNames.getConstArray();
189*cdf0e10cSrcweir 	const ::rtl::OUString* pTabEnd		= pTabBegin + aTabNames.getLength();
190*cdf0e10cSrcweir 	for(;pTabBegin != pTabEnd;++pTabBegin)
191*cdf0e10cSrcweir 	{
192*cdf0e10cSrcweir 		if(match(tableNamePattern,*pTabBegin,'\0'))
193*cdf0e10cSrcweir 		{
194*cdf0e10cSrcweir 			Reference< XColumnsSupplier> xTable;
195*cdf0e10cSrcweir 			::cppu::extractInterface(xTable,xNames->getByName(*pTabBegin));
196*cdf0e10cSrcweir 			aRow[3] = new ORowSetValueDecorator(*pTabBegin);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 			Reference< XNameAccess> xColumns = xTable->getColumns();
199*cdf0e10cSrcweir 			if(!xColumns.is())
200*cdf0e10cSrcweir 				throw SQLException();
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 			Sequence< ::rtl::OUString> aColNames(xColumns->getElementNames());
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 			const ::rtl::OUString* pBegin = aColNames.getConstArray();
205*cdf0e10cSrcweir 			const ::rtl::OUString* pEnd = pBegin + aColNames.getLength();
206*cdf0e10cSrcweir 			Reference< XPropertySet> xColumn;
207*cdf0e10cSrcweir 			for(sal_Int32 i=1;pBegin != pEnd;++pBegin,++i)
208*cdf0e10cSrcweir 			{
209*cdf0e10cSrcweir 				if(match(columnNamePattern,*pBegin,'\0'))
210*cdf0e10cSrcweir 				{
211*cdf0e10cSrcweir 					aRow[4] = new ORowSetValueDecorator(*pBegin);
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 					::cppu::extractInterface(xColumn,xColumns->getByName(*pBegin));
214*cdf0e10cSrcweir 					OSL_ENSURE(xColumn.is(),"Columns contains a column who isn't a fastpropertyset!");
215*cdf0e10cSrcweir 					aRow[5] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))));
216*cdf0e10cSrcweir 					aRow[6] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))));
217*cdf0e10cSrcweir 					aRow[7] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))));
218*cdf0e10cSrcweir 					aRow[9] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
219*cdf0e10cSrcweir 					aRow[11] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))));
220*cdf0e10cSrcweir 					aRow[13] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))));
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 					switch((sal_Int32)aRow[5]->getValue())
223*cdf0e10cSrcweir 					{
224*cdf0e10cSrcweir 					case DataType::CHAR:
225*cdf0e10cSrcweir 					case DataType::VARCHAR:
226*cdf0e10cSrcweir 						aRow[16] = new ORowSetValueDecorator((sal_Int32)254);
227*cdf0e10cSrcweir 						break;
228*cdf0e10cSrcweir 					case DataType::LONGVARCHAR:
229*cdf0e10cSrcweir 						aRow[16] = new ORowSetValueDecorator((sal_Int32)65535);
230*cdf0e10cSrcweir 						break;
231*cdf0e10cSrcweir 					default:
232*cdf0e10cSrcweir 						aRow[16] = new ORowSetValueDecorator((sal_Int32)0);
233*cdf0e10cSrcweir 					}
234*cdf0e10cSrcweir 					aRow[17] = new ORowSetValueDecorator(i);
235*cdf0e10cSrcweir 					switch(sal_Int32(aRow[11]->getValue()))
236*cdf0e10cSrcweir 					{
237*cdf0e10cSrcweir 					case ColumnValue::NO_NULLS:
238*cdf0e10cSrcweir 						aRow[18]  = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("NO"));
239*cdf0e10cSrcweir 						break;
240*cdf0e10cSrcweir 					case ColumnValue::NULLABLE:
241*cdf0e10cSrcweir 						aRow[18]  = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("YES"));
242*cdf0e10cSrcweir 						break;
243*cdf0e10cSrcweir 					default:
244*cdf0e10cSrcweir 						aRow[18]  = new ORowSetValueDecorator(::rtl::OUString());
245*cdf0e10cSrcweir 					}
246*cdf0e10cSrcweir 					aRows.push_back(aRow);
247*cdf0e10cSrcweir 				}
248*cdf0e10cSrcweir 			}
249*cdf0e10cSrcweir 		}
250*cdf0e10cSrcweir 	}
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir     ::connectivity::ODatabaseMetaDataResultSet* pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns);
253*cdf0e10cSrcweir 	Reference< XResultSet > xRef = pResult;
254*cdf0e10cSrcweir 	pResult->setRows(aRows);
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 	return xRef;
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir // -------------------------------------------------------------------------
259*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OFlatDatabaseMetaData::getURL(  ) throw(SQLException, RuntimeException)
260*cdf0e10cSrcweir {
261*cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::getURL" );
262*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
263*cdf0e10cSrcweir 	return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("sdbc:flat:")) + m_pConnection->getURL();
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir // -----------------------------------------------------------------------------
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 
268