2017-03-02 14:37:32 +00:00
|
|
|
using Sqlite;
|
|
|
|
|
|
|
|
namespace Qlite {
|
|
|
|
|
|
|
|
public class DeleteBuilder : StatementBuilder {
|
|
|
|
|
|
|
|
// DELETE FROM [...]
|
2017-04-16 13:11:00 +00:00
|
|
|
private Table? table;
|
2017-03-02 14:37:32 +00:00
|
|
|
private string table_name;
|
|
|
|
|
|
|
|
// WHERE [...]
|
2017-04-16 13:11:00 +00:00
|
|
|
private string selection = "1";
|
|
|
|
private StatementBuilder.AbstractField[] selection_args = {};
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-20 18:27:39 +00:00
|
|
|
internal DeleteBuilder(Database db) {
|
2017-03-02 14:37:32 +00:00
|
|
|
base(db);
|
|
|
|
}
|
|
|
|
|
2017-10-28 21:48:07 +00:00
|
|
|
public DeleteBuilder from(Table table) {
|
|
|
|
if (this.table != null) error("Qlite Error: ILLEGAL QUERY: cannot use from() multiple times.");
|
2017-03-02 14:37:32 +00:00
|
|
|
this.table = table;
|
|
|
|
this.table_name = table.name;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DeleteBuilder from_name(string table) {
|
|
|
|
this.table_name = table;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-10-28 21:48:07 +00:00
|
|
|
public DeleteBuilder where(string selection, string[]? selection_args = null) {
|
|
|
|
if (this.selection != "1") error("selection was already done, but where() was called.");
|
2017-03-02 14:37:32 +00:00
|
|
|
this.selection = selection;
|
2017-04-16 13:11:00 +00:00
|
|
|
foreach (string arg in selection_args) {
|
|
|
|
this.selection_args += new StatementBuilder.StringField(arg);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DeleteBuilder with<T>(Column<T> column, string comp, T value) {
|
2017-04-16 13:11:00 +00:00
|
|
|
selection_args += new Field<T>(column, value);
|
|
|
|
selection = @"($selection) AND $(column.name) $comp ?";
|
2017-03-02 14:37:32 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-10-28 21:48:07 +00:00
|
|
|
internal override Statement prepare() {
|
2017-04-16 13:11:00 +00:00
|
|
|
Statement stmt = db.prepare(@"DELETE FROM $table_name WHERE $selection");
|
2017-03-02 14:37:32 +00:00
|
|
|
for (int i = 0; i < selection_args.length; i++) {
|
|
|
|
selection_args[i].bind(stmt, i+1);
|
|
|
|
}
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
2017-10-28 21:48:07 +00:00
|
|
|
public void perform() {
|
2017-03-02 14:37:32 +00:00
|
|
|
if (prepare().step() != DONE) {
|
2017-10-28 21:48:07 +00:00
|
|
|
error(@"SQLite error: %d - %s", db.errcode(), db.errmsg());
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-28 21:48:07 +00:00
|
|
|
}
|