1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include "dbase/DDatabaseMetaData.hxx"
32 #include <com/sun/star/sdbc/DataType.hpp>
33 #include <com/sun/star/sdbc/ResultSetType.hpp>
34 #include <com/sun/star/sdbc/ColumnValue.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
37 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
38 #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
39 #include "FDatabaseMetaDataResultSet.hxx"
40 #include <com/sun/star/lang/XUnoTunnel.hpp>
41 #include "dbase/DIndex.hxx"
42 #include "connectivity/FValue.hxx"
43 #include <comphelper/extract.hxx>
44 #include <comphelper/types.hxx>
45 #include <ucbhelper/content.hxx>
46 #include <rtl/logfile.hxx>
48 using namespace ::comphelper
;
49 using namespace connectivity::dbase
;
50 using namespace connectivity
;
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::beans
;
53 using namespace ::com::sun::star::sdbcx
;
54 using namespace ::com::sun::star::sdbc
;
55 using namespace ::com::sun::star::container
;
56 using namespace ::com::sun::star::ucb
;
57 using namespace ::com::sun::star::lang
;
59 ODbaseDatabaseMetaData::ODbaseDatabaseMetaData(::connectivity::file::OConnection
* _pCon
) :ODatabaseMetaData(_pCon
)
61 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::ODbaseDatabaseMetaData" );
63 // -------------------------------------------------------------------------
64 ODbaseDatabaseMetaData::~ODbaseDatabaseMetaData()
67 // -------------------------------------------------------------------------
68 Reference
< XResultSet
> ODbaseDatabaseMetaData::impl_getTypeInfo_throw( )
70 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::impl_getTypeInfo_throw" );
71 ::osl::MutexGuard
aGuard( m_aMutex
);
73 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo
);
74 Reference
< XResultSet
> xRef
= pResult
;
76 static ODatabaseMetaDataResultSet::ORows aRows
;
79 ODatabaseMetaDataResultSet::ORow aRow
;
82 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
83 aRow
.push_back(new ORowSetValueDecorator(::rtl::OUString("VARCHAR")));
84 aRow
.push_back(new ORowSetValueDecorator(DataType::VARCHAR
));
85 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)254));
86 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
87 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
88 aRow
.push_back(new ORowSetValueDecorator(::rtl::OUString("length")));
89 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)ColumnValue::NULLABLE
));
90 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
91 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)ColumnSearch::FULL
));
92 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
93 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
94 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
95 aRow
.push_back(new ORowSetValueDecorator(::rtl::OUString("C")));
96 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
97 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
98 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
99 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
100 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)10));
102 aRows
.push_back(aRow
);
104 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("LONGVARCHAR"));
105 aRow
[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR
);
106 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)2147483647);
107 aRow
[6] = new ORowSetValueDecorator();
108 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("M"));
109 aRows
.push_back(aRow
);
111 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("DATE"));
112 aRow
[2] = new ORowSetValueDecorator(DataType::DATE
);
113 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)10);
114 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("D"));
115 aRows
.push_back(aRow
);
117 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("BOOLEAN"));
118 aRow
[2] = new ORowSetValueDecorator(DataType::BIT
);
119 aRow
[3] = ODatabaseMetaDataResultSet::get1Value();
120 aRow
[4] = ODatabaseMetaDataResultSet::getEmptyValue();
121 aRow
[5] = ODatabaseMetaDataResultSet::getEmptyValue();
122 aRow
[6] = new ORowSetValueDecorator(::rtl::OUString());
123 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
124 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("L"));
125 aRows
.push_back(aRow
);
127 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("DOUBLE"));
128 aRow
[2] = new ORowSetValueDecorator(DataType::DOUBLE
);
129 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)8);
130 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("B"));
131 aRows
.push_back(aRow
);
133 aRow
[11] = new ORowSetValueDecorator(sal_True
);
134 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("Y"));
135 aRows
.push_back(aRow
);
137 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("TIMESTAMP"));
138 aRow
[2] = new ORowSetValueDecorator(DataType::TIMESTAMP
);
139 aRow
[11] = new ORowSetValueDecorator(sal_False
);
140 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("T"));
141 aRows
.push_back(aRow
);
143 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("INTEGER"));
144 aRow
[2] = new ORowSetValueDecorator(DataType::INTEGER
);
145 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)10);
146 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("I"));
147 aRows
.push_back(aRow
);
149 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("DECIMAL"));
150 aRow
[2] = new ORowSetValueDecorator(DataType::DECIMAL
);
151 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
152 aRow
[6] = new ORowSetValueDecorator(::rtl::OUString("length,scale"));
153 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("F"));
154 aRows
.push_back(aRow
);
156 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString("NUMERIC"));
157 aRow
[2] = new ORowSetValueDecorator(DataType::DECIMAL
);
158 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)16);
159 aRow
[13] = new ORowSetValueDecorator(::rtl::OUString("N"));
160 aRow
[15] = new ORowSetValueDecorator((sal_Int32
)16);
161 aRows
.push_back(aRow
);
164 pResult
->setRows(aRows
);
167 // -------------------------------------------------------------------------
168 Reference
< XResultSet
> SAL_CALL
ODbaseDatabaseMetaData::getColumns(
169 const Any
& /*catalog*/, const ::rtl::OUString
& /*schemaPattern*/, const ::rtl::OUString
& tableNamePattern
,
170 const ::rtl::OUString
& columnNamePattern
) throw(SQLException
, RuntimeException
)
172 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getColumns" );
173 ::osl::MutexGuard
aGuard( m_aMutex
);
176 Reference
< XTablesSupplier
> xTables
= m_pConnection
->createCatalog();
178 throw SQLException();
180 Reference
< XNameAccess
> xNames
= xTables
->getTables();
182 throw SQLException();
184 ODatabaseMetaDataResultSet::ORows aRows
;
185 ODatabaseMetaDataResultSet::ORow
aRow(19);
187 aRow
[10] = new ORowSetValueDecorator((sal_Int32
)10);
188 Sequence
< ::rtl::OUString
> aTabNames(xNames
->getElementNames());
189 const ::rtl::OUString
* pTabBegin
= aTabNames
.getConstArray();
190 const ::rtl::OUString
* pTabEnd
= pTabBegin
+ aTabNames
.getLength();
191 for(;pTabBegin
!= pTabEnd
;++pTabBegin
)
193 if(match(tableNamePattern
,*pTabBegin
,'\0'))
195 Reference
< XColumnsSupplier
> xTable
;
196 ::cppu::extractInterface(xTable
,xNames
->getByName(*pTabBegin
));
197 OSL_ENSURE(xTable
.is(),"Table not found! Normallya exception had to be thrown here!");
198 aRow
[3] = new ORowSetValueDecorator(*pTabBegin
);
200 Reference
< XNameAccess
> xColumns
= xTable
->getColumns();
202 throw SQLException();
204 Sequence
< ::rtl::OUString
> aColNames(xColumns
->getElementNames());
206 const ::rtl::OUString
* pBegin
= aColNames
.getConstArray();
207 const ::rtl::OUString
* pEnd
= pBegin
+ aColNames
.getLength();
208 Reference
< XPropertySet
> xColumn
;
209 for(sal_Int32 i
=1;pBegin
!= pEnd
;++pBegin
,++i
)
211 if(match(columnNamePattern
,*pBegin
,'\0'))
213 aRow
[4] = new ORowSetValueDecorator(*pBegin
);
215 ::cppu::extractInterface(xColumn
,xColumns
->getByName(*pBegin
));
216 OSL_ENSURE(xColumn
.is(),"Columns contains a column who isn't a fastpropertyset!");
217 aRow
[5] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))));
218 aRow
[6] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME
))));
219 aRow
[7] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION
))));
220 aRow
[9] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE
))));
221 aRow
[11] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
))));
222 aRow
[13] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE
))));
223 switch((sal_Int32
)aRow
[5]->getValue())
226 case DataType::VARCHAR
:
227 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)254);
229 case DataType::LONGVARCHAR
:
230 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)65535);
233 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)0);
235 aRow
[17] = new ORowSetValueDecorator(i
);
236 switch(sal_Int32(aRow
[11]->getValue()))
238 case ColumnValue::NO_NULLS
:
239 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString("NO"));
241 case ColumnValue::NULLABLE
:
242 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString("YES"));
245 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString());
247 aRows
.push_back(aRow
);
252 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns
);
253 Reference
< XResultSet
> xRef
= pResult
;
254 pResult
->setRows(aRows
);
258 // -------------------------------------------------------------------------
259 Reference
< XResultSet
> SAL_CALL
ODbaseDatabaseMetaData::getIndexInfo(
260 const Any
& /*catalog*/, const ::rtl::OUString
& /*schema*/, const ::rtl::OUString
& table
,
261 sal_Bool unique
, sal_Bool
/*approximate*/ ) throw(SQLException
, RuntimeException
)
263 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getIndexInfo" );
264 ::osl::MutexGuard
aGuard( m_aMutex
);
266 Reference
< XTablesSupplier
> xTables
= m_pConnection
->createCatalog();
268 throw SQLException();
270 Reference
< XNameAccess
> xNames
= xTables
->getTables();
272 throw SQLException();
274 ODatabaseMetaDataResultSet::ORows aRows
;
275 ODatabaseMetaDataResultSet::ORow
aRow(14);
277 aRow
[5] = new ORowSetValueDecorator(::rtl::OUString());
278 aRow
[10] = new ORowSetValueDecorator(::rtl::OUString("A"));
280 Reference
< XIndexesSupplier
> xTable
;
281 ::cppu::extractInterface(xTable
,xNames
->getByName(table
));
282 aRow
[3] = new ORowSetValueDecorator(table
);
283 aRow
[7] = new ORowSetValueDecorator((sal_Int32
)3);
285 Reference
< XNameAccess
> xIndexes
= xTable
->getIndexes();
287 throw SQLException();
289 Sequence
< ::rtl::OUString
> aIdxNames(xIndexes
->getElementNames());
291 const ::rtl::OUString
* pBegin
= aIdxNames
.getConstArray();
292 const ::rtl::OUString
* pEnd
= pBegin
+ aIdxNames
.getLength();
293 Reference
< XPropertySet
> xIndex
;
294 for(;pBegin
!= pEnd
;++pBegin
)
296 ::cppu::extractInterface(xIndex
,xIndexes
->getByName(*pBegin
));
297 OSL_ENSURE(xIndex
.is(),"Indexes contains a column who isn't a fastpropertyset!");
299 if(unique
&& !getBOOL(xIndex
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE
))))
301 aRow
[4] = new ORowSetValueDecorator(getBOOL(xIndex
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE
))));
302 aRow
[6] = new ORowSetValueDecorator(*pBegin
);
304 Reference
< XUnoTunnel
> xTunnel(xIndex
,UNO_QUERY
);
307 ODbaseIndex
* pIndex
= reinterpret_cast< ODbaseIndex
* >( xTunnel
->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
310 aRow
[11] = new ORowSetValueDecorator((sal_Int32
)pIndex
->getHeader().db_maxkeys
);
311 aRow
[12] = new ORowSetValueDecorator((sal_Int32
)pIndex
->getHeader().db_pagecount
);
315 Reference
<XColumnsSupplier
> xColumnsSup(xIndex
,UNO_QUERY
);
316 Reference
< XNameAccess
> xColumns
= xColumnsSup
->getColumns();
317 Sequence
< ::rtl::OUString
> aColNames(xColumns
->getElementNames());
319 const ::rtl::OUString
* pColBegin
= aColNames
.getConstArray();
320 const ::rtl::OUString
* pColEnd
= pColBegin
+ aColNames
.getLength();
321 Reference
< XPropertySet
> xColumn
;
322 for(sal_Int32 j
=1;pColBegin
!= pColEnd
;++pColBegin
,++j
)
324 aRow
[8] = new ORowSetValueDecorator(j
);
325 aRow
[9] = new ORowSetValueDecorator(*pColBegin
);
326 aRows
.push_back(aRow
);
330 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eIndexInfo
);
331 Reference
< XResultSet
> xRef
= pResult
;
332 pResult
->setRows(aRows
);
335 // -------------------------------------------------------------------------
336 ::rtl::OUString SAL_CALL
ODbaseDatabaseMetaData::getURL( ) throw(SQLException
, RuntimeException
)
338 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getURL" );
339 ::osl::MutexGuard
aGuard( m_aMutex
);
340 return ::rtl::OUString("sdbc:dbase:") + m_pConnection
->getURL();
342 // -------------------------------------------------------------------------
343 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxBinaryLiteralLength( ) throw(SQLException
, RuntimeException
)
345 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getMaxBinaryLiteralLength" );
346 return STRING_MAXLEN
;
348 // -------------------------------------------------------------------------
349 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxCharLiteralLength( ) throw(SQLException
, RuntimeException
)
351 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getMaxCharLiteralLength" );
354 // -------------------------------------------------------------------------
355 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxColumnNameLength( ) throw(SQLException
, RuntimeException
)
357 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getMaxColumnNameLength" );
360 // -------------------------------------------------------------------------
361 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxColumnsInIndex( ) throw(SQLException
, RuntimeException
)
363 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getMaxColumnsInIndex" );
366 // -------------------------------------------------------------------------
367 sal_Int32 SAL_CALL
ODbaseDatabaseMetaData::getMaxColumnsInTable( ) throw(SQLException
, RuntimeException
)
369 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::getMaxColumnsInTable" );
372 // -----------------------------------------------------------------------------
373 sal_Bool SAL_CALL
ODbaseDatabaseMetaData::supportsAlterTableWithAddColumn( ) throw(SQLException
, RuntimeException
)
375 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::supportsAlterTableWithAddColumn" );
378 // -------------------------------------------------------------------------
379 sal_Bool SAL_CALL
ODbaseDatabaseMetaData::supportsAlterTableWithDropColumn( ) throw(SQLException
, RuntimeException
)
381 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::supportsAlterTableWithDropColumn" );
384 // -----------------------------------------------------------------------------
385 sal_Bool SAL_CALL
ODbaseDatabaseMetaData::isReadOnly( ) throw(SQLException
, RuntimeException
)
387 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::isReadOnly" );
388 ::osl::MutexGuard
aGuard( m_aMutex
);
390 sal_Bool bReadOnly
= sal_False
;
391 static ::rtl::OUString
sReadOnly( "IsReadOnly" );
392 ::ucbhelper::Content
aFile(m_pConnection
->getContent(),Reference
< XCommandEnvironment
>());
393 aFile
.getPropertyValue(sReadOnly
) >>= bReadOnly
;
397 // -----------------------------------------------------------------------------
398 sal_Bool
ODbaseDatabaseMetaData::impl_storesMixedCaseQuotedIdentifiers_throw( )
400 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::impl_storesMixedCaseQuotedIdentifiers_throw" );
403 // -----------------------------------------------------------------------------
404 sal_Bool
ODbaseDatabaseMetaData::impl_supportsMixedCaseQuotedIdentifiers_throw( )
406 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "dbase", "Ocke.Janssen@sun.com", "ODbaseDatabaseMetaData::impl_supportsMixedCaseQuotedIdentifiers_throw" );
409 // -----------------------------------------------------------------------------
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */