Debian Codegraph @

qlite/src/row.vala

raw
1 using Gee;
2 using Sqlite;
3
4 namespace Qlite {
5
6 public class Row {
7 private Map<string, string?> text_map = new HashMap<string, string?>();
8 private Map<string, long> int_map = new HashMap<string, long>();
9 private Map<string, double?> real_map = new HashMap<string, double?>();
10
11 internal Row(Statement stmt) {
12 for (int i = 0; i < stmt.column_count(); i++) {
13 string column_name;
14 if (stmt.column_origin_name(i) != null) {
15 column_name = @"$(stmt.column_table_name(i)).$(stmt.column_origin_name(i))";
16 } else {
17 column_name = stmt.column_name(i);
18 }
19 switch(stmt.column_type(i)) {
20 case TEXT:
21 text_map[column_name] = stmt.column_text(i);
22 break;
23 case INTEGER:
24 int_map[column_name] = (long) stmt.column_int64(i);
25 break;
26 case FLOAT:
27 real_map[column_name] = stmt.column_double(i);
28 break;
29 }
30 }
31 }
32
33 public T get<T>(Column<T> field) {
34 return field[this];
35 }
36
37 private string field_name(string field, string? table) {
38 if (table != null) {
39 return @"$table.$field";
40 } else {
41 return field;
42 }
43 }
44
45 public string? get_text(string field, string? table = null) {
46 if (text_map.has_key(field_name(field, table))) {
47 return text_map[field_name(field, table)];
48 }
49 return null;
50 }
51
52 public long get_integer(string field, string? table = null) {
53 return int_map[field_name(field, table)];
54 }
55
56 public bool has_integer(string field, string? table = null) {
57 return int_map.has_key(field_name(field, table));
58 }
59
60 public double get_real(string field, string? table = null, double def = 0) {
61 return real_map[field_name(field, table)] ?? def;
62 }
63
64 public bool has_real(string field, string? table = null) {
65 return real_map.has_key(field_name(field, table)) && real_map[field_name(field, table)] != null;
66 }
67
68 public string to_string() {
69 string ret = "{";
70
71 foreach (string key in text_map.keys) {
72 if (ret.length > 1) ret += ", ";
73 ret = @"$ret$key: \"$(text_map[key])\"";
74 }
75 foreach (string key in int_map.keys) {
76 if (ret.length > 1) ret += ", ";
77 ret = @"$ret$key: $(int_map[key])";
78 }
79 foreach (string key in real_map.keys) {
80 if (ret.length > 1) ret += ", ";
81 ret = @"$ret$key: $(real_map[key])";
82 }
83
84 return ret + "}";
85 }
86 }
87
88 public class RowIterator {
89 private Database db;
90 private Statement stmt;
91
92 public RowIterator.from_query_builder(Database db, QueryBuilder query) {
93 this.db = db;
94 this.stmt = query.prepare();
95 }
96
97 public RowIterator(Database db, string sql, string[]? args = null) {
98 this.db = db;
99 this.stmt = db.prepare(sql);
100 if (args != null) {
101 for (int i = 0; i < args.length; i++) {
102 stmt.bind_text(i, sql, sql.length);
103 }
104 }
105 }
106
107 public bool next() {
108 int r = stmt.step();
109 if (r == Sqlite.ROW) return true;
110 if (r == Sqlite.DONE) return false;
111 warning(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
112 return false;
113 }
114
115 public Row get() {
116 return new Row(stmt);
117 }
118
119 public Row? get_next() {
120 if (next()) return get();
121 return null;
122 }
123 }
124
125 public class RowOption {
126 public Row? inner { get; private set; }
127
128 public RowOption(Row? row) {
129 this.inner = row;
130 }
131
132 public bool is_present() {
133 return inner != null;
134 }
135
136 public T get<T>(Column<T> field, T def = null) {
137 if (inner == null || field.is_null((!)inner)) return def;
138 return field[(!)inner];
139 }
140
141 internal long get_integer(string field, long def = 0) {
142 if (inner == null || !((!)inner).has_integer(field)) return def;
143 return ((!)inner).get_integer(field);
144 }
145 }
146
147 }