From 98ef191bfb3cc5694b9d1001e46a9b00abbd7195 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sun, 23 Aug 2026 19:32:25 +0200 Subject: Move qlite into libdino --- libdino/src/qlite/row.vala | 147 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 libdino/src/qlite/row.vala (limited to 'libdino/src/qlite/row.vala') diff --git a/libdino/src/qlite/row.vala b/libdino/src/qlite/row.vala new file mode 100644 index 00000000..65c80f02 --- /dev/null +++ b/libdino/src/qlite/row.vala @@ -0,0 +1,147 @@ +using Gee; +using Sqlite; + +namespace Dino.Qlite { + +public class Row { + private Map text_map = new HashMap(); + private Map int_map = new HashMap(); + private Map real_map = new HashMap(); + + internal Row(Statement stmt) { + for (int i = 0; i < stmt.column_count(); i++) { + string column_name; + if (stmt.column_origin_name(i) != null) { + column_name = @"$(stmt.column_table_name(i)).$(stmt.column_origin_name(i))"; + } else { + column_name = stmt.column_name(i); + } + switch(stmt.column_type(i)) { + case TEXT: + text_map[column_name] = stmt.column_text(i); + break; + case INTEGER: + int_map[column_name] = (long) stmt.column_int64(i); + break; + case FLOAT: + real_map[column_name] = stmt.column_double(i); + break; + } + } + } + + public T get(Column field) { + return field[this]; + } + + private string field_name(string field, string? table) { + if (table != null) { + return @"$table.$field"; + } else { + return field; + } + } + + public string? get_text(string field, string? table = null) { + if (text_map.has_key(field_name(field, table))) { + return text_map[field_name(field, table)]; + } + return null; + } + + public long get_integer(string field, string? table = null) { + return int_map[field_name(field, table)]; + } + + public bool has_integer(string field, string? table = null) { + return int_map.has_key(field_name(field, table)); + } + + public double get_real(string field, string? table = null, double def = 0) { + return real_map[field_name(field, table)] ?? def; + } + + public bool has_real(string field, string? table = null) { + return real_map.has_key(field_name(field, table)) && real_map[field_name(field, table)] != null; + } + + public string to_string() { + string ret = "{"; + + foreach (string key in text_map.keys) { + if (ret.length > 1) ret += ", "; + ret = @"$ret$key: \"$(text_map[key])\""; + } + foreach (string key in int_map.keys) { + if (ret.length > 1) ret += ", "; + ret = @"$ret$key: $(int_map[key])"; + } + foreach (string key in real_map.keys) { + if (ret.length > 1) ret += ", "; + ret = @"$ret$key: $(real_map[key])"; + } + + return ret + "}"; + } +} + +public class RowIterator { + private Database db; + private Statement stmt; + + public RowIterator.from_query_builder(Database db, QueryBuilder query) { + this.db = db; + this.stmt = query.prepare(); + } + + public RowIterator(Database db, string sql, string[]? args = null) { + this.db = db; + this.stmt = db.prepare(sql); + if (args != null) { + for (int i = 0; i < args.length; i++) { + stmt.bind_text(i, sql, sql.length); + } + } + } + + public bool next() { + int r = stmt.step(); + if (r == Sqlite.ROW) return true; + if (r == Sqlite.DONE) return false; + warning(@"SQLite error: $(db.errcode()) - $(db.errmsg())"); + return false; + } + + public Row get() { + return new Row(stmt); + } + + public Row? get_next() { + if (next()) return get(); + return null; + } +} + +public class RowOption { + public Row? inner { get; private set; } + + public RowOption(Row? row) { + this.inner = row; + } + + public bool is_present() { + return inner != null; + } + + public T get(Column field, T def = null) { + if (inner == null || field.is_null((!)inner)) return def; + return field[(!)inner]; + } + + internal long get_integer(string field, long def = 0) { + if (inner == null || !((!)inner).has_integer(field)) return def; + return ((!)inner).get_integer(field); + } +} + +} -- cgit v1.3 From 4436001bb361ad4f9e4e05819214ecf0ea77c063 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Fri, 23 Oct 2026 16:57:14 +0200 Subject: database: Add indices to improve performance --- libdino/src/qlite/row.vala | 7 ++++--- libdino/src/service/database.vala | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'libdino/src/qlite/row.vala') diff --git a/libdino/src/qlite/row.vala b/libdino/src/qlite/row.vala index 65c80f02..98fc902a 100644 --- a/libdino/src/qlite/row.vala +++ b/libdino/src/qlite/row.vala @@ -11,8 +11,9 @@ public class Row { internal Row(Statement stmt) { for (int i = 0; i < stmt.column_count(); i++) { string column_name; - if (stmt.column_origin_name(i) != null) { - column_name = @"$(stmt.column_table_name(i)).$(stmt.column_origin_name(i))"; + string origin_name = stmt.column_origin_name(i); + if (origin_name != null) { + column_name = field_name(origin_name, stmt.column_table_name(i)); } else { column_name = stmt.column_name(i); } @@ -34,7 +35,7 @@ public class Row { return field[this]; } - private string field_name(string field, string? table) { + private static string field_name(string field, string? table) { if (table != null) { return @"$table.$field"; } else { diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index fba6f7a2..30032e2c 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -7,7 +7,7 @@ using Dino.Qlite; namespace Dino { public class Database : Qlite.Database { - private const int VERSION = 34; + private const int VERSION = 35; public class AccountTable : Table { public Column id = new Column.Integer("id") { primary_key = true, auto_increment = true }; @@ -49,6 +49,7 @@ public class Database : Qlite.Database { base(db, "entity"); init({id, account_id, jid_id, resource, caps_hash, last_seen}); unique({account_id, jid_id, resource}, "IGNORE"); + index("entity_account_jid_resource_idx", {account_id, jid_id, resource}); } } @@ -376,6 +377,7 @@ public class Database : Qlite.Database { init({entity, feature}); unique({entity, feature}, "IGNORE"); index("entity_feature_idx", {entity}); + index("entity_feature_feature_idx", {entity, feature}); } } -- cgit v1.3