1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OResultSetMetaData.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "odbc/OResultSetMetaData.hxx"
34 #include "odbc/OTools.hxx"
35 #include <rtl/logfile.hxx>
37 using namespace connectivity::odbc
;
38 using namespace com::sun::star::uno
;
39 using namespace com::sun::star::lang
;
40 using namespace com::sun::star::sdbc
;
42 // -------------------------------------------------------------------------
43 OResultSetMetaData::~OResultSetMetaData()
46 // -------------------------------------------------------------------------
47 ::rtl::OUString
OResultSetMetaData::getCharColAttrib(sal_Int32 _column
,sal_Int32 ident
) throw(SQLException
, RuntimeException
)
49 sal_Int32 column
= _column
;
50 if(_column
<(sal_Int32
) m_vMapping
.size()) // use mapping
51 column
= m_vMapping
[_column
];
53 SQLSMALLINT BUFFER_LEN
= 128;
54 char *pName
= new char[BUFFER_LEN
+1];
55 SQLSMALLINT nRealLen
=0;
56 SQLRETURN nRet
= N3SQLColAttribute(m_aStatementHandle
,
64 ::rtl::OUString sValue
;
65 if ( nRet
== SQL_SUCCESS
)
66 sValue
= ::rtl::OUString(pName
,nRealLen
,m_pConnection
->getTextEncoding());
68 OTools::ThrowException(m_pConnection
,nRet
,m_aStatementHandle
,SQL_HANDLE_STMT
,*this);
69 if(nRealLen
> BUFFER_LEN
)
71 pName
= new char[nRealLen
+1];
72 nRet
= N3SQLColAttribute(m_aStatementHandle
,
80 if ( nRet
== SQL_SUCCESS
)
81 sValue
= ::rtl::OUString(pName
,nRealLen
,m_pConnection
->getTextEncoding());
83 OTools::ThrowException(m_pConnection
,nRet
,m_aStatementHandle
,SQL_HANDLE_STMT
,*this);
88 // -------------------------------------------------------------------------
89 SQLLEN
OResultSetMetaData::getNumColAttrib(OConnection
* _pConnection
90 ,SQLHANDLE _aStatementHandle
91 ,const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _xInterface
93 ,sal_Int32 _ident
) throw(SQLException
, RuntimeException
)
96 OTools::ThrowException(_pConnection
,(*(T3SQLColAttribute
)_pConnection
->getOdbcFunction(ODBC3SQLColAttribute
))(_aStatementHandle
,
97 (SQLUSMALLINT
)_column
,
102 &nValue
),_aStatementHandle
,SQL_HANDLE_STMT
,_xInterface
);
105 // -------------------------------------------------------------------------
106 sal_Int32
OResultSetMetaData::getNumColAttrib(sal_Int32 _column
,sal_Int32 ident
) throw(SQLException
, RuntimeException
)
108 sal_Int32 column
= _column
;
109 if(_column
< (sal_Int32
)m_vMapping
.size()) // use mapping
110 column
= m_vMapping
[_column
];
112 return getNumColAttrib(m_pConnection
,m_aStatementHandle
,*this,column
,ident
);
114 // -------------------------------------------------------------------------
115 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnDisplaySize( sal_Int32 column
) throw(SQLException
, RuntimeException
)
117 return getNumColAttrib(column
,SQL_DESC_DISPLAY_SIZE
);
119 // -------------------------------------------------------------------------
120 SQLSMALLINT
OResultSetMetaData::getColumnODBCType(OConnection
* _pConnection
121 ,SQLHANDLE _aStatementHandle
122 ,const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _xInterface
124 throw(::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
)
126 SQLSMALLINT nType
= 0;
129 nType
= (SQLSMALLINT
)getNumColAttrib(_pConnection
,_aStatementHandle
,_xInterface
,column
,SQL_DESC_CONCISE_TYPE
);
130 if(nType
== SQL_UNKNOWN_TYPE
)
131 nType
= (SQLSMALLINT
)getNumColAttrib(_pConnection
,_aStatementHandle
,_xInterface
,column
, SQL_DESC_TYPE
);
133 catch(SQLException
& ) // in this case we have an odbc 2.0 driver
135 nType
= (SQLSMALLINT
)getNumColAttrib(_pConnection
,_aStatementHandle
,_xInterface
,column
,SQL_DESC_CONCISE_TYPE
);
140 // -----------------------------------------------------------------------------
141 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnType( sal_Int32 column
) throw(SQLException
, RuntimeException
)
143 ::std::map
<sal_Int32
,sal_Int32
>::iterator aFind
= m_aColumnTypes
.find(column
);
144 if ( aFind
== m_aColumnTypes
.end() )
147 if(!m_bUseODBC2Types
)
151 nType
= getNumColAttrib(column
,SQL_DESC_CONCISE_TYPE
);
152 if(nType
== SQL_UNKNOWN_TYPE
)
153 nType
= getNumColAttrib(column
, SQL_DESC_TYPE
);
154 nType
= OTools::MapOdbcType2Jdbc(nType
);
156 catch(SQLException
& ) // in this case we have an odbc 2.0 driver
158 m_bUseODBC2Types
= sal_True
;
159 nType
= OTools::MapOdbcType2Jdbc(getNumColAttrib(column
,SQL_DESC_CONCISE_TYPE
));
163 nType
= OTools::MapOdbcType2Jdbc(getNumColAttrib(column
,SQL_DESC_CONCISE_TYPE
));
164 aFind
= m_aColumnTypes
.insert(::std::map
<sal_Int32
,sal_Int32
>::value_type(column
,nType
)).first
;
168 return aFind
->second
;
170 // -------------------------------------------------------------------------
172 sal_Int32 SAL_CALL
OResultSetMetaData::getColumnCount( ) throw(SQLException
, RuntimeException
)
174 if(m_nColCount
!= -1)
176 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::getColumnCount" );
177 sal_Int16 nNumResultCols
=0;
178 OTools::ThrowException(m_pConnection
,N3SQLNumResultCols(m_aStatementHandle
,&nNumResultCols
),m_aStatementHandle
,SQL_HANDLE_STMT
,*this);
179 return m_nColCount
= nNumResultCols
;
181 // -------------------------------------------------------------------------
183 sal_Bool SAL_CALL
OResultSetMetaData::isCaseSensitive( sal_Int32 column
) throw(SQLException
, RuntimeException
)
185 return getNumColAttrib(column
,SQL_DESC_CASE_SENSITIVE
) == SQL_TRUE
;
187 // -------------------------------------------------------------------------
189 ::rtl::OUString SAL_CALL
OResultSetMetaData::getSchemaName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
191 return getCharColAttrib(column
,SQL_DESC_SCHEMA_NAME
);
193 // -------------------------------------------------------------------------
195 ::rtl::OUString SAL_CALL
OResultSetMetaData::getColumnName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
197 return getCharColAttrib(column
,SQL_DESC_NAME
);
199 // -------------------------------------------------------------------------
200 ::rtl::OUString SAL_CALL
OResultSetMetaData::getTableName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
202 return getCharColAttrib(column
,SQL_DESC_TABLE_NAME
);
204 // -------------------------------------------------------------------------
205 ::rtl::OUString SAL_CALL
OResultSetMetaData::getCatalogName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
207 return getCharColAttrib(column
,SQL_DESC_CATALOG_NAME
);
209 // -------------------------------------------------------------------------
210 ::rtl::OUString SAL_CALL
OResultSetMetaData::getColumnTypeName( sal_Int32 column
) throw(SQLException
, RuntimeException
)
212 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::getColumnTypeName" );
213 return getCharColAttrib(column
,SQL_DESC_TYPE_NAME
);
215 // -------------------------------------------------------------------------
216 ::rtl::OUString SAL_CALL
OResultSetMetaData::getColumnLabel( sal_Int32 column
) throw(SQLException
, RuntimeException
)
218 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::getColumnLabel" );
219 return getCharColAttrib(column
,SQL_DESC_LABEL
);
221 // -------------------------------------------------------------------------
222 ::rtl::OUString SAL_CALL
OResultSetMetaData::getColumnServiceName( sal_Int32
/*column*/ ) throw(SQLException
, RuntimeException
)
224 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::getColumnServiceName" );
225 return ::rtl::OUString();
227 // -------------------------------------------------------------------------
229 sal_Bool SAL_CALL
OResultSetMetaData::isCurrency( sal_Int32 column
) throw(SQLException
, RuntimeException
)
231 return getNumColAttrib(column
,SQL_DESC_FIXED_PREC_SCALE
) == SQL_TRUE
;
233 // -------------------------------------------------------------------------
235 sal_Bool SAL_CALL
OResultSetMetaData::isAutoIncrement( sal_Int32 column
) throw(SQLException
, RuntimeException
)
237 return getNumColAttrib(column
,SQL_DESC_AUTO_UNIQUE_VALUE
) == SQL_TRUE
;
239 // -------------------------------------------------------------------------
242 sal_Bool SAL_CALL
OResultSetMetaData::isSigned( sal_Int32 column
) throw(SQLException
, RuntimeException
)
244 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::isSigned" );
245 return getNumColAttrib(column
,SQL_DESC_UNSIGNED
) == SQL_FALSE
;
247 // -------------------------------------------------------------------------
248 sal_Int32 SAL_CALL
OResultSetMetaData::getPrecision( sal_Int32 column
) throw(SQLException
, RuntimeException
)
250 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::getPrecision" );
254 nType
= getNumColAttrib(column
,SQL_DESC_PRECISION
);
256 catch(const SQLException
& ) // in this case we have an odbc 2.0 driver
258 m_bUseODBC2Types
= sal_True
;
259 nType
= getNumColAttrib(column
,SQL_COLUMN_PRECISION
);
263 // -----------------------------------------------------------------------------
264 sal_Int32 SAL_CALL
OResultSetMetaData::getScale( sal_Int32 column
) throw(::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
)
266 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::getScale" );
270 nType
= getNumColAttrib(column
,SQL_DESC_SCALE
);
272 catch(const SQLException
& ) // in this case we have an odbc 2.0 driver
274 m_bUseODBC2Types
= sal_True
;
275 nType
= getNumColAttrib(column
,SQL_COLUMN_SCALE
);
279 // -------------------------------------------------------------------------
281 sal_Int32 SAL_CALL
OResultSetMetaData::isNullable( sal_Int32 column
) throw(SQLException
, RuntimeException
)
283 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::isNullable" );
284 return getNumColAttrib(column
,SQL_DESC_NULLABLE
);
286 // -------------------------------------------------------------------------
288 sal_Bool SAL_CALL
OResultSetMetaData::isSearchable( sal_Int32 column
) throw(SQLException
, RuntimeException
)
290 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::isSearchable" );
291 return getNumColAttrib(column
,SQL_DESC_SEARCHABLE
) != SQL_PRED_NONE
;
293 // -------------------------------------------------------------------------
295 sal_Bool SAL_CALL
OResultSetMetaData::isReadOnly( sal_Int32 column
) throw(SQLException
, RuntimeException
)
297 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::isReadOnly" );
298 return getNumColAttrib(column
,SQL_DESC_UPDATABLE
) == SQL_ATTR_READONLY
;
300 // -------------------------------------------------------------------------
302 sal_Bool SAL_CALL
OResultSetMetaData::isDefinitelyWritable( sal_Int32 column
) throw(SQLException
, RuntimeException
)
304 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::isDefinitelyWritable" );
305 return getNumColAttrib(column
,SQL_DESC_UPDATABLE
) == SQL_ATTR_WRITE
;
308 // -------------------------------------------------------------------------
309 sal_Bool SAL_CALL
OResultSetMetaData::isWritable( sal_Int32 column
) throw(SQLException
, RuntimeException
)
311 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "odbc", "Ocke.Janssen@sun.com", "OResultSetMetaData::isWritable" );
312 return getNumColAttrib(column
,SQL_DESC_UPDATABLE
) == SQL_ATTR_WRITE
;
314 // -------------------------------------------------------------------------