Fix bugs and warnings in qlite

This commit is contained in:
Marvin W 2017-03-09 21:46:16 +01:00
parent 9b8cf706d6
commit 93fd134a92
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A
8 changed files with 36 additions and 48 deletions

View file

@ -19,9 +19,7 @@ public abstract class Column<T> {
return false; return false;
} }
public virtual void bind(Statement stmt, int index, T value) { public abstract void bind(Statement stmt, int index, T value);
throw new DatabaseError.NOT_SUPPORTED(@"bind() was not implemented for field $name");
}
public string to_string() { public string to_string() {
string res = name; string res = name;
@ -158,31 +156,6 @@ public abstract class Column<T> {
stmt.bind_int(index, value ? 1 : 0); stmt.bind_int(index, value ? 1 : 0);
} }
} }
public class RowReference : Column<Row?> {
private Table table;
private Column<int> id_column;
public RowReference(string name, Table table, Column<int> id_column) throws DatabaseError {
base(name, INTEGER);
if (!table.is_known_column(id_column.name)) throw new DatabaseError.ILLEGAL_REFERENCE(@"$(id_column.name) is not a column in $(table.name)");
if (!id_column.primary_key && !id_column.unique) throw new DatabaseError.NON_UNIQUE(@"$(id_column.name) is not suited to identify a row, but used with RowReference");
this.table = table;
this.id_column = id_column;
}
public override Row? get(Row row) {
return table.row_with(id_column, (int)row.get_integer(name));
}
public override void bind(Statement stmt, int index, Row? value) {
if (value != null) {
stmt.bind_int(index, id_column.get(value));
} else {
stmt.bind_null(index);
}
}
}
} }
} }

View file

@ -16,8 +16,8 @@ public class DeleteBuilder : StatementBuilder {
base(db); base(db);
} }
public DeleteBuilder from(Table table) { public DeleteBuilder from(Table table) throws DatabaseError {
if (table != null) throw new DatabaseError.ILLEGAL_QUERY("cannot use from() multiple times."); if (this.table != null) throw new DatabaseError.ILLEGAL_QUERY("cannot use from() multiple times.");
this.table = table; this.table = table;
this.table_name = table.name; this.table_name = table.name;
return this; return this;
@ -28,8 +28,8 @@ public class DeleteBuilder : StatementBuilder {
return this; return this;
} }
public DeleteBuilder where(string selection, string[]? selection_args = null) { public DeleteBuilder where(string selection, string[]? selection_args = null) throws DatabaseError {
if (selection != null) throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called."); if (this.selection != null) throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called.");
this.selection = selection; this.selection = selection;
if (selection_args != null) { if (selection_args != null) {
this.selection_args = new StatementBuilder.Field[selection_args.length]; this.selection_args = new StatementBuilder.Field[selection_args.length];
@ -56,7 +56,7 @@ public class DeleteBuilder : StatementBuilder {
return this; return this;
} }
public override Statement prepare() { public override Statement prepare() throws DatabaseError {
Statement stmt = db.prepare(@"DELETE FROM $table_name $(selection != null ? @"WHERE $selection": "")"); Statement stmt = db.prepare(@"DELETE FROM $table_name $(selection != null ? @"WHERE $selection": "")");
for (int i = 0; i < selection_args.length; i++) { for (int i = 0; i < selection_args.length; i++) {
selection_args[i].bind(stmt, i+1); selection_args[i].bind(stmt, i+1);
@ -64,7 +64,7 @@ public class DeleteBuilder : StatementBuilder {
return stmt; return stmt;
} }
public void perform() { public void perform() throws DatabaseError {
if (prepare().step() != DONE) { if (prepare().step() != DONE) {
throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())"); throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
} }

View file

@ -54,7 +54,7 @@ public class InsertBuilder : StatementBuilder {
return this; return this;
} }
public InsertBuilder value_null<T>(Column<T> column) { public InsertBuilder value_null<T>(Column<T> column) throws DatabaseError {
if (column.not_null) throw new DatabaseError.ILLEGAL_QUERY(@"Can't set non-null column $(column.name) to null"); if (column.not_null) throw new DatabaseError.ILLEGAL_QUERY(@"Can't set non-null column $(column.name) to null");
if (fields == null) { if (fields == null) {
fields = { new NullField<T>(column) }; fields = { new NullField<T>(column) };

View file

@ -3,7 +3,6 @@ using Sqlite;
namespace Qlite { namespace Qlite {
public class QueryBuilder : StatementBuilder { public class QueryBuilder : StatementBuilder {
private bool finished;
private bool single_result; private bool single_result;
// SELECT [...] // SELECT [...]
@ -24,8 +23,6 @@ public class QueryBuilder : StatementBuilder {
// LIMIT [...] // LIMIT [...]
private int limit_val; private int limit_val;
private Row[] result;
protected QueryBuilder(Database db) { protected QueryBuilder(Database db) {
base(db); base(db);
} }

View file

@ -29,7 +29,7 @@ public class Row {
} }
public string? get_text(string field) { public string? get_text(string field) {
if (text_map.contains(field)) { if (text_map.has_key(field)) {
return text_map[field]; return text_map[field];
} }
return null; return null;
@ -40,7 +40,7 @@ public class Row {
} }
public bool has_integer(string field) { public bool has_integer(string field) {
return int_map.contains(field); return int_map.has_key(field);
} }
public double get_real(string field) { public double get_real(string field) {
@ -48,7 +48,7 @@ public class Row {
} }
public bool has_real(string field) { public bool has_real(string field) {
return real_map.contains(field) && real_map[field] != null; return real_map.has_key(field) && real_map[field] != null;
} }
public class RowIterator { public class RowIterator {
@ -58,7 +58,7 @@ public class Row {
this.stmt = query.prepare(); this.stmt = query.prepare();
} }
public RowIterator(Database db, string sql, string[]? args = null) { public RowIterator(Database db, string sql, string[]? args = null) throws DatabaseError {
this.stmt = db.prepare(sql); this.stmt = db.prepare(sql);
if (args != null) { if (args != null) {
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {

View file

@ -23,8 +23,6 @@ public abstract class StatementBuilder {
public virtual void bind(Statement stmt, int index) { public virtual void bind(Statement stmt, int index) {
if (column != null) { if (column != null) {
column.bind(stmt, index, value); column.bind(stmt, index, value);
} else {
throw new DatabaseError.NOT_SUPPORTED("binding was not implemented for this field.");
} }
} }
} }

View file

@ -6,14 +6,31 @@ public class Table {
protected Database db; protected Database db;
public string name { get; private set; } public string name { get; private set; }
protected Column[] columns; protected Column[] columns;
private string constraints;
public Table(Database db, string name) { public Table(Database db, string name) {
this.db = db; this.db = db;
this.name = name; this.name = name;
} }
public void init(Column[] columns) { public void init(Column[] columns, string? constraints = null) {
this.columns = columns; this.columns = columns;
this.constraints = constraints;
}
public void unique(Column[] columns, string? on_conflict = null) {
if (constraints == null) constraints = ""; else constraints += ", ";
constraints += "UNIQUE (";
bool first = true;
foreach(Column c in columns) {
if (!first) constraints += ", ";
constraints += c.name;
first = false;
}
constraints += ")";
if (on_conflict != null) {
constraints += "ON CONFLICT " + on_conflict;
}
} }
private void ensure_init() throws DatabaseError { private void ensure_init() throws DatabaseError {
@ -63,6 +80,9 @@ public class Table {
sql += @"$(i > 0 ? "," : "") $c"; sql += @"$(i > 0 ? "," : "") $c";
} }
} }
if (constraints != null) {
sql += ", " + constraints;
}
sql += ")"; sql += ")";
db.exec(sql); db.exec(sql);
} }

View file

@ -48,7 +48,7 @@ public class UpdateBuilder : StatementBuilder {
return this; return this;
} }
public UpdateBuilder set_null<T>(Column<T> column) { public UpdateBuilder set_null<T>(Column<T> column) throws DatabaseError {
if (column.not_null) throw new DatabaseError.ILLEGAL_QUERY(@"Can't set non-null column $(column.name) to null"); if (column.not_null) throw new DatabaseError.ILLEGAL_QUERY(@"Can't set non-null column $(column.name) to null");
if (fields == null) { if (fields == null) {
fields = { new NullField<T>(column) }; fields = { new NullField<T>(column) };
@ -63,8 +63,8 @@ public class UpdateBuilder : StatementBuilder {
return this; return this;
} }
public UpdateBuilder where(string selection, string[]? selection_args = null) { public UpdateBuilder where(string selection, string[]? selection_args = null) throws DatabaseError {
if (selection != null) throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called."); if (this.selection != null) throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called.");
this.selection = selection; this.selection = selection;
if (selection_args != null) { if (selection_args != null) {
this.selection_args = new StatementBuilder.Field[selection_args.length]; this.selection_args = new StatementBuilder.Field[selection_args.length];