Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #82 from jkc/master
Browse files Browse the repository at this point in the history
Add a method to dataset/query to append a group of OR conditions to an e...
  • Loading branch information
doug-martin committed Dec 13, 2013
2 parents 1cabe63 + a006eec commit 40addd5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/dataset/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p> Dynamically genertated methods include
* <p> Dynamically generated methods include
* <ul>
* <li>Join methods from {@link patio.Dataset.CONDITIONED_JOIN_TYPES} and
* {@link patio.Dataset.UNCONDITIONED_JOIN_TYPES}, these methods handle the type call
Expand Down
76 changes: 73 additions & 3 deletions lib/dataset/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ define(null, {
*
* @throws {patio.QueryError} If no WHERE?HAVING clause exists.
*
* @return {patio.Dataset} a cloned dataset with the condtion added to the WHERE/HAVING clause added.
* @return {patio.Dataset} a cloned dataset with the condition added to the WHERE/HAVING clause added.
*/
and:function () {
var tOpts = this.__opts, clauseObj = tOpts[tOpts.having ? "having" : "where"];
Expand Down Expand Up @@ -144,7 +144,7 @@ define(null, {
* //=> SELECT * FROM items WHERE a OR b
*
* @throws {patio.QueryError} If no WHERE?HAVING clause exists.
* @return {patio.Dataset} a cloned dataset with the condtion added to the WHERE/HAVING clause added.
* @return {patio.Dataset} a cloned dataset with the condition added to the WHERE/HAVING clause added.
*/
or:function () {
var tOpts = this.__opts;
Expand All @@ -153,7 +153,77 @@ define(null, {
var args = argsToArray(arguments);
args = args.length == 1 ? args[0] : args;
var opts = {};
opts[clause] = new BooleanExpression("OR", clauseObj, this._filterExpr(args))
opts[clause] = new BooleanExpression("OR", clauseObj, this._filterExpr(args));
return this.mergeOptions(opts);
} else {
throw new QueryError("No existing filter found");
}
},

/**
* Adds to the where/having clause with an AND a group of ORed conditions wrapped in parens
*
* <p>
* <b>For parameter types see {@link patio.Dataset#filter}.</b>
* </p>
*
* @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
*
* <p>
* <b>For parameter types see {@link patio.Dataset#filter}.</b>
* </p>
*
* @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");
Expand Down
2 changes: 1 addition & 1 deletion lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
31 changes: 29 additions & 2 deletions test/dataset/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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});
Expand Down Expand Up @@ -2256,4 +2284,3 @@ it.describe("Dataset queries",function (it) {
it.afterAll(comb.hitch(patio, "disconnect"));

}).as(module);

0 comments on commit 40addd5

Please sign in to comment.