1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 "ResultSetMetaData.hxx"
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>
29 #include <sal/log.hxx>
31 using namespace connectivity::firebird
;
33 using namespace com::sun::star::lang
;
34 using namespace com::sun::star::sdbc
;
35 using namespace com::sun::star::sdbcx
;
36 using namespace com::sun::star::uno
;
38 OResultSetMetaData::~OResultSetMetaData()
42 OUString
OResultSetMetaData::getCharacterSet( sal_Int32 nIndex
)
44 OUString sTable
= getTableName( nIndex
);
45 if( !sTable
.isEmpty() )
47 OUString sColumnName
= getColumnName( nIndex
);
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("'", "''") +"'";
59 Reference
<XStatement
> xStmt
= m_pConnection
->createStatement();
61 Reference
<XResultSet
> xRes
=
62 xStmt
->executeQuery(sSql
);
63 Reference
<XRow
> xRow ( xRes
, UNO_QUERY
);
66 OUString sCharset
= xRow
->getString(1).trim();
75 void OResultSetMetaData::verifyValidColumn(sal_Int32 column
)
77 if (column
>getColumnCount() || column
< 1)
78 throw SQLException(u
"Invalid column specified"_ustr
, *this, OUString(), 0, Any());
81 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnCount()
83 return m_pSqlda
->sqld
;
86 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnDisplaySize( sal_Int32 column
)
88 verifyValidColumn(column
);
89 return 32; // Hard limit for firebird
92 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnType(sal_Int32 column
)
94 verifyValidColumn(column
);
96 short aType
= m_pSqlda
->sqlvar
[column
-1].sqltype
& ~1;
99 // do not query the character set unnecessarily
100 if(aType
== SQL_TEXT
|| aType
== SQL_VARYING
)
102 sCharset
= getCharacterSet(column
);
105 ColumnTypeInfo
aInfo(m_pSqlda
, column
, sCharset
);
107 return aInfo
.getSdbcType();
110 sal_Bool SAL_CALL
OResultSetMetaData::isCaseSensitive(sal_Int32
)
112 // Firebird is generally case sensitive when using quoted identifiers.
113 // IF THIS CHANGES make ResultSet::findColumn to be case-insensitive as needed.
114 // Generally names that are entirely UPPERCASE are case insensitive, however
115 // there remains some ambiguity if there is another mixed-case-named column
116 // of the same name. For safety always assume case insensitive.
120 OUString SAL_CALL
OResultSetMetaData::getSchemaName(sal_Int32
)
122 return OUString(); // Schemas supported by firebird
125 OUString SAL_CALL
OResultSetMetaData::getColumnName(sal_Int32 column
)
127 verifyValidColumn(column
);
128 char* pColumnName
= m_pSqlda
->sqlvar
[column
- 1].sqlname
;
129 sal_Int32 nColumnNameLength
= m_pSqlda
->sqlvar
[column
- 1].sqlname_length
;
130 // tdf#132924 - return column alias if specified
131 if (m_pSqlda
->sqlvar
[column
- 1].aliasname_length
> 0)
133 pColumnName
= m_pSqlda
->sqlvar
[column
- 1].aliasname
;
134 nColumnNameLength
= m_pSqlda
->sqlvar
[column
- 1].aliasname_length
;
136 OUString
sRet(pColumnName
, nColumnNameLength
, RTL_TEXTENCODING_UTF8
);
137 sanitizeIdentifier(sRet
);
141 OUString SAL_CALL
OResultSetMetaData::getTableName(sal_Int32 column
)
143 verifyValidColumn(column
);
144 return OUString(m_pSqlda
->sqlvar
[column
-1].relname
,
145 m_pSqlda
->sqlvar
[column
-1].relname_length
,
146 RTL_TEXTENCODING_UTF8
);
149 OUString SAL_CALL
OResultSetMetaData::getCatalogName(sal_Int32
)
151 return OUString(); // Catalogs not supported by firebird
154 OUString SAL_CALL
OResultSetMetaData::getColumnTypeName(sal_Int32 column
)
156 verifyValidColumn(column
);
158 ColumnTypeInfo
aInfo(m_pSqlda
, column
);
160 return aInfo
.getColumnTypeName();
163 OUString SAL_CALL
OResultSetMetaData::getColumnLabel(sal_Int32 column
)
166 verifyValidColumn(column
);
167 OUString
sRet(m_pSqlda
->sqlvar
[column
-1].aliasname
,
168 m_pSqlda
->sqlvar
[column
-1].aliasname_length
,
169 RTL_TEXTENCODING_UTF8
);
170 sanitizeIdentifier(sRet
);
174 OUString SAL_CALL
OResultSetMetaData::getColumnServiceName(sal_Int32
)
180 sal_Bool SAL_CALL
OResultSetMetaData::isCurrency(sal_Int32
)
185 sal_Bool SAL_CALL
OResultSetMetaData::isAutoIncrement(sal_Int32 column
)
187 OUString sTable
= getTableName(column
);
188 if( sTable
.isEmpty() )
191 OUString sColumnName
= getColumnName( column
);
193 OUString sSql
= "SELECT RDB$IDENTITY_TYPE FROM RDB$RELATION_FIELDS "
194 "WHERE RDB$RELATION_NAME = '"
195 + sTable
.replaceAll("'", "''") + "' AND "
196 "RDB$FIELD_NAME = '"+ sColumnName
.replaceAll("'", "''") +"'";
198 Reference
<XStatement
> xStmt
=m_pConnection
->createStatement();
200 Reference
<XResultSet
> xRes
=
201 xStmt
->executeQuery(sSql
);
202 Reference
<XRow
> xRow ( xRes
, UNO_QUERY
);
205 int iType
= xRow
->getShort(1);
206 if(iType
== 1) // IDENTITY
211 SAL_WARN("connectivity.firebird","Column '"
213 << "' not found in database");
221 sal_Bool SAL_CALL
OResultSetMetaData::isSigned(sal_Int32
)
223 // Unsigned values aren't supported in firebird.
227 sal_Int32 SAL_CALL
OResultSetMetaData::getPrecision(sal_Int32 column
)
229 sal_Int32 nType
= getColumnType(column
);
230 if( nType
!= DataType::NUMERIC
&& nType
!= DataType::DECIMAL
)
233 OUString sColumnName
= getColumnName( column
);
235 // RDB$FIELD_SOURCE is a unique name of column per database
236 OUString sSql
= "SELECT RDB$FIELD_PRECISION FROM RDB$FIELDS "
237 " INNER JOIN RDB$RELATION_FIELDS "
238 " ON RDB$RELATION_FIELDS.RDB$FIELD_SOURCE = RDB$FIELDS.RDB$FIELD_NAME "
239 "WHERE RDB$RELATION_FIELDS.RDB$RELATION_NAME = '"
240 + getTableName(column
).replaceAll("'", "''") + "' AND "
241 "RDB$RELATION_FIELDS.RDB$FIELD_NAME = '"
242 + sColumnName
.replaceAll("'", "''") +"'";
243 Reference
<XStatement
> xStmt
= m_pConnection
->createStatement();
245 Reference
<XResultSet
> xRes
=
246 xStmt
->executeQuery(sSql
);
247 Reference
<XRow
> xRow ( xRes
, UNO_QUERY
);
250 return static_cast<sal_Int32
>(xRow
->getShort(1));
254 SAL_WARN("connectivity.firebird","Column '"
256 << "' not found in database");
262 sal_Int32 SAL_CALL
OResultSetMetaData::getScale(sal_Int32 column
)
264 return -(m_pSqlda
->sqlvar
[column
-1].sqlscale
); // fb stores negative number
267 sal_Int32 SAL_CALL
OResultSetMetaData::isNullable(sal_Int32 column
)
269 if (m_pSqlda
->sqlvar
[column
-1].sqltype
& 1)
270 return ColumnValue::NULLABLE
;
272 return ColumnValue::NO_NULLS
;
275 sal_Bool SAL_CALL
OResultSetMetaData::isSearchable(sal_Int32
)
277 // TODO: Can the column be used as part of a where clause? Assume yes
281 sal_Bool SAL_CALL
OResultSetMetaData::isReadOnly(sal_Int32
)
283 return m_pConnection
->isReadOnly(); // Readonly only available on db level
286 sal_Bool SAL_CALL
OResultSetMetaData::isDefinitelyWritable(sal_Int32
)
288 return !m_pConnection
->isReadOnly();
291 sal_Bool SAL_CALL
OResultSetMetaData::isWritable( sal_Int32
)
293 return !m_pConnection
->isReadOnly();
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */