1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LDatabaseMetaData.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "LDatabaseMetaData.hxx"
34 #include "LConnection.hxx"
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <com/sun/star/sdbc/ResultSetType.hpp>
37 #include <com/sun/star/sdbc/ColumnValue.hpp>
38 #include <com/sun/star/beans/XFastPropertySet.hpp>
39 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
40 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
41 #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
42 #include <com/sun/star/ucb/XContentAccess.hpp>
43 #ifndef _COM_SUN_STAR_SQLC_XROW_HPP_
44 #include <com/sun/star/sdbc/XRow.hpp>
46 #include <tools/urlobj.hxx>
47 #include "FDatabaseMetaDataResultSet.hxx"
48 #include <com/sun/star/lang/XUnoTunnel.hpp>
49 #include <comphelper/extract.hxx>
50 #include <comphelper/types.hxx>
51 #include "LFolderList.hxx"
52 #include "connectivity/CommonTools.hxx"
53 #include <vos/process.hxx>
54 #include <osl/process.h>
55 #include <tools/debug.hxx>
59 #ifndef CONNECTIVITY_EVOAB_DEBUG_HELPER_HXX
62 #include "diagnose_ex.h"
64 using namespace ::comphelper
;
65 using namespace connectivity
;
66 using namespace connectivity::evoab
;
67 using namespace ::com::sun::star::uno
;
68 using namespace ::com::sun::star::beans
;
69 using namespace ::com::sun::star::sdbcx
;
70 using namespace ::com::sun::star::sdbc
;
71 using namespace ::com::sun::star::container
;
72 using namespace ::com::sun::star::ucb
;
76 namespace connectivity
80 static sal_Int32
const s_nCOLUMN_SIZE
= 256;
81 static sal_Int32
const s_nDECIMAL_DIGITS
= 0;
82 static sal_Int32
const s_nNULLABLE
= 1;
83 static sal_Int32
const s_nCHAR_OCTET_LENGTH
= 65535;
87 OEvoabDatabaseMetaData::OEvoabDatabaseMetaData(::connectivity::file::OConnection
* _pCon
) :ODatabaseMetaData(_pCon
)
90 // -------------------------------------------------------------------------
91 OEvoabDatabaseMetaData::~OEvoabDatabaseMetaData()
94 // -------------------------------------------------------------------------
95 Reference
< XResultSet
> OEvoabDatabaseMetaData::impl_getTypeInfo_throw( )
97 ::osl::MutexGuard
aGuard( m_aMutex
);
99 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo
);
100 Reference
< XResultSet
> xRef
= pResult
;
101 static ODatabaseMetaDataResultSet::ORows aRows
;
104 ODatabaseMetaDataResultSet::ORow aRow
;
106 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
107 aRow
.push_back(new ORowSetValueDecorator(::rtl::OUString::createFromAscii("CHAR")));
108 aRow
.push_back(new ORowSetValueDecorator(DataType::CHAR
));
109 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)254));
110 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
111 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
112 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
113 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)ColumnValue::NULLABLE
));
114 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
115 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)ColumnSearch::CHAR
));
116 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
117 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
118 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
119 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
120 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
121 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
122 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
123 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
124 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)10));
126 aRows
.push_back(aRow
);
128 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("VARCHAR"));
129 aRow
[2] = new ORowSetValueDecorator(DataType::VARCHAR
);
130 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
131 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
132 aRows
.push_back(aRow
);
135 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("LONGVARCHAR"));
136 aRow
[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR
);
137 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)65535);
138 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
139 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
140 aRows
.push_back(aRow
);
142 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DATE"));
143 aRow
[2] = new ORowSetValueDecorator(DataType::DATE
);
144 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)10);
145 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
146 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
147 aRows
.push_back(aRow
);
149 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("TIME"));
150 aRow
[2] = new ORowSetValueDecorator(DataType::TIME
);
151 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)8);
152 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
153 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
154 aRows
.push_back(aRow
);
156 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("TIMESTAMP"));
157 aRow
[2] = new ORowSetValueDecorator(DataType::TIMESTAMP
);
158 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)19);
159 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
160 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
161 aRows
.push_back(aRow
);
163 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("BOOL"));
164 aRow
[2] = new ORowSetValueDecorator(DataType::BIT
);
165 aRow
[3] = ODatabaseMetaDataResultSet::get1Value();
166 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
167 aRows
.push_back(aRow
);
169 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DECIMAL"));
170 aRow
[2] = new ORowSetValueDecorator(DataType::DECIMAL
);
171 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
172 aRow
[15] = new ORowSetValueDecorator((sal_Int32
)15);
173 aRows
.push_back(aRow
);
175 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DOUBLE"));
176 aRow
[2] = new ORowSetValueDecorator(DataType::DOUBLE
);
177 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
178 aRow
[15] = ODatabaseMetaDataResultSet::get0Value();
179 aRows
.push_back(aRow
);
181 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("NUMERIC"));
182 aRow
[2] = new ORowSetValueDecorator(DataType::NUMERIC
);
183 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
184 aRow
[15] = new ORowSetValueDecorator((sal_Int32
)20);
185 aRows
.push_back(aRow
);
188 pResult
->setRows(aRows
);
191 // -------------------------------------------------------------------------
192 Reference
< XResultSet
> SAL_CALL
OEvoabDatabaseMetaData::getColumns(
193 const Any
& /*catalog*/, const ::rtl::OUString
& /*schemaPattern*/, const ::rtl::OUString
& tableNamePattern
,
194 const ::rtl::OUString
& columnNamePattern
) throw(SQLException
, RuntimeException
)
196 OSL_TRACE("OEvoabDatabaseMetaData::getColumns()::Entered\n");
197 EVO_TRACE_STRING("OEvoabDatabaseMetaData::getColumns()::schemaPattern = %s\n", schemaPattern
);
198 EVO_TRACE_STRING("OEvoabDatabaseMetaData::getColumns()::tableNamePattern = %s\n", tableNamePattern
);
199 EVO_TRACE_STRING("OEvoabDatabaseMetaData::getColumns()::columnNamePattern = %s\n", columnNamePattern
);
201 ::osl::MutexGuard
aGuard( m_aMutex
);
203 Reference
< XTablesSupplier
> xTables
= m_pConnection
->createCatalog();
205 throw SQLException();
207 Reference
< XNameAccess
> xNames
= xTables
->getTables();
209 throw SQLException();
211 ODatabaseMetaDataResultSet::ORows aRows
;
212 ODatabaseMetaDataResultSet::ORow
aRow(19);
213 aRow
[10] = new ORowSetValueDecorator((sal_Int32
)10);
214 Sequence
< ::rtl::OUString
> aTabNames(xNames
->getElementNames());
215 const ::rtl::OUString
* pTabBegin
= aTabNames
.getConstArray();
216 const ::rtl::OUString
* pTabEnd
= pTabBegin
+ aTabNames
.getLength();
217 for(;pTabBegin
!= pTabEnd
;++pTabBegin
)
219 if(match(tableNamePattern
,*pTabBegin
,'\0'))
221 Reference
< XColumnsSupplier
> xTable
;
222 ::cppu::extractInterface(xTable
,xNames
->getByName(*pTabBegin
));
223 aRow
[3] = new ORowSetValueDecorator(*pTabBegin
);
225 Reference
< XNameAccess
> xColumns
= xTable
->getColumns();
227 throw SQLException();
229 Sequence
< ::rtl::OUString
> aColNames(xColumns
->getElementNames());
231 const ::rtl::OUString
* pBegin
= aColNames
.getConstArray();
232 const ::rtl::OUString
* pEnd
= pBegin
+ aColNames
.getLength();
233 Reference
< XPropertySet
> xColumn
;
234 for(sal_Int32 i
=1;pBegin
!= pEnd
;++pBegin
,++i
)
236 if(match(columnNamePattern
,*pBegin
,'\0'))
238 aRow
[4] = new ORowSetValueDecorator(*pBegin
);
240 ::cppu::extractInterface(xColumn
,xColumns
->getByName(*pBegin
));
241 OSL_ENSURE(xColumn
.is(),"Columns contains a column who isn't a fastpropertyset!");
242 aRow
[5] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))));
243 aRow
[6] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME
))));
244 aRow
[7] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION
))));
245 aRow
[9] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE
))));
246 aRow
[11] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
))));
247 aRow
[13] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE
))));
249 switch((sal_Int32
)aRow
[5]->getValue())
252 case DataType::VARCHAR
:
253 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)254);
255 case DataType::LONGVARCHAR
:
256 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)65535);
259 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)0);
261 aRow
[17] = new ORowSetValueDecorator(i
);
262 switch(sal_Int32(aRow
[11]->getValue()))
264 case ColumnValue::NO_NULLS
:
265 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("NO"));
267 case ColumnValue::NULLABLE
:
268 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("YES"));
271 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString());
273 aRows
.push_back(aRow
);
279 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns
);
280 Reference
< XResultSet
> xRef
= pResult
;
281 pResult
->setRows(aRows
);
285 // -------------------------------------------------------------------------
286 ::rtl::OUString SAL_CALL
OEvoabDatabaseMetaData::getURL( ) throw(SQLException
, RuntimeException
)
288 ::osl::MutexGuard
aGuard( m_aMutex
);
289 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("sdbc:flat:")) + m_pConnection
->getURL();
291 // -------------------------------------------------------------------------
292 Reference
< XResultSet
> SAL_CALL
OEvoabDatabaseMetaData::getTables(
293 const Any
& /*catalog*/, const ::rtl::OUString
& /*schemaPattern*/,
294 const ::rtl::OUString
& /*tableNamePattern*/, const Sequence
< ::rtl::OUString
>& types
) throw(SQLException
, RuntimeException
)
296 ::osl::MutexGuard
aGuard( m_aMutex
);
299 ODatabaseMetaDataResultSet
* pResult
= new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables
);
300 Reference
< XResultSet
> xRef
= pResult
;
302 // check if any type is given
303 // when no types are given then we have to return all tables e.g. TABLE
305 static const ::rtl::OUString
aTable(::rtl::OUString::createFromAscii("TABLE"));
307 sal_Bool bTableFound
= sal_True
;
308 sal_Int32 nLength
= types
.getLength();
311 bTableFound
= sal_False
;
313 const ::rtl::OUString
* pBegin
= types
.getConstArray();
314 const ::rtl::OUString
* pEnd
= pBegin
+ nLength
;
315 for(;pBegin
!= pEnd
;++pBegin
)
317 if(*pBegin
== aTable
)
319 bTableFound
= sal_True
;
327 OEvoabConnection
* pOEvoabConnection
= (OEvoabConnection
*)m_pConnection
;
328 OEvoabFolderList
* pFolderList
= new OEvoabFolderList( pOEvoabConnection
);
331 ODatabaseMetaDataResultSet::ORows aRows
;
332 sal_Bool bMoreData
= sal_True
;
333 ::rtl::OUString aName
, aLocation
;
334 sal_Int32 nCardsCount
;
336 pFolderList
->initializeRow(3);
337 bMoreData
= pFolderList
->first();
338 OSL_TRACE("OEvoabDatabaseMetaData::getTables()::first %d\n",bMoreData
);
342 bMoreData
= pFolderList
->getRow();
345 aLocation
= pFolderList
->getString(1);
346 aName
= pFolderList
->getString(2);
347 nCardsCount
= pFolderList
->getInt(3);
348 EVO_TRACE_STRING( "OEvoabDatabaseMetaData::getTables()::aLocation = %s\n", aLocation
);
349 EVO_TRACE_STRING( "OEvoabDatabaseMetaData::getTables()::aName = %s\n", aName
);
350 OSL_TRACE("OEvoabDatabaseMetaData::getTables()::nCardsCount = %d\n", nCardsCount
);
352 ODatabaseMetaDataResultSet::ORow
aRow(3);
354 aRow
.push_back(new ORowSetValueDecorator(aName
));
355 aRow
.push_back(new ORowSetValueDecorator(aTable
));
356 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
357 aRows
.push_back(aRow
);
359 ::rtl::OUString aWorkingDir
= pOEvoabConnection
->getDriver()->getWorkingDirPath();
360 ::rtl::OUString aCLICommand
= pOEvoabConnection
->getDriver()->getEvoab_CLI_EffectiveCommand();
361 ::rtl::OUString aArg1
= aLocation
;
362 ::rtl::OUString aArg2
= ::rtl::OUString::createFromAscii(pOEvoabConnection
->getDriver()->getEVOAB_CLI_ARG_OUTPUT_FILE_PREFIX());
363 aArg2
+= aWorkingDir
;
365 aArg2
+= ::rtl::OUString::createFromAscii(".");
366 aArg2
+= ::rtl::OUString(pOEvoabConnection
->getExtension());
367 ::rtl::OUString aArg3
= ::rtl::OUString::createFromAscii(pOEvoabConnection
->getDriver()->getEVOAB_CLI_ARG_OUTPUT_FORMAT());
369 OArgumentList
aArgs(3,&aArg1
,&aArg2
,&aArg3
);
371 EVO_TRACE_STRING( "OEvoabDatabaseMetaData::getTables()::aCLICommand = %s\n", aCLICommand
);
372 EVO_TRACE_STRING( "OEvoabDatabaseMetaData::getTables()::aWorkingDir = %s\n", aWorkingDir
);
373 EVO_TRACE_STRING( "OEvoabDatabaseMetaData::getTables()::aArg1 = %s\n", aArg1
);
374 EVO_TRACE_STRING( "OEvoabDatabaseMetaData::getTables()::aArg2 = %s\n", aArg2
);
375 EVO_TRACE_STRING( "OEvoabDatabaseMetaData::getTables()::aArg3 = %s\n", aArg3
);
377 OProcess
aApp( aCLICommand
,aWorkingDir
);
379 aApp
.execute( (OProcess::TProcessOption
)(OProcess::TOption_Hidden
| OProcess::TOption_Wait
| OProcess::TOption_SearchPath
),aArgs
),
381 "Error at execute evolution-addressbook-exporter to get VCards" );
383 bMoreData
= pFolderList
->next();
391 pResult
->setRows(aRows
);