From fadeea9d028fe9ba8772b52aa751258bdf4d50a9 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Thu, 9 Nov 2017 17:58:05 +0200 Subject: [Qt] Explicit QVariant type as string when binding text --- platform/qt/src/sqlite3.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/platform/qt/src/sqlite3.cpp b/platform/qt/src/sqlite3.cpp index 1d18946364..2f3db12f33 100644 --- a/platform/qt/src/sqlite3.cpp +++ b/platform/qt/src/sqlite3.cpp @@ -277,9 +277,13 @@ void Statement::bind(int offset, const char* value, std::size_t length, bool ret throw std::range_error("value too long"); } + // Qt SQLite driver treats QByteArray as blob: we need to explicitly + // declare the variant type as string. + QVariant text(QVariant::Type::String); + text.setValue(retain ? QByteArray(value, length) : QByteArray::fromRawData(value, length)); + // Field numbering starts at 0. - impl->query.bindValue(offset - 1, retain ? QByteArray(value, length) : - QByteArray::fromRawData(value, length), QSql::In); + impl->query.bindValue(offset - 1, std::move(text), QSql::In); checkQueryError(impl->query); } @@ -289,7 +293,12 @@ void Statement::bind(int offset, const std::string& value, bool retain) { } void Statement::bindBlob(int offset, const void* value_, std::size_t length, bool retain) { + assert(impl); const char* value = reinterpret_cast(value_); + if (length > std::numeric_limits::max()) { + // Kept for consistence with the default implementation. + throw std::range_error("value too long"); + } // Field numbering starts at 0. impl->query.bindValue(offset - 1, retain ? QByteArray(value, length) : -- cgit v1.2.1