From 56bc45ce4d07a7a9a415e9dc8ad2f7c3f3c9e48d Mon Sep 17 00:00:00 2001 From: fiaxh Date: Thu, 2 Mar 2017 15:37:32 +0100 Subject: Initial commit --- qlite/src/delete_builder.vala | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 qlite/src/delete_builder.vala (limited to 'qlite/src/delete_builder.vala') diff --git a/qlite/src/delete_builder.vala b/qlite/src/delete_builder.vala new file mode 100644 index 00000000..5999dc40 --- /dev/null +++ b/qlite/src/delete_builder.vala @@ -0,0 +1,75 @@ +using Sqlite; + +namespace Qlite { + +public class DeleteBuilder : StatementBuilder { + + // DELETE FROM [...] + private Table table; + private string table_name; + + // WHERE [...] + private string selection; + private StatementBuilder.Field[] selection_args; + + protected DeleteBuilder(Database db) { + base(db); + } + + public DeleteBuilder from(Table table) { + if (table != null) throw new DatabaseError.ILLEGAL_QUERY("cannot use from() multiple times."); + this.table = table; + this.table_name = table.name; + return this; + } + + public DeleteBuilder from_name(string table) { + this.table_name = table; + return this; + } + + public DeleteBuilder where(string selection, string[]? selection_args = null) { + if (selection != null) throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called."); + this.selection = selection; + if (selection_args != null) { + this.selection_args = new StatementBuilder.Field[selection_args.length]; + for (int i = 0; i < selection_args.length; i++) { + this.selection_args[i] = new StatementBuilder.StringField(selection_args[i]); + } + } + return this; + } + + public DeleteBuilder with(Column column, string comp, T value) { + if (selection == null) { + selection = @"$(column.name) $comp ?"; + selection_args = { new StatementBuilder.Field(column, value) }; + } else { + selection = @"($selection) AND $(column.name) $comp ?"; + StatementBuilder.Field[] selection_args_new = new StatementBuilder.Field[selection_args.length+1]; + for (int i = 0; i < selection_args.length; i++) { + selection_args_new[i] = selection_args[i]; + } + selection_args_new[selection_args.length] = new Field(column, value); + selection_args = selection_args_new; + } + return this; + } + + public override Statement prepare() { + Statement stmt = db.prepare(@"DELETE FROM $table_name $(selection != null ? @"WHERE $selection": "")"); + for (int i = 0; i < selection_args.length; i++) { + selection_args[i].bind(stmt, i+1); + } + return stmt; + } + + public void perform() { + if (prepare().step() != DONE) { + throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())"); + } + } + +} + +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf