/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
namespace {
size_t sqlTypeLen ( SQLSMALLINT _nType )
{ switch (_nType)
{ case SQL_C_SSHORT: case SQL_C_SHORT: returnsizeof(SQLSMALLINT); case SQL_C_USHORT: returnsizeof(SQLUSMALLINT); case SQL_C_SLONG: case SQL_C_LONG: returnsizeof(SQLINTEGER); case SQL_C_ULONG: returnsizeof(SQLUINTEGER); case SQL_C_FLOAT: returnsizeof(SQLREAL); case SQL_C_DOUBLE:
static_assert(sizeof(SQLDOUBLE) == sizeof(SQLFLOAT), "SQLDOUBLE/SQLFLOAT confusion"); returnsizeof(SQLDOUBLE); case SQL_C_BIT: returnsizeof(SQLCHAR); case SQL_C_STINYINT: case SQL_C_TINYINT: returnsizeof(SQLSCHAR); case SQL_C_UTINYINT: returnsizeof(SQLCHAR); case SQL_C_SBIGINT: returnsizeof(SQLBIGINT); case SQL_C_UBIGINT: returnsizeof(SQLUBIGINT); /* UnixODBC gives this the same value as SQL_C_UBIGINT case SQL_C_BOOKMARK:
return sizeof(BOOKMARK); */ case SQL_C_TYPE_DATE: case SQL_C_DATE: returnsizeof(SQL_DATE_STRUCT); case SQL_C_TYPE_TIME: case SQL_C_TIME: returnsizeof(SQL_TIME_STRUCT); case SQL_C_TYPE_TIMESTAMP: case SQL_C_TIMESTAMP: returnsizeof(SQL_TIMESTAMP_STRUCT); case SQL_C_NUMERIC: returnsizeof(SQL_NUMERIC_STRUCT); case SQL_C_GUID: returnsizeof(SQLGUID); case SQL_C_INTERVAL_YEAR: case SQL_C_INTERVAL_MONTH: case SQL_C_INTERVAL_DAY: case SQL_C_INTERVAL_HOUR: case SQL_C_INTERVAL_MINUTE: case SQL_C_INTERVAL_SECOND: case SQL_C_INTERVAL_YEAR_TO_MONTH: case SQL_C_INTERVAL_DAY_TO_HOUR: case SQL_C_INTERVAL_DAY_TO_MINUTE: case SQL_C_INTERVAL_DAY_TO_SECOND: case SQL_C_INTERVAL_HOUR_TO_MINUTE: case SQL_C_INTERVAL_HOUR_TO_SECOND: case SQL_C_INTERVAL_MINUTE_TO_SECOND: returnsizeof(SQL_INTERVAL_STRUCT); // ** Variable-sized datatypes -> cannot predict length case SQL_C_CHAR: case SQL_C_WCHAR: case SQL_C_BINARY: // UnixODBC gives this the same value as SQL_C_BINARY //case SQL_C_VARBOOKMARK: // Unknown datatype -> cannot predict length default: returnstatic_cast<size_t>(-1);
}
}
// Information for latest operation: // when hstmt != SQL_NULL_HSTMT is (Used from SetStatus in SdbCursor, SdbTable, ...), // then the status of the latest statements will be fetched, without the Status of the last // statements of this connection [what in this case will probably be the same, but the Reference // Manual isn't totally clear in this...]. // corresponding for hdbc. if (bUseWChar && _pConnection->functions().has(ODBC3SQLFunctionId::GetDiagRecW))
{
SQLWCHAR szSqlState[6];
SQLWCHAR szErrorMessage[SQL_MAX_MESSAGE_LENGTH];
szErrorMessage[0] = '\0';
SQLSMALLINT cchErrorMsg = 0;
SQLLEN nReadBytes; // After the SQLGetData that wrote out to aCharArray the last byte of the data, // pcbValue will not be SQL_NO_TOTAL -> we have a reliable count if ( (pcbValue == SQL_NO_TOTAL) || (pcbValue >= nMaxLen) )
{ // we filled the buffer
nReadBytes = nMaxLen;
} else
{
nReadBytes = pcbValue;
} const sal_Int32 nLen = aData.getLength();
aData.realloc(nLen + nReadBytes);
memcpy(aData.getArray() + nLen, aCharArray, nReadBytes);
} return aData;
}
OUString OTools::getStringValue(OConnection const * _pConnection,
SQLHANDLE _aStatementHandle,
sal_Int32 columnIndex,
SQLSMALLINT _fSqlType, bool &_bWasNull, const Reference< XInterface >& _xInterface, const rtl_TextEncoding _nTextEncoding)
{
OUStringBuffer aData; switch(_fSqlType)
{ case SQL_WVARCHAR: case SQL_WCHAR: case SQL_WLONGVARCHAR:
{
SQLWCHAR waCharArray[2048];
static_assert(sizeof(waCharArray) % sizeof(SQLWCHAR) == 0, "must fit in evenly");
static_assert(sizeof(SQLWCHAR) == 2 || sizeof(SQLWCHAR) == 4, "must be 2 or 4"); // Size == number of bytes, Len == number of UTF-16 or UCS4 code units const SQLLEN nMaxSize = sizeof(waCharArray); const SQLLEN nMaxLen = sizeof(waCharArray) / sizeof(SQLWCHAR);
static_assert(nMaxLen * sizeof(SQLWCHAR) == nMaxSize, "sizes must match");
void OTools::GetInfo(OConnection const * _pConnection,
SQLHANDLE _aConnectionHandle,
SQLUSMALLINT _nInfo,
sal_Int32 &_rValue, const Reference< XInterface >& _xInterface)
{
SQLSMALLINT nValueLen;
_rValue = 0; // in case the driver uses only 16 of the 32 bits (as it does, for example, for SQL_CATALOG_LOCATION)
OTools::ThrowException(_pConnection,
_pConnection->functions().GetInfo(_aConnectionHandle,_nInfo,&_rValue,sizeof _rValue,&nValueLen),
_aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
}
void OTools::GetInfo(OConnection const * _pConnection,
SQLHANDLE _aConnectionHandle,
SQLUSMALLINT _nInfo,
SQLUINTEGER &_rValue, const Reference< XInterface >& _xInterface)
{
SQLSMALLINT nValueLen;
_rValue = 0; // in case the driver uses only 16 of the 32 bits (as it does, for example, for SQL_CATALOG_LOCATION)
OTools::ThrowException(_pConnection,
_pConnection->functions().GetInfo(_aConnectionHandle,_nInfo,&_rValue,sizeof _rValue,&nValueLen),
_aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
}
void OTools::GetInfo(OConnection const * _pConnection,
SQLHANDLE _aConnectionHandle,
SQLUSMALLINT _nInfo,
SQLUSMALLINT &_rValue, const Reference< XInterface >& _xInterface)
{
SQLSMALLINT nValueLen;
_rValue = 0; // in case the driver uses only 16 of the 32 bits (as it does, for example, for SQL_CATALOG_LOCATION)
OTools::ThrowException(_pConnection,
_pConnection->functions().GetInfo(_aConnectionHandle,_nInfo,&_rValue,sizeof _rValue,&nValueLen),
_aConnectionHandle,SQL_HANDLE_DBC,_xInterface);
}
sal_Int32 OTools::MapOdbcType2Jdbc(SQLSMALLINT _nType)
{
sal_Int32 nValue = DataType::VARCHAR; switch(_nType)
{ case SQL_BIT:
nValue = DataType::BIT; break; case SQL_TINYINT:
nValue = DataType::TINYINT; break; case SQL_SMALLINT:
nValue = DataType::SMALLINT; break; case SQL_INTEGER:
nValue = DataType::INTEGER; break; case SQL_BIGINT:
nValue = DataType::BIGINT; break; case SQL_FLOAT:
nValue = DataType::FLOAT; break; case SQL_REAL:
nValue = DataType::REAL; break; case SQL_DOUBLE:
nValue = DataType::DOUBLE; break; case SQL_NUMERIC:
nValue = DataType::NUMERIC; break; case SQL_DECIMAL:
nValue = DataType::DECIMAL; break; case SQL_WCHAR: case SQL_CHAR:
nValue = DataType::CHAR; break; case SQL_WVARCHAR: case SQL_VARCHAR:
nValue = DataType::VARCHAR; break; case SQL_WLONGVARCHAR: case SQL_LONGVARCHAR:
nValue = DataType::LONGVARCHAR; break; case SQL_TYPE_DATE: case SQL_DATE:
nValue = DataType::DATE; break; case SQL_TYPE_TIME: case SQL_TIME:
nValue = DataType::TIME; break; case SQL_TYPE_TIMESTAMP: case SQL_TIMESTAMP:
nValue = DataType::TIMESTAMP; break; case SQL_BINARY:
nValue = DataType::BINARY; break; case SQL_VARBINARY: case SQL_GUID:
nValue = DataType::VARBINARY; break; case SQL_LONGVARBINARY:
nValue = DataType::LONGVARBINARY; break; default:
OSL_FAIL("Invalid type");
} return nValue;
}
// jdbcTypeToOdbc // Convert the JDBC SQL type to the correct ODBC type
SQLSMALLINT OTools::jdbcTypeToOdbc(sal_Int32 jdbcType)
{ // For the most part, JDBC types match ODBC types. We'll // just convert the ones that we know are different
sal_Int32 odbcType = jdbcType;
switch (jdbcType)
{ case DataType::DATE:
odbcType = SQL_DATE; break; case DataType::TIME:
odbcType = SQL_TIME; break; case DataType::TIMESTAMP:
odbcType = SQL_TIMESTAMP; break; // ODBC doesn't have any notion of CLOB or BLOB case DataType::CLOB:
odbcType = SQL_LONGVARCHAR; break; case DataType::BLOB:
odbcType = SQL_LONGVARBINARY; break;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.