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
)
35 sal_Int32 column
= _column
;
36 if(_column
<static_cast<sal_Int32
>(m_vMapping
.size())) // use mapping
37 column
= m_vMapping
[_column
];
39 SQLSMALLINT BUFFER_LEN
= 128;
40 std::unique_ptr
<char[]> pName(new char[BUFFER_LEN
+1]);
41 SQLSMALLINT nRealLen
=0;
42 SQLRETURN nRet
= N3SQLColAttribute(m_aStatementHandle
,
43 static_cast<SQLUSMALLINT
>(column
),
44 static_cast<SQLUSMALLINT
>(ident
),
45 static_cast<SQLPOINTER
>(pName
.get()),
51 if ( nRet
== SQL_SUCCESS
)
54 nRealLen
= BUFFER_LEN
;
55 sValue
= OUString(pName
.get(),nRealLen
,m_pConnection
->getTextEncoding());
58 OTools::ThrowException(m_pConnection
,nRet
,m_aStatementHandle
,SQL_HANDLE_STMT
,*this);
59 if(nRealLen
> BUFFER_LEN
)
61 pName
.reset(new char[nRealLen
+1]);
62 nRet
= N3SQLColAttribute(m_aStatementHandle
,
63 static_cast<SQLUSMALLINT
>(column
),
64 static_cast<SQLUSMALLINT
>(ident
),
65 static_cast<SQLPOINTER
>(pName
.get()),
70 if ( nRet
== SQL_SUCCESS
&& nRealLen
> 0)
71 sValue
= OUString(pName
.get(),nRealLen
,m_pConnection
->getTextEncoding());
72 OTools::ThrowException(m_pConnection
,nRet
,m_aStatementHandle
,SQL_HANDLE_STMT
,*this);
78 SQLLEN
OResultSetMetaData::getNumColAttrib(OConnection
const * _pConnection
79 ,SQLHANDLE _aStatementHandle
80 ,const css::uno::Reference
< css::uno::XInterface
>& _xInterface
85 OTools::ThrowException(_pConnection
,(*reinterpret_cast<T3SQLColAttribute
>(_pConnection
->getOdbcFunction(ODBC3SQLFunctionId::ColAttribute
)))(_aStatementHandle
,
86 static_cast<SQLUSMALLINT
>(_column
),
87 static_cast<SQLUSMALLINT
>(_ident
),
91 &nValue
),_aStatementHandle
,SQL_HANDLE_STMT
,_xInterface
);
95 sal_Int32
OResultSetMetaData::getNumColAttrib(sal_Int32 _column
,sal_Int32 ident
)
97 sal_Int32 column
= _column
;
98 if(_column
< static_cast<sal_Int32
>(m_vMapping
.size())) // use mapping
99 column
= m_vMapping
[_column
];
101 return getNumColAttrib(m_pConnection
,m_aStatementHandle
,*this,column
,ident
);
104 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnDisplaySize( sal_Int32 column
)
106 return getNumColAttrib(column
,SQL_DESC_DISPLAY_SIZE
);
109 SQLSMALLINT
OResultSetMetaData::getColumnODBCType(OConnection
const * _pConnection
110 ,SQLHANDLE _aStatementHandle
111 ,const css::uno::Reference
< css::uno::XInterface
>& _xInterface
114 SQLSMALLINT nType
= 0;
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
));
121 catch(SQLException
& ) // in this case we have an odbc 2.0 driver
123 nType
= static_cast<SQLSMALLINT
>(getNumColAttrib(_pConnection
,_aStatementHandle
,_xInterface
,column
,SQL_DESC_CONCISE_TYPE
));
129 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnType( sal_Int32 column
)
131 std::map
<sal_Int32
,sal_Int32
>::iterator aFind
= m_aColumnTypes
.find(column
);
132 if ( aFind
== m_aColumnTypes
.end() )
135 if(!m_bUseODBC2Types
)
139 nType
= getNumColAttrib(column
,SQL_DESC_CONCISE_TYPE
);
140 if(nType
== SQL_UNKNOWN_TYPE
)
141 nType
= getNumColAttrib(column
, SQL_DESC_TYPE
);
142 nType
= OTools::MapOdbcType2Jdbc(nType
);
144 catch(SQLException
& ) // in this case we have an odbc 2.0 driver
146 m_bUseODBC2Types
= true;
147 nType
= OTools::MapOdbcType2Jdbc(getNumColAttrib(column
,SQL_DESC_CONCISE_TYPE
));
151 nType
= OTools::MapOdbcType2Jdbc(getNumColAttrib(column
,SQL_DESC_CONCISE_TYPE
));
152 aFind
= m_aColumnTypes
.emplace(column
,nType
).first
;
156 return aFind
->second
;
160 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnCount( )
162 if(m_nColCount
!= -1)
164 sal_Int16 nNumResultCols
=0;
165 OTools::ThrowException(m_pConnection
,N3SQLNumResultCols(m_aStatementHandle
,&nNumResultCols
),m_aStatementHandle
,SQL_HANDLE_STMT
,*this);
166 m_nColCount
= nNumResultCols
;
171 sal_Bool SAL_CALL
OResultSetMetaData::isCaseSensitive( sal_Int32 column
)
173 return getNumColAttrib(column
,SQL_DESC_CASE_SENSITIVE
) == SQL_TRUE
;
177 OUString SAL_CALL
OResultSetMetaData::getSchemaName( sal_Int32 column
)
179 return getCharColAttrib(column
,SQL_DESC_SCHEMA_NAME
);
183 OUString SAL_CALL
OResultSetMetaData::getColumnName( sal_Int32 column
)
185 return getCharColAttrib(column
,SQL_DESC_NAME
);
188 OUString SAL_CALL
OResultSetMetaData::getTableName( sal_Int32 column
)
190 return getCharColAttrib(column
,SQL_DESC_TABLE_NAME
);
193 OUString SAL_CALL
OResultSetMetaData::getCatalogName( sal_Int32 column
)
195 return getCharColAttrib(column
,SQL_DESC_CATALOG_NAME
);
198 OUString SAL_CALL
OResultSetMetaData::getColumnTypeName( sal_Int32 column
)
200 return getCharColAttrib(column
,SQL_DESC_TYPE_NAME
);
203 OUString SAL_CALL
OResultSetMetaData::getColumnLabel( sal_Int32 column
)
205 return getCharColAttrib(column
,SQL_DESC_LABEL
);
208 OUString SAL_CALL
OResultSetMetaData::getColumnServiceName( sal_Int32
/*column*/ )
214 sal_Bool SAL_CALL
OResultSetMetaData::isCurrency( sal_Int32 column
)
216 return getNumColAttrib(column
,SQL_DESC_FIXED_PREC_SCALE
) == SQL_TRUE
;
220 sal_Bool SAL_CALL
OResultSetMetaData::isAutoIncrement( sal_Int32 column
)
222 return getNumColAttrib(column
,SQL_DESC_AUTO_UNIQUE_VALUE
) == SQL_TRUE
;
226 sal_Bool SAL_CALL
OResultSetMetaData::isSigned( sal_Int32 column
)
228 return getNumColAttrib(column
,SQL_DESC_UNSIGNED
) == SQL_FALSE
;
231 sal_Int32 SAL_CALL
OResultSetMetaData::getPrecision( sal_Int32 column
)
236 nType
= getNumColAttrib(column
,SQL_DESC_PRECISION
);
238 catch(const SQLException
& ) // in this case we have an odbc 2.0 driver
240 m_bUseODBC2Types
= true;
241 nType
= getNumColAttrib(column
,SQL_COLUMN_PRECISION
);
246 sal_Int32 SAL_CALL
OResultSetMetaData::getScale( sal_Int32 column
)
251 nType
= getNumColAttrib(column
,SQL_DESC_SCALE
);
253 catch(const SQLException
& ) // in this case we have an odbc 2.0 driver
255 m_bUseODBC2Types
= true;
256 nType
= getNumColAttrib(column
,SQL_COLUMN_SCALE
);
262 sal_Int32 SAL_CALL
OResultSetMetaData::isNullable( sal_Int32 column
)
264 return getNumColAttrib(column
,SQL_DESC_NULLABLE
);
268 sal_Bool SAL_CALL
OResultSetMetaData::isSearchable( sal_Int32 column
)
270 return getNumColAttrib(column
,SQL_DESC_SEARCHABLE
) != SQL_PRED_NONE
;
274 sal_Bool SAL_CALL
OResultSetMetaData::isReadOnly( sal_Int32 column
)
276 return getNumColAttrib(column
,SQL_DESC_UPDATABLE
) == SQL_ATTR_READONLY
;
280 sal_Bool SAL_CALL
OResultSetMetaData::isDefinitelyWritable( sal_Int32 column
)
282 return getNumColAttrib(column
,SQL_DESC_UPDATABLE
) == SQL_ATTR_WRITE
;
285 sal_Bool SAL_CALL
OResultSetMetaData::isWritable( sal_Int32 column
)
287 return getNumColAttrib(column
,SQL_DESC_UPDATABLE
) == SQL_ATTR_WRITE
;
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */