LibreOffice Module connectivity (master) 1
OResultSetMetaData.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
21#include <odbc/OTools.hxx>
22
23using namespace connectivity::odbc;
24using namespace com::sun::star::uno;
25using namespace com::sun::star::lang;
26using namespace com::sun::star::sdbc;
27
28
29OResultSetMetaData::~OResultSetMetaData()
30{
31}
32
33OUString OResultSetMetaData::getCharColAttrib(sal_Int32 _column,sal_Int32 ident)
34{
35 sal_Int32 column = _column;
36 if(_column <static_cast<sal_Int32>(m_vMapping.size())) // use mapping
37 column = m_vMapping[_column];
38
39 SQLSMALLINT BUFFER_LEN = 128;
40 std::unique_ptr<char[]> pName(new char[BUFFER_LEN+1]);
41 SQLSMALLINT nRealLen=0;
43 static_cast<SQLUSMALLINT>(column),
44 static_cast<SQLUSMALLINT>(ident),
45 static_cast<SQLPOINTER>(pName.get()),
46 BUFFER_LEN,
47 &nRealLen,
48 nullptr
49 );
50 OUString sValue;
51 if ( nRet == SQL_SUCCESS )
52 {
53 if ( nRealLen < 0 )
54 nRealLen = BUFFER_LEN;
55 sValue = OUString(pName.get(),nRealLen,m_pConnection->getTextEncoding());
56 }
57 pName.reset();
59 if(nRealLen > BUFFER_LEN)
60 {
61 pName.reset(new char[nRealLen+1]);
63 static_cast<SQLUSMALLINT>(column),
64 static_cast<SQLUSMALLINT>(ident),
65 static_cast<SQLPOINTER>(pName.get()),
66 nRealLen,
67 &nRealLen,
68 nullptr
69 );
70 if ( nRet == SQL_SUCCESS && nRealLen > 0)
71 sValue = OUString(pName.get(),nRealLen,m_pConnection->getTextEncoding());
73 }
74
75 return sValue;
76}
77
79 ,SQLHANDLE _aStatementHandle
80 ,const css::uno::Reference< css::uno::XInterface >& _xInterface
81 ,sal_Int32 _column
82 ,sal_Int32 _ident)
83{
84 SQLLEN nValue=0;
85 OTools::ThrowException(_pConnection,(*reinterpret_cast<T3SQLColAttribute>(_pConnection->getOdbcFunction(ODBC3SQLFunctionId::ColAttribute)))(_aStatementHandle,
86 static_cast<SQLUSMALLINT>(_column),
87 static_cast<SQLUSMALLINT>(_ident),
88 nullptr,
89 0,
90 nullptr,
91 &nValue),_aStatementHandle,SQL_HANDLE_STMT,_xInterface);
92 return nValue;
93}
94
95sal_Int32 OResultSetMetaData::getNumColAttrib(sal_Int32 _column,sal_Int32 ident)
96{
97 sal_Int32 column = _column;
98 if(_column < static_cast<sal_Int32>(m_vMapping.size())) // use mapping
99 column = m_vMapping[_column];
100
101 return getNumColAttrib(m_pConnection,m_aStatementHandle,*this,column,ident);
102}
103
104sal_Int32 SAL_CALL OResultSetMetaData::getColumnDisplaySize( sal_Int32 column )
105{
106 return getNumColAttrib(column,SQL_DESC_DISPLAY_SIZE);
107}
108
110 ,SQLHANDLE _aStatementHandle
111 ,const css::uno::Reference< css::uno::XInterface >& _xInterface
112 ,sal_Int32 column)
113{
114 SQLSMALLINT nType = 0;
115 try
116 {
117 nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE));
118 if(nType == SQL_UNKNOWN_TYPE)
119 nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column, SQL_DESC_TYPE));
120 }
121 catch(SQLException& ) // in this case we have an odbc 2.0 driver
122 {
123 nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE ));
124 }
125
126 return nType;
127}
128
129sal_Int32 SAL_CALL OResultSetMetaData::getColumnType( sal_Int32 column )
130{
131 std::map<sal_Int32,sal_Int32>::iterator aFind = m_aColumnTypes.find(column);
132 if ( aFind == m_aColumnTypes.end() )
133 {
134 sal_Int32 nType = 0;
136 {
137 try
138 {
139 nType = getNumColAttrib(column,SQL_DESC_CONCISE_TYPE);
140 if(nType == SQL_UNKNOWN_TYPE)
141 nType = getNumColAttrib(column, SQL_DESC_TYPE);
143 }
144 catch(SQLException& ) // in this case we have an odbc 2.0 driver
145 {
146 m_bUseODBC2Types = true;
147 nType = OTools::MapOdbcType2Jdbc(getNumColAttrib(column,SQL_DESC_CONCISE_TYPE ));
148 }
149 }
150 else
151 nType = OTools::MapOdbcType2Jdbc(getNumColAttrib(column,SQL_DESC_CONCISE_TYPE ));
152 aFind = m_aColumnTypes.emplace(column,nType).first;
153 }
154
155
156 return aFind->second;
157}
158
159
161{
162 if(m_nColCount != -1)
163 return m_nColCount;
164 sal_Int16 nNumResultCols=0;
166 m_nColCount = nNumResultCols;
167 return m_nColCount;
168}
169
170
172{
173 return getNumColAttrib(column,SQL_DESC_CASE_SENSITIVE) == SQL_TRUE;
174}
175
176
177OUString SAL_CALL OResultSetMetaData::getSchemaName( sal_Int32 column )
178{
179 return getCharColAttrib(column,SQL_DESC_SCHEMA_NAME);
180}
181
182
183OUString SAL_CALL OResultSetMetaData::getColumnName( sal_Int32 column )
184{
185 return getCharColAttrib(column,SQL_DESC_NAME);
186}
187
188OUString SAL_CALL OResultSetMetaData::getTableName( sal_Int32 column )
189{
190 return getCharColAttrib(column,SQL_DESC_TABLE_NAME);
191}
192
193OUString SAL_CALL OResultSetMetaData::getCatalogName( sal_Int32 column )
194{
195 return getCharColAttrib(column,SQL_DESC_CATALOG_NAME);
196}
197
198OUString SAL_CALL OResultSetMetaData::getColumnTypeName( sal_Int32 column )
199{
200 return getCharColAttrib(column,SQL_DESC_TYPE_NAME);
201}
202
203OUString SAL_CALL OResultSetMetaData::getColumnLabel( sal_Int32 column )
204{
205 return getCharColAttrib(column,SQL_DESC_LABEL);
206}
207
208OUString SAL_CALL OResultSetMetaData::getColumnServiceName( sal_Int32 /*column*/ )
209{
210 return OUString();
211}
212
213
214sal_Bool SAL_CALL OResultSetMetaData::isCurrency( sal_Int32 column )
215{
216 return getNumColAttrib(column,SQL_DESC_FIXED_PREC_SCALE) == SQL_TRUE;
217}
218
219
221{
222 return getNumColAttrib(column,SQL_DESC_AUTO_UNIQUE_VALUE) == SQL_TRUE;
223}
224
225
226sal_Bool SAL_CALL OResultSetMetaData::isSigned( sal_Int32 column )
227{
228 return getNumColAttrib(column,SQL_DESC_UNSIGNED) == SQL_FALSE;
229}
230
231sal_Int32 SAL_CALL OResultSetMetaData::getPrecision( sal_Int32 column )
232{
233 sal_Int32 nType = 0;
234 try
235 {
236 nType = getNumColAttrib(column,SQL_DESC_PRECISION);
237 }
238 catch(const SQLException& ) // in this case we have an odbc 2.0 driver
239 {
240 m_bUseODBC2Types = true;
241 nType = getNumColAttrib(column,SQL_COLUMN_PRECISION );
242 }
243 return nType;
244}
245
246sal_Int32 SAL_CALL OResultSetMetaData::getScale( sal_Int32 column )
247{
248 sal_Int32 nType = 0;
249 try
250 {
251 nType = getNumColAttrib(column,SQL_DESC_SCALE);
252 }
253 catch(const SQLException& ) // in this case we have an odbc 2.0 driver
254 {
255 m_bUseODBC2Types = true;
256 nType = getNumColAttrib(column,SQL_COLUMN_SCALE );
257 }
258 return nType;
259}
260
261
262sal_Int32 SAL_CALL OResultSetMetaData::isNullable( sal_Int32 column )
263{
264 return getNumColAttrib(column,SQL_DESC_NULLABLE);
265}
266
267
268sal_Bool SAL_CALL OResultSetMetaData::isSearchable( sal_Int32 column )
269{
270 return getNumColAttrib(column,SQL_DESC_SEARCHABLE) != SQL_PRED_NONE;
271}
272
273
274sal_Bool SAL_CALL OResultSetMetaData::isReadOnly( sal_Int32 column )
275{
276 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_READONLY;
277}
278
279
281{
282 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
283}
284
285sal_Bool SAL_CALL OResultSetMetaData::isWritable( sal_Int32 column )
286{
287 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
288}
289
290
291/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define N3SQLColAttribute(a, b, c, d, e, f, g)
Definition: OFunctions.hxx:301
#define N3SQLNumResultCols(a, b)
Definition: OFunctions.hxx:279
const char * pName
rtl_TextEncoding getTextEncoding() const
Definition: TConnection.hxx:61
oslGenericFunction getOdbcFunction(ODBC3SQLFunctionId _nIndex) const
Definition: OConnection.cxx:80
virtual sal_Bool SAL_CALL isSearchable(sal_Int32 column) override
virtual sal_Int32 SAL_CALL getPrecision(sal_Int32 column) override
sal_Int32 getNumColAttrib(sal_Int32 column, sal_Int32 ident)
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
OUString getCharColAttrib(sal_Int32 column, sal_Int32 ident)
std::map< sal_Int32, sal_Int32 > m_aColumnTypes
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
static SQLSMALLINT getColumnODBCType(OConnection const *_pConnection, SQLHANDLE _aStatementHandle, const css::uno::Reference< css::uno::XInterface > &_xInterface, sal_Int32 column)
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
static sal_Int32 MapOdbcType2Jdbc(SQLSMALLINT _nType)
Definition: OTools.cxx:578
static void ThrowException(const OConnection *_pConnection, SQLRETURN _rRetCode, SQLHANDLE _pContext, SQLSMALLINT _nHandleType, const css::uno::Reference< css::uno::XInterface > &_xInterface, bool _bNoFound=true)
Definition: OTools.cxx:302
sal_Int16 nValue
SQLRETURN(SQL_API * T3SQLColAttribute)(SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLUSMALLINT FieldIdentifier, SQLPOINTER CharacterAttributePtr, SQLSMALLINT BufferLength, SQLSMALLINT *StringLengthPtr, SQLLEN *NumericAttributePtr)
Definition: OFunctions.hxx:293
QPRO_FUNC_TYPE nType
unsigned char sal_Bool