1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/lang/WrappedTargetRuntimeException.hpp>
22 #include <com/sun/star/sdbc/ColumnSearch.hpp>
23 #include <com/sun/star/sdbc/DataType.hpp>
24 #include <com/sun/star/sdbc/ColumnValue.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
27 #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
28 #include <FDatabaseMetaDataResultSet.hxx>
29 #include <dbase/DIndex.hxx>
30 #include <connectivity/FValue.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <comphelper/servicehelper.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 rtl::Reference
<::connectivity::ODatabaseMetaDataResultSet
> pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo
);
61 static ODatabaseMetaDataResultSet::ORows aRows
;
65 ODatabaseMetaDataResultSet::ORow aRow
67 ODatabaseMetaDataResultSet::getEmptyValue(),
68 new ORowSetValueDecorator(OUString("VARCHAR")),
69 new ORowSetValueDecorator(DataType::VARCHAR
),
70 new ORowSetValueDecorator(sal_Int32(254)),
71 ODatabaseMetaDataResultSet::getQuoteValue(),
72 ODatabaseMetaDataResultSet::getQuoteValue(),
73 new ORowSetValueDecorator(OUString("length")),
74 new ORowSetValueDecorator(sal_Int32(ColumnValue::NULLABLE
)),
75 ODatabaseMetaDataResultSet::get1Value(),
76 new ORowSetValueDecorator(sal_Int32(ColumnSearch::FULL
)),
77 ODatabaseMetaDataResultSet::get1Value(),
78 ODatabaseMetaDataResultSet::get0Value(),
79 ODatabaseMetaDataResultSet::get0Value(),
80 new ORowSetValueDecorator(OUString("C")),
81 ODatabaseMetaDataResultSet::get0Value(),
82 ODatabaseMetaDataResultSet::get0Value(),
83 ODatabaseMetaDataResultSet::getEmptyValue(),
84 ODatabaseMetaDataResultSet::getEmptyValue(),
85 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(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(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
);
154 Reference
< XResultSet
> SAL_CALL
ODbaseDatabaseMetaData::getColumns(
155 const Any
& /*catalog*/, const OUString
& /*schemaPattern*/, const OUString
& tableNamePattern
,
156 const OUString
& columnNamePattern
)
158 ::osl::MutexGuard
aGuard( m_aMutex
);
160 Reference
< XTablesSupplier
> xTables
= m_pConnection
->createCatalog();
162 throw SQLException();
164 Reference
< XNameAccess
> xNames
= xTables
->getTables();
166 throw SQLException();
168 ODatabaseMetaDataResultSet::ORows aRows
;
169 ODatabaseMetaDataResultSet::ORow
aRow(19);
173 aRow
[10] = new ORowSetValueDecorator(sal_Int32(10));
174 Sequence
< OUString
> aTabNames(xNames
->getElementNames());
175 const OUString
* pTabBegin
= aTabNames
.getConstArray();
176 const OUString
* pTabEnd
= pTabBegin
+ aTabNames
.getLength();
177 for(;pTabBegin
!= pTabEnd
;++pTabBegin
)
179 if(match(tableNamePattern
,*pTabBegin
,'\0'))
181 Reference
< XColumnsSupplier
> xTable(
182 xNames
->getByName(*pTabBegin
), css::uno::UNO_QUERY
);
183 OSL_ENSURE(xTable
.is(),"Table not found! Normally an exception had to be thrown here!");
184 aRow
[3] = new ORowSetValueDecorator(*pTabBegin
);
186 Reference
< XNameAccess
> xColumns
= xTable
->getColumns();
188 throw SQLException();
190 Sequence
< OUString
> aColNames(xColumns
->getElementNames());
192 const OUString
* pBegin
= aColNames
.getConstArray();
193 const OUString
* pEnd
= pBegin
+ aColNames
.getLength();
194 Reference
< XPropertySet
> xColumn
;
195 for(sal_Int32 i
=1;pBegin
!= pEnd
;++pBegin
,++i
)
197 if(match(columnNamePattern
,*pBegin
,'\0'))
199 aRow
[4] = new ORowSetValueDecorator(*pBegin
);
202 xColumns
->getByName(*pBegin
), css::uno::UNO_QUERY
);
203 OSL_ENSURE(xColumn
.is(),"Columns contains a column who isn't a fastpropertyset!");
204 aRow
[5] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))));
205 aRow
[6] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME
))));
206 aRow
[7] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION
))));
207 aRow
[9] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE
))));
208 aRow
[11] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
))));
209 aRow
[13] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE
))));
210 switch(static_cast<sal_Int32
>(aRow
[5]->getValue()))
213 case DataType::VARCHAR
:
214 aRow
[16] = new ORowSetValueDecorator(sal_Int32(254));
216 case DataType::LONGVARCHAR
:
217 aRow
[16] = new ORowSetValueDecorator(sal_Int32(65535));
220 aRow
[16] = new ORowSetValueDecorator(sal_Int32(0));
222 aRow
[17] = new ORowSetValueDecorator(i
);
223 switch(sal_Int32(aRow
[11]->getValue()))
225 case ColumnValue::NO_NULLS
:
226 aRow
[18] = new ORowSetValueDecorator(OUString("NO"));
228 case ColumnValue::NULLABLE
:
229 aRow
[18] = new ORowSetValueDecorator(OUString("YES"));
232 aRow
[18] = new ORowSetValueDecorator(OUString());
234 aRows
.push_back(aRow
);
240 catch (const WrappedTargetException
& e
)
243 if (e
.TargetException
>>= aSql
)
245 throw WrappedTargetRuntimeException(e
.Message
, e
.Context
, e
.TargetException
);
247 rtl::Reference
<::connectivity::ODatabaseMetaDataResultSet
> pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns
);
248 pResult
->setRows(aRows
);
253 Reference
< XResultSet
> SAL_CALL
ODbaseDatabaseMetaData::getIndexInfo(
254 const Any
& /*catalog*/, const OUString
& /*schema*/, const OUString
& table
,
255 sal_Bool unique
, sal_Bool
/*approximate*/ )
257 ::osl::MutexGuard
aGuard( m_aMutex
);
259 Reference
< XTablesSupplier
> xTables
= m_pConnection
->createCatalog();
261 throw SQLException();
263 Reference
< XNameAccess
> xNames
= xTables
->getTables();
265 throw SQLException();
267 ODatabaseMetaDataResultSet::ORows aRows
;
268 ODatabaseMetaDataResultSet::ORow
aRow(14);
270 aRow
[5] = new ORowSetValueDecorator(OUString());
271 aRow
[10] = new ORowSetValueDecorator(OUString("A"));
273 Reference
< XIndexesSupplier
> xTable(
274 xNames
->getByName(table
), css::uno::UNO_QUERY
);
275 aRow
[3] = new ORowSetValueDecorator(table
);
276 aRow
[7] = new ORowSetValueDecorator(sal_Int32(3));
278 Reference
< XNameAccess
> xIndexes
= xTable
->getIndexes();
280 throw SQLException();
282 Sequence
< OUString
> aIdxNames(xIndexes
->getElementNames());
284 const OUString
* pBegin
= aIdxNames
.getConstArray();
285 const OUString
* pEnd
= pBegin
+ aIdxNames
.getLength();
286 Reference
< XPropertySet
> xIndex
;
287 for(;pBegin
!= pEnd
;++pBegin
)
289 xIndex
.set(xIndexes
->getByName(*pBegin
), css::uno::UNO_QUERY
);
290 OSL_ENSURE(xIndex
.is(),"Indexes contains a column who isn't a fastpropertyset!");
292 if(unique
&& !getBOOL(xIndex
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE
))))
294 aRow
[4] = new ORowSetValueDecorator(getBOOL(xIndex
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE
))));
295 aRow
[6] = new ORowSetValueDecorator(*pBegin
);
297 auto pIndex
= comphelper::getUnoTunnelImplementation
<ODbaseIndex
>(xIndex
);
300 aRow
[11] = new ORowSetValueDecorator(static_cast<sal_Int32
>(pIndex
->getHeader().db_maxkeys
));
301 aRow
[12] = new ORowSetValueDecorator(static_cast<sal_Int32
>(pIndex
->getHeader().db_pagecount
));
304 Reference
<XColumnsSupplier
> xColumnsSup(xIndex
,UNO_QUERY
);
305 Reference
< XNameAccess
> xColumns
= xColumnsSup
->getColumns();
306 Sequence
< OUString
> aColNames(xColumns
->getElementNames());
308 const OUString
* pColBegin
= aColNames
.getConstArray();
309 const OUString
* pColEnd
= pColBegin
+ aColNames
.getLength();
310 for(sal_Int32 j
=1;pColBegin
!= pColEnd
;++pColBegin
,++j
)
312 aRow
[8] = new ORowSetValueDecorator(j
);
313 aRow
[9] = new ORowSetValueDecorator(*pColBegin
);
314 aRows
.push_back(aRow
);
318 rtl::Reference
<::connectivity::ODatabaseMetaDataResultSet
> pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eIndexInfo
);
319 pResult
->setRows(aRows
);
323 OUString SAL_CALL
ODbaseDatabaseMetaData::getURL( )
325 ::osl::MutexGuard
aGuard( m_aMutex
);
326 return "sdbc:dbase:" + m_pConnection
->getURL();
329 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxBinaryLiteralLength( )
331 return SAL_MAX_INT32
;
334 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxCharLiteralLength( )
339 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxColumnNameLength( )
344 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxColumnsInIndex( )
349 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxColumnsInTable( )
354 sal_Bool SAL_CALL
ODbaseDatabaseMetaData::supportsAlterTableWithAddColumn( )
359 sal_Bool SAL_CALL
ODbaseDatabaseMetaData::supportsAlterTableWithDropColumn( )
364 sal_Bool SAL_CALL
ODbaseDatabaseMetaData::isReadOnly( )
366 ::osl::MutexGuard
aGuard( m_aMutex
);
368 bool bReadOnly
= false;
369 ::ucbhelper::Content
aFile(m_pConnection
->getContent(),Reference
< XCommandEnvironment
>(), comphelper::getProcessComponentContext());
370 aFile
.getPropertyValue("IsReadOnly") >>= bReadOnly
;
375 bool ODbaseDatabaseMetaData::impl_storesMixedCaseQuotedIdentifiers_throw( )
380 bool ODbaseDatabaseMetaData::impl_supportsMixedCaseQuotedIdentifiers_throw( )
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */