From 9076046a109cb8c60254314f7d29f7e044c5946f Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Thu, 16 Feb 2017 14:46:09 +0100 Subject: SQLite: enable support for named placeholders SQLite has been supporting named placeholders for some time now and the code of the module is also written in that sense. The only thing that currently fails is the parameter count check. If the named placeholder is used several times then the parameter count will not match the value count. This patch adds a second check in that case. This use case is already tested by tst_qsqlquery. [ChangeLog][QtSql][SQLite] Named placeholder can now be used. If compiling Qt by hand and using system libraries, this feature requires at least SQLite 3.3.11. Change-Id: I1f6fa93f72bd809136894eafce0a2ae5cf6a62db Reviewed-by: Jesus Fernandez Reviewed-by: Andy Shaw --- src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp | 29 +++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src/plugins/sqldrivers/sqlite') diff --git a/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp b/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp index 8f336fd173..dd773fb022 100644 --- a/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp +++ b/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp @@ -432,8 +432,27 @@ bool QSQLiteResult::exec() d->finalize(); return false; } + int paramCount = sqlite3_bind_parameter_count(d->stmt); - if (paramCount == values.count()) { + bool paramCountIsValid = paramCount == values.count(); + +#if (SQLITE_VERSION_NUMBER >= 3003011) + // In the case of the reuse of a named placeholder + if (!paramCountIsValid) { + const auto countIndexes = [](int counter, const QList& indexList) { + return counter + indexList.length(); + }; + + const int bindParamCount = std::accumulate(d->indexes.cbegin(), + d->indexes.cend(), + 0, + countIndexes); + + paramCountIsValid = bindParamCount == values.count(); + } +#endif + + if (paramCountIsValid) { for (int i = 0; i < paramCount; ++i) { res = SQLITE_OK; const QVariant value = values.at(i); @@ -629,11 +648,17 @@ bool QSQLiteDriver::hasFeature(DriverFeature f) const case EventNotifications: return true; case QuerySize: - case NamedPlaceholders: case BatchOperations: case MultipleResultSets: case CancelQuery: return false; + case NamedPlaceholders: +#if (SQLITE_VERSION_NUMBER < 3003011) + return false; +#else + return true; +#endif + } return false; } -- cgit v1.2.3