From a98c96e314cfefc45c4738d2486e1b87ff8da283 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 6 Sep 2013 12:22:25 +0200 Subject: [PATCH] SqliteTableModel: Fix invalid SQL syntax if there's already a LIMIT Fix the syntax of the SQL string generated by fetchData() if there is already a LIMIT statement at the end of the query. In this case don't add an additional one. This fixes half of issue #31. --- src/sqlitetablemodel.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 122f3ba9..8e87d41b 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -287,9 +287,18 @@ void SqliteTableModel::fetchData(unsigned int from, unsigned to) QString sLimitQuery; if(m_sQuery.startsWith("PRAGMA", Qt::CaseInsensitive) || m_sQuery.startsWith("EXPLAIN", Qt::CaseInsensitive)) + { sLimitQuery = m_sQuery; - else - sLimitQuery = QString("%1 LIMIT %2, %3;").arg(m_sQuery).arg(from).arg(to-from); + } else { + // Remove trailing trailing semicolon + QString queryTemp = rtrimChar(m_sQuery, ';'); + + // If the query ends with a LIMIT statement take it as it is, if not append our own LIMIT part for lazy population + if(queryTemp.contains(QRegExp("LIMIT\\s+\\d+\\s*,\\s*\\d+\\s*$", Qt::CaseInsensitive))) + sLimitQuery = queryTemp; + else + sLimitQuery = QString("%1 LIMIT %2, %3;").arg(queryTemp).arg(from).arg(to-from); + } m_db->logSQL(sLimitQuery, kLogMsg_App); QByteArray utf8Query = sLimitQuery.toUtf8(); sqlite3_stmt *stmt;