1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "TConnection.hxx"
31 
32 #ifndef _CONNECTIVITY_ADO_ADATABASEMETADATARESULTSET_HXX_
33 #include "odbc/ODatabaseMetaDataResultSet.hxx"
34 #endif
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <com/sun/star/sdbc/KeyRule.hpp>
37 #include <com/sun/star/sdbc/ProcedureResult.hpp>
38 #include <com/sun/star/sdbc/IndexType.hpp>
39 #include <comphelper/property.hxx>
40 #include <com/sun/star/lang/DisposedException.hpp>
41 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
42 #include <com/sun/star/sdbc/ResultSetType.hpp>
43 #include <com/sun/star/sdbc/FetchDirection.hpp>
44 #include <cppuhelper/typeprovider.hxx>
45 #include <comphelper/sequence.hxx>
46 #include "odbc/OResultSetMetaData.hxx"
47 #include "odbc/OTools.hxx"
48 #include <comphelper/types.hxx>
49 #include "FDatabaseMetaDataResultSetMetaData.hxx"
50 #include <connectivity/dbexception.hxx>
51 
52 using namespace ::comphelper;
53 
54 
55 using namespace connectivity::odbc;
56 using namespace cppu;
57 //------------------------------------------------------------------------------
58 using namespace ::com::sun::star::lang;
59 using namespace com::sun::star::uno;
60 using namespace com::sun::star::beans;
61 using namespace com::sun::star::sdbc;
62 using namespace com::sun::star::util;
63 
64 // -------------------------------------------------------------------------
65 ODatabaseMetaDataResultSet::ODatabaseMetaDataResultSet(OConnection* _pConnection)
66 	:ODatabaseMetaDataResultSet_BASE(m_aMutex)
67 	,OPropertySetHelper(ODatabaseMetaDataResultSet_BASE::rBHelper)
68 
69     ,m_aStatementHandle(_pConnection->createStatementHandle())
70 	,m_aStatement(NULL)
71     ,m_xMetaData(NULL)
72     ,m_pRowStatusArray(NULL)
73     ,m_pConnection(_pConnection)
74     ,m_nTextEncoding(_pConnection->getTextEncoding())
75     ,m_nRowPos(-1)
76     ,m_nLastColumnPos(0)
77     ,m_nDriverColumnCount(0)
78     ,m_nCurrentFetchState(0)
79     ,m_bWasNull(sal_True)
80     ,m_bEOF(sal_False)
81 	,m_bFreeHandle(sal_False)
82 {
83 	OSL_ENSURE(m_pConnection,"ODatabaseMetaDataResultSet::ODatabaseMetaDataResultSet: No parent set!");
84 	osl_incrementInterlockedCount( &m_refCount );
85 	m_pConnection->acquire();
86 	m_pRowStatusArray = new SQLUSMALLINT[1]; // the default value
87 	osl_decrementInterlockedCount( &m_refCount );
88 	//	allocBuffer();
89 }
90 
91 // -------------------------------------------------------------------------
92 ODatabaseMetaDataResultSet::~ODatabaseMetaDataResultSet()
93 {
94 	OSL_ENSURE(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed,"Object wasn't disposed!");
95 	if(!ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed)
96 	{
97 		osl_incrementInterlockedCount( &m_refCount );
98 		dispose();
99 	}
100 	delete [] m_pRowStatusArray;
101 }
102 // -------------------------------------------------------------------------
103 void ODatabaseMetaDataResultSet::disposing(void)
104 {
105 	OPropertySetHelper::disposing();
106 
107 	::osl::MutexGuard aGuard(m_aMutex);
108 	if(m_bFreeHandle)
109 		m_pConnection->freeStatementHandle(m_aStatementHandle);
110 
111 	m_aStatement	= NULL;
112 m_xMetaData.clear();
113 	m_pConnection->release();
114 }
115 // -------------------------------------------------------------------------
116 Any SAL_CALL ODatabaseMetaDataResultSet::queryInterface( const Type & rType ) throw(RuntimeException)
117 {
118 	Any aRet = OPropertySetHelper::queryInterface(rType);
119 	return aRet.hasValue() ? aRet : ODatabaseMetaDataResultSet_BASE::queryInterface(rType);
120 }
121 // -----------------------------------------------------------------------------
122 Reference< XPropertySetInfo > SAL_CALL ODatabaseMetaDataResultSet::getPropertySetInfo(  ) throw(RuntimeException)
123 {
124 	return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
125 }
126 // -----------------------------------------------------------------------------
127 void SAL_CALL ODatabaseMetaDataResultSet::acquire() throw()
128 {
129 	ODatabaseMetaDataResultSet_BASE::acquire();
130 }
131 // -----------------------------------------------------------------------------
132 void SAL_CALL ODatabaseMetaDataResultSet::release() throw()
133 {
134 	ODatabaseMetaDataResultSet_BASE::release();
135 }
136 // -------------------------------------------------------------------------
137 Sequence< Type > SAL_CALL ODatabaseMetaDataResultSet::getTypes(  ) throw(RuntimeException)
138 {
139 	::cppu::OTypeCollection aTypes(	::getCppuType( (const Reference< XMultiPropertySet > *)0 ),
140 									::getCppuType( (const Reference< XFastPropertySet > *)0 ),
141 									::getCppuType( (const Reference< XPropertySet > *)0 ));
142 
143 	return ::comphelper::concatSequences(aTypes.getTypes(),ODatabaseMetaDataResultSet_BASE::getTypes());
144 }
145 // -----------------------------------------------------------------------------
146 sal_Int32 ODatabaseMetaDataResultSet::mapColumn (sal_Int32	column)
147 {
148 	sal_Int32	map = column;
149 
150 	if (!m_aColMapping.empty())
151 	{
152 		// Validate column number
153 		map = m_aColMapping[column];
154 	}
155 
156 	return map;
157 }
158 // -------------------------------------------------------------------------
159 
160 sal_Int32 SAL_CALL ODatabaseMetaDataResultSet::findColumn( const ::rtl::OUString& columnName ) throw(SQLException, RuntimeException)
161 {
162 
163 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
164 	::osl::MutexGuard aGuard( m_aMutex );
165 
166 
167 	Reference< XResultSetMetaData > xMeta = getMetaData();
168 	sal_Int32 nLen = xMeta->getColumnCount();
169 	sal_Int32 i = 1;
170 	for(;i<=nLen;++i)
171 		if(xMeta->isCaseSensitive(i) ? columnName == xMeta->getColumnName(i) :
172 				columnName.equalsIgnoreAsciiCase(xMeta->getColumnName(i)))
173 			break;
174 	return i;
175 }
176 // -------------------------------------------------------------------------
177 Reference< ::com::sun::star::io::XInputStream > SAL_CALL ODatabaseMetaDataResultSet::getBinaryStream( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException)
178 {
179     ::dbtools::throwFunctionNotSupportedException( "XRow::getBinaryStream", *this );
180 	return NULL;
181 }
182 // -------------------------------------------------------------------------
183 Reference< ::com::sun::star::io::XInputStream > SAL_CALL ODatabaseMetaDataResultSet::getCharacterStream( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException)
184 {
185     ::dbtools::throwFunctionNotSupportedException( "XRow::getCharacterStream", *this );
186     return NULL;
187 }
188 
189 // -------------------------------------------------------------------------
190 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::getBoolean( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
191 {
192 
193 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
194 	::osl::MutexGuard aGuard( m_aMutex );
195 
196 	columnIndex = mapColumn(columnIndex);
197 
198 	sal_Bool bRet = sal_False;
199 	if(columnIndex <= m_nDriverColumnCount)
200 	{
201 		sal_Int32 nType = getMetaData()->getColumnType(columnIndex);
202 		switch(nType)
203 		{
204 			case DataType::BIT:
205 				{
206 					sal_Int8 nValue = 0;
207 					OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_BIT,m_bWasNull,**this,&nValue,sizeof nValue);
208 					bRet = nValue != 0;
209 				}
210 				break;
211 			default:
212 				bRet = getInt(columnIndex) != 0;
213 		}
214 	}
215 	return bRet;
216 }
217 // -------------------------------------------------------------------------
218 
219 sal_Int8 SAL_CALL ODatabaseMetaDataResultSet::getByte( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
220 {
221 
222 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
223 	::osl::MutexGuard aGuard( m_aMutex );
224 
225 
226 	columnIndex = mapColumn(columnIndex);
227 	sal_Int8  nVal = 0;
228 	if(columnIndex <= m_nDriverColumnCount)
229 	{
230 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_TINYINT,m_bWasNull,**this,&nVal,sizeof nVal);
231 
232 		::std::map<sal_Int32, ::connectivity::TInt2IntMap >::iterator	aValueRangeIter;
233 		if ( !m_aValueRange.empty() && (aValueRangeIter = m_aValueRange.find(columnIndex)) != m_aValueRange.end())
234 			return sal_Int8((*aValueRangeIter).second[(sal_Int32)nVal]);
235 	}
236 	else
237 		m_bWasNull = sal_True;
238 	return nVal;
239 }
240 // -------------------------------------------------------------------------
241 
242 Sequence< sal_Int8 > SAL_CALL ODatabaseMetaDataResultSet::getBytes( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
243 {
244 
245 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
246 	::osl::MutexGuard aGuard( m_aMutex );
247 
248 
249 	columnIndex = mapColumn(columnIndex);
250 	if(columnIndex <= m_nDriverColumnCount)
251 	{
252 		sal_Int32 nType = getMetaData()->getColumnType(columnIndex);
253 		switch(nType)
254 		{
255 			case DataType::VARCHAR:
256 			case DataType::LONGVARCHAR:
257 			{
258 				::rtl::OUString aRet = OTools::getStringValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_BINARY,m_bWasNull,**this,m_nTextEncoding);
259 				return Sequence<sal_Int8>(reinterpret_cast<const sal_Int8*>(aRet.getStr()),sizeof(sal_Unicode)*aRet.getLength());
260             }
261 		}
262 		return OTools::getBytesValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_BINARY,m_bWasNull,**this);
263 	}
264 	else
265 		m_bWasNull = sal_True;
266 	return Sequence<sal_Int8>();
267 }
268 // -------------------------------------------------------------------------
269 
270 ::com::sun::star::util::Date SAL_CALL ODatabaseMetaDataResultSet::getDate( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
271 {
272 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
273 	::osl::MutexGuard aGuard( m_aMutex );
274 
275 
276 	columnIndex = mapColumn(columnIndex);
277 	if(columnIndex <= m_nDriverColumnCount)
278 	{
279 		DATE_STRUCT aDate;
280 		aDate.day = 0;
281 		aDate.month = 0;
282 		aDate.year = 0;
283 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,m_pConnection->useOldDateFormat() ? SQL_C_DATE : SQL_C_TYPE_DATE,m_bWasNull,**this,&aDate,sizeof aDate);
284 		return Date(aDate.day,aDate.month,aDate.year);
285 	}
286 	else
287 		m_bWasNull = sal_True;
288 	return Date();
289 }
290 // -------------------------------------------------------------------------
291 
292 double SAL_CALL ODatabaseMetaDataResultSet::getDouble( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
293 {
294 
295 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
296 	::osl::MutexGuard aGuard( m_aMutex );
297 
298 
299 	columnIndex = mapColumn(columnIndex);
300 	double nValue(0.0);
301 	if(columnIndex <= m_nDriverColumnCount)
302 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_DOUBLE,m_bWasNull,**this,&nValue,sizeof nValue);
303 	else
304 		m_bWasNull = sal_True;
305 	return nValue;
306 }
307 // -------------------------------------------------------------------------
308 
309 float SAL_CALL ODatabaseMetaDataResultSet::getFloat( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
310 {
311 
312 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
313 	::osl::MutexGuard aGuard( m_aMutex );
314 
315 
316 	columnIndex = mapColumn(columnIndex);
317 	float nVal(0);
318 	if(columnIndex <= m_nDriverColumnCount)
319 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_FLOAT,m_bWasNull,**this,&nVal,sizeof nVal);
320 	else
321 		m_bWasNull = sal_True;
322 	return nVal;
323 }
324 // -------------------------------------------------------------------------
325 
326 sal_Int32 SAL_CALL ODatabaseMetaDataResultSet::getInt( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
327 {
328 
329 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
330 	::osl::MutexGuard aGuard( m_aMutex );
331 
332 
333 	columnIndex = mapColumn(columnIndex);
334 	sal_Int32 nVal = 0;
335 	if(columnIndex <= m_nDriverColumnCount)
336 	{
337 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_LONG,m_bWasNull,**this,&nVal,sizeof nVal);
338 
339 		::std::map<sal_Int32, ::connectivity::TInt2IntMap >::iterator	aValueRangeIter;
340 		if ( !m_aValueRange.empty() && (aValueRangeIter = m_aValueRange.find(columnIndex)) != m_aValueRange.end())
341 			return (*aValueRangeIter).second[(sal_Int32)nVal];
342 	}
343 	else
344 		m_bWasNull = sal_True;
345 	return nVal;
346 }
347 // -------------------------------------------------------------------------
348 
349 sal_Int32 SAL_CALL ODatabaseMetaDataResultSet::getRow(  ) throw(SQLException, RuntimeException)
350 {
351 	return 0;
352 }
353 // -------------------------------------------------------------------------
354 
355 sal_Int64 SAL_CALL ODatabaseMetaDataResultSet::getLong( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException)
356 {
357     ::dbtools::throwFunctionNotSupportedException( "XRow::getLong", *this );
358     return 0;
359 }
360 // -------------------------------------------------------------------------
361 
362 Reference< XResultSetMetaData > SAL_CALL ODatabaseMetaDataResultSet::getMetaData(  ) throw(SQLException, RuntimeException)
363 {
364 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
365 	::osl::MutexGuard aGuard( m_aMutex );
366 	return m_xMetaData.is() ? m_xMetaData :  (m_xMetaData = new OResultSetMetaData(m_pConnection,m_aStatementHandle));
367 }
368 // -------------------------------------------------------------------------
369 Reference< XArray > SAL_CALL ODatabaseMetaDataResultSet::getArray( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException)
370 {
371     ::dbtools::throwFunctionNotSupportedException( "XRow::getArray", *this );
372     return NULL;
373 }
374 // -------------------------------------------------------------------------
375 Reference< XClob > SAL_CALL ODatabaseMetaDataResultSet::getClob( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException)
376 {
377     ::dbtools::throwFunctionNotSupportedException( "XRow::getClob", *this );
378     return NULL;
379 }
380 // -------------------------------------------------------------------------
381 Reference< XBlob > SAL_CALL ODatabaseMetaDataResultSet::getBlob( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException)
382 {
383     ::dbtools::throwFunctionNotSupportedException( "XRow::getBlob", *this );
384     return NULL;
385 }
386 // -------------------------------------------------------------------------
387 
388 Reference< XRef > SAL_CALL ODatabaseMetaDataResultSet::getRef( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException)
389 {
390     ::dbtools::throwFunctionNotSupportedException( "XRow::getRef", *this );
391     return NULL;
392 }
393 // -------------------------------------------------------------------------
394 
395 Any SAL_CALL ODatabaseMetaDataResultSet::getObject( sal_Int32 /*columnIndex*/, const Reference< ::com::sun::star::container::XNameAccess >& /*typeMap*/ ) throw(SQLException, RuntimeException)
396 {
397     ::dbtools::throwFunctionNotSupportedException( "XRow::getObject", *this );
398     return Any();
399 }
400 // -------------------------------------------------------------------------
401 
402 sal_Int16 SAL_CALL ODatabaseMetaDataResultSet::getShort( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
403 {
404 
405 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
406 	::osl::MutexGuard aGuard( m_aMutex );
407 
408 	columnIndex = mapColumn(columnIndex);
409 	sal_Int16 nVal = 0;
410 	if(columnIndex <= m_nDriverColumnCount)
411 	{
412 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,SQL_C_SHORT,m_bWasNull,**this,&nVal,sizeof nVal);
413 
414 		::std::map<sal_Int32, ::connectivity::TInt2IntMap >::iterator	aValueRangeIter;
415 		if ( !m_aValueRange.empty() && (aValueRangeIter = m_aValueRange.find(columnIndex)) != m_aValueRange.end())
416 			return sal_Int16((*aValueRangeIter).second[(sal_Int32)nVal]);
417 	}
418 	else
419 		m_bWasNull = sal_True;
420 	return nVal;
421 }
422 // -------------------------------------------------------------------------
423 
424 ::rtl::OUString SAL_CALL ODatabaseMetaDataResultSet::getString( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
425 {
426 
427 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
428 	::osl::MutexGuard aGuard( m_aMutex );
429 
430 
431 	columnIndex = mapColumn(columnIndex);
432 	::rtl::OUString aVal;
433 	if(columnIndex <= m_nDriverColumnCount)
434 		aVal = OTools::getStringValue(m_pConnection,m_aStatementHandle,columnIndex,impl_getColumnType_nothrow(columnIndex),m_bWasNull,**this,m_nTextEncoding);
435 	else
436 		m_bWasNull = sal_True;
437 
438 	return aVal;
439 }
440 
441 // -------------------------------------------------------------------------
442 
443 
444 ::com::sun::star::util::Time SAL_CALL ODatabaseMetaDataResultSet::getTime( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
445 {
446 
447 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
448 	::osl::MutexGuard aGuard( m_aMutex );
449 
450 
451 	columnIndex = mapColumn(columnIndex);
452 	TIME_STRUCT aTime={0,0,0};
453 	if(columnIndex <= m_nDriverColumnCount)
454 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,m_pConnection->useOldDateFormat() ? SQL_C_TIME : SQL_C_TYPE_TIME,m_bWasNull,**this,&aTime,sizeof aTime);
455 	else
456 		m_bWasNull = sal_True;
457 	return Time(0,aTime.second,aTime.minute,aTime.hour);
458 }
459 // -------------------------------------------------------------------------
460 
461 
462 ::com::sun::star::util::DateTime SAL_CALL ODatabaseMetaDataResultSet::getTimestamp( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
463 {
464 
465 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
466 	::osl::MutexGuard aGuard( m_aMutex );
467 
468 
469 	columnIndex = mapColumn(columnIndex);
470 	TIMESTAMP_STRUCT aTime={0,0,0,0,0,0,0};
471 	if(columnIndex <= m_nDriverColumnCount)
472 		OTools::getValue(m_pConnection,m_aStatementHandle,columnIndex,m_pConnection->useOldDateFormat() ? SQL_C_TIMESTAMP : SQL_C_TYPE_TIMESTAMP,m_bWasNull,**this,&aTime,sizeof aTime);
473 	else
474 		m_bWasNull = sal_True;
475 	return DateTime((sal_uInt16)aTime.fraction*1000,aTime.second,aTime.minute,aTime.hour,aTime.day,aTime.month,aTime.year);
476 }
477 // -------------------------------------------------------------------------
478 
479 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::isAfterLast(  ) throw(SQLException, RuntimeException)
480 {
481 
482 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
483 	::osl::MutexGuard aGuard( m_aMutex );
484 
485 
486 	return m_nCurrentFetchState == SQL_NO_DATA;
487 }
488 // -------------------------------------------------------------------------
489 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::isFirst(  ) throw(SQLException, RuntimeException)
490 {
491 
492 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
493 	::osl::MutexGuard aGuard( m_aMutex );
494 
495 
496 	return m_nRowPos == 1;
497 }
498 // -------------------------------------------------------------------------
499 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::isLast(  ) throw(SQLException, RuntimeException)
500 {
501 
502 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
503 	::osl::MutexGuard aGuard( m_aMutex );
504 
505 
506 	return m_bEOF && m_nCurrentFetchState != SQL_NO_DATA;
507 }
508 // -------------------------------------------------------------------------
509 void SAL_CALL ODatabaseMetaDataResultSet::beforeFirst(  ) throw(SQLException, RuntimeException)
510 {
511 
512 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
513 	::osl::MutexGuard aGuard( m_aMutex );
514 
515 
516 	if(first())
517 		previous();
518 	m_nCurrentFetchState = SQL_SUCCESS;
519 }
520 // -------------------------------------------------------------------------
521 void SAL_CALL ODatabaseMetaDataResultSet::afterLast(  ) throw(SQLException, RuntimeException)
522 {
523 
524 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
525 	::osl::MutexGuard aGuard( m_aMutex );
526 
527 
528 	if(last())
529 		next();
530 	m_bEOF = sal_True;
531 }
532 // -------------------------------------------------------------------------
533 
534 void SAL_CALL ODatabaseMetaDataResultSet::close(  ) throw(SQLException, RuntimeException)
535 {
536 	{
537 
538 		checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
539 		::osl::MutexGuard aGuard( m_aMutex );
540 
541 	}
542 	dispose();
543 }
544 // -------------------------------------------------------------------------
545 
546 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::first(  ) throw(SQLException, RuntimeException)
547 {
548 
549 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
550 	::osl::MutexGuard aGuard( m_aMutex );
551 
552 
553 	m_nCurrentFetchState = N3SQLFetchScroll(m_aStatementHandle,SQL_FETCH_FIRST,0);
554 	OTools::ThrowException(m_pConnection,m_nCurrentFetchState,m_aStatementHandle,SQL_HANDLE_STMT,*this);
555     sal_Bool bRet = ( m_nCurrentFetchState == SQL_SUCCESS || m_nCurrentFetchState == SQL_SUCCESS_WITH_INFO );
556 	if( bRet )
557 		m_nRowPos = 1;
558 	return bRet;
559 }
560 // -------------------------------------------------------------------------
561 
562 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::last(  ) throw(SQLException, RuntimeException)
563 {
564 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed );
565 	::osl::MutexGuard aGuard( m_aMutex );
566 
567 
568 	m_nCurrentFetchState = N3SQLFetchScroll(m_aStatementHandle,SQL_FETCH_LAST,0);
569 	OTools::ThrowException(m_pConnection,m_nCurrentFetchState,m_aStatementHandle,SQL_HANDLE_STMT,*this);
570 	// here I know definitely that I stand on the last record
571 	return (m_nCurrentFetchState == SQL_SUCCESS || m_nCurrentFetchState == SQL_SUCCESS_WITH_INFO);
572 }
573 // -------------------------------------------------------------------------
574 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::absolute( sal_Int32 row ) throw(SQLException, RuntimeException)
575 {
576 
577 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
578 	::osl::MutexGuard aGuard( m_aMutex );
579 
580 
581 	m_nCurrentFetchState = N3SQLFetchScroll(m_aStatementHandle,SQL_FETCH_ABSOLUTE,row);
582 	OTools::ThrowException(m_pConnection,m_nCurrentFetchState,m_aStatementHandle,SQL_HANDLE_STMT,*this);
583 	sal_Bool bRet = m_nCurrentFetchState == SQL_SUCCESS || m_nCurrentFetchState == SQL_SUCCESS_WITH_INFO;
584 	if(bRet)
585 		m_nRowPos = row;
586 	return bRet;
587 }
588 // -------------------------------------------------------------------------
589 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::relative( sal_Int32 row ) throw(SQLException, RuntimeException)
590 {
591 
592 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
593 	::osl::MutexGuard aGuard( m_aMutex );
594 
595 
596 	m_nCurrentFetchState = N3SQLFetchScroll(m_aStatementHandle,SQL_FETCH_RELATIVE,row);
597 	OTools::ThrowException(m_pConnection,m_nCurrentFetchState,m_aStatementHandle,SQL_HANDLE_STMT,*this);
598 	sal_Bool bRet = m_nCurrentFetchState == SQL_SUCCESS || m_nCurrentFetchState == SQL_SUCCESS_WITH_INFO;
599 	if(bRet)
600 		m_nRowPos += row;
601 	return bRet;
602 }
603 // -------------------------------------------------------------------------
604 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::previous(  ) throw(SQLException, RuntimeException)
605 {
606 
607 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
608 	::osl::MutexGuard aGuard( m_aMutex );
609 
610 
611 	m_nCurrentFetchState = N3SQLFetchScroll(m_aStatementHandle,SQL_FETCH_PRIOR,0);
612 	OTools::ThrowException(m_pConnection,m_nCurrentFetchState,m_aStatementHandle,SQL_HANDLE_STMT,*this);
613 	sal_Bool bRet = m_nCurrentFetchState == SQL_SUCCESS || m_nCurrentFetchState == SQL_SUCCESS_WITH_INFO;
614 	if(bRet)
615 		--m_nRowPos;
616 	return bRet;
617 }
618 // -------------------------------------------------------------------------
619 Reference< XInterface > SAL_CALL ODatabaseMetaDataResultSet::getStatement(  ) throw(SQLException, RuntimeException)
620 {
621 	return NULL;
622 }
623 // -------------------------------------------------------------------------
624 
625 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::rowDeleted(  ) throw(SQLException, RuntimeException)
626 {
627 
628 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
629 	::osl::MutexGuard aGuard( m_aMutex );
630 
631 
632 	return m_pRowStatusArray[0] == SQL_ROW_DELETED;
633 }
634 // -------------------------------------------------------------------------
635 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::rowInserted(  ) throw(SQLException, RuntimeException)
636 {
637 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
638 	::osl::MutexGuard aGuard( m_aMutex );
639 
640 
641 	return m_pRowStatusArray[0] == SQL_ROW_ADDED;
642 }
643 // -------------------------------------------------------------------------
644 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::rowUpdated(  ) throw(SQLException, RuntimeException)
645 {
646 
647 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
648 	::osl::MutexGuard aGuard( m_aMutex );
649 
650 
651 	return m_pRowStatusArray[0] == SQL_ROW_UPDATED;
652 }
653 // -------------------------------------------------------------------------
654 
655 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::isBeforeFirst(  ) throw(SQLException, RuntimeException)
656 {
657 
658 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
659 	::osl::MutexGuard aGuard( m_aMutex );
660 
661 
662 	return m_nRowPos == 0;
663 }
664 // -------------------------------------------------------------------------
665 
666 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::next(  ) throw(SQLException, RuntimeException)
667 {
668 
669 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
670 	::osl::MutexGuard aGuard( m_aMutex );
671 
672 
673 	//	m_nCurrentFetchState = N3SQLFetchScroll(m_aStatementHandle,SQL_FETCH_NEXT,0);
674 	m_nCurrentFetchState = N3SQLFetch(m_aStatementHandle);
675 	OTools::ThrowException(m_pConnection,m_nCurrentFetchState,m_aStatementHandle,SQL_HANDLE_STMT,*this);
676 	return m_nCurrentFetchState == SQL_SUCCESS || m_nCurrentFetchState == SQL_SUCCESS_WITH_INFO;
677 }
678 // -------------------------------------------------------------------------
679 
680 sal_Bool SAL_CALL ODatabaseMetaDataResultSet::wasNull(  ) throw(SQLException, RuntimeException)
681 {
682 
683 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
684 	::osl::MutexGuard aGuard( m_aMutex );
685 
686 
687 	return m_bWasNull;
688 }
689 // -------------------------------------------------------------------------
690 void SAL_CALL ODatabaseMetaDataResultSet::refreshRow(  ) throw(SQLException, RuntimeException)
691 {
692 
693 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
694 	::osl::MutexGuard aGuard( m_aMutex );
695 
696 }
697 // -------------------------------------------------------------------------
698 
699 void SAL_CALL ODatabaseMetaDataResultSet::cancel(  ) throw(RuntimeException)
700 {
701 
702 	checkDisposed(ODatabaseMetaDataResultSet_BASE::rBHelper.bDisposed);
703 	::osl::MutexGuard aGuard( m_aMutex );
704 
705 
706 	OTools::ThrowException(m_pConnection,N3SQLCancel(m_aStatementHandle),m_aStatementHandle,SQL_HANDLE_STMT,*this);
707 }
708 // -------------------------------------------------------------------------
709 void SAL_CALL ODatabaseMetaDataResultSet::clearWarnings(  ) throw(SQLException, RuntimeException)
710 {
711 }
712 // -------------------------------------------------------------------------
713 Any SAL_CALL ODatabaseMetaDataResultSet::getWarnings(  ) throw(SQLException, RuntimeException)
714 {
715 	return Any();
716 }
717 //------------------------------------------------------------------------------
718 sal_Int32 ODatabaseMetaDataResultSet::getResultSetConcurrency() const throw(SQLException, RuntimeException)
719 {
720 	return ResultSetConcurrency::READ_ONLY;
721 }
722 //------------------------------------------------------------------------------
723 sal_Int32 ODatabaseMetaDataResultSet::getResultSetType() const throw(SQLException, RuntimeException)
724 {
725 	return ResultSetType::FORWARD_ONLY;
726 }
727 //------------------------------------------------------------------------------
728 sal_Int32 ODatabaseMetaDataResultSet::getFetchDirection() const throw(SQLException, RuntimeException)
729 {
730 	return FetchDirection::FORWARD;
731 }
732 //------------------------------------------------------------------------------
733 sal_Int32 ODatabaseMetaDataResultSet::getFetchSize() const throw(SQLException, RuntimeException)
734 {
735 	sal_Int32 nValue=1;
736 	return nValue;
737 }
738 //------------------------------------------------------------------------------
739 ::rtl::OUString ODatabaseMetaDataResultSet::getCursorName() const throw(SQLException, RuntimeException)
740 {
741 	return ::rtl::OUString();
742 }
743 
744 // -------------------------------------------------------------------------
745 ::cppu::IPropertyArrayHelper* ODatabaseMetaDataResultSet::createArrayHelper( ) const
746 {
747 
748 	Sequence< com::sun::star::beans::Property > aProps(5);
749 	com::sun::star::beans::Property* pProperties = aProps.getArray();
750 	sal_Int32 nPos = 0;
751 	DECL_PROP0(CURSORNAME,			::rtl::OUString);
752 	DECL_PROP0(FETCHDIRECTION,		sal_Int32);
753 	DECL_PROP0(FETCHSIZE,			sal_Int32);
754 	DECL_PROP0(RESULTSETCONCURRENCY,sal_Int32);
755 	DECL_PROP0(RESULTSETTYPE,		sal_Int32);
756 
757 	return new ::cppu::OPropertyArrayHelper(aProps);
758 }
759 // -------------------------------------------------------------------------
760 ::cppu::IPropertyArrayHelper & ODatabaseMetaDataResultSet::getInfoHelper()
761 {
762 	return *const_cast<ODatabaseMetaDataResultSet*>(this)->getArrayHelper();
763 }
764 // -------------------------------------------------------------------------
765 sal_Bool ODatabaseMetaDataResultSet::convertFastPropertyValue(
766 							Any & rConvertedValue,
767 							Any & rOldValue,
768 							sal_Int32 nHandle,
769 							const Any& rValue )
770 								throw (::com::sun::star::lang::IllegalArgumentException)
771 {
772 	switch(nHandle)
773 	{
774 		case PROPERTY_ID_CURSORNAME:
775 		case PROPERTY_ID_RESULTSETCONCURRENCY:
776 		case PROPERTY_ID_RESULTSETTYPE:
777 			throw ::com::sun::star::lang::IllegalArgumentException();
778 		case PROPERTY_ID_FETCHDIRECTION:
779 			return ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getFetchDirection());
780 		case PROPERTY_ID_FETCHSIZE:
781 			return ::comphelper::tryPropertyValue(rConvertedValue, rOldValue, rValue, getFetchSize());
782 		default:
783 			;
784 	}
785 	return sal_False;
786 }
787 // -------------------------------------------------------------------------
788 void ODatabaseMetaDataResultSet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& /*rValue*/ ) throw (Exception)
789 {
790 	switch(nHandle)
791 	{
792 		case PROPERTY_ID_CURSORNAME:
793 		case PROPERTY_ID_RESULTSETCONCURRENCY:
794 		case PROPERTY_ID_RESULTSETTYPE:
795 		case PROPERTY_ID_FETCHDIRECTION:
796 		case PROPERTY_ID_FETCHSIZE:
797 			throw Exception();
798 		default:
799 			OSL_ENSURE(0,"setFastPropertyValue_NoBroadcast: Illegal handle value!");
800 	}
801 }
802 // -------------------------------------------------------------------------
803 void ODatabaseMetaDataResultSet::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
804 {
805 	switch(nHandle)
806 	{
807 		case PROPERTY_ID_CURSORNAME:
808 			rValue <<= getCursorName();
809 			break;
810 		case PROPERTY_ID_RESULTSETCONCURRENCY:
811 			rValue <<= getResultSetConcurrency();
812 			break;
813 		case PROPERTY_ID_RESULTSETTYPE:
814 			rValue <<= getResultSetType();
815 			break;
816 		case PROPERTY_ID_FETCHDIRECTION:
817 			rValue <<= getFetchDirection();
818 			break;
819 		case PROPERTY_ID_FETCHSIZE:
820 			rValue <<= getFetchSize();
821 			break;
822 	}
823 }
824 // -------------------------------------------------------------------------
825 void ODatabaseMetaDataResultSet::openTypeInfo() throw(SQLException, RuntimeException)
826 {
827 	TInt2IntMap aMap;
828 	aMap[SQL_BIT]				= DataType::BIT;
829 	aMap[SQL_TINYINT]			= DataType::TINYINT;
830 	aMap[SQL_SMALLINT]			= DataType::SMALLINT;
831 	aMap[SQL_INTEGER]			= DataType::INTEGER;
832 	aMap[SQL_FLOAT]				= DataType::FLOAT;
833 	aMap[SQL_REAL]				= DataType::REAL;
834 	aMap[SQL_DOUBLE]			= DataType::DOUBLE;
835 	aMap[SQL_BIGINT]			= DataType::BIGINT;
836 
837 	aMap[SQL_CHAR]				= DataType::CHAR;
838 	aMap[SQL_WCHAR]				= DataType::CHAR;
839 	aMap[SQL_VARCHAR]			= DataType::VARCHAR;
840 	aMap[SQL_WVARCHAR]			= DataType::VARCHAR;
841 	aMap[SQL_LONGVARCHAR]		= DataType::LONGVARCHAR;
842 	aMap[SQL_WLONGVARCHAR]		= DataType::LONGVARCHAR;
843 
844 	aMap[SQL_TYPE_DATE]			= DataType::DATE;
845 	aMap[SQL_DATE]				= DataType::DATE;
846 	aMap[SQL_TYPE_TIME]			= DataType::TIME;
847 	aMap[SQL_TIME]				= DataType::TIME;
848 	aMap[SQL_TYPE_TIMESTAMP]	= DataType::TIMESTAMP;
849 	aMap[SQL_TIMESTAMP]			= DataType::TIMESTAMP;
850 
851 	aMap[SQL_DECIMAL]			= DataType::DECIMAL;
852 	aMap[SQL_NUMERIC]			= DataType::NUMERIC;
853 
854 	aMap[SQL_BINARY]			= DataType::BINARY;
855 	aMap[SQL_VARBINARY]			= DataType::VARBINARY;
856 	aMap[SQL_LONGVARBINARY]		= DataType::LONGVARBINARY;
857 
858 	aMap[SQL_GUID]				= DataType::VARBINARY;
859 
860 
861 	m_aValueRange[2] = aMap;
862 
863 	OTools::ThrowException(m_pConnection,N3SQLGetTypeInfo(m_aStatementHandle, SQL_ALL_TYPES),m_aStatementHandle,SQL_HANDLE_STMT,*this);
864 	checkColumnCount();
865 }
866 //-----------------------------------------------------------------------------
867 void ODatabaseMetaDataResultSet::openTables(const Any& catalog, const ::rtl::OUString& schemaPattern,
868 							const ::rtl::OUString& tableNamePattern,
869 							const Sequence< ::rtl::OUString >& types )	throw(SQLException, RuntimeException)
870 {
871 	m_bFreeHandle = sal_True;
872 	::rtl::OString aPKQ,aPKO,aPKN,aCOL;
873 	const ::rtl::OUString *pSchemaPat = NULL;
874 
875 	if(schemaPattern.toChar() != '%')
876 		pSchemaPat = &schemaPattern;
877 	else
878 		pSchemaPat = NULL;
879 
880     if ( catalog.hasValue() )
881 	    aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
882 	aPKO = ::rtl::OUStringToOString(schemaPattern,m_nTextEncoding);
883 
884 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
885 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
886 				*pPKN = aPKN = ::rtl::OUStringToOString(tableNamePattern,m_nTextEncoding).getStr();
887 
888 
889 	const char	*pCOL = NULL;
890 	const char* pComma = ",";
891 	const ::rtl::OUString* pBegin = types.getConstArray();
892 	const ::rtl::OUString* pEnd = pBegin + types.getLength();
893 	for(;pBegin != pEnd;++pBegin)
894 	{
895 		aCOL += ::rtl::OUStringToOString(*pBegin,m_nTextEncoding);
896 		aCOL += pComma;
897 	}
898 	if ( aCOL.getLength() )
899 	{
900 		aCOL = aCOL.replaceAt(aCOL.getLength()-1,1,pComma);
901 		pCOL = aCOL.getStr();
902 	}
903 	else
904 		pCOL = SQL_ALL_TABLE_TYPES;
905 
906 	SQLRETURN nRetcode = N3SQLTables(m_aStatementHandle,
907 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
908 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0,
909 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS,
910 							(SDB_ODBC_CHAR *) pCOL, pCOL ? SQL_NTS : 0);
911 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
912 	checkColumnCount();
913 
914 }
915 //-----------------------------------------------------------------------------
916 void ODatabaseMetaDataResultSet::openTablesTypes( )	throw(SQLException, RuntimeException)
917 {
918 	m_bFreeHandle = sal_True;
919 	SQLRETURN nRetcode = N3SQLTables(m_aStatementHandle,
920 							0,0,
921 							0,0,
922 							0,0,
923 							(SDB_ODBC_CHAR *) SQL_ALL_TABLE_TYPES,SQL_NTS);
924 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
925 
926 	m_aColMapping.clear();
927 	m_aColMapping.push_back(-1);
928 	m_aColMapping.push_back(4);
929 	m_xMetaData = new OResultSetMetaData(m_pConnection,m_aStatementHandle,m_aColMapping);
930 	checkColumnCount();
931 }
932 // -------------------------------------------------------------------------
933 void ODatabaseMetaDataResultSet::openCatalogs() throw(SQLException, RuntimeException)
934 {
935 	m_bFreeHandle = sal_True;
936 	SQLRETURN nRetcode = N3SQLTables(m_aStatementHandle,
937 							(SDB_ODBC_CHAR *) SQL_ALL_CATALOGS,SQL_NTS,
938 							(SDB_ODBC_CHAR *) "",SQL_NTS,
939 							(SDB_ODBC_CHAR *) "",SQL_NTS,
940 							(SDB_ODBC_CHAR *) "",SQL_NTS);
941 
942 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
943 
944 	m_aColMapping.clear();
945 	m_aColMapping.push_back(-1);
946 	m_aColMapping.push_back(1);
947 	m_xMetaData = new OResultSetMetaData(m_pConnection,m_aStatementHandle,m_aColMapping);
948 	checkColumnCount();
949 }
950 // -------------------------------------------------------------------------
951 void ODatabaseMetaDataResultSet::openSchemas() throw(SQLException, RuntimeException)
952 {
953 	m_bFreeHandle = sal_True;
954 	SQLRETURN nRetcode = N3SQLTables(m_aStatementHandle,
955 							(SDB_ODBC_CHAR *) "",SQL_NTS,
956 							(SDB_ODBC_CHAR *) SQL_ALL_SCHEMAS,SQL_NTS,
957 							(SDB_ODBC_CHAR *) "",SQL_NTS,
958 							(SDB_ODBC_CHAR *) "",SQL_NTS);
959 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
960 
961 	m_aColMapping.clear();
962 	m_aColMapping.push_back(-1);
963 	m_aColMapping.push_back(2);
964 	m_xMetaData = new OResultSetMetaData(m_pConnection,m_aStatementHandle,m_aColMapping);
965 	checkColumnCount();
966 }
967 // -------------------------------------------------------------------------
968 void ODatabaseMetaDataResultSet::openColumnPrivileges(	const Any& catalog,	const ::rtl::OUString& schema,
969 										const ::rtl::OUString& table,	const ::rtl::OUString& columnNamePattern )
970 										throw(SQLException, RuntimeException)
971 {
972 	const ::rtl::OUString *pSchemaPat = NULL;
973 
974 	if(schema.toChar() != '%')
975 		pSchemaPat = &schema;
976 	else
977 		pSchemaPat = NULL;
978 
979 	m_bFreeHandle = sal_True;
980 	::rtl::OString aPKQ,aPKO,aPKN,aCOL;
981 
982 	if ( catalog.hasValue() )
983 		aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
984 	aPKO = ::rtl::OUStringToOString(schema,m_nTextEncoding);
985 
986 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
987 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
988 				*pPKN = aPKN = ::rtl::OUStringToOString(table,m_nTextEncoding).getStr(),
989 				*pCOL = aCOL = ::rtl::OUStringToOString(columnNamePattern,m_nTextEncoding).getStr();
990 
991 
992 	SQLRETURN nRetcode = N3SQLColumnPrivileges(m_aStatementHandle,
993 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
994 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0 ,
995 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS,
996 							(SDB_ODBC_CHAR *) pCOL, SQL_NTS);
997 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
998 
999 	checkColumnCount();
1000 }
1001 // -------------------------------------------------------------------------
1002 void ODatabaseMetaDataResultSet::openColumns(	const Any& catalog,				const ::rtl::OUString& schemaPattern,
1003 								const ::rtl::OUString& tableNamePattern,	const ::rtl::OUString& columnNamePattern )
1004 								throw(SQLException, RuntimeException)
1005 {
1006 	const ::rtl::OUString *pSchemaPat = NULL;
1007 
1008 	if(schemaPattern.toChar() != '%')
1009 		pSchemaPat = &schemaPattern;
1010 	else
1011 		pSchemaPat = NULL;
1012 
1013 	m_bFreeHandle = sal_True;
1014 	::rtl::OString aPKQ,aPKO,aPKN,aCOL;
1015 	if ( catalog.hasValue() )
1016 		aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1017 	aPKO = ::rtl::OUStringToOString(schemaPattern,m_nTextEncoding);
1018 
1019 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1020 				*pPKO = pSchemaPat && pSchemaPat->getLength() && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
1021 				*pPKN = aPKN = ::rtl::OUStringToOString(tableNamePattern,m_nTextEncoding).getStr(),
1022 				*pCOL = aCOL = ::rtl::OUStringToOString(columnNamePattern,m_nTextEncoding).getStr();
1023 
1024 
1025 	SQLRETURN nRetcode = N3SQLColumns(m_aStatementHandle,
1026 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1027 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0,
1028 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS,
1029 							(SDB_ODBC_CHAR *) pCOL, SQL_NTS);
1030 
1031 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1032 	TInt2IntMap aMap;
1033 	aMap[SQL_BIT]				= DataType::BIT;
1034 	aMap[SQL_TINYINT]			= DataType::TINYINT;
1035 	aMap[SQL_SMALLINT]			= DataType::SMALLINT;
1036 	aMap[SQL_INTEGER]			= DataType::INTEGER;
1037 	aMap[SQL_FLOAT]				= DataType::FLOAT;
1038 	aMap[SQL_REAL]				= DataType::REAL;
1039 	aMap[SQL_DOUBLE]			= DataType::DOUBLE;
1040 	aMap[SQL_BIGINT]			= DataType::BIGINT;
1041 
1042 	aMap[SQL_CHAR]				= DataType::CHAR;
1043 	aMap[SQL_WCHAR]				= DataType::CHAR;
1044 	aMap[SQL_VARCHAR]			= DataType::VARCHAR;
1045 	aMap[SQL_WVARCHAR]			= DataType::VARCHAR;
1046 	aMap[SQL_LONGVARCHAR]		= DataType::LONGVARCHAR;
1047 	aMap[SQL_WLONGVARCHAR]		= DataType::LONGVARCHAR;
1048 
1049 	aMap[SQL_TYPE_DATE]			= DataType::DATE;
1050 	aMap[SQL_DATE]				= DataType::DATE;
1051 	aMap[SQL_TYPE_TIME]			= DataType::TIME;
1052 	aMap[SQL_TIME]				= DataType::TIME;
1053 	aMap[SQL_TYPE_TIMESTAMP]	= DataType::TIMESTAMP;
1054 	aMap[SQL_TIMESTAMP]			= DataType::TIMESTAMP;
1055 
1056 	aMap[SQL_DECIMAL]			= DataType::DECIMAL;
1057 	aMap[SQL_NUMERIC]			= DataType::NUMERIC;
1058 
1059 	aMap[SQL_BINARY]			= DataType::BINARY;
1060 	aMap[SQL_VARBINARY]			= DataType::VARBINARY;
1061 	aMap[SQL_LONGVARBINARY]		= DataType::LONGVARBINARY;
1062 
1063 	aMap[SQL_GUID]				= DataType::VARBINARY;
1064 
1065 	m_aValueRange[5] = aMap;
1066 	checkColumnCount();
1067 }
1068 // -------------------------------------------------------------------------
1069 void ODatabaseMetaDataResultSet::openProcedureColumns(	const Any& catalog,		const ::rtl::OUString& schemaPattern,
1070 								const ::rtl::OUString& procedureNamePattern,const ::rtl::OUString& columnNamePattern )
1071 								throw(SQLException, RuntimeException)
1072 {
1073 	const ::rtl::OUString *pSchemaPat = NULL;
1074 
1075 	if(schemaPattern.toChar() != '%')
1076 		pSchemaPat = &schemaPattern;
1077 	else
1078 		pSchemaPat = NULL;
1079 
1080 	m_bFreeHandle = sal_True;
1081 	::rtl::OString aPKQ,aPKO,aPKN,aCOL;
1082 	if ( catalog.hasValue() )
1083 		aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1084 	aPKO = ::rtl::OUStringToOString(schemaPattern,m_nTextEncoding);
1085 
1086 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1087 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
1088 				*pPKN = aPKN = ::rtl::OUStringToOString(procedureNamePattern,m_nTextEncoding).getStr(),
1089 				*pCOL = aCOL = ::rtl::OUStringToOString(columnNamePattern,m_nTextEncoding).getStr();
1090 
1091 
1092 	SQLRETURN nRetcode = N3SQLProcedureColumns(m_aStatementHandle,
1093 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1094 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0 ,
1095 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS,
1096 							(SDB_ODBC_CHAR *) pCOL, SQL_NTS);
1097 
1098 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1099 	checkColumnCount();
1100 }
1101 // -------------------------------------------------------------------------
1102 void ODatabaseMetaDataResultSet::openProcedures(const Any& catalog, const ::rtl::OUString& schemaPattern,
1103 								const ::rtl::OUString& procedureNamePattern)
1104 								throw(SQLException, RuntimeException)
1105 {
1106 	const ::rtl::OUString *pSchemaPat = NULL;
1107 
1108 	if(schemaPattern.toChar() != '%')
1109 		pSchemaPat = &schemaPattern;
1110 	else
1111 		pSchemaPat = NULL;
1112 
1113 	m_bFreeHandle = sal_True;
1114 	::rtl::OString aPKQ,aPKO,aPKN,aCOL;
1115 
1116 	if ( catalog.hasValue() )
1117 		aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1118 	aPKO = ::rtl::OUStringToOString(schemaPattern,m_nTextEncoding);
1119 
1120 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1121 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
1122 				*pPKN = aPKN = ::rtl::OUStringToOString(procedureNamePattern,m_nTextEncoding).getStr();
1123 
1124 
1125 	SQLRETURN nRetcode = N3SQLProcedures(m_aStatementHandle,
1126 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1127 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0 ,
1128 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS);
1129 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1130 	checkColumnCount();
1131 }
1132 // -------------------------------------------------------------------------
1133 void ODatabaseMetaDataResultSet::openSpecialColumns(sal_Bool _bRowVer,const Any& catalog, const ::rtl::OUString& schema,
1134 									const ::rtl::OUString& table,sal_Int32 scope, 	sal_Bool nullable )
1135 									throw(SQLException, RuntimeException)
1136 {
1137 	const ::rtl::OUString *pSchemaPat = NULL;
1138 
1139 	if(schema.toChar() != '%')
1140 		pSchemaPat = &schema;
1141 	else
1142 		pSchemaPat = NULL;
1143 
1144 	m_bFreeHandle = sal_True;
1145 	::rtl::OString aPKQ,aPKO,aPKN,aCOL;
1146 	if ( catalog.hasValue() )
1147 	aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1148 	aPKO = ::rtl::OUStringToOString(schema,m_nTextEncoding);
1149 
1150 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1151 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
1152 				*pPKN = aPKN = ::rtl::OUStringToOString(table,m_nTextEncoding).getStr();
1153 
1154 
1155 	SQLRETURN nRetcode = N3SQLSpecialColumns(m_aStatementHandle,_bRowVer ? SQL_ROWVER : SQL_BEST_ROWID,
1156 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1157 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0 ,
1158 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS,
1159 							(SQLSMALLINT)scope,
1160 							nullable ? SQL_NULLABLE : SQL_NO_NULLS);
1161 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1162 	checkColumnCount();
1163 }
1164 // -------------------------------------------------------------------------
1165 void ODatabaseMetaDataResultSet::openVersionColumns(const Any& catalog, const ::rtl::OUString& schema,
1166 									const ::rtl::OUString& table)  throw(SQLException, RuntimeException)
1167 {
1168 	openSpecialColumns(sal_True,catalog,schema,table,SQL_SCOPE_TRANSACTION,sal_False);
1169 }
1170 // -------------------------------------------------------------------------
1171 void ODatabaseMetaDataResultSet::openBestRowIdentifier( const Any& catalog, const ::rtl::OUString& schema,
1172 										const ::rtl::OUString& table,sal_Int32 scope,sal_Bool nullable ) throw(SQLException, RuntimeException)
1173 {
1174 	openSpecialColumns(sal_False,catalog,schema,table,scope,nullable);
1175 }
1176 // -------------------------------------------------------------------------
1177 void ODatabaseMetaDataResultSet::openForeignKeys( const Any& catalog, const ::rtl::OUString* schema,
1178 								  const ::rtl::OUString* table,
1179 								  const Any& catalog2, const ::rtl::OUString* schema2,
1180 								  const ::rtl::OUString* table2) throw(SQLException, RuntimeException)
1181 {
1182 	m_bFreeHandle = sal_True;
1183 
1184 	::rtl::OString aPKQ,aPKO,aPKN, aFKQ, aFKO, aFKN;
1185     if ( catalog.hasValue() )
1186 	    aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1187     if ( catalog2.hasValue() )
1188 	    aFKQ = ::rtl::OUStringToOString(comphelper::getString(catalog2),m_nTextEncoding);
1189 
1190 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1191 				*pPKO = schema && schema->getLength() ? ::rtl::OUStringToOString(*schema,m_nTextEncoding).getStr() : NULL,
1192 				*pPKN = table   ? (aPKN = ::rtl::OUStringToOString(*table,m_nTextEncoding)).getStr(): NULL,
1193 				*pFKQ = catalog2.hasValue() && aFKQ.getLength() ? aFKQ.getStr()	: NULL,
1194 				*pFKO = schema2 && schema2->getLength() ? (aFKO = ::rtl::OUStringToOString(*schema2,m_nTextEncoding)).getStr() : NULL,
1195 				*pFKN = table2  ? (aFKN = ::rtl::OUStringToOString(*table2,m_nTextEncoding)).getStr() : NULL;
1196 
1197 
1198 	SQLRETURN nRetcode = N3SQLForeignKeys(m_aStatementHandle,
1199 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1200 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0,
1201 							(SDB_ODBC_CHAR *) pPKN, pPKN ? SQL_NTS : 0,
1202 							(SDB_ODBC_CHAR *) pFKQ, (catalog2.hasValue() && aFKQ.getLength()) ? SQL_NTS : 0,
1203 							(SDB_ODBC_CHAR *) pFKO, pFKO ? SQL_NTS : 0,
1204 							(SDB_ODBC_CHAR *) pFKN, SQL_NTS
1205 							);
1206 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1207 	checkColumnCount();
1208 }
1209 // -------------------------------------------------------------------------
1210 void ODatabaseMetaDataResultSet::openImportedKeys(const Any& catalog, const ::rtl::OUString& schema,
1211 								  const ::rtl::OUString& table)	throw(SQLException, RuntimeException)
1212 {
1213 
1214 	openForeignKeys(Any(),NULL,NULL,catalog,!schema.compareToAscii("%") ? &schema : NULL,&table);
1215 }
1216 // -------------------------------------------------------------------------
1217 void ODatabaseMetaDataResultSet::openExportedKeys(const Any& catalog, const ::rtl::OUString& schema,
1218 								  const ::rtl::OUString& table)	throw(SQLException, RuntimeException)
1219 {
1220 	openForeignKeys(catalog,!schema.compareToAscii("%") ? &schema : NULL,&table,Any(),NULL,NULL);
1221 }
1222 // -------------------------------------------------------------------------
1223 void ODatabaseMetaDataResultSet::openPrimaryKeys(const Any& catalog, const ::rtl::OUString& schema,
1224 								  const ::rtl::OUString& table)	throw(SQLException, RuntimeException)
1225 {
1226 	const ::rtl::OUString *pSchemaPat = NULL;
1227 
1228 	if(schema.toChar() != '%')
1229 		pSchemaPat = &schema;
1230 	else
1231 		pSchemaPat = NULL;
1232 
1233 	m_bFreeHandle = sal_True;
1234 	::rtl::OString aPKQ,aPKO,aPKN,aCOL;
1235 
1236 	if ( catalog.hasValue() )
1237 		aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1238 	aPKO = ::rtl::OUStringToOString(schema,m_nTextEncoding);
1239 
1240 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1241 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
1242 				*pPKN = (aPKN = ::rtl::OUStringToOString(table,m_nTextEncoding)).getStr();
1243 
1244 
1245 	SQLRETURN nRetcode = N3SQLPrimaryKeys(m_aStatementHandle,
1246 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1247 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0 ,
1248 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS);
1249 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1250 	checkColumnCount();
1251 }
1252 // -------------------------------------------------------------------------
1253 void ODatabaseMetaDataResultSet::openTablePrivileges(const Any& catalog, const ::rtl::OUString& schemaPattern,
1254 								  const ::rtl::OUString& tableNamePattern) throw(SQLException, RuntimeException)
1255 {
1256 	const ::rtl::OUString *pSchemaPat = NULL;
1257 
1258 	if(schemaPattern.toChar() != '%')
1259 		pSchemaPat = &schemaPattern;
1260 	else
1261 		pSchemaPat = NULL;
1262 
1263 	m_bFreeHandle = sal_True;
1264 	::rtl::OString aPKQ,aPKO,aPKN;
1265 
1266 	if ( catalog.hasValue() )
1267 		aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1268 	aPKO = ::rtl::OUStringToOString(schemaPattern,m_nTextEncoding);
1269 
1270 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1271 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
1272 				*pPKN = (aPKN = ::rtl::OUStringToOString(tableNamePattern,m_nTextEncoding)).getStr();
1273 
1274 
1275 	SQLRETURN nRetcode = N3SQLTablePrivileges(m_aStatementHandle,
1276 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1277 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0 ,
1278 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS);
1279 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1280 	checkColumnCount();
1281 }
1282 // -------------------------------------------------------------------------
1283 void ODatabaseMetaDataResultSet::openIndexInfo( const Any& catalog, const ::rtl::OUString& schema,
1284 								const ::rtl::OUString& table,sal_Bool unique,sal_Bool approximate )
1285 								throw(SQLException, RuntimeException)
1286 {
1287 	const ::rtl::OUString *pSchemaPat = NULL;
1288 
1289 	if(schema.toChar() != '%')
1290 		pSchemaPat = &schema;
1291 	else
1292 		pSchemaPat = NULL;
1293 
1294 	m_bFreeHandle = sal_True;
1295 	::rtl::OString aPKQ,aPKO,aPKN;
1296 
1297 	if ( catalog.hasValue() )
1298 		aPKQ = ::rtl::OUStringToOString(comphelper::getString(catalog),m_nTextEncoding);
1299 	aPKO = ::rtl::OUStringToOString(schema,m_nTextEncoding);
1300 
1301 	const char	*pPKQ = catalog.hasValue() && aPKQ.getLength() ? aPKQ.getStr()	: NULL,
1302 				*pPKO = pSchemaPat && pSchemaPat->getLength() ? aPKO.getStr() : NULL,
1303 				*pPKN = (aPKN = ::rtl::OUStringToOString(table,m_nTextEncoding)).getStr();
1304 
1305 
1306 	SQLRETURN nRetcode = N3SQLStatistics(m_aStatementHandle,
1307 							(SDB_ODBC_CHAR *) pPKQ, (catalog.hasValue() && aPKQ.getLength()) ? SQL_NTS : 0,
1308 							(SDB_ODBC_CHAR *) pPKO, pPKO ? SQL_NTS : 0 ,
1309 							(SDB_ODBC_CHAR *) pPKN, SQL_NTS,
1310 							unique ? SQL_INDEX_UNIQUE : SQL_INDEX_ALL,
1311 							approximate);
1312 	OTools::ThrowException(m_pConnection,nRetcode,m_aStatementHandle,SQL_HANDLE_STMT,*this);
1313 	checkColumnCount();
1314 }
1315 // -------------------------------------------------------------------------
1316 void ODatabaseMetaDataResultSet::checkColumnCount()
1317 {
1318 	sal_Int16 nNumResultCols=0;
1319 	OTools::ThrowException(m_pConnection,N3SQLNumResultCols(m_aStatementHandle,&nNumResultCols),m_aStatementHandle,SQL_HANDLE_STMT,*this);
1320 	m_nDriverColumnCount = nNumResultCols;
1321 }
1322 // -----------------------------------------------------------------------------
1323 
1324 SWORD ODatabaseMetaDataResultSet::impl_getColumnType_nothrow(sal_Int32 columnIndex)
1325 {
1326     ::std::map<sal_Int32,SWORD>::iterator aFind = m_aODBCColumnTypes.find(columnIndex);
1327     if ( aFind == m_aODBCColumnTypes.end() )
1328         aFind = m_aODBCColumnTypes.insert(::std::map<sal_Int32,SWORD>::value_type(columnIndex,OResultSetMetaData::getColumnODBCType(m_pConnection,m_aStatementHandle,*this,columnIndex))).first;
1329     return aFind->second;
1330 }
1331 
1332