dino/qlite/src/row.vala

148 lines
4.1 KiB
Vala
Raw Permalink Normal View History

2017-03-02 14:37:32 +00:00
using Gee;
using Sqlite;
namespace Qlite {
public class Row {
2017-04-16 13:11:00 +00:00
private Map<string, string?> text_map = new HashMap<string, string?>();
2017-03-02 14:37:32 +00:00
private Map<string, long> int_map = new HashMap<string, long>();
private Map<string, double?> real_map = new HashMap<string, double?>();
internal Row(Statement stmt) {
2017-03-02 14:37:32 +00:00
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);
}
2017-03-02 14:37:32 +00:00
switch(stmt.column_type(i)) {
case TEXT:
text_map[column_name] = stmt.column_text(i);
2017-03-02 14:37:32 +00:00
break;
case INTEGER:
int_map[column_name] = (long) stmt.column_int64(i);
2017-03-02 14:37:32 +00:00
break;
case FLOAT:
real_map[column_name] = stmt.column_double(i);
2017-03-02 14:37:32 +00:00
break;
}
}
}
public T get<T>(Column<T> 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)];
2017-03-02 14:37:32 +00:00
}
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));
2017-03-02 14:37:32 +00:00
}
public double get_real(string field, string? table = null, double def = 0) {
return real_map[field_name(field, table)] ?? def;
2017-03-02 14:37:32 +00:00
}
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;
2017-03-02 14:37:32 +00:00
}
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 + "}";
2017-03-02 14:37:32 +00:00
}
}
2017-03-02 14:37:32 +00:00
public class RowIterator {
private Database db;
private Statement stmt;
2017-03-02 14:37:32 +00:00
2017-10-28 21:48:07 +00:00
public RowIterator.from_query_builder(Database db, QueryBuilder query) {
2017-03-22 22:55:19 +00:00
this.db = db;
this.stmt = query.prepare();
}
2017-03-02 14:37:32 +00:00
2017-10-28 21:48:07 +00:00
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);
2017-03-02 14:37:32 +00:00
}
}
}
2017-03-02 14:37:32 +00:00
2017-04-16 13:11:00 +00:00
public bool next() {
int r = stmt.step();
2017-04-16 13:11:00 +00:00
if (r == Sqlite.ROW) return true;
if (r == Sqlite.DONE) return false;
2019-03-15 19:56:19 +00:00
warning(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
2017-04-16 13:11:00 +00:00
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<T>(Column<T> field, T def = null) {
2017-04-16 13:11:00 +00:00
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);
2017-03-02 14:37:32 +00:00
}
}
2017-10-28 21:48:07 +00:00
}