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: EDatabaseMetaData.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"
35 #include "flat/EDatabaseMetaData.hxx"
36 #include <com/sun/star/sdbc/DataType.hpp>
37 #include <com/sun/star/sdbc/ResultSetType.hpp>
38 #include <com/sun/star/sdbc/ColumnValue.hpp>
39 #include <com/sun/star/beans/XFastPropertySet.hpp>
40 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
41 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
42 #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
43 #include <tools/urlobj.hxx>
44 #include "FDatabaseMetaDataResultSet.hxx"
45 #include <com/sun/star/lang/XUnoTunnel.hpp>
46 #include <comphelper/extract.hxx>
47 #include <comphelper/types.hxx>
48 #include <rtl/logfile.hxx>
50 using namespace ::comphelper
;
52 using namespace connectivity
;
53 using namespace connectivity::flat
;
54 // using namespace connectivity::file;
55 using namespace ::com::sun::star::uno
;
56 using namespace ::com::sun::star::beans
;
57 using namespace ::com::sun::star::sdbcx
;
58 using namespace ::com::sun::star::sdbc
;
59 using namespace ::com::sun::star::container
;
63 OFlatDatabaseMetaData::OFlatDatabaseMetaData(::connectivity::file::OConnection
* _pCon
) :ODatabaseMetaData(_pCon
)
65 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::OFlatDatabaseMetaData" );
67 // -------------------------------------------------------------------------
68 OFlatDatabaseMetaData::~OFlatDatabaseMetaData()
70 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::~OFlatDatabaseMetaData" );
72 // -------------------------------------------------------------------------
73 Reference
< XResultSet
> OFlatDatabaseMetaData::impl_getTypeInfo_throw( )
75 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::impl_getTypeInfo_throw" );
76 ::osl::MutexGuard
aGuard( m_aMutex
);
78 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo
);
79 Reference
< XResultSet
> xRef
= pResult
;
81 static ODatabaseMetaDataResultSet::ORows aRows
;
84 ODatabaseMetaDataResultSet::ORow aRow
;
86 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
87 aRow
.push_back(new ORowSetValueDecorator(::rtl::OUString::createFromAscii("CHAR")));
88 aRow
.push_back(new ORowSetValueDecorator(DataType::CHAR
));
89 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)254));
90 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
91 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
92 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
93 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)ColumnValue::NULLABLE
));
94 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
95 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)ColumnSearch::CHAR
));
96 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
97 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
98 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
99 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
100 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
101 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
102 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
103 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
104 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)10));
106 aRows
.push_back(aRow
);
108 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("VARCHAR"));
109 aRow
[2] = new ORowSetValueDecorator(DataType::VARCHAR
);
110 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
111 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
112 aRows
.push_back(aRow
);
115 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("LONGVARCHAR"));
116 aRow
[2] = new ORowSetValueDecorator(DataType::LONGVARCHAR
);
117 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)65535);
118 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
119 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
120 aRows
.push_back(aRow
);
122 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DATE"));
123 aRow
[2] = new ORowSetValueDecorator(DataType::DATE
);
124 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)10);
125 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
126 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
127 aRows
.push_back(aRow
);
129 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("TIME"));
130 aRow
[2] = new ORowSetValueDecorator(DataType::TIME
);
131 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)8);
132 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
133 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
134 aRows
.push_back(aRow
);
136 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("TIMESTAMP"));
137 aRow
[2] = new ORowSetValueDecorator(DataType::TIMESTAMP
);
138 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)19);
139 aRow
[4] = ODatabaseMetaDataResultSet::getQuoteValue();
140 aRow
[5] = ODatabaseMetaDataResultSet::getQuoteValue();
141 aRows
.push_back(aRow
);
143 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("BOOL"));
144 aRow
[2] = new ORowSetValueDecorator(DataType::BIT
);
145 aRow
[3] = ODatabaseMetaDataResultSet::get1Value();
146 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
147 aRows
.push_back(aRow
);
149 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DECIMAL"));
150 aRow
[2] = new ORowSetValueDecorator(DataType::DECIMAL
);
151 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
152 aRow
[15] = new ORowSetValueDecorator((sal_Int32
)15);
153 aRows
.push_back(aRow
);
155 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("DOUBLE"));
156 aRow
[2] = new ORowSetValueDecorator(DataType::DOUBLE
);
157 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
158 aRow
[15] = ODatabaseMetaDataResultSet::get0Value();
159 aRows
.push_back(aRow
);
161 aRow
[1] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("NUMERIC"));
162 aRow
[2] = new ORowSetValueDecorator(DataType::NUMERIC
);
163 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
164 aRow
[15] = new ORowSetValueDecorator((sal_Int32
)20);
165 aRows
.push_back(aRow
);
168 pResult
->setRows(aRows
);
171 // -------------------------------------------------------------------------
172 Reference
< XResultSet
> SAL_CALL
OFlatDatabaseMetaData::getColumns(
173 const Any
& /*catalog*/, const ::rtl::OUString
& /*schemaPattern*/, const ::rtl::OUString
& tableNamePattern
,
174 const ::rtl::OUString
& columnNamePattern
) throw(SQLException
, RuntimeException
)
176 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::getColumns" );
177 ::osl::MutexGuard
aGuard( m_aMutex
);
179 Reference
< XTablesSupplier
> xTables
= m_pConnection
->createCatalog();
181 throw SQLException();
183 Reference
< XNameAccess
> xNames
= xTables
->getTables();
185 throw SQLException();
187 ODatabaseMetaDataResultSet::ORows aRows
;
188 ODatabaseMetaDataResultSet::ORow
aRow(19);
189 aRow
[10] = new ORowSetValueDecorator((sal_Int32
)10);
190 Sequence
< ::rtl::OUString
> aTabNames(xNames
->getElementNames());
191 const ::rtl::OUString
* pTabBegin
= aTabNames
.getConstArray();
192 const ::rtl::OUString
* pTabEnd
= pTabBegin
+ aTabNames
.getLength();
193 for(;pTabBegin
!= pTabEnd
;++pTabBegin
)
195 if(match(tableNamePattern
,*pTabBegin
,'\0'))
197 Reference
< XColumnsSupplier
> xTable
;
198 ::cppu::extractInterface(xTable
,xNames
->getByName(*pTabBegin
));
199 aRow
[3] = new ORowSetValueDecorator(*pTabBegin
);
201 Reference
< XNameAccess
> xColumns
= xTable
->getColumns();
203 throw SQLException();
205 Sequence
< ::rtl::OUString
> aColNames(xColumns
->getElementNames());
207 const ::rtl::OUString
* pBegin
= aColNames
.getConstArray();
208 const ::rtl::OUString
* pEnd
= pBegin
+ aColNames
.getLength();
209 Reference
< XPropertySet
> xColumn
;
210 for(sal_Int32 i
=1;pBegin
!= pEnd
;++pBegin
,++i
)
212 if(match(columnNamePattern
,*pBegin
,'\0'))
214 aRow
[4] = new ORowSetValueDecorator(*pBegin
);
216 ::cppu::extractInterface(xColumn
,xColumns
->getByName(*pBegin
));
217 OSL_ENSURE(xColumn
.is(),"Columns contains a column who isn't a fastpropertyset!");
218 aRow
[5] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))));
219 aRow
[6] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME
))));
220 aRow
[7] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION
))));
221 aRow
[9] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE
))));
222 aRow
[11] = new ORowSetValueDecorator(getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
))));
223 aRow
[13] = new ORowSetValueDecorator(getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE
))));
225 switch((sal_Int32
)aRow
[5]->getValue())
228 case DataType::VARCHAR
:
229 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)254);
231 case DataType::LONGVARCHAR
:
232 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)65535);
235 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)0);
237 aRow
[17] = new ORowSetValueDecorator(i
);
238 switch(sal_Int32(aRow
[11]->getValue()))
240 case ColumnValue::NO_NULLS
:
241 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("NO"));
243 case ColumnValue::NULLABLE
:
244 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString::createFromAscii("YES"));
247 aRow
[18] = new ORowSetValueDecorator(::rtl::OUString());
249 aRows
.push_back(aRow
);
255 ::connectivity::ODatabaseMetaDataResultSet
* pResult
= new ::connectivity::ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns
);
256 Reference
< XResultSet
> xRef
= pResult
;
257 pResult
->setRows(aRows
);
261 // -------------------------------------------------------------------------
262 ::rtl::OUString SAL_CALL
OFlatDatabaseMetaData::getURL( ) throw(SQLException
, RuntimeException
)
264 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "flat", "Ocke.Janssen@sun.com", "OFlatDatabaseMetaData::getURL" );
265 ::osl::MutexGuard
aGuard( m_aMutex
);
266 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("sdbc:flat:")) + m_pConnection
->getURL();
268 // -----------------------------------------------------------------------------