diff options
author | Bill King <bill.king@nokia.com> | 2010-03-25 16:36:32 +1000 |
---|---|---|
committer | Bill King <bill.king@nokia.com> | 2010-03-25 16:36:32 +1000 |
commit | 9c4bfd1dbdda1a073be12cf6212492abc7e9472b (patch) | |
tree | d4c70c58f3417c79a93fefe6e55284538f218c5e /src/sql/drivers | |
parent | b763094c5ec554023e75974cfb01d7e287044d40 (diff) | |
download | qt4-tools-9c4bfd1dbdda1a073be12cf6212492abc7e9472b.tar.gz |
Unicode fixes for ODBC SQL Driver.
Better unicode handling detection, plus turn off unicode if FreeTDS,
plus handling of returning strings under non-unicode scenarios.
Task-number: QTBUG-8846
Reviewed-by: Justin McPherson
Diffstat (limited to 'src/sql/drivers')
-rw-r--r-- | src/sql/drivers/odbc/qsql_odbc.cpp | 155 |
1 files changed, 117 insertions, 38 deletions
diff --git a/src/sql/drivers/odbc/qsql_odbc.cpp b/src/sql/drivers/odbc/qsql_odbc.cpp index e75c19d795..1506c3c63a 100644 --- a/src/sql/drivers/odbc/qsql_odbc.cpp +++ b/src/sql/drivers/odbc/qsql_odbc.cpp @@ -131,11 +131,10 @@ class QODBCDriverPrivate public: enum DefaultCase{Lower, Mixed, Upper, Sensitive}; QODBCDriverPrivate() - : hEnv(0), hDbc(0), useSchema(false), disconnectCount(0), isMySqlServer(false), - isMSSqlServer(false), hasSQLFetchScroll(true), hasMultiResultSets(false), - isQuoteInitialized(false), quote(QLatin1Char('"')) + : hEnv(0), hDbc(0), unicode(false), useSchema(false), disconnectCount(0), isMySqlServer(false), + isMSSqlServer(false), isFreeTDSDriver(false), hasSQLFetchScroll(true), + hasMultiResultSets(false), isQuoteInitialized(false), quote(QLatin1Char('"')) { - unicode = false; } SQLHANDLE hEnv; @@ -146,6 +145,7 @@ public: int disconnectCount; bool isMySqlServer; bool isMSSqlServer; + bool isFreeTDSDriver; bool hasSQLFetchScroll; bool hasMultiResultSets; @@ -172,7 +172,10 @@ public: QODBCPrivate(QODBCDriverPrivate *dpp) : hStmt(0), useSchema(false), hasSQLFetchScroll(true), driverPrivate(dpp), userForwardOnly(false) { - unicode = false; + unicode = dpp->unicode; + useSchema = dpp->useSchema; + disconnectCount = dpp->disconnectCount; + hasSQLFetchScroll = dpp->hasSQLFetchScroll; } inline void clearValues() @@ -374,44 +377,88 @@ static QString qGetStringData(SQLHANDLE hStmt, int column, int colSize, bool uni } else { colSize++; // make sure there is room for more than the 0 termination } - r = SQLGetData(hStmt, - column+1, - SQL_C_TCHAR, - NULL, - 0, - &lengthIndicator); - if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && lengthIndicator > 0) - colSize = lengthIndicator/sizeof(SQLTCHAR) + 1; - QVarLengthArray<SQLTCHAR> buf(colSize); - while (true) { + if(unicode) { r = SQLGetData(hStmt, column+1, SQL_C_TCHAR, - (SQLPOINTER)buf.data(), - colSize*sizeof(SQLTCHAR), + NULL, + 0, &lengthIndicator); - if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) { - if (lengthIndicator == SQL_NULL_DATA || lengthIndicator == SQL_NO_TOTAL) { + if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && lengthIndicator > 0) + colSize = lengthIndicator/sizeof(SQLTCHAR) + 1; + QVarLengthArray<SQLTCHAR> buf(colSize); + memset(buf.data(), 0, colSize*sizeof(SQLTCHAR)); + while (true) { + r = SQLGetData(hStmt, + column+1, + SQL_C_TCHAR, + (SQLPOINTER)buf.data(), + colSize*sizeof(SQLTCHAR), + &lengthIndicator); + if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) { + if (lengthIndicator == SQL_NULL_DATA || lengthIndicator == SQL_NO_TOTAL) { + fieldVal.clear(); + break; + } + // if SQL_SUCCESS_WITH_INFO is returned, indicating that + // more data can be fetched, the length indicator does NOT + // contain the number of bytes returned - it contains the + // total number of bytes that CAN be fetched + // colSize-1: remove 0 termination when there is more data to fetch + int rSize = (r == SQL_SUCCESS_WITH_INFO) ? colSize : lengthIndicator/sizeof(SQLTCHAR); + fieldVal += fromSQLTCHAR(buf, rSize); + if (lengthIndicator < (unsigned int)colSize*sizeof(SQLTCHAR)) { + // workaround for Drivermanagers that don't return SQL_NO_DATA + break; + } + } else if (r == SQL_NO_DATA) { + break; + } else { + qWarning() << "qGetStringData: Error while fetching data (" << qWarnODBCHandle(SQL_HANDLE_STMT, hStmt) << ')'; fieldVal.clear(); break; } - // if SQL_SUCCESS_WITH_INFO is returned, indicating that - // more data can be fetched, the length indicator does NOT - // contain the number of bytes returned - it contains the - // total number of bytes that CAN be fetched - // colSize-1: remove 0 termination when there is more data to fetch - int rSize = (r == SQL_SUCCESS_WITH_INFO) ? colSize : lengthIndicator/sizeof(SQLTCHAR); - fieldVal += fromSQLTCHAR(buf, rSize); - if (lengthIndicator < (unsigned int)colSize*sizeof(SQLTCHAR)) { - // workaround for Drivermanagers that don't return SQL_NO_DATA + } + } else { + r = SQLGetData(hStmt, + column+1, + SQL_C_CHAR, + NULL, + 0, + &lengthIndicator); + if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && lengthIndicator > 0) + colSize = lengthIndicator + 1; + QVarLengthArray<SQLCHAR> buf(colSize); + while (true) { + r = SQLGetData(hStmt, + column+1, + SQL_C_CHAR, + (SQLPOINTER)buf.data(), + colSize, + &lengthIndicator); + if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) { + if (lengthIndicator == SQL_NULL_DATA || lengthIndicator == SQL_NO_TOTAL) { + fieldVal.clear(); + break; + } + // if SQL_SUCCESS_WITH_INFO is returned, indicating that + // more data can be fetched, the length indicator does NOT + // contain the number of bytes returned - it contains the + // total number of bytes that CAN be fetched + // colSize-1: remove 0 termination when there is more data to fetch + int rSize = (r == SQL_SUCCESS_WITH_INFO) ? colSize : lengthIndicator; + fieldVal += QString::fromUtf8((const char *)buf.constData(), rSize); + if (lengthIndicator < (unsigned int)colSize) { + // workaround for Drivermanagers that don't return SQL_NO_DATA + break; + } + } else if (r == SQL_NO_DATA) { + break; + } else { + qWarning() << "qGetStringData: Error while fetching data (" << qWarnODBCHandle(SQL_HANDLE_STMT, hStmt) << ')'; + fieldVal.clear(); break; } - } else if (r == SQL_NO_DATA) { - break; - } else { - qWarning() << "qGetStringData: Error while fetching data (" << qWarnODBCHandle(SQL_HANDLE_STMT, hStmt) << ')'; - fieldVal.clear(); - break; } } return fieldVal; @@ -866,10 +913,6 @@ QODBCResult::QODBCResult(const QODBCDriver * db, QODBCDriverPrivate* p) : QSqlResult(db) { d = new QODBCPrivate(p); - d->unicode = p->unicode; - d->useSchema = p->useSchema; - d->disconnectCount = p->disconnectCount; - d->hasSQLFetchScroll = p->hasSQLFetchScroll; } QODBCResult::~QODBCResult() @@ -1846,6 +1889,7 @@ bool QODBCDriver::open(const QString & db, SQLSMALLINT cb; QVarLengthArray<SQLTCHAR> connOut(1024); + memset(connOut.data(), 0, connOut.size() * sizeof(SQLTCHAR)); r = SQLDriverConnect(d->hDbc, NULL, #ifdef UNICODE @@ -1943,6 +1987,7 @@ void QODBCDriverPrivate::checkUnicode() NULL); if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && (fFunc & SQL_CVT_WCHAR)) { unicode = true; + return; } r = SQLGetInfo(hDbc, @@ -1952,6 +1997,7 @@ void QODBCDriverPrivate::checkUnicode() NULL); if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && (fFunc & SQL_CVT_WVARCHAR)) { unicode = true; + return; } r = SQLGetInfo(hDbc, @@ -1961,7 +2007,25 @@ void QODBCDriverPrivate::checkUnicode() NULL); if ((r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) && (fFunc & SQL_CVT_WLONGVARCHAR)) { unicode = true; + return; + } + SQLHANDLE hStmt; + r = SQLAllocHandle(SQL_HANDLE_STMT, + hDbc, + &hStmt); + + r = SQLExecDirect(hStmt, toSQLTCHAR(QLatin1String("select 'test'")).data(), SQL_NTS); + if(r == SQL_SUCCESS) { + r = SQLFetch(hStmt); + if(r == SQL_SUCCESS) { + QVarLengthArray<SQLWCHAR> buffer(10); + r = SQLGetData(hStmt, 1, SQL_C_WCHAR, buffer.data(), buffer.size() * sizeof(SQLWCHAR), NULL); + if(r == SQL_SUCCESS && fromSQLTCHAR(buffer) == QLatin1String("test")) { + unicode = true; + } + } } + r = SQLFreeHandle(SQL_HANDLE_STMT, hStmt); } bool QODBCDriverPrivate::checkDriver() const @@ -2053,6 +2117,21 @@ void QODBCDriverPrivate::checkSqlServer() isMySqlServer = serverType.contains(QLatin1String("mysql"), Qt::CaseInsensitive); isMSSqlServer = serverType.contains(QLatin1String("Microsoft SQL Server"), Qt::CaseInsensitive); } + r = SQLGetInfo(hDbc, + SQL_DRIVER_NAME, + serverString.data(), + serverString.size() * sizeof(SQLTCHAR), + &t); + if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) { + QString serverType; +#ifdef UNICODE + serverType = fromSQLTCHAR(serverString, t/sizeof(SQLTCHAR)); +#else + serverType = QString::fromUtf8((const char *)serverString.constData(), t); +#endif + isFreeTDSDriver = serverType.contains(QLatin1String("tdsodbc"), Qt::CaseInsensitive); + unicode = isFreeTDSDriver == false; + } } void QODBCDriverPrivate::checkHasSQLFetchScroll() |