From cec0404cba8ffaaf10057739add6e00e4a304d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 24 Mar 2020 18:11:24 +1100 Subject: [PATCH] Support for LIMIT/OFFSET in SQLite3_result() --- include/sqlite3db.h | 3 ++- lib/sqlite3db.cpp | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/include/sqlite3db.h b/include/sqlite3db.h index 0750289e5..75ae9dc5b 100644 --- a/include/sqlite3db.h +++ b/include/sqlite3db.h @@ -44,10 +44,11 @@ class SQLite3_result { SQLite3_result(SQLite3_result *); unsigned long long get_size(); void add_column_definition(int a, const char *b); - int add_row(sqlite3_stmt *stmt); + int add_row(sqlite3_stmt *stmt, bool skip=false); int add_row(char **_fields); int add_row(SQLite3_row *old_row); SQLite3_result(sqlite3_stmt *stmt); + SQLite3_result(sqlite3_stmt *stmt, int *found_rows, unsigned int offset, unsigned int limit); SQLite3_result(int num_columns, bool en_mutex=false); ~SQLite3_result(); void dump_to_stderr(); diff --git a/lib/sqlite3db.cpp b/lib/sqlite3db.cpp index 8c6458f09..537903f0f 100644 --- a/lib/sqlite3db.cpp +++ b/lib/sqlite3db.cpp @@ -554,13 +554,15 @@ void SQLite3_result::add_column_definition(int a, const char *b) { column_definition.push_back(cf); } -int SQLite3_result::add_row(sqlite3_stmt *stmt) { +int SQLite3_result::add_row(sqlite3_stmt *stmt, bool skip) { int rc=sqlite3_step(stmt); if (rc!=SQLITE_ROW) return rc; - SQLite3_row *row=new SQLite3_row(columns); - row->add_fields(stmt); - rows.push_back(row); - rows_count++; + if (skip==false) { + SQLite3_row *row=new SQLite3_row(columns); + row->add_fields(stmt); + rows.push_back(row); + rows_count++; + } return SQLITE_ROW; } @@ -595,6 +597,31 @@ SQLite3_result::SQLite3_result(sqlite3_stmt *stmt) { while (add_row(stmt)==SQLITE_ROW) {}; } +SQLite3_result::SQLite3_result(sqlite3_stmt *stmt, int * found_rows, unsigned int offset, unsigned int limit) { + rows_count=0; + int fr = 0; + columns=sqlite3_column_count(stmt); + for (int i=0; i 0 && rc==SQLITE_ROW) { + rc = add_row(stmt, true); + if (rc == SQLITE_ROW) fr++; + offset--; + } + while (limit > 0 && rc==SQLITE_ROW) { + rc = add_row(stmt, false); + if (rc == SQLITE_ROW) fr++; + limit--; + } + while (rc == SQLITE_ROW) { + rc=add_row(stmt, true); + if (rc == SQLITE_ROW) fr++; + } + *found_rows = fr; +} + SQLite3_result::SQLite3_result(int num_columns, bool en_mutex) { rows_count=0; columns=num_columns;