summaryrefslogtreecommitdiffstats
path: root/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp
diff options
context:
space:
mode:
authorSamuel Gaist <samuel.gaist@edeltech.ch>2017-02-16 14:46:09 +0100
committerSamuel Gaist <samuel.gaist@edeltech.ch>2017-04-04 12:02:10 +0000
commit9076046a109cb8c60254314f7d29f7e044c5946f (patch)
tree60fd45021f75210ca66b0dd8e837d6f777d9bc90 /src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp
parent8f93d6632ac1e2876a0c90d267be0294173ac97b (diff)
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 <Jesus.Fernandez@qt.io> Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp')
-rw-r--r--src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp29
1 files changed, 27 insertions, 2 deletions
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<int>& 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;
}