diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2023-01-25 20:37:48 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2023-02-07 22:27:37 +0100 |
commit | 6b58a40d32527caa5387d9171bbe4443e3c85648 (patch) | |
tree | 3db0f3c93ed7a56daf58fe50c36c0d3fc397b7d1 /src/plugins/sqldrivers/odbc/qsql_odbc.cpp | |
parent | d619a952cdcdf0a0ca163e8513f0843f8bc9c2dc (diff) |
SQL/ODBC: misc cleanup for fromSQLTCHAR()
Change fromSQLTCHAR() to honor the fact that we know the size of
SQLTCHAR during compile time.
Change-Id: I1cebf2550be7228980083703e143c7577e8ad377
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/plugins/sqldrivers/odbc/qsql_odbc.cpp')
-rw-r--r-- | src/plugins/sqldrivers/odbc/qsql_odbc.cpp | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp index 4b7ad455f1..b4b6ece1dd 100644 --- a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp +++ b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp @@ -36,28 +36,22 @@ static const SQLSMALLINT TABLENAMESIZE = 128; //Map Qt parameter types to ODBC types static const SQLSMALLINT qParamType[4] = { SQL_PARAM_INPUT, SQL_PARAM_INPUT, SQL_PARAM_OUTPUT, SQL_PARAM_INPUT_OUTPUT }; -inline static QString fromSQLTCHAR(const QVarLengthArray<SQLTCHAR>& input, qsizetype size=-1) +template<typename C, int SIZE = sizeof(SQLTCHAR)> +inline static QString fromSQLTCHAR(const C &input, qsizetype size = -1) { - QString result; - // Remove any trailing \0 as some drivers misguidedly append one - int realsize = qMin(size, input.size()); - if (realsize > 0 && input[realsize-1] == 0) + qsizetype realsize = qMin(size, input.size()); + if (realsize > 0 && input[realsize - 1] == 0) realsize--; - switch(sizeof(SQLTCHAR)) { - case 1: - result=QString::fromUtf8((const char *)input.constData(), realsize); - break; - case 2: - result = QString::fromUtf16(reinterpret_cast<const char16_t *>(input.constData()), realsize); - break; - case 4: - result = QString::fromUcs4(reinterpret_cast<const char32_t *>(input.constData()), realsize); - break; - default: - qCritical("sizeof(SQLTCHAR) is %d. Don't know how to handle this.", int(sizeof(SQLTCHAR))); - } - return result; + if constexpr (SIZE == 1) + return QString::fromUtf8(reinterpret_cast<const char *>(input.constData()), realsize); + else if constexpr (SIZE == 2) + return QString::fromUtf16(reinterpret_cast<const char16_t *>(input.constData()), realsize); + else if constexpr (SIZE == 4) + return QString::fromUcs4(reinterpret_cast<const char32_t *>(input.constData()), realsize); + else + static_assert(QtPrivate::value_dependent_false<SIZE>(), + "Don't know how to handle sizeof(SQLTCHAR) != 1/2/4"); } template<int SIZE = sizeof(SQLTCHAR)> |