From 56bc45ce4d07a7a9a415e9dc8ad2f7c3f3c9e48d Mon Sep 17 00:00:00 2001 From: fiaxh Date: Thu, 2 Mar 2017 15:37:32 +0100 Subject: Initial commit --- qlite/src/row.vala | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 qlite/src/row.vala (limited to 'qlite/src/row.vala') diff --git a/qlite/src/row.vala b/qlite/src/row.vala new file mode 100644 index 00000000..905d12a1 --- /dev/null +++ b/qlite/src/row.vala @@ -0,0 +1,79 @@ +using Gee; +using Sqlite; + +namespace Qlite { + +public class Row { + private Map text_map = new HashMap(); + private Map int_map = new HashMap(); + private Map real_map = new HashMap(); + + public Row(Statement stmt) { + for (int i = 0; i < stmt.column_count(); i++) { + switch(stmt.column_type(i)) { + case TEXT: + text_map[stmt.column_name(i)] = stmt.column_text(i); + break; + case INTEGER: + int_map[stmt.column_name(i)] = (long) stmt.column_int64(i); + break; + case FLOAT: + real_map[stmt.column_name(i)] = stmt.column_double(i); + break; + } + } + } + + public T get(Column field) { + return field[this]; + } + + public string? get_text(string field) { + if (text_map.contains(field)) { + return text_map[field]; + } + return null; + } + + public long get_integer(string field) { + return int_map[field]; + } + + public bool has_integer(string field) { + return int_map.contains(field); + } + + public double get_real(string field) { + return real_map[field]; + } + + public bool has_real(string field) { + return real_map.contains(field) && real_map[field] != null; + } + + public class RowIterator { + private Statement stmt; + + public RowIterator.from_query_builder(QueryBuilder query) throws DatabaseError { + this.stmt = query.prepare(); + } + + public RowIterator(Database db, string sql, string[]? args = null) { + this.stmt = db.prepare(sql); + if (args != null) { + for (int i = 0; i < args.length; i++) { + stmt.bind_text(i, sql, sql.length); + } + } + } + + public Row? next_value() { + if (stmt.step() == Sqlite.ROW) { + return new Row(stmt); + } + return null; + } + } +} + +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf