aboutsummaryrefslogtreecommitdiff
path: root/qlite
diff options
context:
space:
mode:
Diffstat (limited to 'qlite')
-rw-r--r--qlite/src/database.vala55
-rw-r--r--qlite/src/delete_builder.vala16
-rw-r--r--qlite/src/insert_builder.vala12
-rw-r--r--qlite/src/query_builder.vala24
-rw-r--r--qlite/src/row.vala6
-rw-r--r--qlite/src/statement_builder.vala4
-rw-r--r--qlite/src/table.vala30
-rw-r--r--qlite/src/update_builder.vala16
-rw-r--r--qlite/src/upsert_builder.vala14
9 files changed, 81 insertions, 96 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
+}
diff --git a/qlite/src/delete_builder.vala b/qlite/src/delete_builder.vala
index 5483b652..e7e3b784 100644
--- a/qlite/src/delete_builder.vala
+++ b/qlite/src/delete_builder.vala
@@ -16,8 +16,8 @@ public class DeleteBuilder : StatementBuilder {
base(db);
}
- public DeleteBuilder from(Table table) throws DatabaseError {
- if (this.table != null) throw new DatabaseError.ILLEGAL_QUERY("cannot use from() multiple times.");
+ public DeleteBuilder from(Table table) {
+ if (this.table != null) error("Qlite Error: ILLEGAL QUERY: cannot use from() multiple times.");
this.table = table;
this.table_name = table.name;
return this;
@@ -28,8 +28,8 @@ public class DeleteBuilder : StatementBuilder {
return this;
}
- public DeleteBuilder where(string selection, string[]? selection_args = null) throws DatabaseError {
- if (this.selection != "1") throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called.");
+ public DeleteBuilder where(string selection, string[]? selection_args = null) {
+ if (this.selection != "1") error("selection was already done, but where() was called.");
this.selection = selection;
foreach (string arg in selection_args) {
this.selection_args += new StatementBuilder.StringField(arg);
@@ -43,7 +43,7 @@ public class DeleteBuilder : StatementBuilder {
return this;
}
- internal override Statement prepare() throws DatabaseError {
+ internal override Statement prepare() {
Statement stmt = db.prepare(@"DELETE FROM $table_name WHERE $selection");
for (int i = 0; i < selection_args.length; i++) {
selection_args[i].bind(stmt, i+1);
@@ -51,12 +51,12 @@ public class DeleteBuilder : StatementBuilder {
return stmt;
}
- public void perform() throws DatabaseError {
+ public void perform() {
if (prepare().step() != DONE) {
- throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
+ error(@"SQLite error: %d - %s", db.errcode(), db.errmsg());
}
}
}
-} \ No newline at end of file
+}
diff --git a/qlite/src/insert_builder.vala b/qlite/src/insert_builder.vala
index 91388f69..b66464a6 100644
--- a/qlite/src/insert_builder.vala
+++ b/qlite/src/insert_builder.vala
@@ -45,13 +45,13 @@ public class InsertBuilder : StatementBuilder {
return this;
}
- public InsertBuilder value_null<T>(Column<T> column) throws DatabaseError {
- if (column.not_null) throw new DatabaseError.ILLEGAL_QUERY(@"Can't set non-null column $(column.name) to null");
+ public InsertBuilder value_null<T>(Column<T> column) {
+ if (column.not_null) error("Qlite Error: ILLEGAL QUERY: Can't set non-null column %s to null", column.name);
fields += new NullField<T>(column);
return this;
}
- internal override Statement prepare() throws DatabaseError {
+ internal override Statement prepare() {
string fields_text = "";
string value_qs = "";
for (int i = 0; i < fields.length; i++) {
@@ -72,13 +72,13 @@ public class InsertBuilder : StatementBuilder {
return stmt;
}
- public int64 perform() throws DatabaseError {
+ public int64 perform() {
if (prepare().step() != DONE) {
- throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
+ error(@"SQLite error: %d - %s", db.errcode(), db.errmsg());
}
return db.last_insert_rowid();
}
}
-} \ No newline at end of file
+}
diff --git a/qlite/src/query_builder.vala b/qlite/src/query_builder.vala
index f46fe98c..dbfdef2a 100644
--- a/qlite/src/query_builder.vala
+++ b/qlite/src/query_builder.vala
@@ -49,8 +49,8 @@ public class QueryBuilder : StatementBuilder {
return this;
}
- public QueryBuilder from(Table table) throws DatabaseError {
- if (this.table_name != null) throw new DatabaseError.ILLEGAL_QUERY("cannot use from() multiple times.");
+ public QueryBuilder from(Table table) {
+ if (this.table_name != null) error("cannot use from() multiple times.");
this.table = table;
this.table_name = table.name;
return this;
@@ -61,8 +61,8 @@ public class QueryBuilder : StatementBuilder {
return this;
}
- public QueryBuilder where(string selection, string[] selection_args = {}) throws DatabaseError {
- if (this.selection != "1") throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called.");
+ public QueryBuilder where(string selection, string[] selection_args = {}) {
+ if (this.selection != "1") error("selection was already done, but where() was called.");
this.selection = selection;
foreach (string arg in selection_args) {
this.selection_args += new StatementBuilder.StringField(arg);
@@ -102,26 +102,26 @@ public class QueryBuilder : StatementBuilder {
return this;
}
- public int64 count() throws DatabaseError {
+ public int64 count() {
this.column_selector = @"COUNT($column_selector) AS count";
this.single_result = true;
return row().get_integer("count");
}
- private Row? row_() throws DatabaseError {
- if (!single_result) throw new DatabaseError.NON_UNIQUE("query is not suited to return a single row, but row() was called.");
+ private Row? row_() {
+ if (!single_result) error("query is not suited to return a single row, but row() was called.");
return iterator().get_next();
}
- public RowOption row() throws DatabaseError {
+ public RowOption row() {
return new RowOption(row_());
}
- public T get<T>(Column<T> field) throws DatabaseError {
+ public T get<T>(Column<T> field) {
return row()[field];
}
- internal override Statement prepare() throws DatabaseError {
+ internal override Statement prepare() {
Statement stmt = db.prepare(@"SELECT $column_selector $(table_name == null ? "" : @"FROM $((!) table_name)") WHERE $selection $(OrderingTerm.all_to_string(order_by_terms)) $(limit_val > 0 ? @" LIMIT $limit_val" : "")");
for (int i = 0; i < selection_args.length; i++) {
selection_args[i].bind(stmt, i+1);
@@ -129,7 +129,7 @@ public class QueryBuilder : StatementBuilder {
return stmt;
}
- public RowIterator iterator() throws DatabaseError {
+ public RowIterator iterator() {
return new RowIterator.from_query_builder(db, this);
}
@@ -164,4 +164,4 @@ public class QueryBuilder : StatementBuilder {
}
}
-} \ No newline at end of file
+}
diff --git a/qlite/src/row.vala b/qlite/src/row.vala
index 8854656f..be459719 100644
--- a/qlite/src/row.vala
+++ b/qlite/src/row.vala
@@ -56,12 +56,12 @@ public class RowIterator {
private Database db;
private Statement stmt;
- public RowIterator.from_query_builder(Database db, QueryBuilder query) throws DatabaseError {
+ public RowIterator.from_query_builder(Database db, QueryBuilder query) {
this.db = db;
this.stmt = query.prepare();
}
- public RowIterator(Database db, string sql, string[]? args = null) throws DatabaseError {
+ public RowIterator(Database db, string sql, string[]? args = null) {
this.db = db;
this.stmt = db.prepare(sql);
if (args != null) {
@@ -111,4 +111,4 @@ public class RowOption {
}
}
-} \ No newline at end of file
+}
diff --git a/qlite/src/statement_builder.vala b/qlite/src/statement_builder.vala
index 6097a9cc..f4dac121 100644
--- a/qlite/src/statement_builder.vala
+++ b/qlite/src/statement_builder.vala
@@ -9,7 +9,7 @@ public abstract class StatementBuilder {
this.db = db;
}
- internal abstract Statement prepare() throws DatabaseError;
+ internal abstract Statement prepare();
internal abstract class AbstractField<T> {
public T value;
@@ -55,4 +55,4 @@ public abstract class StatementBuilder {
}
}
-} \ No newline at end of file
+}
diff --git a/qlite/src/table.vala b/qlite/src/table.vala
index 82759dd1..0d9fe2f6 100644
--- a/qlite/src/table.vala
+++ b/qlite/src/table.vala
@@ -49,42 +49,42 @@ public class Table {
add_post_statement(stmt);
}
- private void ensure_init() throws DatabaseError {
- if (columns == null) throw new DatabaseError.NOT_INITIALIZED(@"Table $name was not initialized, call init()");
+ private void ensure_init() {
+ if (columns == null) error("Table %s was not initialized, call init()", name);
}
- public QueryBuilder select(Column[]? columns = null) throws DatabaseError {
+ public QueryBuilder select(Column[]? columns = null) {
ensure_init();
return db.select(columns).from(this);
}
- public InsertBuilder insert() throws DatabaseError {
+ public InsertBuilder insert() {
ensure_init();
return db.insert().into(this);
}
- public UpdateBuilder update() throws DatabaseError {
+ public UpdateBuilder update() {
ensure_init();
return db.update(this);
}
- public UpsertBuilder upsert() throws DatabaseError {
+ public UpsertBuilder upsert() {
ensure_init();
return db.upsert(this);
}
- public DeleteBuilder delete() throws DatabaseError {
+ public DeleteBuilder delete() {
ensure_init();
return db.delete().from(this);
}
- public RowOption row_with<T>(Column<T> column, T value) throws DatabaseError {
+ public RowOption row_with<T>(Column<T> column, T value) {
ensure_init();
- if (!column.unique && !column.primary_key) throw new DatabaseError.NON_UNIQUE(@"$(column.name) is not suited to identify a row, but used with row_with()");
+ if (!column.unique && !column.primary_key) error("%s is not suited to identify a row, but used with row_with()", column.name);
return select().with(column, "=", value).row();
}
- public bool is_known_column(string column) throws DatabaseError {
+ public bool is_known_column(string column) {
ensure_init();
foreach (Column c in columns) {
if (c.name == column) return true;
@@ -92,7 +92,7 @@ public class Table {
return false;
}
- public void create_table_at_version(long version) throws DatabaseError {
+ public void create_table_at_version(long version) {
ensure_init();
string sql = @"CREATE TABLE IF NOT EXISTS $name (";
for (int i = 0; i < columns.length; i++) {
@@ -105,7 +105,7 @@ public class Table {
db.exec(sql);
}
- public void add_columns_for_version(long old_version, long new_version) throws DatabaseError {
+ public void add_columns_for_version(long old_version, long new_version) {
ensure_init();
foreach (Column c in columns) {
if (c.min_version <= new_version && c.max_version >= new_version && c.min_version > old_version) {
@@ -114,7 +114,7 @@ public class Table {
}
}
- public void delete_columns_for_version(long old_version, long new_version) throws DatabaseError {
+ public void delete_columns_for_version(long old_version, long new_version) {
bool column_deletion_required = false;
string column_list = "";
foreach (Column c in columns) {
@@ -137,11 +137,11 @@ public class Table {
}
}
- internal void post() throws DatabaseError {
+ internal void post() {
foreach (string stmt in post_statements) {
db.exec(stmt);
}
}
}
-} \ No newline at end of file
+}
diff --git a/qlite/src/update_builder.vala b/qlite/src/update_builder.vala
index 139009c5..f675a85c 100644
--- a/qlite/src/update_builder.vala
+++ b/qlite/src/update_builder.vala
@@ -39,14 +39,14 @@ public class UpdateBuilder : StatementBuilder {
return this;
}
- public UpdateBuilder set_null<T>(Column<T> column) throws DatabaseError {
- if (column.not_null) throw new DatabaseError.ILLEGAL_QUERY(@"Can't set non-null column $(column.name) to null");
+ public UpdateBuilder set_null<T>(Column<T> column) {
+ if (column.not_null) error("Can't set non-null column %s to null", column.name);
fields += new NullField<T>(column);
return this;
}
- public UpdateBuilder where(string selection, string[] selection_args = {}) throws DatabaseError {
- if (this.selection != "1") throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called.");
+ public UpdateBuilder where(string selection, string[] selection_args = {}) {
+ if (this.selection != "1") error("selection was already done, but where() was called.");
this.selection = selection;
foreach (string arg in selection_args) {
this.selection_args += new StatementBuilder.StringField(arg);
@@ -70,7 +70,7 @@ public class UpdateBuilder : StatementBuilder {
return this;
}
- internal override Statement prepare() throws DatabaseError {
+ internal override Statement prepare() {
string sql = "UPDATE";
if (or_val != null) sql += @" OR $((!)or_val)";
sql += @" $table_name SET ";
@@ -91,13 +91,13 @@ public class UpdateBuilder : StatementBuilder {
return stmt;
}
- public void perform() throws DatabaseError {
+ public void perform() {
if (fields.length == 0) return;
if (prepare().step() != DONE) {
- throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
+ error("SQLite error: %d - %s", db.errcode(), db.errmsg());
}
}
}
-} \ No newline at end of file
+}
diff --git a/qlite/src/upsert_builder.vala b/qlite/src/upsert_builder.vala
index eb835027..54ba9924 100644
--- a/qlite/src/upsert_builder.vala
+++ b/qlite/src/upsert_builder.vala
@@ -26,14 +26,14 @@ public class UpsertBuilder : StatementBuilder {
return this;
}
- public UpsertBuilder value_null<T>(Column<T> column) throws DatabaseError {
- if (column.not_null) throw new DatabaseError.ILLEGAL_QUERY(@"Can't set non-null column $(column.name) to null");
+ public UpsertBuilder value_null<T>(Column<T> column) {
+ if (column.not_null) error("Can't set non-null column %s to null", column.name);
fields += new NullField<T>(column);
return this;
}
- internal override Statement prepare() throws DatabaseError {
- throw new DatabaseError.NOT_SUPPORTED("prepare() not available for upsert.");
+ internal override Statement prepare() {
+ error("prepare() not available for upsert.");
}
internal Statement prepare_update() {
@@ -98,13 +98,13 @@ public class UpsertBuilder : StatementBuilder {
return stmt;
}
- public int64 perform() throws DatabaseError {
+ public int64 perform() {
if (prepare_update().step() != DONE || prepare_insert().step() != DONE) {
- throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
+ error(@"SQLite error: %d - %s", db.errcode(), db.errmsg());
}
return db.last_insert_rowid();
}
}
-} \ No newline at end of file
+}