update emoji autocorrect entries from po-files
[LibreOffice.git] / connectivity / source / drivers / odbc / OTools.cxx
blobd7ea63e939a1814fb942b862809fec66d72405b3
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/OTools.hxx"
21 #include "odbc/OFunctions.hxx"
22 #include <com/sun/star/sdbc/DataType.hpp>
23 #include <osl/diagnose.h>
24 #include "odbc/OConnection.hxx"
25 #include "diagnose_ex.h"
26 #include <rtl/ustrbuf.hxx>
28 #include <appendsqlwchars.hxx>
30 #include <string.h>
31 #include <string>
32 #include <algorithm>
34 using namespace connectivity::odbc;
35 using namespace com::sun::star::uno;
36 using namespace com::sun::star::sdbc;
37 using namespace com::sun::star::util;
39 namespace {
40 size_t sqlTypeLen ( SQLSMALLINT _nType )
42 switch (_nType)
44 case SQL_C_SSHORT:
45 case SQL_C_SHORT:
46 return sizeof(SQLSMALLINT);
47 case SQL_C_USHORT:
48 return sizeof(SQLUSMALLINT);
49 case SQL_C_SLONG:
50 case SQL_C_LONG:
51 return sizeof(SQLINTEGER);
52 case SQL_C_ULONG:
53 return sizeof(SQLUINTEGER);
54 case SQL_C_FLOAT:
55 return sizeof(SQLREAL);
56 case SQL_C_DOUBLE:
57 OSL_ENSURE(sizeof(SQLDOUBLE) == sizeof(SQLFLOAT), "SQLDOUBLE/SQLFLOAT confusion");
58 return sizeof(SQLDOUBLE);
59 case SQL_C_BIT:
60 return sizeof(SQLCHAR);
61 case SQL_C_STINYINT:
62 case SQL_C_TINYINT:
63 return sizeof(SQLSCHAR);
64 case SQL_C_UTINYINT:
65 return sizeof(SQLCHAR);
66 case SQL_C_SBIGINT:
67 return sizeof(SQLBIGINT);
68 case SQL_C_UBIGINT:
69 return sizeof(SQLUBIGINT);
70 /* UnixODBC gives this the same value as SQL_C_UBIGINT
71 case SQL_C_BOOKMARK:
72 return sizeof(BOOKMARK); */
73 case SQL_C_TYPE_DATE:
74 case SQL_C_DATE:
75 return sizeof(SQL_DATE_STRUCT);
76 case SQL_C_TYPE_TIME:
77 case SQL_C_TIME:
78 return sizeof(SQL_TIME_STRUCT);
79 case SQL_C_TYPE_TIMESTAMP:
80 case SQL_C_TIMESTAMP:
81 return sizeof(SQL_TIMESTAMP_STRUCT);
82 case SQL_C_NUMERIC:
83 return sizeof(SQL_NUMERIC_STRUCT);
84 case SQL_C_GUID:
85 return sizeof(SQLGUID);
86 case SQL_C_INTERVAL_YEAR:
87 case SQL_C_INTERVAL_MONTH:
88 case SQL_C_INTERVAL_DAY:
89 case SQL_C_INTERVAL_HOUR:
90 case SQL_C_INTERVAL_MINUTE:
91 case SQL_C_INTERVAL_SECOND:
92 case SQL_C_INTERVAL_YEAR_TO_MONTH:
93 case SQL_C_INTERVAL_DAY_TO_HOUR:
94 case SQL_C_INTERVAL_DAY_TO_MINUTE:
95 case SQL_C_INTERVAL_DAY_TO_SECOND:
96 case SQL_C_INTERVAL_HOUR_TO_MINUTE:
97 case SQL_C_INTERVAL_HOUR_TO_SECOND:
98 case SQL_C_INTERVAL_MINUTE_TO_SECOND:
99 return sizeof(SQL_INTERVAL_STRUCT);
100 // ** Variable-sized datatypes -> cannot predict length
101 case SQL_C_CHAR:
102 case SQL_C_WCHAR:
103 case SQL_C_BINARY:
104 // UnixODBC gives this the same value as SQL_C_BINARY
105 //case SQL_C_VARBOOKMARK:
106 // Unknown datatype -> cannot predict length
107 default:
108 return static_cast<size_t>(-1);
114 void OTools::getValue( OConnection* _pConnection,
115 SQLHANDLE _aStatementHandle,
116 sal_Int32 columnIndex,
117 SQLSMALLINT _nType,
118 bool &_bWasNull,
119 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
120 void* _pValue,
121 SQLLEN _nSize) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
123 const size_t properSize = sqlTypeLen(_nType);
124 if ( properSize == static_cast<size_t>(-1) )
125 SAL_WARN( "connectivity.drivers", "connectivity::odbc::OTools::getValue: unknown SQL type - cannot check buffer size");
126 else
128 OSL_ENSURE(static_cast<size_t>(_nSize) == properSize, "connectivity::odbc::OTools::getValue got wrongly sized memory region to write result to");
129 if ( static_cast<size_t>(_nSize) > properSize )
131 SAL_WARN( "connectivity.drivers", "memory region is too big - trying to fudge it");
132 memset(_pValue, 0, _nSize);
133 #ifdef OSL_BIGENDIAN
134 // This is skewed in favour of integer types
135 _pValue += _nSize - properSize;
136 #endif
139 OSL_ENSURE(static_cast<size_t>(_nSize) >= properSize, "memory region is too small");
140 SQLLEN pcbValue = SQL_NULL_DATA;
141 OTools::ThrowException(_pConnection,
142 (*reinterpret_cast<T3SQLGetData>(_pConnection->getOdbcFunction(ODBC3SQLGetData)))(_aStatementHandle,
143 (SQLUSMALLINT)columnIndex,
144 _nType,
145 _pValue,
146 _nSize,
147 &pcbValue),
148 _aStatementHandle,SQL_HANDLE_STMT,_xInterface,false);
149 _bWasNull = pcbValue == SQL_NULL_DATA;
152 void OTools::bindValue( OConnection* _pConnection,
153 SQLHANDLE _aStatementHandle,
154 sal_Int32 columnIndex,
155 SQLSMALLINT _nType,
156 SQLSMALLINT _nMaxLen,
157 const void* _pValue,
158 void* _pData,
159 SQLLEN * const pLen,
160 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
161 rtl_TextEncoding _nTextEncoding,
162 bool _bUseOldTimeDate) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
164 SQLRETURN nRetcode;
165 SQLSMALLINT fSqlType;
166 SQLSMALLINT fCType;
167 SQLLEN nMaxLen = _nMaxLen;
169 OTools::getBindTypes( false,
170 _bUseOldTimeDate,
171 _nType,
172 fCType,
173 fSqlType);
175 if (columnIndex != 0 && !_pValue)
177 *pLen = SQL_NULL_DATA;
178 nRetcode = (*reinterpret_cast<T3SQLBindCol>(_pConnection->getOdbcFunction(ODBC3SQLBindCol)))(_aStatementHandle,
179 (SQLUSMALLINT)columnIndex,
180 fCType,
181 _pData,
182 nMaxLen,
183 pLen
186 else
190 switch (_nType)
192 case SQL_CHAR:
193 case SQL_VARCHAR:
195 OString aString(OUStringToOString(*static_cast<OUString const *>(_pValue),_nTextEncoding));
196 *pLen = SQL_NTS;
197 *static_cast<OString*>(_pData) = aString;
198 _nMaxLen = (SQLSMALLINT)aString.getLength();
200 // Pointer on Char*
201 _pData = (void*)aString.getStr();
202 } break;
203 case SQL_BIGINT:
204 *static_cast<sal_Int64*>(_pData) = *static_cast<sal_Int64 const *>(_pValue);
205 *pLen = sizeof(sal_Int64);
206 break;
207 case SQL_DECIMAL:
208 case SQL_NUMERIC:
210 OString aString = OString::number(*static_cast<double const *>(_pValue));
211 _nMaxLen = (SQLSMALLINT)aString.getLength();
212 *pLen = _nMaxLen;
213 *static_cast<OString*>(_pData) = aString;
214 // Pointer on Char*
215 _pData = (void*)static_cast<OString*>(_pData)->getStr();
216 } break;
217 case SQL_BIT:
218 case SQL_TINYINT:
219 *static_cast<sal_Int8*>(_pData) = *static_cast<sal_Int8 const *>(_pValue);
220 *pLen = sizeof(sal_Int8);
221 break;
223 case SQL_SMALLINT:
224 *static_cast<sal_Int16*>(_pData) = *static_cast<sal_Int16 const *>(_pValue);
225 *pLen = sizeof(sal_Int16);
226 break;
227 case SQL_INTEGER:
228 *static_cast<sal_Int32*>(_pData) = *static_cast<sal_Int32 const *>(_pValue);
229 *pLen = sizeof(sal_Int32);
230 break;
231 case SQL_FLOAT:
232 *static_cast<float*>(_pData) = *static_cast<float const *>(_pValue);
233 *pLen = sizeof(float);
234 break;
235 case SQL_REAL:
236 case SQL_DOUBLE:
237 *static_cast<double*>(_pData) = *static_cast<double const *>(_pValue);
238 *pLen = sizeof(double);
239 break;
240 case SQL_BINARY:
241 case SQL_VARBINARY:
243 _pData = (void*)static_cast<const ::com::sun::star::uno::Sequence< sal_Int8 > *>(_pValue)->getConstArray();
244 *pLen = static_cast<const ::com::sun::star::uno::Sequence< sal_Int8 > *>(_pValue)->getLength();
245 } break;
246 case SQL_LONGVARBINARY:
248 _pData = reinterpret_cast<void*>(columnIndex);
249 sal_Int32 nLen = 0;
250 nLen = static_cast<const ::com::sun::star::uno::Sequence< sal_Int8 > *>(_pValue)->getLength();
251 *pLen = (SQLLEN)SQL_LEN_DATA_AT_EXEC(nLen);
253 break;
254 case SQL_LONGVARCHAR:
256 _pData = reinterpret_cast<void*>(columnIndex);
257 sal_Int32 nLen = 0;
258 nLen = static_cast<OUString const *>(_pValue)->getLength();
259 *pLen = (SQLLEN)SQL_LEN_DATA_AT_EXEC(nLen);
260 } break;
261 case SQL_DATE:
262 *pLen = sizeof(DATE_STRUCT);
263 *static_cast<DATE_STRUCT*>(_pData) = *static_cast<DATE_STRUCT const *>(_pValue);
264 break;
265 case SQL_TIME:
266 *pLen = sizeof(TIME_STRUCT);
267 *static_cast<TIME_STRUCT*>(_pData) = *static_cast<TIME_STRUCT const *>(_pValue);
268 break;
269 case SQL_TIMESTAMP:
270 *pLen = sizeof(TIMESTAMP_STRUCT);
271 *static_cast<TIMESTAMP_STRUCT*>(_pData) = *static_cast<TIMESTAMP_STRUCT const *>(_pValue);
272 break;
275 catch ( ... )
279 nRetcode = (*reinterpret_cast<T3SQLBindCol>(_pConnection->getOdbcFunction(ODBC3SQLBindCol)))(_aStatementHandle,
280 (SQLUSMALLINT)columnIndex,
281 fCType,
282 _pData,
283 nMaxLen,
284 pLen
288 OTools::ThrowException(_pConnection,nRetcode,_aStatementHandle,SQL_HANDLE_STMT,_xInterface);
291 void OTools::ThrowException(const OConnection* _pConnection,
292 const SQLRETURN _rRetCode,
293 const SQLHANDLE _pContext,
294 const SQLSMALLINT _nHandleType,
295 const Reference< XInterface >& _xInterface,
296 const bool _bNoFound,
297 const rtl_TextEncoding _nTextEncoding) throw(SQLException)
299 switch(_rRetCode)
301 case SQL_NEED_DATA:
302 case SQL_STILL_EXECUTING:
303 case SQL_SUCCESS:
305 case SQL_SUCCESS_WITH_INFO:
306 return;
307 case SQL_NO_DATA_FOUND:
308 if(_bNoFound)
309 return; // no need to throw a exception
310 case SQL_ERROR: break;
313 case SQL_INVALID_HANDLE: SAL_WARN( "connectivity.drivers", "SdbODBC3_SetStatus: SQL_INVALID_HANDLE");
314 throw SQLException();
317 // Additional Information on the latest ODBC-functioncall available
318 // SQLError provides this Information.
320 SDB_ODBC_CHAR szSqlState[5];
321 SQLINTEGER pfNativeError;
322 SDB_ODBC_CHAR szErrorMessage[SQL_MAX_MESSAGE_LENGTH];
323 szErrorMessage[0] = '\0';
324 SQLSMALLINT pcbErrorMsg = 0;
326 // Information for latest operation:
327 // when hstmt != SQL_NULL_HSTMT is (Used from SetStatus in SdbCursor, SdbTable, ...),
328 // then the status of the latest statements will be fetched, without the Status of the last
329 // statements of this connection [what in this case will probably be the same, but the Reference
330 // Manual isn't totally clear in this...].
331 // corresponding for hdbc.
332 SQLRETURN n = (*reinterpret_cast<T3SQLGetDiagRec>(_pConnection->getOdbcFunction(ODBC3SQLGetDiagRec)))(_nHandleType,_pContext,1,
333 szSqlState,
334 &pfNativeError,
335 szErrorMessage,sizeof szErrorMessage - 1,&pcbErrorMsg);
336 OSL_UNUSED( n );
337 OSL_ENSURE(n != SQL_INVALID_HANDLE,"SdbODBC3_SetStatus: SQLError returned SQL_INVALID_HANDLE");
338 OSL_ENSURE(n == SQL_SUCCESS || n == SQL_SUCCESS_WITH_INFO || n == SQL_NO_DATA_FOUND || n == SQL_ERROR,"SdbODBC3_SetStatus: SQLError failed");
340 // For the Return Code of SQLError see ODBC 2.0 Programmer's Reference Page 287ff
341 throw SQLException( OUString(reinterpret_cast<char *>(szErrorMessage), pcbErrorMsg, _nTextEncoding),
342 _xInterface,
343 OUString(reinterpret_cast<char *>(szSqlState), 5, _nTextEncoding),
344 pfNativeError,
345 Any()
350 Sequence<sal_Int8> OTools::getBytesValue(const OConnection* _pConnection,
351 const SQLHANDLE _aStatementHandle,
352 const sal_Int32 columnIndex,
353 const SQLSMALLINT _fSqlType,
354 bool &_bWasNull,
355 const Reference< XInterface >& _xInterface) throw(SQLException, RuntimeException)
357 sal_Int8 aCharArray[2048];
358 // First try to fetch the data with the little Buffer:
359 const SQLLEN nMaxLen = sizeof aCharArray;
360 SQLLEN pcbValue = SQL_NO_TOTAL;
361 Sequence<sal_Int8> aData;
363 OSL_ENSURE( _fSqlType != SQL_CHAR && _fSqlType != SQL_VARCHAR && _fSqlType != SQL_LONGVARCHAR &&
364 _fSqlType != SQL_WCHAR && _fSqlType != SQL_WVARCHAR && _fSqlType != SQL_WLONGVARCHAR,
365 "connectivity::odbc::OTools::getBytesValue called with character _fSqlType");
367 while (pcbValue == SQL_NO_TOTAL || pcbValue > nMaxLen)
369 OTools::ThrowException(_pConnection,
370 (*reinterpret_cast<T3SQLGetData>(_pConnection->getOdbcFunction(ODBC3SQLGetData)))(
371 _aStatementHandle,
372 (SQLUSMALLINT)columnIndex,
373 _fSqlType,
374 (SQLPOINTER)aCharArray,
375 nMaxLen,
376 &pcbValue),
377 _aStatementHandle,SQL_HANDLE_STMT,_xInterface);
379 _bWasNull = pcbValue == SQL_NULL_DATA;
380 if(_bWasNull)
381 return Sequence<sal_Int8>();
383 SQLLEN nReadBytes;
384 // After the SQLGetData that wrote out to aCharArray the last byte of the data,
385 // pcbValue will not be SQL_NO_TOTAL -> we have a reliable count
386 if ( (pcbValue == SQL_NO_TOTAL) || (pcbValue >= nMaxLen) )
388 // we filled the buffer
389 nReadBytes = nMaxLen;
391 else
393 nReadBytes = pcbValue;
395 const sal_Int32 nLen = aData.getLength();
396 aData.realloc(nLen + nReadBytes);
397 memcpy(aData.getArray() + nLen, aCharArray, nReadBytes);
399 return aData;
402 OUString OTools::getStringValue(OConnection* _pConnection,
403 SQLHANDLE _aStatementHandle,
404 sal_Int32 columnIndex,
405 SQLSMALLINT _fSqlType,
406 bool &_bWasNull,
407 const Reference< XInterface >& _xInterface,
408 const rtl_TextEncoding _nTextEncoding) throw(SQLException, RuntimeException)
410 OUStringBuffer aData;
411 switch(_fSqlType)
413 case SQL_WVARCHAR:
414 case SQL_WCHAR:
415 case SQL_WLONGVARCHAR:
417 SQLWCHAR waCharArray[2048];
418 static_assert(sizeof(waCharArray) % sizeof(SQLWCHAR) == 0, "must fit in evenly");
419 static_assert(sizeof(SQLWCHAR) == 2 || sizeof(SQLWCHAR) == 4, "must be 2 or 4");
420 // Size == number of bytes, Len == number of UTF-16 or UCS4 code units
421 const SQLLEN nMaxSize = sizeof(waCharArray);
422 const SQLLEN nMaxLen = sizeof(waCharArray) / sizeof(SQLWCHAR);
423 static_assert(nMaxLen * sizeof(SQLWCHAR) == nMaxSize, "sizes must match");
425 // read the unicode data
426 SQLLEN pcbValue = SQL_NO_TOTAL;
427 while ((pcbValue == SQL_NO_TOTAL ) || (pcbValue >= nMaxSize) )
429 OTools::ThrowException(_pConnection,
430 (*reinterpret_cast<T3SQLGetData>(_pConnection->getOdbcFunction(ODBC3SQLGetData)))(
431 _aStatementHandle,
432 (SQLUSMALLINT)columnIndex,
433 SQL_C_WCHAR,
434 &waCharArray,
435 (SQLLEN)nMaxLen*sizeof(sal_Unicode),
436 &pcbValue),
437 _aStatementHandle,SQL_HANDLE_STMT,_xInterface);
438 _bWasNull = pcbValue == SQL_NULL_DATA;
439 if(_bWasNull)
440 return OUString();
442 SQLLEN nReadChars;
443 OSL_ENSURE( (pcbValue < 0) || (pcbValue % 2 == 0),
444 "ODBC: SQLGetData of SQL_C_WCHAR returned odd number of bytes");
445 if ( (pcbValue == SQL_NO_TOTAL) || (pcbValue >= nMaxSize) )
447 // we filled the buffer; remove the terminating null character
448 nReadChars = nMaxLen-1;
449 if ( waCharArray[nReadChars] != 0)
451 SAL_WARN( "connectivity.drivers", "Buggy ODBC driver? Did not null-terminate (variable length) data!");
452 ++nReadChars;
455 else
457 nReadChars = pcbValue/sizeof(SQLWCHAR);
460 appendSQLWCHARs(aData, waCharArray, nReadChars);
462 break;
464 default:
466 char aCharArray[2048];
467 // read the unicode data
468 const SQLLEN nMaxLen = sizeof(aCharArray);
469 SQLLEN pcbValue = SQL_NO_TOTAL;
471 while ((pcbValue == SQL_NO_TOTAL ) || (pcbValue >= nMaxLen) )
473 OTools::ThrowException(_pConnection,
474 (*reinterpret_cast<T3SQLGetData>(_pConnection->getOdbcFunction(ODBC3SQLGetData)))(
475 _aStatementHandle,
476 (SQLUSMALLINT)columnIndex,
477 SQL_C_CHAR,
478 &aCharArray,
479 nMaxLen,
480 &pcbValue),
481 _aStatementHandle,SQL_HANDLE_STMT,_xInterface);
482 _bWasNull = pcbValue == SQL_NULL_DATA;
483 if(_bWasNull)
484 return OUString();
486 SQLLEN nReadChars;
487 if ( (pcbValue == SQL_NO_TOTAL) || (pcbValue >= nMaxLen) )
489 // we filled the buffer; remove the terminating null character
490 nReadChars = nMaxLen-1;
491 if ( aCharArray[nReadChars] != 0)
493 SAL_WARN( "connectivity.drivers", "Buggy ODBC driver? Did not null-terminate (variable length) data!");
494 ++nReadChars;
497 else
499 nReadChars = pcbValue;
502 aData.append(OUString(aCharArray, nReadChars, _nTextEncoding));
505 break;
509 return aData.makeStringAndClear();
512 void OTools::GetInfo(OConnection* _pConnection,
513 SQLHANDLE _aConnectionHandle,
514 SQLUSMALLINT _nInfo,
515 OUString &_rValue,
516 const Reference< XInterface >& _xInterface,
517 rtl_TextEncoding _nTextEncoding) throw(SQLException, RuntimeException)
519 char aValue[512];
520 SQLSMALLINT nValueLen=0;
521 OTools::ThrowException(_pConnection,
522 (*reinterpret_cast<T3SQLGetInfo>(_pConnection->getOdbcFunction(ODBC3SQLGetInfo)))(_aConnectionHandle,_nInfo,aValue,(sizeof aValue)-1,&nValueLen),
523 _aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
525 _rValue = OUString(aValue,nValueLen,_nTextEncoding);
528 void OTools::GetInfo(OConnection* _pConnection,
529 SQLHANDLE _aConnectionHandle,
530 SQLUSMALLINT _nInfo,
531 sal_Int32 &_rValue,
532 const Reference< XInterface >& _xInterface) throw(SQLException, RuntimeException)
534 SQLSMALLINT nValueLen;
535 _rValue = 0; // in case the driver uses only 16 of the 32 bits (as it does, for example, for SQL_CATALOG_LOCATION)
536 OTools::ThrowException(_pConnection,
537 (*reinterpret_cast<T3SQLGetInfo>(_pConnection->getOdbcFunction(ODBC3SQLGetInfo)))(_aConnectionHandle,_nInfo,&_rValue,sizeof _rValue,&nValueLen),
538 _aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
541 void OTools::GetInfo(OConnection* _pConnection,
542 SQLHANDLE _aConnectionHandle,
543 SQLUSMALLINT _nInfo,
544 SQLUINTEGER &_rValue,
545 const Reference< XInterface >& _xInterface) throw(SQLException, RuntimeException)
547 SQLSMALLINT nValueLen;
548 _rValue = 0; // in case the driver uses only 16 of the 32 bits (as it does, for example, for SQL_CATALOG_LOCATION)
549 OTools::ThrowException(_pConnection,
550 (*reinterpret_cast<T3SQLGetInfo>(_pConnection->getOdbcFunction(ODBC3SQLGetInfo)))(_aConnectionHandle,_nInfo,&_rValue,sizeof _rValue,&nValueLen),
551 _aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
554 void OTools::GetInfo(OConnection* _pConnection,
555 SQLHANDLE _aConnectionHandle,
556 SQLUSMALLINT _nInfo,
557 SQLUSMALLINT &_rValue,
558 const Reference< XInterface >& _xInterface) throw(SQLException, RuntimeException)
560 SQLSMALLINT nValueLen;
561 _rValue = 0; // in case the driver uses only 16 of the 32 bits (as it does, for example, for SQL_CATALOG_LOCATION)
562 OTools::ThrowException(_pConnection,
563 (*reinterpret_cast<T3SQLGetInfo>(_pConnection->getOdbcFunction(ODBC3SQLGetInfo)))(_aConnectionHandle,_nInfo,&_rValue,sizeof _rValue,&nValueLen),
564 _aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
567 void OTools::GetInfo(OConnection* _pConnection,
568 SQLHANDLE _aConnectionHandle,
569 SQLUSMALLINT _nInfo,
570 bool &_rValue,
571 const Reference< XInterface >& _xInterface) throw(SQLException, RuntimeException)
573 SQLSMALLINT nValueLen;
574 OTools::ThrowException(_pConnection,
575 (*reinterpret_cast<T3SQLGetInfo>(_pConnection->getOdbcFunction(ODBC3SQLGetInfo)))(_aConnectionHandle,_nInfo,&_rValue,sizeof _rValue,&nValueLen),
576 _aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
579 sal_Int32 OTools::MapOdbcType2Jdbc(SQLSMALLINT _nType)
581 sal_Int32 nValue = DataType::VARCHAR;
582 switch(_nType)
584 case SQL_BIT:
585 nValue = DataType::BIT;
586 break;
587 case SQL_TINYINT:
588 nValue = DataType::TINYINT;
589 break;
590 case SQL_SMALLINT:
591 nValue = DataType::SMALLINT;
592 break;
593 case SQL_INTEGER:
594 nValue = DataType::INTEGER;
595 break;
596 case SQL_BIGINT:
597 nValue = DataType::BIGINT;
598 break;
599 case SQL_FLOAT:
600 nValue = DataType::FLOAT;
601 break;
602 case SQL_REAL:
603 nValue = DataType::REAL;
604 break;
605 case SQL_DOUBLE:
606 nValue = DataType::DOUBLE;
607 break;
608 case SQL_NUMERIC:
609 nValue = DataType::NUMERIC;
610 break;
611 case SQL_DECIMAL:
612 nValue = DataType::DECIMAL;
613 break;
614 case SQL_WCHAR:
615 case SQL_CHAR:
616 nValue = DataType::CHAR;
617 break;
618 case SQL_WVARCHAR:
619 case SQL_VARCHAR:
620 nValue = DataType::VARCHAR;
621 break;
622 case SQL_WLONGVARCHAR:
623 case SQL_LONGVARCHAR:
624 nValue = DataType::LONGVARCHAR;
625 break;
626 case SQL_TYPE_DATE:
627 case SQL_DATE:
628 nValue = DataType::DATE;
629 break;
630 case SQL_TYPE_TIME:
631 case SQL_TIME:
632 nValue = DataType::TIME;
633 break;
634 case SQL_TYPE_TIMESTAMP:
635 case SQL_TIMESTAMP:
636 nValue = DataType::TIMESTAMP;
637 break;
638 case SQL_BINARY:
639 nValue = DataType::BINARY;
640 break;
641 case SQL_VARBINARY:
642 case SQL_GUID:
643 nValue = DataType::VARBINARY;
644 break;
645 case SQL_LONGVARBINARY:
646 nValue = DataType::LONGVARBINARY;
647 break;
648 default:
649 OSL_FAIL("Invalid type");
651 return nValue;
654 // jdbcTypeToOdbc
655 // Convert the JDBC SQL type to the correct ODBC type
657 SQLSMALLINT OTools::jdbcTypeToOdbc(sal_Int32 jdbcType)
659 // For the most part, JDBC types match ODBC types. We'll
660 // just convert the ones that we know are different
662 sal_Int32 odbcType = jdbcType;
664 switch (jdbcType)
666 case DataType::DATE:
667 odbcType = SQL_DATE;
668 break;
669 case DataType::TIME:
670 odbcType = SQL_TIME;
671 break;
672 case DataType::TIMESTAMP:
673 odbcType = SQL_TIMESTAMP;
674 break;
675 // ODBC doesn't have any notion of CLOB or BLOB
676 case DataType::CLOB:
677 odbcType = SQL_LONGVARCHAR;
678 break;
679 case DataType::BLOB:
680 odbcType = SQL_LONGVARBINARY;
681 break;
684 return odbcType;
687 void OTools::getBindTypes(bool _bUseWChar,
688 bool _bUseOldTimeDate,
689 SQLSMALLINT _nOdbcType,
690 SQLSMALLINT& fCType,
691 SQLSMALLINT& fSqlType
694 switch(_nOdbcType)
696 case SQL_CHAR: if(_bUseWChar)
698 fCType = SQL_C_WCHAR;
699 fSqlType = SQL_WCHAR;
701 else
703 fCType = SQL_C_CHAR;
704 fSqlType = SQL_CHAR;
706 break;
707 case SQL_VARCHAR: if(_bUseWChar)
709 fCType = SQL_C_WCHAR;
710 fSqlType = SQL_WVARCHAR;
712 else
714 fCType = SQL_C_CHAR;
715 fSqlType = SQL_VARCHAR;
717 break;
718 case SQL_LONGVARCHAR: if(_bUseWChar)
720 fCType = SQL_C_WCHAR;
721 fSqlType = SQL_WLONGVARCHAR;
723 else
725 fCType = SQL_C_CHAR;
726 fSqlType = SQL_LONGVARCHAR;
728 break;
729 case SQL_DECIMAL: fCType = _bUseWChar ? SQL_C_WCHAR : SQL_C_CHAR;
730 fSqlType = SQL_DECIMAL; break;
731 case SQL_NUMERIC: fCType = _bUseWChar ? SQL_C_WCHAR : SQL_C_CHAR;
732 fSqlType = SQL_NUMERIC; break;
733 case SQL_BIT: fCType = SQL_C_TINYINT;
734 fSqlType = SQL_INTEGER; break;
735 case SQL_TINYINT: fCType = SQL_C_TINYINT;
736 fSqlType = SQL_TINYINT; break;
737 case SQL_SMALLINT: fCType = SQL_C_SHORT;
738 fSqlType = SQL_SMALLINT; break;
739 case SQL_INTEGER: fCType = SQL_C_LONG;
740 fSqlType = SQL_INTEGER; break;
741 case SQL_BIGINT: fCType = SQL_C_SBIGINT;
742 fSqlType = SQL_BIGINT; break;
743 case SQL_FLOAT: fCType = SQL_C_FLOAT;
744 fSqlType = SQL_FLOAT; break;
745 case SQL_REAL: fCType = SQL_C_DOUBLE;
746 fSqlType = SQL_REAL; break;
747 case SQL_DOUBLE: fCType = SQL_C_DOUBLE;
748 fSqlType = SQL_DOUBLE; break;
749 case SQL_BINARY: fCType = SQL_C_BINARY;
750 fSqlType = SQL_BINARY; break;
751 case SQL_VARBINARY:
752 fCType = SQL_C_BINARY;
753 fSqlType = SQL_VARBINARY; break;
754 case SQL_LONGVARBINARY: fCType = SQL_C_BINARY;
755 fSqlType = SQL_LONGVARBINARY; break;
756 case SQL_DATE:
757 if(_bUseOldTimeDate)
759 fCType = SQL_C_DATE;
760 fSqlType = SQL_DATE;
762 else
764 fCType = SQL_C_TYPE_DATE;
765 fSqlType = SQL_TYPE_DATE;
767 break;
768 case SQL_TIME:
769 if(_bUseOldTimeDate)
771 fCType = SQL_C_TIME;
772 fSqlType = SQL_TIME;
774 else
776 fCType = SQL_C_TYPE_TIME;
777 fSqlType = SQL_TYPE_TIME;
779 break;
780 case SQL_TIMESTAMP:
781 if(_bUseOldTimeDate)
783 fCType = SQL_C_TIMESTAMP;
784 fSqlType = SQL_TIMESTAMP;
786 else
788 fCType = SQL_C_TYPE_TIMESTAMP;
789 fSqlType = SQL_TYPE_TIMESTAMP;
791 break;
792 default: fCType = SQL_C_BINARY;
793 fSqlType = SQL_LONGVARBINARY; break;
798 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */