diff options
Diffstat (limited to 'qlite/src/query_builder.vala')
-rw-r--r-- | qlite/src/query_builder.vala | 70 |
1 files changed, 22 insertions, 48 deletions
diff --git a/qlite/src/query_builder.vala b/qlite/src/query_builder.vala index 06232fc1..f46fe98c 100644 --- a/qlite/src/query_builder.vala +++ b/qlite/src/query_builder.vala @@ -7,18 +7,18 @@ public class QueryBuilder : StatementBuilder { // SELECT [...] private string column_selector = "*"; - private Column[] columns; + private Column[] columns = {}; // FROM [...] - private Table table; - private string table_name; + private Table? table; + private string? table_name; // WHERE [...] - private string selection; - private StatementBuilder.Field[] selection_args; + private string selection = "1"; + private StatementBuilder.AbstractField[] selection_args = {}; // ORDER BY [...] - private OrderingTerm[] order_by_terms; + private OrderingTerm[]? order_by_terms = {}; // LIMIT [...] private int limit_val; @@ -27,9 +27,9 @@ public class QueryBuilder : StatementBuilder { base(db); } - public QueryBuilder select(Column[]? columns = null) { + public QueryBuilder select(Column[] columns = {}) { this.columns = columns; - if (columns != null) { + if (columns.length == 0) { for (int i = 0; i < columns.length; i++) { if (column_selector == "*") { column_selector = columns[0].name; @@ -44,7 +44,7 @@ public class QueryBuilder : StatementBuilder { } public QueryBuilder select_string(string column_selector) { - this.columns = null; + this.columns = {}; this.column_selector = column_selector; return this; } @@ -61,32 +61,19 @@ public class QueryBuilder : StatementBuilder { return this; } - public QueryBuilder 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."); + public QueryBuilder where(string selection, string[] selection_args = {}) throws DatabaseError { + if (this.selection != "1") 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]; - for (int i = 0; i < selection_args.length; i++) { - this.selection_args[i] = new StatementBuilder.StringField(selection_args[i]); - } + foreach (string arg in selection_args) { + this.selection_args += new StatementBuilder.StringField(arg); } return this; } public QueryBuilder with<T>(Column<T> column, string comp, T value) { if ((column.unique || column.primary_key) && comp == "=") single_result = true; - if (selection == null) { - selection = @"$(column.name) $comp ?"; - selection_args = { new StatementBuilder.Field<T>(column, value) }; - } else { - selection = @"($selection) AND $(column.name) $comp ?"; - StatementBuilder.Field[] selection_args_new = new StatementBuilder.Field[selection_args.length+1]; - for (int i = 0; i < selection_args.length; i++) { - selection_args_new[i] = selection_args[i]; - } - selection_args_new[selection_args.length] = new Field<T>(column, value); - selection_args = selection_args_new; - } + selection_args += new Field<T>(column, value); + selection = @"($selection) AND $(column.name) $comp ?"; return this; } @@ -100,26 +87,13 @@ public class QueryBuilder : StatementBuilder { return this; } - private void add_order_by(OrderingTerm term) { - if (order_by_terms == null) { - order_by_terms = { term }; - } else { - OrderingTerm[] order_by_terms_new = new OrderingTerm[order_by_terms.length+1]; - for (int i = 0; i < order_by_terms.length; i++) { - order_by_terms_new[i] = order_by_terms[i]; - } - order_by_terms_new[order_by_terms.length] = term; - order_by_terms = order_by_terms_new; - } - } - public QueryBuilder order_by(Column column, string dir = "ASC") { - add_order_by(new OrderingTerm(column, dir)); + order_by_terms += new OrderingTerm(column, dir); return this; } public QueryBuilder order_by_name(string name, string dir) { - add_order_by(new OrderingTerm.by_name(name, dir)); + order_by_terms += new OrderingTerm.by_name(name, dir); return this; } @@ -131,12 +105,12 @@ public class QueryBuilder : StatementBuilder { public int64 count() throws DatabaseError { this.column_selector = @"COUNT($column_selector) AS count"; this.single_result = true; - return row_().get_integer("count"); + return row().get_integer("count"); } private Row? row_() throws DatabaseError { if (!single_result) throw new DatabaseError.NON_UNIQUE("query is not suited to return a single row, but row() was called."); - return iterator().next_value(); + return iterator().get_next(); } public RowOption row() throws DatabaseError { @@ -148,7 +122,7 @@ public class QueryBuilder : StatementBuilder { } internal override Statement prepare() throws DatabaseError { - Statement stmt = db.prepare(@"SELECT $column_selector FROM $table_name $(selection != null ? @"WHERE $selection" : "") $(order_by_terms != null ? OrderingTerm.all_to_string(order_by_terms) : "") $(limit_val > 0 ? @" LIMIT $limit_val" : "")"); + Statement stmt = db.prepare(@"SELECT $column_selector $(table_name == null ? "" : @"FROM $((!) table_name)") WHERE $selection $(OrderingTerm.all_to_string(order_by_terms)) $(limit_val > 0 ? @" LIMIT $limit_val" : "")"); for (int i = 0; i < selection_args.length; i++) { selection_args[i].bind(stmt, i+1); } @@ -179,8 +153,8 @@ public class QueryBuilder : StatementBuilder { return @"$column_name $dir"; } - public static string all_to_string(OrderingTerm[] terms) { - if (terms.length == 0) return ""; + public static string all_to_string(OrderingTerm[]? terms) { + if (terms == null || terms.length == 0) return ""; string res = "ORDER BY "+terms[0].to_string(); for (int i = 1; i < terms.length; i++) { res += @", $(terms[i])"; |