Debian Codegraph @

qlite/src/delete_builder.vala

raw
1 using Sqlite;
2
3 namespace Qlite {
4
5 public class DeleteBuilder : StatementBuilder {
6
7 // DELETE FROM [...]
8 private Table? table;
9 private string table_name;
10
11 // WHERE [...]
12 private string selection = "1";
13 private StatementBuilder.AbstractField[] selection_args = {};
14
15 internal DeleteBuilder(Database db) {
16 base(db);
17 }
18
19 public DeleteBuilder from(Table table) {
20 if (this.table != null) error("Qlite Error: ILLEGAL QUERY: cannot use from() multiple times.");
21 this.table = table;
22 this.table_name = table.name;
23 return this;
24 }
25
26 public DeleteBuilder from_name(string table) {
27 this.table_name = table;
28 return this;
29 }
30
31 public DeleteBuilder where(string selection, string[]? selection_args = null) {
32 if (this.selection != "1") error("selection was already done, but where() was called.");
33 this.selection = selection;
34 foreach (string arg in selection_args) {
35 this.selection_args += new StatementBuilder.StringField(arg);
36 }
37 return this;
38 }
39
40 public DeleteBuilder with<T>(Column<T> column, string comp, T value) {
41 selection_args += new Field<T>(column, value);
42 selection = @"($selection) AND $(column.name) $comp ?";
43 return this;
44 }
45
46 internal override Statement prepare() {
47 Statement stmt = db.prepare(@"DELETE FROM $table_name WHERE $selection");
48 for (int i = 0; i < selection_args.length; i++) {
49 selection_args[i].bind(stmt, i+1);
50 }
51 return stmt;
52 }
53
54 public void perform() {
55 if (prepare().step() != DONE) {
56 critical(@"SQLite error: %d - %s", db.errcode(), db.errmsg());
57 }
58 }
59
60 }
61
62 }