LibreOffice Module connectivity (master) 1
firebird/ResultSetMetaData.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "ResultSetMetaData.hxx"
21#include "Util.hxx"
22
23#include <com/sun/star/sdbc/ColumnValue.hpp>
24#include <com/sun/star/sdbc/SQLException.hpp>
25#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26#include <com/sun/star/sdbc/XRow.hpp>
27#include <com/sun/star/sdbc/DataType.hpp>
28
29#include <sal/log.hxx>
30
31using namespace connectivity::firebird;
32
33using namespace com::sun::star::lang;
34using namespace com::sun::star::sdbc;
35using namespace com::sun::star::sdbcx;
36using namespace com::sun::star::uno;
37
38OResultSetMetaData::~OResultSetMetaData()
39{
40}
41
42OUString OResultSetMetaData::getCharacterSet( sal_Int32 nIndex )
43{
44 OUString sTable = getTableName( nIndex );
45 if( !sTable.isEmpty() )
46 {
47 OUString sColumnName = getColumnName( nIndex );
48
49 OUString sSql = "SELECT charset.RDB$CHARACTER_SET_NAME "
50 "FROM RDB$CHARACTER_SETS charset "
51 "JOIN RDB$FIELDS fields "
52 "ON (fields.RDB$CHARACTER_SET_ID = charset.RDB$CHARACTER_SET_ID) "
53 "JOIN RDB$RELATION_FIELDS relfields "
54 "ON (fields.RDB$FIELD_NAME = relfields.RDB$FIELD_SOURCE) "
55 "WHERE relfields.RDB$RELATION_NAME = '"
56 + sTable.replaceAll("'", "''") + "' AND "
57 "relfields.RDB$FIELD_NAME = '"+ sColumnName.replaceAll("'", "''") +"'";
58
59 Reference<XStatement> xStmt= m_pConnection->createStatement();
60
62 xStmt->executeQuery(sSql);
63 Reference<XRow> xRow ( xRes, UNO_QUERY);
64 if(xRes->next())
65 {
66 OUString sCharset = xRow->getString(1).trim();
67 return sCharset;
68 }
69 }
70 return OUString();
71
72
73}
74
76{
77 if (column>getColumnCount() || column < 1)
78 throw SQLException("Invalid column specified", *this, OUString(), 0, Any());
79}
80
82{
83 return m_pSqlda->sqld;
84}
85
86sal_Int32 SAL_CALL OResultSetMetaData::getColumnDisplaySize( sal_Int32 column )
87{
88 verifyValidColumn(column);
89 return 32; // Hard limit for firebird
90}
91
92sal_Int32 SAL_CALL OResultSetMetaData::getColumnType(sal_Int32 column)
93{
94 verifyValidColumn(column);
95
96 short aType = m_pSqlda->sqlvar[column-1].sqltype & ~1;
97 OUString sCharset;
98
99 // do not query the character set unnecessarily
100 if(aType == SQL_TEXT || aType == SQL_VARYING)
101 {
102 sCharset = getCharacterSet(column);
103 }
104
105 ColumnTypeInfo aInfo( m_pSqlda->sqlvar[column-1].sqltype,
106 m_pSqlda->sqlvar[column-1].sqlsubtype,
107 -(m_pSqlda->sqlvar[column-1].sqlscale),
108 sCharset );
109
110 return aInfo.getSdbcType();
111}
112
114{
115 // Firebird is generally case sensitive when using quoted identifiers.
116 // IF THIS CHANGES make ResultSet::findColumn to be case-insensitive as needed.
117 // Generally names that are entirely UPPERCASE are case insensitive, however
118 // there remains some ambiguity if there is another mixed-case-named column
119 // of the same name. For safety always assume case insensitive.
120 return true;
121}
122
123OUString SAL_CALL OResultSetMetaData::getSchemaName(sal_Int32)
124{
125 return OUString(); // Schemas supported by firebird
126}
127
128OUString SAL_CALL OResultSetMetaData::getColumnName(sal_Int32 column)
129{
130 verifyValidColumn(column);
131 char* pColumnName = m_pSqlda->sqlvar[column - 1].sqlname;
132 sal_Int32 nColumnNameLength = m_pSqlda->sqlvar[column - 1].sqlname_length;
133 // tdf#132924 - return column alias if specified
134 if (m_pSqlda->sqlvar[column - 1].aliasname_length > 0)
135 {
136 pColumnName = m_pSqlda->sqlvar[column - 1].aliasname;
137 nColumnNameLength = m_pSqlda->sqlvar[column - 1].aliasname_length;
138 }
139 OUString sRet(pColumnName, nColumnNameLength, RTL_TEXTENCODING_UTF8);
140 sanitizeIdentifier(sRet);
141 return sRet;
142}
143
144OUString SAL_CALL OResultSetMetaData::getTableName(sal_Int32 column)
145{
146 verifyValidColumn(column);
147 return OUString(m_pSqlda->sqlvar[column-1].relname,
148 m_pSqlda->sqlvar[column-1].relname_length,
149 RTL_TEXTENCODING_UTF8);
150}
151
152OUString SAL_CALL OResultSetMetaData::getCatalogName(sal_Int32)
153{
154 return OUString(); // Catalogs not supported by firebird
155}
156
157OUString SAL_CALL OResultSetMetaData::getColumnTypeName(sal_Int32 column)
158{
159 verifyValidColumn(column);
160
161 ColumnTypeInfo aInfo( m_pSqlda->sqlvar[column-1].sqltype,
162 m_pSqlda->sqlvar[column-1].sqlsubtype,
163 -(m_pSqlda->sqlvar[column-1].sqlscale) );
164
165 return aInfo.getColumnTypeName();
166}
167
168OUString SAL_CALL OResultSetMetaData::getColumnLabel(sal_Int32 column)
169{
170 // aliasname
171 verifyValidColumn(column);
172 OUString sRet(m_pSqlda->sqlvar[column-1].aliasname,
173 m_pSqlda->sqlvar[column-1].aliasname_length,
174 RTL_TEXTENCODING_UTF8);
175 sanitizeIdentifier(sRet);
176 return sRet;
177}
178
180{
181 // TODO: implement
182 return OUString();
183}
184
186{
187 return false;
188}
189
191{
192 OUString sTable = getTableName(column);
193 if( sTable.isEmpty() )
194 return false;
195
196 OUString sColumnName = getColumnName( column );
197
198 OUString sSql = "SELECT RDB$IDENTITY_TYPE FROM RDB$RELATION_FIELDS "
199 "WHERE RDB$RELATION_NAME = '"
200 + sTable.replaceAll("'", "''") + "' AND "
201 "RDB$FIELD_NAME = '"+ sColumnName.replaceAll("'", "''") +"'";
202
203 Reference<XStatement> xStmt =m_pConnection ->createStatement();
204
206 xStmt->executeQuery(sSql);
207 Reference<XRow> xRow ( xRes, UNO_QUERY);
208 if(xRes->next())
209 {
210 int iType = xRow->getShort(1);
211 if(iType == 1) // IDENTITY
212 return true;
213 }
214 else
215 {
216 SAL_WARN("connectivity.firebird","Column '"
217 << sColumnName
218 << "' not found in database");
219
220 return false;
221 }
222 return false;
223}
224
225
227{
228 // Unsigned values aren't supported in firebird.
229 return true;
230}
231
232sal_Int32 SAL_CALL OResultSetMetaData::getPrecision(sal_Int32 column)
233{
234 sal_Int32 nType = getColumnType(column);
235 if( nType != DataType::NUMERIC && nType != DataType::DECIMAL )
236 return 0;
237
238 OUString sColumnName = getColumnName( column );
239
240 // RDB$FIELD_SOURCE is a unique name of column per database
241 OUString sSql = "SELECT RDB$FIELD_PRECISION FROM RDB$FIELDS "
242 " INNER JOIN RDB$RELATION_FIELDS "
243 " ON RDB$RELATION_FIELDS.RDB$FIELD_SOURCE = RDB$FIELDS.RDB$FIELD_NAME "
244 "WHERE RDB$RELATION_FIELDS.RDB$RELATION_NAME = '"
245 + getTableName(column).replaceAll("'", "''") + "' AND "
246 "RDB$RELATION_FIELDS.RDB$FIELD_NAME = '"
247 + sColumnName.replaceAll("'", "''") +"'";
248 Reference<XStatement> xStmt= m_pConnection->createStatement();
249
251 xStmt->executeQuery(sSql);
252 Reference<XRow> xRow ( xRes, UNO_QUERY);
253 if(xRes->next())
254 {
255 return static_cast<sal_Int32>(xRow->getShort(1));
256 }
257 else
258 {
259 SAL_WARN("connectivity.firebird","Column '"
260 << sColumnName
261 << "' not found in database");
262 return 0;
263 }
264 return 0;
265}
266
267sal_Int32 SAL_CALL OResultSetMetaData::getScale(sal_Int32 column)
268{
269 return -(m_pSqlda->sqlvar[column-1].sqlscale); // fb stores negative number
270}
271
272sal_Int32 SAL_CALL OResultSetMetaData::isNullable(sal_Int32 column)
273{
274 if (m_pSqlda->sqlvar[column-1].sqltype & 1)
275 return ColumnValue::NULLABLE;
276 else
277 return ColumnValue::NO_NULLS;
278}
279
281{
282 // TODO: Can the column be used as part of a where clause? Assume yes
283 return true;
284}
285
287{
288 return m_pConnection->isReadOnly(); // Readonly only available on db level
289}
290
292{
293 return !m_pConnection->isReadOnly();
294}
295
297{
298 return !m_pConnection->isReadOnly();
299}
300
301/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
OUString getColumnTypeName() const
Definition: Util.cxx:183
virtual sal_Bool SAL_CALL isSearchable(sal_Int32 column) override
virtual sal_Int32 SAL_CALL getPrecision(sal_Int32 column) override
virtual OUString SAL_CALL getSchemaName(sal_Int32 column) override
virtual sal_Bool SAL_CALL isCaseSensitive(sal_Int32 column) override
virtual sal_Bool SAL_CALL isCurrency(sal_Int32 column) override
virtual sal_Int32 SAL_CALL getColumnType(sal_Int32 column) override
virtual OUString SAL_CALL getColumnTypeName(sal_Int32 column) override
virtual sal_Bool SAL_CALL isReadOnly(sal_Int32 column) override
virtual OUString SAL_CALL getColumnLabel(sal_Int32 column) override
virtual sal_Bool SAL_CALL isWritable(sal_Int32 column) override
virtual sal_Bool SAL_CALL isAutoIncrement(sal_Int32 column) override
virtual OUString SAL_CALL getCatalogName(sal_Int32 column) override
virtual sal_Int32 SAL_CALL isNullable(sal_Int32 column) override
virtual OUString SAL_CALL getColumnServiceName(sal_Int32 column) override
virtual sal_Bool SAL_CALL isSigned(sal_Int32 column) override
virtual OUString SAL_CALL getTableName(sal_Int32 column) override
virtual sal_Int32 SAL_CALL getColumnCount() override
virtual OUString SAL_CALL getColumnName(sal_Int32 column) override
virtual sal_Int32 SAL_CALL getScale(sal_Int32 column) override
virtual sal_Int32 SAL_CALL getColumnDisplaySize(sal_Int32 column) override
virtual sal_Bool SAL_CALL isDefinitelyWritable(sal_Int32 column) override
sal_Int32 nIndex
#define SAL_WARN(area, stream)
OUString sanitizeIdentifier(std::u16string_view rIdentifier)
Make sure an identifier is safe to use within the database.
QPRO_FUNC_TYPE nType
unsigned char sal_Bool