aboutsummaryrefslogtreecommitdiff
path: root/qlite/src/database.vala
diff options
context:
space:
mode:
Diffstat (limited to 'qlite/src/database.vala')
-rw-r--r--qlite/src/database.vala55
1 files changed, 20 insertions, 35 deletions
diff --git a/qlite/src/database.vala b/qlite/src/database.vala
index ac53d51c..ad8505be 100644
--- a/qlite/src/database.vala
+++ b/qlite/src/database.vala
@@ -2,17 +2,6 @@ using Sqlite;
namespace Qlite {
-public errordomain DatabaseError {
- ILLEGAL_QUERY,
- NOT_SUPPORTED,
- OPEN_ERROR,
- PREPARE_ERROR,
- EXEC_ERROR,
- NON_UNIQUE,
- ILLEGAL_REFERENCE,
- NOT_INITIALIZED
-}
-
public class Database {
private string file_name;
private Sqlite.Database db;
@@ -33,29 +22,25 @@ public class Database {
meta_table.init({meta_name, meta_int_val, meta_text_val});
}
- public void init(Table[] tables) throws DatabaseError {
+ public void init(Table[] tables) {
Sqlite.config(Config.SERIALIZED);
int ec = Sqlite.Database.open_v2(file_name, out db, OPEN_READWRITE | OPEN_CREATE | 0x00010000);
if (ec != Sqlite.OK) {
- throw new DatabaseError.OPEN_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
+ error(@"SQLite error: %d - %s", db.errcode(), db.errmsg());
}
this.tables = tables;
start_migration();
if (debug) db.trace((message) => print(@"Qlite trace: $message\n"));
}
- public void ensure_init() throws DatabaseError {
- if (tables == null) throw new DatabaseError.NOT_INITIALIZED(@"Database $file_name was not initialized, call init()");
+ public void ensure_init() {
+ if (tables == null) error(@"Database $file_name was not initialized, call init()");
}
- private void start_migration() throws DatabaseError {
+ private void start_migration() {
meta_table.create_table_at_version(expected_version);
long old_version = 0;
- try {
- old_version = meta_table.row_with(meta_name, "version")[meta_int_val, -1];
- } catch (DatabaseError e) {
- old_version = -1;
- }
+ old_version = meta_table.row_with(meta_name, "version")[meta_int_val, -1];
if (old_version == -1) {
foreach (Table t in tables) {
t.create_table_at_version(expected_version);
@@ -97,61 +82,61 @@ public class Database {
// To be implemented by actual implementation if required
// new table columns are added, outdated columns are still present and will be removed afterwards
- public virtual void migrate(long old_version) throws DatabaseError {
+ public virtual void migrate(long old_version) {
}
- public QueryBuilder select(Column[]? columns = null) throws DatabaseError {
+ public QueryBuilder select(Column[]? columns = null) {
ensure_init();
return new QueryBuilder(this).select(columns);
}
- public InsertBuilder insert() throws DatabaseError {
+ public InsertBuilder insert() {
ensure_init();
return new InsertBuilder(this);
}
- public UpdateBuilder update(Table table) throws DatabaseError {
+ public UpdateBuilder update(Table table) {
ensure_init();
return new UpdateBuilder(this, table);
}
- public UpsertBuilder upsert(Table table) throws DatabaseError {
+ public UpsertBuilder upsert(Table table) {
ensure_init();
return new UpsertBuilder(this, table);
}
- public UpdateBuilder update_named(string table) throws DatabaseError {
+ public UpdateBuilder update_named(string table) {
ensure_init();
return new UpdateBuilder.for_name(this, table);
}
- public DeleteBuilder delete() throws DatabaseError {
+ public DeleteBuilder delete() {
ensure_init();
return new DeleteBuilder(this);
}
- public RowIterator query_sql(string sql, string[]? args = null) throws DatabaseError {
+ public RowIterator query_sql(string sql, string[]? args = null) {
ensure_init();
return new RowIterator(this, sql, args);
}
- internal Statement prepare(string sql) throws DatabaseError {
+ internal Statement prepare(string sql) {
ensure_init();
Sqlite.Statement statement;
if (db.prepare_v2(sql, sql.length, out statement) != OK) {
- throw new DatabaseError.PREPARE_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
+ error("SQLite error: %d - %s", db.errcode(), db.errmsg());
}
return statement;
}
- public void exec(string sql) throws DatabaseError {
+ public void exec(string sql) {
ensure_init();
if (db.exec(sql) != OK) {
- throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
+ throw new Error(-1, 0, @"SQLite error: $(db.errcode()) - $(db.errmsg())");
}
}
- public bool is_known_column(string table, string field) throws DatabaseError {
+ public bool is_known_column(string table, string field) {
ensure_init();
foreach (Table t in tables) {
if (t.is_known_column(field)) return true;
@@ -160,4 +145,4 @@ public class Database {
}
}
-} \ No newline at end of file
+}