cid#1636677 Uninitialized scalar field
[LibreOffice.git] / connectivity / source / drivers / odbc / OResultSetMetaData.cxx
blob260c2111fbfce7d17bfc3fe78a0f9b9c2321ad88
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <odbc/OResultSetMetaData.hxx>
21 #include <odbc/OTools.hxx>
23 using namespace connectivity::odbc;
24 using namespace com::sun::star::uno;
25 using namespace com::sun::star::sdbc;
28 OResultSetMetaData::~OResultSetMetaData()
32 OUString OResultSetMetaData::getCharColAttrib(sal_Int32 _column,sal_Int32 ident)
34 sal_Int32 column = _column;
35 if(_column <static_cast<sal_Int32>(m_vMapping.size())) // use mapping
36 column = m_vMapping[_column];
38 OUString sValue;
39 SQLSMALLINT cbRealLen = 0;
40 if (bUseWChar && functions().has(ODBC3SQLFunctionId::ColAttributeW))
42 // SQLColAttributeW gets/returns count of bytes, not characters
43 SQLSMALLINT cbBufferLen = 128 * sizeof(SQLWCHAR);
44 auto pName = std::make_unique<SQLWCHAR[]>(cbBufferLen / sizeof(SQLWCHAR) + 1);
45 SQLRETURN nRet = functions().ColAttributeW(m_aStatementHandle,
46 column,
47 ident,
48 pName.get(),
49 cbBufferLen,
50 &cbRealLen,
51 nullptr);
52 OTools::ThrowException(m_pConnection, nRet, m_aStatementHandle, SQL_HANDLE_STMT, *this);
53 if (nRet == SQL_SUCCESS)
55 if (cbRealLen < 0)
56 cbRealLen = cbBufferLen;
57 sValue = toUString(pName.get(), cbRealLen / sizeof(SQLWCHAR));
59 if (cbRealLen > cbBufferLen)
61 cbBufferLen = (cbRealLen + 1) & ~1; // Make sure it's even
62 pName = std::make_unique<SQLWCHAR[]>(cbBufferLen / sizeof(SQLWCHAR) + 1);
63 nRet = functions().ColAttributeW(m_aStatementHandle,
64 column,
65 ident,
66 pName.get(),
67 cbBufferLen,
68 &cbRealLen,
69 nullptr);
70 OTools::ThrowException(m_pConnection, nRet, m_aStatementHandle, SQL_HANDLE_STMT, *this);
71 if (nRet == SQL_SUCCESS && cbRealLen > 0)
72 sValue = toUString(pName.get(), cbRealLen / sizeof(SQLWCHAR));
75 else
77 SQLSMALLINT BUFFER_LEN = 128;
78 auto pName = std::make_unique<SQLCHAR[]>(BUFFER_LEN + 1);
79 SQLRETURN nRet = functions().ColAttribute(m_aStatementHandle,
80 static_cast<SQLUSMALLINT>(column),
81 static_cast<SQLUSMALLINT>(ident),
82 static_cast<SQLPOINTER>(pName.get()),
83 BUFFER_LEN,
84 &cbRealLen,
85 nullptr
87 if ( nRet == SQL_SUCCESS )
89 if ( cbRealLen < 0 )
90 cbRealLen = BUFFER_LEN;
91 sValue = toUString(pName.get(), cbRealLen, m_pConnection->getTextEncoding());
93 OTools::ThrowException(m_pConnection,nRet,m_aStatementHandle,SQL_HANDLE_STMT,*this);
94 if(cbRealLen > BUFFER_LEN)
96 pName = std::make_unique<SQLCHAR[]>(cbRealLen + 1);
97 nRet = functions().ColAttribute(m_aStatementHandle,
98 static_cast<SQLUSMALLINT>(column),
99 static_cast<SQLUSMALLINT>(ident),
100 static_cast<SQLPOINTER>(pName.get()),
101 cbRealLen,
102 &cbRealLen,
103 nullptr
105 if ( nRet == SQL_SUCCESS && cbRealLen > 0)
106 sValue = toUString(pName.get(), cbRealLen, m_pConnection->getTextEncoding());
107 OTools::ThrowException(m_pConnection,nRet,m_aStatementHandle,SQL_HANDLE_STMT,*this);
111 return sValue;
114 SQLLEN OResultSetMetaData::getNumColAttrib(OConnection const * _pConnection
115 ,SQLHANDLE _aStatementHandle
116 ,const css::uno::Reference< css::uno::XInterface >& _xInterface
117 ,sal_Int32 _column
118 ,sal_Int32 _ident)
120 SQLLEN nValue=0;
121 OTools::ThrowException(_pConnection,_pConnection->functions().ColAttribute(_aStatementHandle,
122 static_cast<SQLUSMALLINT>(_column),
123 static_cast<SQLUSMALLINT>(_ident),
124 nullptr,
126 nullptr,
127 &nValue),_aStatementHandle,SQL_HANDLE_STMT,_xInterface);
128 return nValue;
131 sal_Int32 OResultSetMetaData::getNumColAttrib(sal_Int32 _column,sal_Int32 ident)
133 sal_Int32 column = _column;
134 if(_column < static_cast<sal_Int32>(m_vMapping.size())) // use mapping
135 column = m_vMapping[_column];
137 return getNumColAttrib(m_pConnection,m_aStatementHandle,*this,column,ident);
140 sal_Int32 SAL_CALL OResultSetMetaData::getColumnDisplaySize( sal_Int32 column )
142 return getNumColAttrib(column,SQL_DESC_DISPLAY_SIZE);
145 SQLSMALLINT OResultSetMetaData::getColumnODBCType(OConnection const * _pConnection
146 ,SQLHANDLE _aStatementHandle
147 ,const css::uno::Reference< css::uno::XInterface >& _xInterface
148 ,sal_Int32 column)
150 SQLSMALLINT nType = 0;
153 nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE));
154 if(nType == SQL_UNKNOWN_TYPE)
155 nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column, SQL_DESC_TYPE));
157 catch(SQLException& ) // in this case we have an odbc 2.0 driver
159 nType = static_cast<SQLSMALLINT>(getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE ));
162 return nType;
165 sal_Int32 SAL_CALL OResultSetMetaData::getColumnType( sal_Int32 column )
167 std::map<sal_Int32,sal_Int32>::iterator aFind = m_aColumnTypes.find(column);
168 if ( aFind == m_aColumnTypes.end() )
170 sal_Int32 nType = 0;
171 if(!m_bUseODBC2Types)
175 nType = getNumColAttrib(column,SQL_DESC_CONCISE_TYPE);
176 if(nType == SQL_UNKNOWN_TYPE)
177 nType = getNumColAttrib(column, SQL_DESC_TYPE);
178 nType = OTools::MapOdbcType2Jdbc(nType);
180 catch(SQLException& ) // in this case we have an odbc 2.0 driver
182 m_bUseODBC2Types = true;
185 if (m_bUseODBC2Types)
186 nType = OTools::MapOdbcType2Jdbc(getNumColAttrib(column, SQL_DESC_CONCISE_TYPE));
187 aFind = m_aColumnTypes.emplace(column,nType).first;
191 return aFind->second;
195 sal_Int32 SAL_CALL OResultSetMetaData::getColumnCount( )
197 if(m_nColCount != -1)
198 return m_nColCount;
199 sal_Int16 nNumResultCols=0;
200 OTools::ThrowException(m_pConnection,functions().NumResultCols(m_aStatementHandle,&nNumResultCols),m_aStatementHandle,SQL_HANDLE_STMT,*this);
201 m_nColCount = nNumResultCols;
202 return m_nColCount;
206 sal_Bool SAL_CALL OResultSetMetaData::isCaseSensitive( sal_Int32 column )
208 return getNumColAttrib(column,SQL_DESC_CASE_SENSITIVE) == SQL_TRUE;
212 OUString SAL_CALL OResultSetMetaData::getSchemaName( sal_Int32 column )
214 return getCharColAttrib(column,SQL_DESC_SCHEMA_NAME);
218 OUString SAL_CALL OResultSetMetaData::getColumnName( sal_Int32 column )
220 return getCharColAttrib(column,SQL_DESC_NAME);
223 OUString SAL_CALL OResultSetMetaData::getTableName( sal_Int32 column )
225 return getCharColAttrib(column,SQL_DESC_TABLE_NAME);
228 OUString SAL_CALL OResultSetMetaData::getCatalogName( sal_Int32 column )
230 return getCharColAttrib(column,SQL_DESC_CATALOG_NAME);
233 OUString SAL_CALL OResultSetMetaData::getColumnTypeName( sal_Int32 column )
235 return getCharColAttrib(column,SQL_DESC_TYPE_NAME);
238 OUString SAL_CALL OResultSetMetaData::getColumnLabel( sal_Int32 column )
240 return getCharColAttrib(column,SQL_DESC_LABEL);
243 OUString SAL_CALL OResultSetMetaData::getColumnServiceName( sal_Int32 /*column*/ )
245 return OUString();
249 sal_Bool SAL_CALL OResultSetMetaData::isCurrency( sal_Int32 column )
251 return getNumColAttrib(column,SQL_DESC_FIXED_PREC_SCALE) == SQL_TRUE;
255 sal_Bool SAL_CALL OResultSetMetaData::isAutoIncrement( sal_Int32 column )
257 return getNumColAttrib(column,SQL_DESC_AUTO_UNIQUE_VALUE) == SQL_TRUE;
261 sal_Bool SAL_CALL OResultSetMetaData::isSigned( sal_Int32 column )
263 return getNumColAttrib(column,SQL_DESC_UNSIGNED) == SQL_FALSE;
266 sal_Int32 SAL_CALL OResultSetMetaData::getPrecision( sal_Int32 column )
268 sal_Int32 nType = 0;
269 if (!m_bUseODBC2Types)
273 nType = getNumColAttrib(column, SQL_DESC_PRECISION);
275 catch (const SQLException&) // in this case we have an odbc 2.0 driver
277 m_bUseODBC2Types = true;
280 if (m_bUseODBC2Types)
281 nType = getNumColAttrib(column, SQL_COLUMN_PRECISION);
282 return nType;
285 sal_Int32 SAL_CALL OResultSetMetaData::getScale( sal_Int32 column )
287 sal_Int32 nType = 0;
288 if (!m_bUseODBC2Types)
292 nType = getNumColAttrib(column, SQL_DESC_SCALE);
294 catch (const SQLException&) // in this case we have an odbc 2.0 driver
296 m_bUseODBC2Types = true;
299 if (m_bUseODBC2Types)
300 nType = getNumColAttrib(column, SQL_COLUMN_SCALE);
301 return nType;
305 sal_Int32 SAL_CALL OResultSetMetaData::isNullable( sal_Int32 column )
307 return getNumColAttrib(column,SQL_DESC_NULLABLE);
311 sal_Bool SAL_CALL OResultSetMetaData::isSearchable( sal_Int32 column )
313 return getNumColAttrib(column,SQL_DESC_SEARCHABLE) != SQL_PRED_NONE;
317 sal_Bool SAL_CALL OResultSetMetaData::isReadOnly( sal_Int32 column )
319 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_READONLY;
323 sal_Bool SAL_CALL OResultSetMetaData::isDefinitelyWritable( sal_Int32 column )
325 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
328 sal_Bool SAL_CALL OResultSetMetaData::isWritable( sal_Int32 column )
330 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */