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