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 "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
,
51 if ( nRet
== SQL_SUCCESS
)
54 nRealLen
= BUFFER_LEN
;
55 sValue
= OUString(pName
,nRealLen
,m_pConnection
->getTextEncoding());
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
,
70 if ( nRet
== SQL_SUCCESS
&& nRealLen
> 0)
71 sValue
= OUString(pName
,nRealLen
,m_pConnection
->getTextEncoding());
73 OTools::ThrowException(m_pConnection
,nRet
,m_aStatementHandle
,SQL_HANDLE_STMT
,*this);
79 SQLLEN
OResultSetMetaData::getNumColAttrib(OConnection
* _pConnection
80 ,SQLHANDLE _aStatementHandle
81 ,const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _xInterface
83 ,sal_Int32 _ident
) throw(SQLException
, RuntimeException
)
86 OTools::ThrowException(_pConnection
,(*(T3SQLColAttribute
)_pConnection
->getOdbcFunction(ODBC3SQLColAttribute
))(_aStatementHandle
,
87 (SQLUSMALLINT
)_column
,
92 &nValue
),_aStatementHandle
,SQL_HANDLE_STMT
,_xInterface
);
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
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
);
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() )
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
));
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)
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
)
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
)
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
);
248 sal_Int32 SAL_CALL
OResultSetMetaData::getScale( sal_Int32 column
) throw(::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
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
);
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: */