From 0ceaaadb201a710baa2b6f358c3b29b4fcc192d6 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Tue, 19 Jun 2018 12:52:00 +0200 Subject: QLite: Allow to easily fetch a single row from table --- qlite/src/query_builder.vala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'qlite/src') diff --git a/qlite/src/query_builder.vala b/qlite/src/query_builder.vala index dbfdef2a..75fe0499 100644 --- a/qlite/src/query_builder.vala +++ b/qlite/src/query_builder.vala @@ -98,10 +98,16 @@ public class QueryBuilder : StatementBuilder { } public QueryBuilder limit(int limit) { + if (this.limit_val != 0 && limit > this.limit_val) error("tried to increase an existing limit"); this.limit_val = limit; return this; } + public QueryBuilder single() { + this.single_result = true; + return limit(1); + } + public int64 count() { this.column_selector = @"COUNT($column_selector) AS count"; this.single_result = true; @@ -117,8 +123,8 @@ public class QueryBuilder : StatementBuilder { return new RowOption(row_()); } - public T get(Column field) { - return row()[field]; + public T get(Column field, T def = null) { + return row().get(field, def); } internal override Statement prepare() { -- cgit v1.2.3-70-g09d2 From 063d0146f922b1ed6388f40e05ae8eb0c547083d Mon Sep 17 00:00:00 2001 From: Marvin W Date: Thu, 12 Jul 2018 20:27:50 +0200 Subject: QLite: Add OFFSET statement --- qlite/src/query_builder.vala | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'qlite/src') diff --git a/qlite/src/query_builder.vala b/qlite/src/query_builder.vala index 75fe0499..915e2d2d 100644 --- a/qlite/src/query_builder.vala +++ b/qlite/src/query_builder.vala @@ -20,8 +20,9 @@ public class QueryBuilder : StatementBuilder { // ORDER BY [...] private OrderingTerm[]? order_by_terms = {}; - // LIMIT [...] + // LIMIT [...] OFFSET [...] private int limit_val; + private int offset_val; internal QueryBuilder(Database db) { base(db); @@ -103,6 +104,12 @@ public class QueryBuilder : StatementBuilder { return this; } + public QueryBuilder offset(int offset) { + if (this.limit_val == 0) error("limit required before offset"); + this.offset_val = offset; + return this; + } + public QueryBuilder single() { this.single_result = true; return limit(1); @@ -128,7 +135,7 @@ public class QueryBuilder : StatementBuilder { } internal override Statement prepare() { - Statement stmt = db.prepare(@"SELECT $column_selector $(table_name == null ? "" : @"FROM $((!) table_name)") WHERE $selection $(OrderingTerm.all_to_string(order_by_terms)) $(limit_val > 0 ? @" LIMIT $limit_val" : "")"); + Statement stmt = db.prepare(@"SELECT $column_selector $(table_name == null ? "" : @"FROM $((!) table_name)") WHERE $selection $(OrderingTerm.all_to_string(order_by_terms)) $(limit_val > 0 ? @" LIMIT $limit_val OFFSET $offset_val" : "")"); for (int i = 0; i < selection_args.length; i++) { selection_args[i].bind(stmt, i+1); } -- cgit v1.2.3-70-g09d2