diff --git a/lib/dataset/index.js b/lib/dataset/index.js index 84cbadba..f05bcce8 100755 --- a/lib/dataset/index.js +++ b/lib/dataset/index.js @@ -34,9 +34,9 @@ define([actions, graph, features, query, sql], { /**@lends patio.Dataset.prototype*/ /** - * Class that is used for querying/retirving datasets from a database. + * Class that is used for querying/retrieving datasets from a database. * - *
Dynamically genertated methods include + *
Dynamically generated methods include *
+ * For parameter types see {@link patio.Dataset#filter}. + *
+ * + * @example + * + * DB.from("items").filter({id, [1,2,3]}).andGroupedOr([{price: {lt : 0}}, {price: {gt: 10}]).sql; + * //=> SELECT + * * + * FROM + * items + * WHERE + * ((id IN (1, 2, 3)) AND ((price < 0) OR (price > 10))) + * + * @throws {patio.QueryError} If no WHERE?HAVING clause exists. + * @return {patio.Dataset} a cloned dataset with the condition 'or group' added to the WHERE/HAVING clause. + */ + andGroupedOr:function () { + var tOpts = this.__opts, + clause = (tOpts.having ? "having" : "where"), + clauseObj = tOpts[clause]; + if (clauseObj) { + var args = argsToArray(arguments); + args = args.length == 1 ? args[0] : args; + var opts = {}; + opts[clause] = new BooleanExpression("AND", clauseObj, BooleanExpression.fromValuePairs(args,"OR")); + return this.mergeOptions(opts); + } else { + throw new QueryError("No existing filter found"); + } + }, + + /** + * Adds to the where/having clause with an OR a group of ANDed conditions wrapped in parens + * + *+ * For parameter types see {@link patio.Dataset#filter}. + *
+ * + * @example + * + * DB.from("items").filter({id, [1,2,3]}).orGroupedAnd([{price: {lt : 0}}, {price: {gt: 10}]).sql; + * //=> SELECT + * * + * FROM + * items + * WHERE + * ((id IN (1, 2, 3)) OR ((price > 0) AND (price < 10))) + * + * @throws {patio.QueryError} If no WHERE?HAVING clause exists. + * @return {patio.Dataset} a cloned dataset with the condition 'and group' added to the WHERE/HAVING clause. + */ + orGroupedAnd:function () { + var tOpts = this.__opts, + clause = (tOpts.having ? "having" : "where"), + clauseObj = tOpts[clause]; + if (clauseObj) { + var args = argsToArray(arguments); + args = args.length == 1 ? args[0] : args; + var opts = {}; + opts[clause] = new BooleanExpression("OR", clauseObj, BooleanExpression.fromValuePairs(args,"AND")); return this.mergeOptions(opts); } else { throw new QueryError("No existing filter found"); diff --git a/lib/sql.js b/lib/sql.js index 6b089026..8fcfb4aa 100755 --- a/lib/sql.js +++ b/lib/sql.js @@ -1984,7 +1984,7 @@ var Constant = define(GenericExpression, { /** * @class Represents boolean constants such as NULL, NOTNULL, TRUE, and FALSE. - * @auments patio.sql.Constant + * @augments patio.sql.Constant * @name BooleanConstant * @memberOf patio.sql */ diff --git a/test/dataset/query.test.js b/test/dataset/query.test.js index f55fa57e..f04aaa78 100755 --- a/test/dataset/query.test.js +++ b/test/dataset/query.test.js @@ -409,7 +409,7 @@ it.describe("Dataset queries",function (it) { }).sql, "SELECT * FROM test WHERE (((name < 'b') AND (table.id = 1)) OR is_active(blah, xx, x.y_z))"); }); - it.should("eval the block in the context of sql if sql isnt an arugment", function () { + it.should("eval the block in the context of sql if sql isnt an argument", function () { var x = null; dataset.filter(function (r) { x = this; @@ -454,6 +454,34 @@ it.describe("Dataset queries",function (it) { }); + it.describe("#andGroupedOr", function (it) { + var dataset = new Dataset().from("test"), + d1 = dataset.where({x:1}); + + it.should("raise if no filter exists", function () { + assert.throws(comb.hitch(dataset, "andGroupedOr", [{a:1},{y:2}])); + }); + + it.should("add an alternate expression of ORed conditions wrapped in parens to the where clause", function () { + assert.equal(d1.andGroupedOr([['y',2],['y',3]]).sql, "SELECT * FROM test WHERE ((x = 1) AND ((y = 2) OR (y = 3)))"); + }); + + }); + + it.describe("#orGroupedAnd", function (it) { + var dataset = new Dataset().from("test"), + d1 = dataset.where({x:1}); + + it.should("raise if no filter exists", function () { + assert.throws(comb.hitch(dataset, "orGroupedAnd", [{a:1},{y:2}])); + }); + + it.should("add an additional expression of ANDed conditions wrapped in parens to the where clause", function () { + assert.equal(d1.orGroupedAnd([['x',2],['y',3]]).sql, "SELECT * FROM test WHERE ((x = 1) OR ((x = 2) AND (y = 3)))"); + }); + + }); + it.describe("#and", function (it) { var dataset = new Dataset().from("test"), d1 = dataset.where({x:1}); @@ -2256,4 +2284,3 @@ it.describe("Dataset queries",function (it) { it.afterAll(comb.hitch(patio, "disconnect")); }).as(module); -