Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / dbase / DDatabaseMetaData.cxx
blobb3020417d5bb7e8783beb7a0819a86a0e2cf60d9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "dbase/DDatabaseMetaData.hxx"
21 #include <com/sun/star/sdbc/DataType.hpp>
22 #include <com/sun/star/sdbc/ResultSetType.hpp>
23 #include <com/sun/star/sdbc/ColumnValue.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
26 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
27 #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
28 #include "FDatabaseMetaDataResultSet.hxx"
29 #include <com/sun/star/lang/XUnoTunnel.hpp>
30 #include "dbase/DIndex.hxx"
31 #include "connectivity/FValue.hxx"
32 #include <comphelper/processfactory.hxx>
33 #include <comphelper/types.hxx>
34 #include <ucbhelper/content.hxx>
36 using namespace ::comphelper;
37 using namespace connectivity::dbase;
38 using namespace connectivity;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::sdbcx;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::container;
44 using namespace ::com::sun::star::ucb;
45 using namespace ::com::sun::star::lang;
47 ODbaseDatabaseMetaData::ODbaseDatabaseMetaData(::connectivity::file::OConnection* _pCon) :ODatabaseMetaData(_pCon)
51 ODbaseDatabaseMetaData::~ODbaseDatabaseMetaData()
55 Reference< XResultSet > ODbaseDatabaseMetaData::impl_getTypeInfo_throw( )
57 ::osl::MutexGuard aGuard( m_aMutex );
59 ::connectivity::ODatabaseMetaDataResultSet* pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo);
60 Reference< XResultSet > xRef = pResult;
62 static ODatabaseMetaDataResultSet::ORows aRows;
63 if(aRows.empty())
65 ODatabaseMetaDataResultSet::ORow aRow;
66 aRow.reserve(19);
68 aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
69 aRow.push_back(new ORowSetValueDecorator(OUString("VARCHAR")));
70 aRow.push_back(new ORowSetValueDecorator(DataType::VARCHAR));
71 aRow.push_back(new ORowSetValueDecorator((sal_Int32)254));
72 aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
73 aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
74 aRow.push_back(new ORowSetValueDecorator(OUString("length")));
75 aRow.push_back(new ORowSetValueDecorator((sal_Int32)ColumnValue::NULLABLE));
76 aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
77 aRow.push_back(new ORowSetValueDecorator((sal_Int32)ColumnSearch::FULL));
78 aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
79 aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
80 aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
81 aRow.push_back(new ORowSetValueDecorator(OUString("C")));
82 aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
83 aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
84 aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
85 aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
86 aRow.push_back(new ORowSetValueDecorator((sal_Int32)10));
88 aRows.push_back(aRow);
90 aRow[1] = new ORowSetValueDecorator(OUString("LONGVARCHAR"));
91 aRow[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR);
92 aRow[3] = new ORowSetValueDecorator((sal_Int32)2147483647);
93 aRow[6] = new ORowSetValueDecorator();
94 aRow[13] = new ORowSetValueDecorator(OUString("M"));
95 aRows.push_back(aRow);
97 aRow[1] = new ORowSetValueDecorator(OUString("DATE"));
98 aRow[2] = new ORowSetValueDecorator(DataType::DATE);
99 aRow[3] = new ORowSetValueDecorator((sal_Int32)10);
100 aRow[13] = new ORowSetValueDecorator(OUString("D"));
101 aRows.push_back(aRow);
103 aRow[1] = new ORowSetValueDecorator(OUString("BOOLEAN"));
104 aRow[2] = new ORowSetValueDecorator(DataType::BIT);
105 aRow[3] = ODatabaseMetaDataResultSet::get1Value();
106 aRow[4] = ODatabaseMetaDataResultSet::getEmptyValue();
107 aRow[5] = ODatabaseMetaDataResultSet::getEmptyValue();
108 aRow[6] = new ORowSetValueDecorator(OUString());
109 aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
110 aRow[13] = new ORowSetValueDecorator(OUString("L"));
111 aRows.push_back(aRow);
113 aRow[1] = new ORowSetValueDecorator(OUString("DOUBLE"));
114 aRow[2] = new ORowSetValueDecorator(DataType::DOUBLE);
115 aRow[3] = new ORowSetValueDecorator((sal_Int32)8);
116 aRow[13] = new ORowSetValueDecorator(OUString("B"));
117 aRows.push_back(aRow);
119 aRow[11] = new ORowSetValueDecorator(sal_True);
120 aRow[13] = new ORowSetValueDecorator(OUString("Y"));
121 aRows.push_back(aRow);
123 aRow[1] = new ORowSetValueDecorator(OUString("TIMESTAMP"));
124 aRow[2] = new ORowSetValueDecorator(DataType::TIMESTAMP);
125 aRow[11] = new ORowSetValueDecorator(sal_False);
126 aRow[13] = new ORowSetValueDecorator(OUString("T"));
127 aRows.push_back(aRow);
129 aRow[1] = new ORowSetValueDecorator(OUString("INTEGER"));
130 aRow[2] = new ORowSetValueDecorator(DataType::INTEGER);
131 aRow[3] = new ORowSetValueDecorator((sal_Int32)10);
132 aRow[13] = new ORowSetValueDecorator(OUString("I"));
133 aRows.push_back(aRow);
135 aRow[1] = new ORowSetValueDecorator(OUString("DECIMAL"));
136 aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
137 aRow[3] = new ORowSetValueDecorator((sal_Int32)20);
138 aRow[6] = new ORowSetValueDecorator(OUString("length,scale"));
139 aRow[13] = new ORowSetValueDecorator(OUString("F"));
140 aRows.push_back(aRow);
142 aRow[1] = new ORowSetValueDecorator(OUString("NUMERIC"));
143 aRow[2] = new ORowSetValueDecorator(DataType::DECIMAL);
144 aRow[3] = new ORowSetValueDecorator((sal_Int32)16);
145 aRow[13] = new ORowSetValueDecorator(OUString("N"));
146 aRow[15] = new ORowSetValueDecorator((sal_Int32)16);
147 aRows.push_back(aRow);
150 pResult->setRows(aRows);
151 return xRef;
154 Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getColumns(
155 const Any& /*catalog*/, const OUString& /*schemaPattern*/, const OUString& tableNamePattern,
156 const OUString& columnNamePattern ) throw(SQLException, RuntimeException, std::exception)
158 ::osl::MutexGuard aGuard( m_aMutex );
161 Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
162 if(!xTables.is())
163 throw SQLException();
165 Reference< XNameAccess> xNames = xTables->getTables();
166 if(!xNames.is())
167 throw SQLException();
169 ODatabaseMetaDataResultSet::ORows aRows;
170 ODatabaseMetaDataResultSet::ORow aRow(19);
172 aRow[10] = new ORowSetValueDecorator((sal_Int32)10);
173 Sequence< OUString> aTabNames(xNames->getElementNames());
174 const OUString* pTabBegin = aTabNames.getConstArray();
175 const OUString* pTabEnd = pTabBegin + aTabNames.getLength();
176 for(;pTabBegin != pTabEnd;++pTabBegin)
178 if(match(tableNamePattern,*pTabBegin,'\0'))
180 Reference< XColumnsSupplier> xTable(
181 xNames->getByName(*pTabBegin), css::uno::UNO_QUERY);
182 OSL_ENSURE(xTable.is(),"Table not found! Normallya exception had to be thrown here!");
183 aRow[3] = new ORowSetValueDecorator(*pTabBegin);
185 Reference< XNameAccess> xColumns = xTable->getColumns();
186 if(!xColumns.is())
187 throw SQLException();
189 Sequence< OUString> aColNames(xColumns->getElementNames());
191 const OUString* pBegin = aColNames.getConstArray();
192 const OUString* pEnd = pBegin + aColNames.getLength();
193 Reference< XPropertySet> xColumn;
194 for(sal_Int32 i=1;pBegin != pEnd;++pBegin,++i)
196 if(match(columnNamePattern,*pBegin,'\0'))
198 aRow[4] = new ORowSetValueDecorator(*pBegin);
200 xColumn.set(
201 xColumns->getByName(*pBegin), css::uno::UNO_QUERY);
202 OSL_ENSURE(xColumn.is(),"Columns contains a column who isn't a fastpropertyset!");
203 aRow[5] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))));
204 aRow[6] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))));
205 aRow[7] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))));
206 aRow[9] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
207 aRow[11] = new ORowSetValueDecorator(getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))));
208 aRow[13] = new ORowSetValueDecorator(getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))));
209 switch((sal_Int32)aRow[5]->getValue())
211 case DataType::CHAR:
212 case DataType::VARCHAR:
213 aRow[16] = new ORowSetValueDecorator((sal_Int32)254);
214 break;
215 case DataType::LONGVARCHAR:
216 aRow[16] = new ORowSetValueDecorator((sal_Int32)65535);
217 break;
218 default:
219 aRow[16] = new ORowSetValueDecorator((sal_Int32)0);
221 aRow[17] = new ORowSetValueDecorator(i);
222 switch(sal_Int32(aRow[11]->getValue()))
224 case ColumnValue::NO_NULLS:
225 aRow[18] = new ORowSetValueDecorator(OUString("NO"));
226 break;
227 case ColumnValue::NULLABLE:
228 aRow[18] = new ORowSetValueDecorator(OUString("YES"));
229 break;
230 default:
231 aRow[18] = new ORowSetValueDecorator(OUString());
233 aRows.push_back(aRow);
238 ::connectivity::ODatabaseMetaDataResultSet* pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns);
239 Reference< XResultSet > xRef = pResult;
240 pResult->setRows(aRows);
242 return xRef;
245 Reference< XResultSet > SAL_CALL ODbaseDatabaseMetaData::getIndexInfo(
246 const Any& /*catalog*/, const OUString& /*schema*/, const OUString& table,
247 sal_Bool unique, sal_Bool /*approximate*/ ) throw(SQLException, RuntimeException, std::exception)
249 ::osl::MutexGuard aGuard( m_aMutex );
251 Reference< XTablesSupplier > xTables = m_pConnection->createCatalog();
252 if(!xTables.is())
253 throw SQLException();
255 Reference< XNameAccess> xNames = xTables->getTables();
256 if(!xNames.is())
257 throw SQLException();
259 ODatabaseMetaDataResultSet::ORows aRows;
260 ODatabaseMetaDataResultSet::ORow aRow(14);
262 aRow[5] = new ORowSetValueDecorator(OUString());
263 aRow[10] = new ORowSetValueDecorator(OUString("A"));
265 Reference< XIndexesSupplier> xTable(
266 xNames->getByName(table), css::uno::UNO_QUERY);
267 aRow[3] = new ORowSetValueDecorator(table);
268 aRow[7] = new ORowSetValueDecorator((sal_Int32)3);
270 Reference< XNameAccess> xIndexes = xTable->getIndexes();
271 if(!xIndexes.is())
272 throw SQLException();
274 Sequence< OUString> aIdxNames(xIndexes->getElementNames());
276 const OUString* pBegin = aIdxNames.getConstArray();
277 const OUString* pEnd = pBegin + aIdxNames.getLength();
278 Reference< XPropertySet> xIndex;
279 for(;pBegin != pEnd;++pBegin)
281 xIndex.set(xIndexes->getByName(*pBegin), css::uno::UNO_QUERY);
282 OSL_ENSURE(xIndex.is(),"Indexes contains a column who isn't a fastpropertyset!");
284 if(unique && !getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))))
285 continue;
286 aRow[4] = new ORowSetValueDecorator(getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))));
287 aRow[6] = new ORowSetValueDecorator(*pBegin);
289 Reference< XUnoTunnel> xTunnel(xIndex,UNO_QUERY);
290 if(xTunnel.is())
292 ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( xTunnel->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
293 if(pIndex)
295 aRow[11] = new ORowSetValueDecorator((sal_Int32)pIndex->getHeader().db_maxkeys);
296 aRow[12] = new ORowSetValueDecorator((sal_Int32)pIndex->getHeader().db_pagecount);
300 Reference<XColumnsSupplier> xColumnsSup(xIndex,UNO_QUERY);
301 Reference< XNameAccess> xColumns = xColumnsSup->getColumns();
302 Sequence< OUString> aColNames(xColumns->getElementNames());
304 const OUString* pColBegin = aColNames.getConstArray();
305 const OUString* pColEnd = pColBegin + aColNames.getLength();
306 Reference< XPropertySet> xColumn;
307 for(sal_Int32 j=1;pColBegin != pColEnd;++pColBegin,++j)
309 aRow[8] = new ORowSetValueDecorator(j);
310 aRow[9] = new ORowSetValueDecorator(*pColBegin);
311 aRows.push_back(aRow);
315 ::connectivity::ODatabaseMetaDataResultSet* pResult = new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eIndexInfo);
316 Reference< XResultSet > xRef = pResult;
317 pResult->setRows(aRows);
318 return xRef;
321 OUString SAL_CALL ODbaseDatabaseMetaData::getURL( ) throw(SQLException, RuntimeException, std::exception)
323 ::osl::MutexGuard aGuard( m_aMutex );
324 return OUString("sdbc:dbase:") + m_pConnection->getURL();
327 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxBinaryLiteralLength( ) throw(SQLException, RuntimeException, std::exception)
329 return SAL_MAX_INT32;
332 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxCharLiteralLength( ) throw(SQLException, RuntimeException, std::exception)
334 return 254;
337 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnNameLength( ) throw(SQLException, RuntimeException, std::exception)
339 return 10;
342 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnsInIndex( ) throw(SQLException, RuntimeException, std::exception)
344 return 1;
347 sal_Int32 SAL_CALL ODbaseDatabaseMetaData::getMaxColumnsInTable( ) throw(SQLException, RuntimeException, std::exception)
349 return 128;
352 sal_Bool SAL_CALL ODbaseDatabaseMetaData::supportsAlterTableWithAddColumn( ) throw(SQLException, RuntimeException, std::exception)
354 return sal_True;
357 sal_Bool SAL_CALL ODbaseDatabaseMetaData::supportsAlterTableWithDropColumn( ) throw(SQLException, RuntimeException, std::exception)
359 return sal_False;
362 sal_Bool SAL_CALL ODbaseDatabaseMetaData::isReadOnly( ) throw(SQLException, RuntimeException, std::exception)
364 ::osl::MutexGuard aGuard( m_aMutex );
366 bool bReadOnly = false;
367 static OUString sReadOnly( "IsReadOnly" );
368 ::ucbhelper::Content aFile(m_pConnection->getContent(),Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
369 aFile.getPropertyValue(sReadOnly) >>= bReadOnly;
371 return bReadOnly;
374 bool ODbaseDatabaseMetaData::impl_storesMixedCaseQuotedIdentifiers_throw( )
376 return true;
379 bool ODbaseDatabaseMetaData::impl_supportsMixedCaseQuotedIdentifiers_throw( )
381 return true;
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */