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?>();
|
|
|
|
|
2017-03-20 18:27:39 +00:00
|
|
|
internal Row(Statement stmt) {
|
2017-03-02 14:37:32 +00:00
|
|
|
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<T>(Column<T> field) {
|
|
|
|
return field[this];
|
|
|
|
}
|
|
|
|
|
|
|
|
public string? get_text(string field) {
|
2017-03-09 20:46:16 +00:00
|
|
|
if (text_map.has_key(field)) {
|
2017-03-02 14:37:32 +00:00
|
|
|
return text_map[field];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long get_integer(string field) {
|
|
|
|
return int_map[field];
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool has_integer(string field) {
|
2017-03-09 20:46:16 +00:00
|
|
|
return int_map.has_key(field);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 13:11:00 +00:00
|
|
|
public double get_real(string field, double def = 0) {
|
|
|
|
return real_map[field] ?? def;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool has_real(string field) {
|
2017-03-09 20:46:16 +00:00
|
|
|
return real_map.has_key(field) && real_map[field] != null;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-03-12 18:33:31 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-12 18:33:31 +00:00
|
|
|
public class RowIterator {
|
2017-03-20 21:12:20 +00:00
|
|
|
private Database db;
|
2017-03-12 18:33:31 +00:00
|
|
|
private Statement stmt;
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-22 22:55:19 +00:00
|
|
|
public RowIterator.from_query_builder(Database db, QueryBuilder query) throws DatabaseError {
|
|
|
|
this.db = db;
|
2017-03-12 18:33:31 +00:00
|
|
|
this.stmt = query.prepare();
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-12 18:33:31 +00:00
|
|
|
public RowIterator(Database db, string sql, string[]? args = null) throws DatabaseError {
|
2017-03-20 21:12:20 +00:00
|
|
|
this.db = db;
|
2017-03-12 18:33:31 +00:00
|
|
|
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-12 18:33:31 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-04-16 13:11:00 +00:00
|
|
|
public bool next() {
|
2017-03-20 21:12:20 +00:00
|
|
|
int r = stmt.step();
|
2017-04-16 13:11:00 +00:00
|
|
|
if (r == Sqlite.ROW) return true;
|
|
|
|
if (r == Sqlite.DONE) return false;
|
|
|
|
print(@"SQLite error: $(db.errcode()) - $(db.errmsg())\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Row get() {
|
|
|
|
return new Row(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Row? get_next() {
|
|
|
|
if (next()) return get();
|
|
|
|
return null;
|
2017-03-12 18:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|