Subclass of patio.sql.ComplexExpression where the expression results in a boolean value in SQL.

Extends

Constructor

Defined sql.js

__filterObject Static Function Private


Defined sql.js

Example
  1. Dataset._filterObject({a : 1}) //=> WHERE (a = 1)
  2. Dataset._filterObject({x : {gt : 1}}) //=> WHERE (x > 1)
  3. Dataset._filterObject({x : {gt : 1}, a : 1}) //=> WHERE ((x > 1) AND (a = 1))
  4. Dataset._filterObject({x : {like : "name"}}) //=> WHERE (x LIKE 'name')
  5. Dataset._filterObject({x : {iLike : "name"}}) //=> WHERE (x LIKE 'name')
  6. Dataset._filterObject({x : {between : [1,10]}}) //=> WHERE ((x >= 1) AND (x <= 10))
  7. Dataset._filterObject({x : {notBetween : [1,10]}}) //=> WHERE ((x < 1) OR (x > 10))
  8. Dataset._filterObject({x : {neq : 1}}) //=> WHERE (x != 1)
Arguments Returns Source
  1. function (expr,key,op){
  2. /*jshint loopfunc:true*/
  3. var pairs = [], opts, newKey;
  4. var twoArityOperators = this.TWO_ARITY_OPERATORS;
  5. for (var k in expr) {
  6. var v = expr[k];
  7. if (isHash(v)) { //its a hash too filter it too!
  8. pairs.push(this.__filterObject(v, k, op));
  9. } else if (key && (twoArityOperators[k.toUpperCase()] || k.match(/between/i))) {
  10. //its a two arrity operator (e.g. '=', '>')
  11. newKey = isString(key) ? key.split(",") : [key];
  12. if (newKey.length > 1) {
  13. //this represents a hash where the key represents two columns
  14. //(e.g. {"col1,col2" : 1}) => WHERE (col1 = 1 AND col2 = 1)
  15. pairs = pairs.concat(newKey.map(function (k) {
  16. //filter each column with the expression
  17. return this.__filterObject(expr, k, op);
  18. }, this));
  19. } else {
  20. newKey = [sql.stringToIdentifier(newKey[0])];
  21. if (k.match(/^like$/)) {
  22. //its a like clause {col : {like : "hello"}}
  23. pairs.push(StringExpression.like.apply(StringExpression, (newKey.concat(isArray(v) ? v : [v]))));
  24. } else if (k.match(/^iLike$/)) {
  25. //its a like clause {col : {iLike : "hello"}}
  26. pairs.push(StringExpression.like.apply(StringExpression, (newKey.concat(isArray(v) ? v : [v]).concat({caseInsensitive: true}))));
  27. } else if (k.match(/between/i)) {
  28. //its a like clause {col : {between : [1,10]}}
  29. var between = sql.stringToIdentifier(newKey[0]).between(v);
  30. k === "notBetween" && (between = between.not());
  31. pairs.push(between);
  32. } else {
  33. //otherwise is just a boolean expressio
  34. //it its not a valid operator then we
  35. //BooleanExpression with throw an error
  36. pairs.push(new BooleanExpression(k, newKey[0], v));
  37. }
  38. }
  39. } else {
  40. //we're not a twoarity operator
  41. //so we create a boolean expression out of it
  42. newKey = k.split(",");
  43. if (newKey.length === 1) {
  44. newKey = sql.stringToIdentifier(newKey[0]);
  45. }
  46. opts = [
  47. [newKey, v]
  48. ];
  49. pairs.push(BooleanExpression.fromValuePairs(opts));
  50. }
  51. }
  52. //if the total of pairs is one then we just return the first element
  53. //otherwise we join them all with an AND
  54. return pairs.length === 1 ? pairs[0] : BooleanExpression.fromArgs([op || "AND"].concat(pairs));
  55. }

fromValuePairs Static Function Public


Defined sql.js

Take pairs of values (e.g. a hash or array of two element arrays) and converts it to a patio.sql.BooleanExpression. The operator and args used depends on the case of the right (2nd) argument:

BooleanExpression.fromValuePairs({a : [1,2,3]}) //=> a IN (1,2,3)
BooleanExpression.fromValuePairs({a : true}); // a IS TRUE;
BooleanExpression.fromValuePairs({a : /^A/i}); // a *~ '^A'

If multiple arguments are given, they are joined with the op given (AND by default, OR possible). If negate is set to true, all subexpressions are inverted before used.

BooleanExpression.fromValuePairs({a : [1,2,3], b : true}) //=> a IN (1,2,3) AND b IS TRUE
BooleanExpression.fromValuePairs({a : [1,2,3], b : true}, "OR") //=> a IN (1,2,3) OR b IS TRUE
BooleanExpression.fromValuePairs({a : [1,2,3], b : true}, "OR", true) //=> a NOT IN (1,2,3) AND b IS NOT TRUE

Arguments Returns Source
  1. function (a,op,negate){
  2. !Dataset && (Dataset = require("./dataset"));
  3. op = op || "AND", negate = negate || false;
  4. var pairArr = [];
  5. var isArr = isArray(a) && Expression.isConditionSpecifier(a);
  6. if (isHash(a)) {
  7. pairArr.push(this.__filterObject(a, null, op));
  8. } else {
  9. for (var k in a) {
  10. var v = isArr ? a[k][1] : a[k], ret;
  11. k = isArr ? a[k][0] : k;
  12. if (isArray(v) || isInstanceOf(v, Dataset)) {
  13. k = isArray(k) ? k.map(sql.stringToIdentifier) : sql.stringToIdentifier(k);
  14. ret = new BooleanExpression("IN", k, v);
  15. } else if (isInstanceOf(v, NegativeBooleanConstant)) {
  16. ret = new BooleanExpression("ISNOT", k, v.constant);
  17. } else if (isInstanceOf(v, BooleanConstant)) {
  18. ret = new BooleanExpression("IS", k, v.constant);
  19. } else if (isNull(v) || isBoolean(v)) {
  20. ret = new BooleanExpression("IS", k, v);
  21. } else if (isHash(v)) {
  22. ret = BooleanExpression.__filterObject(v, k, op);
  23. } else if (isRegExp(v)) {
  24. ret = StringExpression.like(sql.stringToIdentifier(k), v);
  25. } else {
  26. ret = new BooleanExpression("EQ", sql.stringToIdentifier(k), v);
  27. }
  28. negate && (ret = BooleanExpression.invert(ret));
  29. pairArr.push(ret);
  30. }
  31. }
  32. //if We just have one then return the first otherwise create a new Boolean expression
  33. return pairArr.length === 1 ? pairArr[0] : BooleanExpression.fromArgs([op].concat(pairArr));
  34. }

invert Static Function Public


Defined sql.js

Invert the expression, if possible. If the expression cannot be inverted, it throws an patio.error.ExpressionError. An inverted expression should match everything that the uninverted expression did not match, and vice-versa, except for possible issues with SQL NULL (i.e. 1 == NULL is NULL and 1 != NULL is also NULL).

Example
  1. BooleanExpression.invert(sql.a) //=> NOT "a"
Arguments Returns Source
  1. function (expression){
  2. if (isInstanceOf(expression, BooleanExpression)) {
  3. var op = expression.op, newArgs;
  4. if (op === "AND" || op === "OR") {
  5. newArgs = [OPERTATOR_INVERSIONS[op]].concat(expression.args.map(function (arg) {
  6. return BooleanExpression.invert(arg);
  7. }));
  8. return BooleanExpression.fromArgs(newArgs);
  9. } else {
  10. newArgs = [OPERTATOR_INVERSIONS[op]].concat(expression.args);
  11. return BooleanExpression.fromArgs(newArgs);
  12. }
  13. } else if (isInstanceOf(expression, StringExpression) || isInstanceOf(expression, NumericExpression)) {
  14. throw new ExpressionError(format("Cannot invert %4j", [expression]));
  15. } else {
  16. return new BooleanExpression("NOT", expression);
  17. }
  18. }

Documentation generated using coddoc.