Skip to content

Commit a98c96e

Browse files
committed
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 sqlitebrowser#31.
1 parent 36b9c25 commit a98c96e

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/sqlitetablemodel.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,18 @@ void SqliteTableModel::fetchData(unsigned int from, unsigned to)
287287

288288
QString sLimitQuery;
289289
if(m_sQuery.startsWith("PRAGMA", Qt::CaseInsensitive) || m_sQuery.startsWith("EXPLAIN", Qt::CaseInsensitive))
290+
{
290291
sLimitQuery = m_sQuery;
291-
else
292-
sLimitQuery = QString("%1 LIMIT %2, %3;").arg(m_sQuery).arg(from).arg(to-from);
292+
} else {
293+
// Remove trailing trailing semicolon
294+
QString queryTemp = rtrimChar(m_sQuery, ';');
295+
296+
// If the query ends with a LIMIT statement take it as it is, if not append our own LIMIT part for lazy population
297+
if(queryTemp.contains(QRegExp("LIMIT\\s+\\d+\\s*,\\s*\\d+\\s*$", Qt::CaseInsensitive)))
298+
sLimitQuery = queryTemp;
299+
else
300+
sLimitQuery = QString("%1 LIMIT %2, %3;").arg(queryTemp).arg(from).arg(to-from);
301+
}
293302
m_db->logSQL(sLimitQuery, kLogMsg_App);
294303
QByteArray utf8Query = sLimitQuery.toUtf8();
295304
sqlite3_stmt *stmt;

0 commit comments

Comments
 (0)