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 "writer/WDatabaseMetaData.hxx"
21 #include "writer/WConnection.hxx"
22 #include <com/sun/star/sdbc/DataType.hpp>
23 #include <com/sun/star/sdbc/ResultSetType.hpp>
24 #include <com/sun/star/sdbc/ColumnValue.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
27 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
28 #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
29 #include <com/sun/star/text/XTextDocument.hpp>
30 #include <com/sun/star/text/XTextTablesSupplier.hpp>
31 #include "FDatabaseMetaDataResultSet.hxx"
32 #include <com/sun/star/lang/XUnoTunnel.hpp>
33 #include <comphelper/types.hxx>
35 using namespace connectivity::file
;
36 using namespace ::com::sun::star
;
38 namespace connectivity
43 OWriterDatabaseMetaData::OWriterDatabaseMetaData(OConnection
* pConnection
) :ODatabaseMetaData(pConnection
)
47 OWriterDatabaseMetaData::~OWriterDatabaseMetaData()
51 uno::Reference
<sdbc::XResultSet
> OWriterDatabaseMetaData::impl_getTypeInfo_throw()
53 ::osl::MutexGuard
aGuard(m_aMutex
);
55 ODatabaseMetaDataResultSet
* pResult
= new ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo
);
56 uno::Reference
<sdbc::XResultSet
> xRef
= pResult
;
58 static ODatabaseMetaDataResultSet::ORows aRows
;
61 ODatabaseMetaDataResultSet::ORow aRow
;
66 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
67 aRow
.push_back(new ORowSetValueDecorator(OUString("VARCHAR")));
68 aRow
.push_back(new ORowSetValueDecorator(sdbc::DataType::VARCHAR
));
69 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)65535));
70 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
71 aRow
.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
72 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
73 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value()); // ORowSetValue((sal_Int32)ColumnValue::NULLABLE)
74 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
75 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)sdbc::ColumnSearch::CHAR
));
76 aRow
.push_back(ODatabaseMetaDataResultSet::get1Value());
77 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
78 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
79 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
80 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
81 aRow
.push_back(ODatabaseMetaDataResultSet::get0Value());
82 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
83 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
84 aRow
.push_back(new ORowSetValueDecorator((sal_Int32
)10));
85 aRows
.push_back(aRow
);
87 aRow
[1] = new ORowSetValueDecorator(OUString("DECIMAL"));
88 aRow
[2] = new ORowSetValueDecorator(sdbc::DataType::DECIMAL
);
89 aRow
[3] = ODatabaseMetaDataResultSet::get0Value();
90 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
91 aRow
[15] = ODatabaseMetaDataResultSet::get0Value();
92 aRows
.push_back(aRow
);
94 aRow
[1] = new ORowSetValueDecorator(OUString("BOOL"));
95 aRow
[2] = new ORowSetValueDecorator(sdbc::DataType::BIT
);
96 aRow
[3] = new ORowSetValueDecorator((sal_Int32
)20);
97 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
98 aRow
[15] = new ORowSetValueDecorator((sal_Int32
)15);
99 aRows
.push_back(aRow
);
101 aRow
[1] = new ORowSetValueDecorator(OUString("DATE"));
102 aRow
[2] = new ORowSetValueDecorator(sdbc::DataType::DATE
);
103 aRow
[3] = ODatabaseMetaDataResultSet::get0Value();
104 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
105 aRow
[15] = ODatabaseMetaDataResultSet::get0Value();
106 aRows
.push_back(aRow
);
108 aRow
[1] = new ORowSetValueDecorator(OUString("TIME"));
109 aRow
[2] = new ORowSetValueDecorator(sdbc::DataType::TIME
);
110 aRow
[3] = ODatabaseMetaDataResultSet::get0Value();
111 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
112 aRow
[15] = ODatabaseMetaDataResultSet::get0Value();
113 aRows
.push_back(aRow
);
115 aRow
[1] = new ORowSetValueDecorator(OUString("TIMESTAMP"));
116 aRow
[2] = new ORowSetValueDecorator(sdbc::DataType::TIMESTAMP
);
117 aRow
[3] = ODatabaseMetaDataResultSet::get0Value();
118 aRow
[9] = ODatabaseMetaDataResultSet::getBasicValue();
119 aRow
[15] = ODatabaseMetaDataResultSet::get0Value();
120 aRows
.push_back(aRow
);
123 pResult
->setRows(aRows
);
128 uno::Reference
<sdbc::XResultSet
> SAL_CALL
OWriterDatabaseMetaData::getColumns(
129 const uno::Any
& /*catalog*/, const OUString
& /*schemaPattern*/, const OUString
& tableNamePattern
,
130 const OUString
& columnNamePattern
) throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
132 ::osl::MutexGuard
aGuard(m_aMutex
);
134 uno::Reference
<sdbcx::XTablesSupplier
> xTables
= m_pConnection
->createCatalog();
136 throw sdbc::SQLException();
138 uno::Reference
<container::XNameAccess
> xNames
= xTables
->getTables();
140 throw sdbc::SQLException();
142 ODatabaseMetaDataResultSet::ORows aRows
;
143 ODatabaseMetaDataResultSet::ORow
aRow(19);
145 aRow
[10] = new ORowSetValueDecorator((sal_Int32
)10);
147 uno::Sequence
<OUString
> aTabNames(xNames
->getElementNames());
148 const OUString
* pTabIter
= aTabNames
.getConstArray();
149 const OUString
* pTabEnd
= pTabIter
+ aTabNames
.getLength();
150 for (; pTabIter
!= pTabEnd
; ++pTabIter
)
152 if (match(tableNamePattern
,*pTabIter
,'\0'))
154 uno::Reference
<sdbcx::XColumnsSupplier
> xTable(xNames
->getByName(*pTabIter
), uno::UNO_QUERY_THROW
);
155 aRow
[3] = new ORowSetValueDecorator(*pTabIter
);
157 uno::Reference
<container::XNameAccess
> xColumns
= xTable
->getColumns();
159 throw sdbc::SQLException();
161 uno::Sequence
<OUString
> aColNames(xColumns
->getElementNames());
163 const OUString
* pColumnIter
= aColNames
.getConstArray();
164 const OUString
* pEnd
= pColumnIter
+ aColNames
.getLength();
165 uno::Reference
<beans::XPropertySet
> xColumn
;
166 for (sal_Int32 i
=1; pColumnIter
!= pEnd
; ++pColumnIter
,++i
)
168 if (match(columnNamePattern
,*pColumnIter
,'\0'))
170 aRow
[4] = new ORowSetValueDecorator(*pColumnIter
);
172 xColumns
->getByName(*pColumnIter
) >>= xColumn
;
173 OSL_ENSURE(xColumn
.is(),"Columns contains a column who isn't a fastpropertyset!");
174 aRow
[5] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))));
175 aRow
[6] = new ORowSetValueDecorator(::comphelper::getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME
))));
176 aRow
[7] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION
))));
177 // aRow[8] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
178 aRow
[9] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE
))));
179 aRow
[11] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
))));
180 // aRow[12] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
181 aRow
[13] = new ORowSetValueDecorator(::comphelper::getString(xColumn
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE
))));
182 // aRow[14] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
183 // aRow[15] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
184 switch (sal_Int32(aRow
[5]->getValue()))
186 case sdbc::DataType::CHAR
:
187 case sdbc::DataType::VARCHAR
:
188 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)254);
190 case sdbc::DataType::LONGVARCHAR
:
191 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)65535);
194 aRow
[16] = new ORowSetValueDecorator((sal_Int32
)0);
196 aRow
[17] = new ORowSetValueDecorator(i
);
197 switch (sal_Int32(aRow
[11]->getValue()))
199 case sdbc::ColumnValue::NO_NULLS
:
200 aRow
[18] = new ORowSetValueDecorator(OUString("NO"));
202 case sdbc::ColumnValue::NULLABLE
:
203 aRow
[18] = new ORowSetValueDecorator(OUString("YES"));
206 aRow
[18] = new ORowSetValueDecorator(OUString());
208 aRows
.push_back(aRow
);
214 ODatabaseMetaDataResultSet
* pResult
= new ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns
);
215 uno::Reference
<sdbc::XResultSet
> xRef
= pResult
;
216 pResult
->setRows(aRows
);
222 OUString SAL_CALL
OWriterDatabaseMetaData::getURL() throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
224 ::osl::MutexGuard
aGuard(m_aMutex
);
226 return "sdbc:writer:" + m_pConnection
->getURL();
230 sal_Int32 SAL_CALL
OWriterDatabaseMetaData::getMaxBinaryLiteralLength() throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
232 return SAL_MAX_INT32
;
236 sal_Int32 SAL_CALL
OWriterDatabaseMetaData::getMaxCharLiteralLength() throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
238 return SAL_MAX_INT32
;
241 sal_Int32 SAL_CALL
OWriterDatabaseMetaData::getMaxColumnNameLength() throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
243 return SAL_MAX_INT32
;
246 sal_Int32 SAL_CALL
OWriterDatabaseMetaData::getMaxColumnsInIndex() throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
251 sal_Int32 SAL_CALL
OWriterDatabaseMetaData::getMaxColumnsInTable() throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
256 uno::Reference
<sdbc::XResultSet
> SAL_CALL
OWriterDatabaseMetaData::getTables(
257 const uno::Any
& /*catalog*/, const OUString
& /*schemaPattern*/,
258 const OUString
& tableNamePattern
, const uno::Sequence
< OUString
>& types
) throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
260 ::osl::MutexGuard
aGuard(m_aMutex
);
262 ODatabaseMetaDataResultSet
* pResult
= new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables
);
263 uno::Reference
<sdbc::XResultSet
> xRef
= pResult
;
265 // check if ORowSetValue type is given
266 // when no types are given then we have to return all tables e.g. TABLE
268 OUString
aTable("TABLE");
270 bool bTableFound
= true;
271 sal_Int32 nLength
= types
.getLength();
276 const OUString
* pIter
= types
.getConstArray();
277 const OUString
* pEnd
= pIter
+ nLength
;
278 for (; pIter
!= pEnd
; ++pIter
)
280 if (*pIter
== aTable
)
290 // get the table names from the document
292 OWriterConnection::ODocHolder
aDocHolder(static_cast<OWriterConnection
*>(m_pConnection
));
293 uno::Reference
<text::XTextTablesSupplier
> xDoc(aDocHolder
.getDoc(), uno::UNO_QUERY
);
295 throw sdbc::SQLException();
296 uno::Reference
<container::XNameAccess
> xTables
= xDoc
->getTextTables();
298 throw sdbc::SQLException();
299 uno::Sequence
<OUString
> aTableNames
= xTables
->getElementNames();
301 ODatabaseMetaDataResultSet::ORows aRows
;
302 sal_Int32 nTableCount
= aTableNames
.getLength();
303 for (sal_Int32 nTable
=0; nTable
<nTableCount
; nTable
++)
305 OUString aName
= aTableNames
[nTable
];
306 if (match(tableNamePattern
,aName
,'\0'))
308 ODatabaseMetaDataResultSet::ORow aRow
{ nullptr, nullptr, nullptr };
310 aRow
.push_back(new ORowSetValueDecorator(aName
));
311 aRow
.push_back(new ORowSetValueDecorator(aTable
));
312 aRow
.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
313 aRows
.push_back(aRow
);
317 pResult
->setRows(aRows
);
322 } // namespace writer
323 } // namespace connectivity
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */