From 93fd134a92fb50cc6e8a9b5db3d6f25e84e6fa10 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Thu, 9 Mar 2017 21:46:16 +0100 Subject: [PATCH] Fix bugs and warnings in qlite --- qlite/src/column.vala | 29 +---------------------------- qlite/src/delete_builder.vala | 12 ++++++------ qlite/src/insert_builder.vala | 2 +- qlite/src/query_builder.vala | 3 --- qlite/src/row.vala | 8 ++++---- qlite/src/statement_builder.vala | 2 -- qlite/src/table.vala | 22 +++++++++++++++++++++- qlite/src/update_builder.vala | 6 +++--- 8 files changed, 36 insertions(+), 48 deletions(-) diff --git a/qlite/src/column.vala b/qlite/src/column.vala index f7b3114f..105b4bbb 100644 --- a/qlite/src/column.vala +++ b/qlite/src/column.vala @@ -19,9 +19,7 @@ public abstract class Column { 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 { stmt.bind_int(index, value ? 1 : 0); } } - - public class RowReference : Column { - private Table table; - private Column id_column; - - public RowReference(string name, Table table, Column 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); - } - } - } } } \ No newline at end of file diff --git a/qlite/src/delete_builder.vala b/qlite/src/delete_builder.vala index 5999dc40..679c8284 100644 --- a/qlite/src/delete_builder.vala +++ b/qlite/src/delete_builder.vala @@ -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())"); } diff --git a/qlite/src/insert_builder.vala b/qlite/src/insert_builder.vala index 654935a6..c1c17123 100644 --- a/qlite/src/insert_builder.vala +++ b/qlite/src/insert_builder.vala @@ -54,7 +54,7 @@ public class InsertBuilder : StatementBuilder { return this; } - public InsertBuilder value_null(Column column) { + public InsertBuilder value_null(Column 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(column) }; diff --git a/qlite/src/query_builder.vala b/qlite/src/query_builder.vala index 0c9f4d98..65cbb8f6 100644 --- a/qlite/src/query_builder.vala +++ b/qlite/src/query_builder.vala @@ -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); } diff --git a/qlite/src/row.vala b/qlite/src/row.vala index 905d12a1..ff98405f 100644 --- a/qlite/src/row.vala +++ b/qlite/src/row.vala @@ -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++) { diff --git a/qlite/src/statement_builder.vala b/qlite/src/statement_builder.vala index 8df069dd..e0055e87 100644 --- a/qlite/src/statement_builder.vala +++ b/qlite/src/statement_builder.vala @@ -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."); } } } diff --git a/qlite/src/table.vala b/qlite/src/table.vala index 209a5a96..7396136e 100644 --- a/qlite/src/table.vala +++ b/qlite/src/table.vala @@ -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); } diff --git a/qlite/src/update_builder.vala b/qlite/src/update_builder.vala index 5f721a32..41fd28b2 100644 --- a/qlite/src/update_builder.vala +++ b/qlite/src/update_builder.vala @@ -48,7 +48,7 @@ public class UpdateBuilder : StatementBuilder { return this; } - public UpdateBuilder set_null(Column column) { + public UpdateBuilder set_null(Column 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(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];