From b9df78e4494879752e9e68dcc5d54e03fffe9467 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 28 Oct 2017 23:48:07 +0200 Subject: [PATCH] Move DatabaseError handling into Qlite --- libdino/src/service/database.vala | 2 +- plugins/http-files/src/plugin.vala | 20 +++---- .../omemo/src/account_settings_widget.vala | 20 +++---- plugins/omemo/src/database.vala | 10 ++-- plugins/omemo/src/manager.vala | 39 ++++--------- plugins/omemo/src/plugin.vala | 42 +++++++------- plugins/omemo/src/pre_key_store.vala | 28 ++++------ plugins/omemo/src/session_store.vala | 32 ++++------- plugins/omemo/src/signed_pre_key_store.vala | 29 ++++------ qlite/src/database.vala | 55 +++++++------------ qlite/src/delete_builder.vala | 16 +++--- qlite/src/insert_builder.vala | 12 ++-- qlite/src/query_builder.vala | 24 ++++---- qlite/src/row.vala | 6 +- qlite/src/statement_builder.vala | 4 +- qlite/src/table.vala | 30 +++++----- qlite/src/update_builder.vala | 16 +++--- qlite/src/upsert_builder.vala | 14 ++--- 18 files changed, 166 insertions(+), 233 deletions(-) diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index 532d9f0d..aaceede1 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -186,7 +186,7 @@ public class Database : Qlite.Database { public Map jid_table_reverse = new HashMap(); public Map account_table_cache = new HashMap(); - public Database(string fileName) throws DatabaseError { + public Database(string fileName) { base(fileName, VERSION); account = new AccountTable(this); jid = new JidTable(this); diff --git a/plugins/http-files/src/plugin.vala b/plugins/http-files/src/plugin.vala index b1c08d80..1fc0c9fd 100644 --- a/plugins/http-files/src/plugin.vala +++ b/plugins/http-files/src/plugin.vala @@ -9,21 +9,17 @@ public class Plugin : RootInterface, Object { public FileProvider file_provider; public void registered(Dino.Application app) { - try { - this.app = app; - Manager.start(this.app.stream_interactor, app.db); + this.app = app; + Manager.start(this.app.stream_interactor, app.db); - file_provider = new FileProvider(app.stream_interactor, app.db); + file_provider = new FileProvider(app.stream_interactor, app.db); - app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { - list.add(new UploadStreamModule()); - }); + app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { + list.add(new UploadStreamModule()); + }); - app.stream_interactor.get_module(FileManager.IDENTITY).add_provider(file_provider); - app.plugin_registry.register_message_display(new FileMessageFilterDisplay(app.db)); - } catch (Error e) { - print(@"Error initializing http-files: $(e.message)\n"); - } + app.stream_interactor.get_module(FileManager.IDENTITY).add_provider(file_provider); + app.plugin_registry.register_message_display(new FileMessageFilterDisplay(app.db)); } public void shutdown() { diff --git a/plugins/omemo/src/account_settings_widget.vala b/plugins/omemo/src/account_settings_widget.vala index da3f6ca2..cec0eecd 100644 --- a/plugins/omemo/src/account_settings_widget.vala +++ b/plugins/omemo/src/account_settings_widget.vala @@ -36,17 +36,13 @@ public class AccountSettingWidget : Plugins.AccountSettingsWidget, Box { public void set_account(Account account) { this.account = account; btn.visible = false; - try { - Qlite.Row? row = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id).inner; - if (row == null) { - fingerprint.set_markup("%s\n%s".printf(_("Own fingerprint"), _("Will be generated on first connect"))); - } else { - string res = fingerprint_markup(fingerprint_from_base64(((!)row)[plugin.db.identity.identity_key_public_base64])); - fingerprint.set_markup("%s\n%s".printf(_("Own fingerprint"), res)); - btn.visible = true; - } - } catch (Qlite.DatabaseError e) { - fingerprint.set_markup("%s\n%s".printf(_("Own fingerprint"), _("Database error"))); + Qlite.Row? row = plugin.db.identity.row_with(plugin.db.identity.account_id, account.id).inner; + if (row == null) { + fingerprint.set_markup("%s\n%s".printf(_("Own fingerprint"), _("Will be generated on first connect"))); + } else { + string res = fingerprint_markup(fingerprint_from_base64(((!)row)[plugin.db.identity.identity_key_public_base64])); + fingerprint.set_markup("%s\n%s".printf(_("Own fingerprint"), res)); + btn.visible = true; } } @@ -54,4 +50,4 @@ public class AccountSettingWidget : Plugins.AccountSettingsWidget, Box { } } -} \ No newline at end of file +} diff --git a/plugins/omemo/src/database.vala b/plugins/omemo/src/database.vala index aa52daf0..52a1b15d 100644 --- a/plugins/omemo/src/database.vala +++ b/plugins/omemo/src/database.vala @@ -23,11 +23,11 @@ public class Database : Qlite.Database { index("identity_meta_list_idx", {address_name}); } - public QueryBuilder with_address(string address_name) throws DatabaseError { + public QueryBuilder with_address(string address_name) { return select().with(this.address_name, "=", address_name); } - public void insert_device_list(string address_name, ArrayList devices) throws DatabaseError { + public void insert_device_list(string address_name, ArrayList devices) { update().with(this.address_name, "=", address_name).set(now_active, false).perform(); foreach (int32 device_id in devices) { upsert() @@ -39,7 +39,7 @@ public class Database : Qlite.Database { } } - public int64 insert_device_bundle(string address_name, int device_id, Bundle bundle) throws DatabaseError { + public int64 insert_device_bundle(string address_name, int device_id, Bundle bundle) { if (bundle == null || bundle.identity_key == null) return -1; return upsert() .value(this.address_name, address_name, true) @@ -108,7 +108,7 @@ public class Database : Qlite.Database { public PreKeyTable pre_key { get; private set; } public SessionTable session { get; private set; } - public Database(string fileName) throws DatabaseError { + public Database(string fileName) { base(fileName, VERSION); identity_meta = new IdentityMetaTable(this); identity = new IdentityTable(this); @@ -124,4 +124,4 @@ public class Database : Qlite.Database { } } -} \ No newline at end of file +} diff --git a/plugins/omemo/src/manager.vala b/plugins/omemo/src/manager.vala index 9b6f3681..5a7cb9ef 100644 --- a/plugins/omemo/src/manager.vala +++ b/plugins/omemo/src/manager.vala @@ -196,39 +196,24 @@ public class Manager : StreamInteractionModule, Object { if (module == null) { return; } - try { - ArrayList device_list = module.get_device_list(jid); - db.identity_meta.insert_device_list(jid, device_list); - int inc = 0; - foreach (Row row in db.identity_meta.with_address(jid).with_null(db.identity_meta.identity_key_public_base64)) { - module.fetch_bundle(stream, row[db.identity_meta.address_name], row[db.identity_meta.device_id]); - inc++; - } - if (inc > 0) { - if (Plugin.DEBUG) print(@"OMEMO: new bundles $inc/$(device_list.size) for $jid\n"); - } - } catch (DatabaseError e) { - // Ignore - print(@"OMEMO: failed to use database: $(e.message)\n"); + ArrayList device_list = module.get_device_list(jid); + db.identity_meta.insert_device_list(jid, device_list); + int inc = 0; + foreach (Row row in db.identity_meta.with_address(jid).with_null(db.identity_meta.identity_key_public_base64)) { + module.fetch_bundle(stream, row[db.identity_meta.address_name], row[db.identity_meta.device_id]); + inc++; + } + if (inc > 0) { + if (Plugin.DEBUG) print(@"OMEMO: new bundles $inc/$(device_list.size) for $jid\n"); } } public void on_bundle_fetched(Account account, string jid, int32 device_id, Bundle bundle) { - try { - db.identity_meta.insert_device_bundle(jid, device_id, bundle); - } catch (DatabaseError e) { - // Ignore - print(@"OMEMO: failed to use database: $(e.message)\n"); - } + db.identity_meta.insert_device_bundle(jid, device_id, bundle); } private void on_store_created(Account account, Store store) { - Qlite.Row? row = null; - try { - row = db.identity.row_with(db.identity.account_id, account.id).inner; - } catch (Error e) { - // Ignore error - } + Qlite.Row? row = db.identity.row_with(db.identity.account_id, account.id).inner; int identity_id = -1; if (row == null) { @@ -280,4 +265,4 @@ public class Manager : StreamInteractionModule, Object { } } -} \ No newline at end of file +} diff --git a/plugins/omemo/src/plugin.vala b/plugins/omemo/src/plugin.vala index 18661403..b9ce500d 100644 --- a/plugins/omemo/src/plugin.vala +++ b/plugins/omemo/src/plugin.vala @@ -30,31 +30,27 @@ public class Plugin : RootInterface, Object { public ContactDetailsProvider contact_details_provider; public void registered(Dino.Application app) { - try { - ensure_context(); - this.app = app; - this.db = new Database(Path.build_filename(Application.get_storage_dir(), "omemo.db")); - this.list_entry = new EncryptionListEntry(this); - this.settings_entry = new AccountSettingsEntry(this); - this.contact_details_provider = new ContactDetailsProvider(this); - this.app.plugin_registry.register_encryption_list_entry(list_entry); - this.app.plugin_registry.register_account_settings_entry(settings_entry); - this.app.plugin_registry.register_contact_details_entry(contact_details_provider); - this.app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { - list.add(new StreamModule()); - }); - Manager.start(this.app.stream_interactor, db); + ensure_context(); + this.app = app; + this.db = new Database(Path.build_filename(Application.get_storage_dir(), "omemo.db")); + this.list_entry = new EncryptionListEntry(this); + this.settings_entry = new AccountSettingsEntry(this); + this.contact_details_provider = new ContactDetailsProvider(this); + this.app.plugin_registry.register_encryption_list_entry(list_entry); + this.app.plugin_registry.register_account_settings_entry(settings_entry); + this.app.plugin_registry.register_contact_details_entry(contact_details_provider); + this.app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { + list.add(new StreamModule()); + }); + Manager.start(this.app.stream_interactor, db); - string locales_dir; - if (app.search_path_generator != null) { - locales_dir = ((!)app.search_path_generator).get_locale_path(GETTEXT_PACKAGE, LOCALE_INSTALL_DIR); - } else { - locales_dir = LOCALE_INSTALL_DIR; - } - internationalize(GETTEXT_PACKAGE, locales_dir); - } catch (Error e) { - print(@"Error initializing OMEMO: $(e.message)\n"); + string locales_dir; + if (app.search_path_generator != null) { + locales_dir = ((!)app.search_path_generator).get_locale_path(GETTEXT_PACKAGE, LOCALE_INSTALL_DIR); + } else { + locales_dir = LOCALE_INSTALL_DIR; } + internationalize(GETTEXT_PACKAGE, locales_dir); } public void shutdown() { diff --git a/plugins/omemo/src/pre_key_store.vala b/plugins/omemo/src/pre_key_store.vala index 0fd78ffc..93ec2dfe 100644 --- a/plugins/omemo/src/pre_key_store.vala +++ b/plugins/omemo/src/pre_key_store.vala @@ -27,27 +27,19 @@ private class BackedPreKeyStore : SimplePreKeyStore { } public void on_pre_key_stored(PreKeyStore.Key key) { - try { - db.pre_key.insert().or("REPLACE") - .value(db.pre_key.identity_id, identity_id) - .value(db.pre_key.pre_key_id, (int) key.key_id) - .value(db.pre_key.record_base64, Base64.encode(key.record)) - .perform(); - } catch (Error e) { - print(@"OMEMO: Error while updating pre key store: $(e.message)\n"); - } + db.pre_key.insert().or("REPLACE") + .value(db.pre_key.identity_id, identity_id) + .value(db.pre_key.pre_key_id, (int) key.key_id) + .value(db.pre_key.record_base64, Base64.encode(key.record)) + .perform(); } public void on_pre_key_deleted(PreKeyStore.Key key) { - try { - db.pre_key.delete() - .with(db.pre_key.identity_id, "=", identity_id) - .with(db.pre_key.pre_key_id, "=", (int) key.key_id) - .perform(); - } catch (Error e) { - print(@"OMEMO: Error while updating pre key store: $(e.message)\n"); - } + db.pre_key.delete() + .with(db.pre_key.identity_id, "=", identity_id) + .with(db.pre_key.pre_key_id, "=", (int) key.key_id) + .perform(); } } -} \ No newline at end of file +} diff --git a/plugins/omemo/src/session_store.vala b/plugins/omemo/src/session_store.vala index 333fdc08..25b4d719 100644 --- a/plugins/omemo/src/session_store.vala +++ b/plugins/omemo/src/session_store.vala @@ -29,29 +29,21 @@ private class BackedSessionStore : SimpleSessionStore { } public void on_session_stored(SessionStore.Session session) { - try { - db.session.insert().or("REPLACE") - .value(db.session.identity_id, identity_id) - .value(db.session.address_name, session.name) - .value(db.session.device_id, session.device_id) - .value(db.session.record_base64, Base64.encode(session.record)) - .perform(); - } catch (Error e) { - print(@"OMEMO: Error while updating session store: $(e.message)\n"); - } + db.session.insert().or("REPLACE") + .value(db.session.identity_id, identity_id) + .value(db.session.address_name, session.name) + .value(db.session.device_id, session.device_id) + .value(db.session.record_base64, Base64.encode(session.record)) + .perform(); } public void on_session_deleted(SessionStore.Session session) { - try { - db.session.delete() - .with(db.session.identity_id, "=", identity_id) - .with(db.session.address_name, "=", session.name) - .with(db.session.device_id, "=", session.device_id) - .perform(); - } catch (Error e) { - print(@"OMEMO: Error while updating session store: $(e.message)\n"); - } + db.session.delete() + .with(db.session.identity_id, "=", identity_id) + .with(db.session.address_name, "=", session.name) + .with(db.session.device_id, "=", session.device_id) + .perform(); } } -} \ No newline at end of file +} diff --git a/plugins/omemo/src/signed_pre_key_store.vala b/plugins/omemo/src/signed_pre_key_store.vala index 44d8b3b4..d96ded1f 100644 --- a/plugins/omemo/src/signed_pre_key_store.vala +++ b/plugins/omemo/src/signed_pre_key_store.vala @@ -27,28 +27,19 @@ private class BackedSignedPreKeyStore : SimpleSignedPreKeyStore { } public void on_signed_pre_key_stored(SignedPreKeyStore.Key key) { - try { - db.signed_pre_key.insert().or("REPLACE") - .value(db.signed_pre_key.identity_id, identity_id) - .value(db.signed_pre_key.signed_pre_key_id, (int) key.key_id) - .value(db.signed_pre_key.record_base64, Base64.encode(key.record)) - .perform(); - } catch (Error e) { - print(@"OMEMO: Error while updating signed pre key store: $(e.message)\n"); - } - + db.signed_pre_key.insert().or("REPLACE") + .value(db.signed_pre_key.identity_id, identity_id) + .value(db.signed_pre_key.signed_pre_key_id, (int) key.key_id) + .value(db.signed_pre_key.record_base64, Base64.encode(key.record)) + .perform(); } public void on_signed_pre_key_deleted(SignedPreKeyStore.Key key) { - try { - db.signed_pre_key.delete() - .with(db.signed_pre_key.identity_id, "=", identity_id) - .with(db.signed_pre_key.signed_pre_key_id, "=", (int) key.key_id) - .perform(); - } catch (Error e) { - print(@"OMEMO: Error while updating signed pre key store: $(e.message)\n"); - } + db.signed_pre_key.delete() + .with(db.signed_pre_key.identity_id, "=", identity_id) + .with(db.signed_pre_key.signed_pre_key_id, "=", (int) key.key_id) + .perform(); } } -} \ No newline at end of file +} 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(Column 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(Column column) { + if (column.not_null) error("Qlite Error: ILLEGAL QUERY: Can't set non-null column %s to null", column.name); fields += new NullField(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(Column field) throws DatabaseError { + public T get(Column 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 { 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(Column column, T value) throws DatabaseError { + public RowOption row_with(Column 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(Column 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(Column column) { + if (column.not_null) error("Can't set non-null column %s to null", column.name); fields += new NullField(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(Column 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(Column column) { + if (column.not_null) error("Can't set non-null column %s to null", column.name); fields += new NullField(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 +}