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 "ResultSetMetaData.hxx"
23 #include <com/sun/star/sdbc/ColumnValue.hpp>
24 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
25 #include <com/sun/star/sdbc/XRow.hpp>
26 #include <com/sun/star/sdbc/DataType.hpp>
28 using namespace connectivity::firebird
;
30 using namespace com::sun::star::lang
;
31 using namespace com::sun::star::sdbc
;
32 using namespace com::sun::star::sdbcx
;
33 using namespace com::sun::star::uno
;
35 using com::sun::star::beans::XPropertySet
;
36 using com::sun::star::container::XNameAccess
;
38 OResultSetMetaData::~OResultSetMetaData()
42 void OResultSetMetaData::verifyValidColumn(sal_Int32 column
)
45 if (column
>getColumnCount() || column
< 1)
46 throw SQLException("Invalid column specified", *this, OUString(), 0, Any());
49 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnCount() throw(SQLException
, RuntimeException
, std::exception
)
51 return m_pSqlda
->sqld
;
54 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnDisplaySize( sal_Int32 column
) throw(SQLException
, RuntimeException
, std::exception
)
56 verifyValidColumn(column
);
57 return 32; // Hard limit for firebird
60 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnType(sal_Int32 column
)
61 throw(SQLException
, RuntimeException
, std::exception
)
63 verifyValidColumn(column
);
65 short aType
= m_pSqlda
->sqlvar
[column
-1].sqltype
;
66 short aSubType
= m_pSqlda
->sqlvar
[column
-1].sqlsubtype
;
68 return getColumnTypeFromFBType(aType
, aSubType
);
71 sal_Bool SAL_CALL
OResultSetMetaData::isCaseSensitive(sal_Int32 column
)
72 throw(SQLException
, RuntimeException
, std::exception
)
74 // Firebird is generally case sensitive when using quoted identifiers.
75 // IF THIS CHANGES make ResultSet::findColumn to be case-insensitive as needed.
76 // Generally names that are entirely UPPERCASE are case insensitive, however
77 // there remains some ambiguity if there is another mixed-case-named column
78 // of the same name. For safety always assume case insensitive.
83 OUString SAL_CALL
OResultSetMetaData::getSchemaName(sal_Int32 column
)
84 throw(SQLException
, RuntimeException
, std::exception
)
87 return OUString(); // Schemas supported by firebird
90 OUString SAL_CALL
OResultSetMetaData::getColumnName(sal_Int32 column
)
91 throw(SQLException
, RuntimeException
, std::exception
)
93 verifyValidColumn(column
);
94 OUString
sRet(m_pSqlda
->sqlvar
[column
-1].sqlname
,
95 m_pSqlda
->sqlvar
[column
-1].sqlname_length
,
96 RTL_TEXTENCODING_UTF8
);
97 sanitizeIdentifier(sRet
);
101 OUString SAL_CALL
OResultSetMetaData::getTableName(sal_Int32 column
)
102 throw(SQLException
, RuntimeException
, std::exception
)
104 verifyValidColumn(column
);
105 return OUString(m_pSqlda
->sqlvar
[column
-1].relname
,
106 m_pSqlda
->sqlvar
[column
-1].relname_length
,
107 RTL_TEXTENCODING_UTF8
);
110 OUString SAL_CALL
OResultSetMetaData::getCatalogName(sal_Int32 column
)
111 throw(SQLException
, RuntimeException
, std::exception
)
114 return OUString(); // Catalogs not supported by firebird
117 OUString SAL_CALL
OResultSetMetaData::getColumnTypeName(sal_Int32 column
)
118 throw(SQLException
, RuntimeException
, std::exception
)
120 verifyValidColumn(column
);
122 short aType
= m_pSqlda
->sqlvar
[column
-1].sqltype
;
123 short aSubType
= m_pSqlda
->sqlvar
[column
-1].sqlsubtype
;
125 return getColumnTypeNameFromFBType(aType
, aSubType
);
128 OUString SAL_CALL
OResultSetMetaData::getColumnLabel(sal_Int32 column
)
129 throw(SQLException
, RuntimeException
, std::exception
)
132 verifyValidColumn(column
);
133 OUString
sRet(m_pSqlda
->sqlvar
[column
-1].aliasname
,
134 m_pSqlda
->sqlvar
[column
-1].aliasname_length
,
135 RTL_TEXTENCODING_UTF8
);
136 sanitizeIdentifier(sRet
);
140 OUString SAL_CALL
OResultSetMetaData::getColumnServiceName(sal_Int32 column
)
141 throw(SQLException
, RuntimeException
, std::exception
)
148 sal_Bool SAL_CALL
OResultSetMetaData::isCurrency(sal_Int32 column
)
149 throw(SQLException
, RuntimeException
, std::exception
)
155 sal_Bool SAL_CALL
OResultSetMetaData::isAutoIncrement(sal_Int32 column
)
156 throw(SQLException
, RuntimeException
, std::exception
)
158 OUString sTable
= getTableName(column
);
159 if( !sTable
.isEmpty() )
161 OUString sColumnName
= getColumnName( column
);
163 OUString sSql
= "SELECT RDB$IDENTITY_TYPE FROM RDB$RELATION_FIELDS "
164 "WHERE RDB$RELATION_NAME = '"
165 + escapeWith(sTable
, '\'', '\'') + "' AND "
166 "RDB$FIELD_NAME = '"+ escapeWith(sColumnName
, '\'', '\'') +"'";
168 Reference
<XStatement
> xStmt
=m_pConnection
->createStatement();
170 Reference
<XResultSet
> xRes
=
171 xStmt
->executeQuery(sSql
);
172 Reference
<XRow
> xRow ( xRes
, UNO_QUERY
);
175 int iType
= xRow
->getShort(1);
176 if(iType
== 1) // IDENTITY
181 SAL_WARN("connectivity.firebird","Column '"
183 << "' not found in database");
192 sal_Bool SAL_CALL
OResultSetMetaData::isSigned(sal_Int32 column
)
193 throw(SQLException
, RuntimeException
, std::exception
)
195 // Unsigned values aren't supported in firebird.
200 sal_Int32 SAL_CALL
OResultSetMetaData::getPrecision(sal_Int32 column
)
201 throw(SQLException
, RuntimeException
, std::exception
)
203 sal_Int32 nType
= getColumnType(column
);
204 if( nType
== DataType::NUMERIC
|| nType
== DataType::DECIMAL
)
206 OUString sColumnName
= getColumnName( column
);
208 // RDB$FIELD_SOURCE is a unique name of column per database
209 OUString sSql
= "SELECT RDB$FIELD_PRECISION FROM RDB$FIELDS "
210 " INNER JOIN RDB$RELATION_FIELDS "
211 " ON RDB$RELATION_FIELDS.RDB$FIELD_SOURCE = RDB$FIELDS.RDB$FIELD_NAME "
212 "WHERE RDB$RELATION_FIELDS.RDB$RELATION_NAME = '"
213 + escapeWith(getTableName(column
), '\'', '\'') + "' AND "
214 "RDB$RELATION_FIELDS.RDB$FIELD_NAME = '"
215 + escapeWith(sColumnName
, '\'', '\'') +"'";
216 Reference
<XStatement
> xStmt
= m_pConnection
->createStatement();
218 Reference
<XResultSet
> xRes
=
219 xStmt
->executeQuery(sSql
);
220 Reference
<XRow
> xRow ( xRes
, UNO_QUERY
);
223 return (sal_Int32
) xRow
->getShort(1);
227 SAL_WARN("connectivity.firebird","Column '"
229 << "' not found in database");
236 sal_Int32 SAL_CALL
OResultSetMetaData::getScale(sal_Int32 column
)
237 throw(css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
)
239 return -(m_pSqlda
->sqlvar
[column
-1].sqlscale
); // fb stores negative number
242 sal_Int32 SAL_CALL
OResultSetMetaData::isNullable(sal_Int32 column
)
243 throw(SQLException
, RuntimeException
, std::exception
)
245 if (m_pSqlda
->sqlvar
[column
-1].sqltype
& 1)
246 return ColumnValue::NULLABLE
;
248 return ColumnValue::NO_NULLS
;
251 sal_Bool SAL_CALL
OResultSetMetaData::isSearchable(sal_Int32 column
)
252 throw(SQLException
, RuntimeException
, std::exception
)
254 // TODO: Can the column be used as part of a where clause? Assume yes
259 sal_Bool SAL_CALL
OResultSetMetaData::isReadOnly(sal_Int32 column
)
260 throw(SQLException
, RuntimeException
, std::exception
)
263 return m_pConnection
->isReadOnly(); // Readonly only available on db level
266 sal_Bool SAL_CALL
OResultSetMetaData::isDefinitelyWritable(sal_Int32 column
)
267 throw(SQLException
, RuntimeException
, std::exception
)
270 return !m_pConnection
->isReadOnly();
273 sal_Bool SAL_CALL
OResultSetMetaData::isWritable( sal_Int32 column
) throw(SQLException
, RuntimeException
, std::exception
)
276 return !m_pConnection
->isReadOnly();
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */