Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / odbc / OResultSetMetaData.cxx
blob5eb1bb1cae2b6803335589a41d5ed261d98e4e4b
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::lang;
26 using namespace com::sun::star::sdbc;
29 OResultSetMetaData::~OResultSetMetaData()
33 OUString OResultSetMetaData::getCharColAttrib(sal_Int32 _column,sal_Int32 ident) throw(SQLException, RuntimeException)
35 sal_Int32 column = _column;
36 if(_column <(sal_Int32) m_vMapping.size()) // use mapping
37 column = m_vMapping[_column];
39 SQLSMALLINT BUFFER_LEN = 128;
40 char *pName = new char[BUFFER_LEN+1];
41 SQLSMALLINT nRealLen=0;
42 SQLRETURN nRet = N3SQLColAttribute(m_aStatementHandle,
43 (SQLUSMALLINT)column,
44 (SQLUSMALLINT)ident,
45 (SQLPOINTER)pName,
46 BUFFER_LEN,
47 &nRealLen,
48 NULL
50 OUString sValue;
51 if ( nRet == SQL_SUCCESS )
53 if ( nRealLen < 0 )
54 nRealLen = BUFFER_LEN;
55 sValue = OUString(pName,nRealLen,m_pConnection->getTextEncoding());
57 delete [] pName;
58 OTools::ThrowException(m_pConnection,nRet,m_aStatementHandle,SQL_HANDLE_STMT,*this);
59 if(nRealLen > BUFFER_LEN)
61 pName = new char[nRealLen+1];
62 nRet = N3SQLColAttribute(m_aStatementHandle,
63 (SQLUSMALLINT)column,
64 (SQLUSMALLINT)ident,
65 (SQLPOINTER)pName,
66 nRealLen,
67 &nRealLen,
68 NULL
70 if ( nRet == SQL_SUCCESS && nRealLen > 0)
71 sValue = OUString(pName,nRealLen,m_pConnection->getTextEncoding());
72 delete [] pName;
73 OTools::ThrowException(m_pConnection,nRet,m_aStatementHandle,SQL_HANDLE_STMT,*this);
76 return sValue;
79 SQLLEN OResultSetMetaData::getNumColAttrib(OConnection* _pConnection
80 ,SQLHANDLE _aStatementHandle
81 ,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface
82 ,sal_Int32 _column
83 ,sal_Int32 _ident) throw(SQLException, RuntimeException)
85 SQLLEN nValue=0;
86 OTools::ThrowException(_pConnection,(*(T3SQLColAttribute)_pConnection->getOdbcFunction(ODBC3SQLColAttribute))(_aStatementHandle,
87 (SQLUSMALLINT)_column,
88 (SQLUSMALLINT)_ident,
89 NULL,
91 NULL,
92 &nValue),_aStatementHandle,SQL_HANDLE_STMT,_xInterface);
93 return nValue;
96 sal_Int32 OResultSetMetaData::getNumColAttrib(sal_Int32 _column,sal_Int32 ident) throw(SQLException, RuntimeException)
98 sal_Int32 column = _column;
99 if(_column < (sal_Int32)m_vMapping.size()) // use mapping
100 column = m_vMapping[_column];
102 return getNumColAttrib(m_pConnection,m_aStatementHandle,*this,column,ident);
105 sal_Int32 SAL_CALL OResultSetMetaData::getColumnDisplaySize( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
107 return getNumColAttrib(column,SQL_DESC_DISPLAY_SIZE);
110 SQLSMALLINT OResultSetMetaData::getColumnODBCType(OConnection* _pConnection
111 ,SQLHANDLE _aStatementHandle
112 ,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface
113 ,sal_Int32 column)
114 throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
116 SQLSMALLINT nType = 0;
119 nType = (SQLSMALLINT)getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE);
120 if(nType == SQL_UNKNOWN_TYPE)
121 nType = (SQLSMALLINT)getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column, SQL_DESC_TYPE);
123 catch(SQLException& ) // in this case we have an odbc 2.0 driver
125 nType = (SQLSMALLINT)getNumColAttrib(_pConnection,_aStatementHandle,_xInterface,column,SQL_DESC_CONCISE_TYPE );
128 return nType;
131 sal_Int32 SAL_CALL OResultSetMetaData::getColumnType( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
133 ::std::map<sal_Int32,sal_Int32>::iterator aFind = m_aColumnTypes.find(column);
134 if ( aFind == m_aColumnTypes.end() )
136 sal_Int32 nType = 0;
137 if(!m_bUseODBC2Types)
141 nType = getNumColAttrib(column,SQL_DESC_CONCISE_TYPE);
142 if(nType == SQL_UNKNOWN_TYPE)
143 nType = getNumColAttrib(column, SQL_DESC_TYPE);
144 nType = OTools::MapOdbcType2Jdbc(nType);
146 catch(SQLException& ) // in this case we have an odbc 2.0 driver
148 m_bUseODBC2Types = true;
149 nType = OTools::MapOdbcType2Jdbc(getNumColAttrib(column,SQL_DESC_CONCISE_TYPE ));
152 else
153 nType = OTools::MapOdbcType2Jdbc(getNumColAttrib(column,SQL_DESC_CONCISE_TYPE ));
154 aFind = m_aColumnTypes.insert(::std::map<sal_Int32,sal_Int32>::value_type(column,nType)).first;
158 return aFind->second;
162 sal_Int32 SAL_CALL OResultSetMetaData::getColumnCount( ) throw(SQLException, RuntimeException, std::exception)
164 if(m_nColCount != -1)
165 return m_nColCount;
166 sal_Int16 nNumResultCols=0;
167 OTools::ThrowException(m_pConnection,N3SQLNumResultCols(m_aStatementHandle,&nNumResultCols),m_aStatementHandle,SQL_HANDLE_STMT,*this);
168 return m_nColCount = nNumResultCols;
172 sal_Bool SAL_CALL OResultSetMetaData::isCaseSensitive( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
174 return getNumColAttrib(column,SQL_DESC_CASE_SENSITIVE) == SQL_TRUE;
178 OUString SAL_CALL OResultSetMetaData::getSchemaName( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
180 return getCharColAttrib(column,SQL_DESC_SCHEMA_NAME);
184 OUString SAL_CALL OResultSetMetaData::getColumnName( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
186 return getCharColAttrib(column,SQL_DESC_NAME);
189 OUString SAL_CALL OResultSetMetaData::getTableName( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
191 return getCharColAttrib(column,SQL_DESC_TABLE_NAME);
194 OUString SAL_CALL OResultSetMetaData::getCatalogName( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
196 return getCharColAttrib(column,SQL_DESC_CATALOG_NAME);
199 OUString SAL_CALL OResultSetMetaData::getColumnTypeName( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
201 return getCharColAttrib(column,SQL_DESC_TYPE_NAME);
204 OUString SAL_CALL OResultSetMetaData::getColumnLabel( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
206 return getCharColAttrib(column,SQL_DESC_LABEL);
209 OUString SAL_CALL OResultSetMetaData::getColumnServiceName( sal_Int32 /*column*/ ) throw(SQLException, RuntimeException, std::exception)
211 return OUString();
215 sal_Bool SAL_CALL OResultSetMetaData::isCurrency( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
217 return getNumColAttrib(column,SQL_DESC_FIXED_PREC_SCALE) == SQL_TRUE;
221 sal_Bool SAL_CALL OResultSetMetaData::isAutoIncrement( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
223 return getNumColAttrib(column,SQL_DESC_AUTO_UNIQUE_VALUE) == SQL_TRUE;
228 sal_Bool SAL_CALL OResultSetMetaData::isSigned( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
230 return getNumColAttrib(column,SQL_DESC_UNSIGNED) == SQL_FALSE;
233 sal_Int32 SAL_CALL OResultSetMetaData::getPrecision( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
235 sal_Int32 nType = 0;
238 nType = getNumColAttrib(column,SQL_DESC_PRECISION);
240 catch(const SQLException& ) // in this case we have an odbc 2.0 driver
242 m_bUseODBC2Types = true;
243 nType = getNumColAttrib(column,SQL_COLUMN_PRECISION );
245 return nType;
248 sal_Int32 SAL_CALL OResultSetMetaData::getScale( sal_Int32 column ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception)
250 sal_Int32 nType = 0;
253 nType = getNumColAttrib(column,SQL_DESC_SCALE);
255 catch(const SQLException& ) // in this case we have an odbc 2.0 driver
257 m_bUseODBC2Types = true;
258 nType = getNumColAttrib(column,SQL_COLUMN_SCALE );
260 return nType;
264 sal_Int32 SAL_CALL OResultSetMetaData::isNullable( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
266 return getNumColAttrib(column,SQL_DESC_NULLABLE);
270 sal_Bool SAL_CALL OResultSetMetaData::isSearchable( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
272 return getNumColAttrib(column,SQL_DESC_SEARCHABLE) != SQL_PRED_NONE;
276 sal_Bool SAL_CALL OResultSetMetaData::isReadOnly( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
278 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_READONLY;
282 sal_Bool SAL_CALL OResultSetMetaData::isDefinitelyWritable( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
284 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
287 sal_Bool SAL_CALL OResultSetMetaData::isWritable( sal_Int32 column ) throw(SQLException, RuntimeException, std::exception)
289 return getNumColAttrib(column,SQL_DESC_UPDATABLE) == SQL_ATTR_WRITE;
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */