| 1 |
using Sqlite; |
| 2 |
|
| 3 |
namespace Qlite { |
| 4 |
|
| 5 |
public class UpdateBuilder : StatementBuilder { |
| 6 |
|
| 7 |
// UPDATE [OR ...] |
| 8 |
private string? or_val; |
| 9 |
|
| 10 |
// [...] |
| 11 |
private Table? table; |
| 12 |
private string table_name; |
| 13 |
|
| 14 |
// SET [...] |
| 15 |
private StatementBuilder.AbstractField[] fields = {}; |
| 16 |
|
| 17 |
// WHERE [...] |
| 18 |
private string selection = "1"; |
| 19 |
private StatementBuilder.AbstractField[] selection_args = {}; |
| 20 |
|
| 21 |
internal UpdateBuilder(Database db, Table table) { |
| 22 |
base(db); |
| 23 |
this.table = table; |
| 24 |
this.table_name = table.name; |
| 25 |
} |
| 26 |
|
| 27 |
internal UpdateBuilder.for_name(Database db, string table) { |
| 28 |
base(db); |
| 29 |
this.table_name = table; |
| 30 |
} |
| 31 |
|
| 32 |
public UpdateBuilder or(string or) { |
| 33 |
this.or_val = or; |
| 34 |
return this; |
| 35 |
} |
| 36 |
|
| 37 |
public UpdateBuilder set<T>(Column<T> column, T value) { |
| 38 |
fields += new Field<T>(column, value); |
| 39 |
return this; |
| 40 |
} |
| 41 |
|
| 42 |
public UpdateBuilder set_null<T>(Column<T> column) { |
| 43 |
if (column.not_null) error("Can't set non-null column %s to null", column.name); |
| 44 |
fields += new NullField<T>(column); |
| 45 |
return this; |
| 46 |
} |
| 47 |
|
| 48 |
public UpdateBuilder where(string selection, string[] selection_args = {}) { |
| 49 |
if (this.selection != "1") error("selection was already done, but where() was called."); |
| 50 |
this.selection = selection; |
| 51 |
foreach (string arg in selection_args) { |
| 52 |
this.selection_args += new StatementBuilder.StringField(arg); |
| 53 |
} |
| 54 |
return this; |
| 55 |
} |
| 56 |
|
| 57 |
public UpdateBuilder with<T>(Column<T> column, string comp, T value) { |
| 58 |
selection_args += new Field<T>(column, value); |
| 59 |
selection = @"($selection) AND $(column.name) $comp ?"; |
| 60 |
return this; |
| 61 |
} |
| 62 |
|
| 63 |
public UpdateBuilder with_null<T>(Column<T> column) { |
| 64 |
selection = @"($selection) AND $(column.name) ISNULL"; |
| 65 |
return this; |
| 66 |
} |
| 67 |
|
| 68 |
public UpdateBuilder without_null<T>(Column<T> column) { |
| 69 |
selection = @"($selection) AND $(column.name) NOT NULL"; |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
internal override Statement prepare() { |
| 74 |
string sql = "UPDATE"; |
| 75 |
if (or_val != null) sql += @" OR $((!)or_val)"; |
| 76 |
sql += @" $table_name SET "; |
| 77 |
for (int i = 0; i < fields.length; i++) { |
| 78 |
if (i != 0) { |
| 79 |
sql += ", "; |
| 80 |
} |
| 81 |
sql += @"$(((!)fields[i].column).name) = ?"; |
| 82 |
} |
| 83 |
sql += @" WHERE $selection"; |
| 84 |
Statement stmt = db.prepare(sql); |
| 85 |
for (int i = 0; i < fields.length; i++) { |
| 86 |
fields[i].bind(stmt, i+1); |
| 87 |
} |
| 88 |
for (int i = 0; i < selection_args.length; i++) { |
| 89 |
selection_args[i].bind(stmt, i + fields.length + 1); |
| 90 |
} |
| 91 |
return stmt; |
| 92 |
} |
| 93 |
|
| 94 |
public void perform() { |
| 95 |
if (fields.length == 0) return; |
| 96 |
if (prepare().step() != DONE) { |
| 97 |
critical("SQLite error: %d - %s", db.errcode(), db.errmsg()); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
} |