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;
}
public virtual void bind(Statement stmt, int index, T value) {
throw new DatabaseError.NOT_SUPPORTED(@"bind() was not implemented for field $name");
}
public abstract void bind(Statement stmt, int index, T value);
public string to_string() {
string res = name;
@ -158,31 +156,6 @@ public abstract class Column<T> {
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);
}
public DeleteBuilder from(Table table) {
if (table != null) throw new DatabaseError.ILLEGAL_QUERY("cannot use from() multiple times.");
public DeleteBuilder from(Table table) throws DatabaseError {
if (this.table != null) throw new DatabaseError.ILLEGAL_QUERY("cannot use from() multiple times.");
this.table = table;
this.table_name = table.name;
return this;
@ -28,8 +28,8 @@ public class DeleteBuilder : StatementBuilder {
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.");
public DeleteBuilder where(string selection, string[]? selection_args = null) throws DatabaseError {
if (this.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];
@ -56,7 +56,7 @@ public class DeleteBuilder : StatementBuilder {
return this;
}
public override Statement prepare() {
public override Statement prepare() throws DatabaseError {
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);
@ -64,7 +64,7 @@ public class DeleteBuilder : StatementBuilder {
return stmt;
}
public void perform() {
public void perform() throws DatabaseError {
if (prepare().step() != DONE) {
throw new DatabaseError.EXEC_ERROR(@"SQLite error: $(db.errcode()) - $(db.errmsg())");
}

View File

@ -54,7 +54,7 @@ public class InsertBuilder : StatementBuilder {
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 (fields == null) {
fields = { new NullField<T>(column) };

View File

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

View File

@ -29,7 +29,7 @@ public class Row {
}
public string? get_text(string field) {
if (text_map.contains(field)) {
if (text_map.has_key(field)) {
return text_map[field];
}
return null;
@ -40,7 +40,7 @@ public class Row {
}
public bool has_integer(string field) {
return int_map.contains(field);
return int_map.has_key(field);
}
public double get_real(string field) {
@ -48,7 +48,7 @@ public class Row {
}
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 {
@ -58,7 +58,7 @@ public class Row {
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);
if (args != null) {
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) {
if (column != null) {
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;
public string name { get; private set; }
protected Column[] columns;
private string constraints;
public Table(Database db, string name) {
this.db = db;
this.name = name;
}
public void init(Column[] columns) {
public void init(Column[] columns, string? constraints = null) {
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 {
@ -63,6 +80,9 @@ public class Table {
sql += @"$(i > 0 ? "," : "") $c";
}
}
if (constraints != null) {
sql += ", " + constraints;
}
sql += ")";
db.exec(sql);
}

View File

@ -48,7 +48,7 @@ public class UpdateBuilder : StatementBuilder {
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 (fields == null) {
fields = { new NullField<T>(column) };
@ -63,8 +63,8 @@ public class UpdateBuilder : StatementBuilder {
return this;
}
public UpdateBuilder where(string selection, string[]? selection_args = null) {
if (selection != null) throw new DatabaseError.ILLEGAL_QUERY("selection was already done, but where() was called.");
public UpdateBuilder where(string selection, string[]? selection_args = null) throws DatabaseError {
if (this.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];