| 1 |
using Sqlite; |
| 2 |
|
| 3 |
namespace Qlite { |
| 4 |
|
| 5 |
public class Database { |
| 6 |
private string file_name; |
| 7 |
private Sqlite.Database db; |
| 8 |
private long expected_version; |
| 9 |
private Table[]? tables; |
| 10 |
|
| 11 |
private Column<string?> meta_name = new Column.Text("name") { primary_key = true }; |
| 12 |
private Column<long> meta_int_val = new Column.Long("int_val"); |
| 13 |
private Column<string?> meta_text_val = new Column.Text("text_val"); |
| 14 |
private Table meta_table; |
| 15 |
|
| 16 |
public bool debug = false; |
| 17 |
|
| 18 |
public Database(string file_name, long expected_version) { |
| 19 |
this.file_name = file_name; |
| 20 |
this.expected_version = expected_version; |
| 21 |
meta_table = new Table(this, "_meta"); |
| 22 |
meta_table.init({meta_name, meta_int_val, meta_text_val}); |
| 23 |
} |
| 24 |
|
| 25 |
public void init(Table[] tables) { |
| 26 |
Sqlite.config(Config.SERIALIZED); |
| 27 |
int ec = Sqlite.Database.open_v2(file_name, out db, OPEN_READWRITE | OPEN_CREATE | 0x00010000); |
| 28 |
if (ec != Sqlite.OK) { |
| 29 |
error(@"SQLite error: %d - %s", db.errcode(), db.errmsg()); |
| 30 |
} |
| 31 |
this.tables = tables; |
| 32 |
if (debug) db.trace((message) => print(@"Qlite trace: $message\n")); |
| 33 |
start_migration(); |
| 34 |
} |
| 35 |
|
| 36 |
public void ensure_init() { |
| 37 |
if (tables == null) error(@"Database $file_name was not initialized, call init()"); |
| 38 |
} |
| 39 |
|
| 40 |
private void start_migration() { |
| 41 |
try { |
| 42 |
exec("BEGIN TRANSACTION"); |
| 43 |
} catch (Error e) { |
| 44 |
error("SQLite error: %d - %s", db.errcode(), db.errmsg()); |
| 45 |
} |
| 46 |
meta_table.create_table_at_version(expected_version); |
| 47 |
long old_version = 0; |
| 48 |
old_version = meta_table.row_with(meta_name, "version")[meta_int_val, -1]; |
| 49 |
if (old_version == -1) { |
| 50 |
foreach (Table t in tables) { |
| 51 |
t.create_table_at_version(expected_version); |
| 52 |
} |
| 53 |
meta_table.insert().value(meta_name, "version").value(meta_int_val, expected_version).perform(); |
| 54 |
} else if (expected_version != old_version) { |
| 55 |
foreach (Table t in tables) { |
| 56 |
t.create_table_at_version(old_version); |
| 57 |
} |
| 58 |
foreach (Table t in tables) { |
| 59 |
t.add_columns_for_version(old_version, expected_version); |
| 60 |
} |
| 61 |
migrate(old_version); |
| 62 |
foreach (Table t in tables) { |
| 63 |
t.delete_columns_for_version(old_version, expected_version); |
| 64 |
} |
| 65 |
if (old_version == -1) { |
| 66 |
meta_table.insert().value(meta_name, "version").value(meta_int_val, expected_version).perform(); |
| 67 |
} else { |
| 68 |
meta_table.update().with(meta_name, "=", "version").set(meta_int_val, expected_version).perform(); |
| 69 |
} |
| 70 |
} |
| 71 |
foreach (Table t in tables) { |
| 72 |
t.post(); |
| 73 |
} |
| 74 |
try { |
| 75 |
exec("END TRANSACTION"); |
| 76 |
} catch (Error e) { |
| 77 |
error("SQLite error: %d - %s", db.errcode(), db.errmsg()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
internal int errcode() { |
| 82 |
return db.errcode(); |
| 83 |
} |
| 84 |
|
| 85 |
internal string errmsg() { |
| 86 |
return db.errmsg(); |
| 87 |
} |
| 88 |
|
| 89 |
internal int64 last_insert_rowid() { |
| 90 |
return db.last_insert_rowid(); |
| 91 |
} |
| 92 |
|
| 93 |
// To be implemented by actual implementation if required |
| 94 |
// new table columns are added, outdated columns are still present and will be removed afterwards |
| 95 |
public virtual void migrate(long old_version) { |
| 96 |
} |
| 97 |
|
| 98 |
public QueryBuilder select(Column[]? columns = null) { |
| 99 |
ensure_init(); |
| 100 |
return new QueryBuilder(this).select(columns); |
| 101 |
} |
| 102 |
|
| 103 |
internal MatchQueryBuilder match_query(Table table) { |
| 104 |
ensure_init(); |
| 105 |
return new MatchQueryBuilder(this, table); |
| 106 |
} |
| 107 |
|
| 108 |
public InsertBuilder insert() { |
| 109 |
ensure_init(); |
| 110 |
return new InsertBuilder(this); |
| 111 |
} |
| 112 |
|
| 113 |
public UpdateBuilder update(Table table) { |
| 114 |
ensure_init(); |
| 115 |
return new UpdateBuilder(this, table); |
| 116 |
} |
| 117 |
|
| 118 |
public UpsertBuilder upsert(Table table) { |
| 119 |
ensure_init(); |
| 120 |
return new UpsertBuilder(this, table); |
| 121 |
} |
| 122 |
|
| 123 |
public UpdateBuilder update_named(string table) { |
| 124 |
ensure_init(); |
| 125 |
return new UpdateBuilder.for_name(this, table); |
| 126 |
} |
| 127 |
|
| 128 |
public DeleteBuilder delete() { |
| 129 |
ensure_init(); |
| 130 |
return new DeleteBuilder(this); |
| 131 |
} |
| 132 |
|
| 133 |
public RowIterator query_sql(string sql, string[]? args = null) { |
| 134 |
ensure_init(); |
| 135 |
return new RowIterator(this, sql, args); |
| 136 |
} |
| 137 |
|
| 138 |
internal Statement prepare(string sql) { |
| 139 |
ensure_init(); |
| 140 |
Sqlite.Statement statement; |
| 141 |
if (db.prepare_v2(sql, sql.length, out statement) != OK) { |
| 142 |
error("SQLite error: %d - %s: %s", db.errcode(), db.errmsg(), sql); |
| 143 |
} |
| 144 |
return statement; |
| 145 |
} |
| 146 |
|
| 147 |
public void exec(string sql) throws Error { |
| 148 |
ensure_init(); |
| 149 |
if (db.exec(sql) != OK) { |
| 150 |
throw new Error(-1, 0, "SQLite error: %d - %s", db.errcode(), db.errmsg()); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public bool is_known_column(string table, string field) { |
| 155 |
ensure_init(); |
| 156 |
foreach (Table t in tables) { |
| 157 |
if (t.is_known_column(field)) return true; |
| 158 |
} |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
} |