From 2528540a85ec4509f98a52e74af095eeca1ff167 Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Fri, 21 Jun 2019 10:37:41 -0500 Subject: [PATCH 1/4] Support expressions without values Fixes #20 --- clause-handlers.js | 11 ++++++++--- test.js | 11 ++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/clause-handlers.js b/clause-handlers.js index 17f33f7..e9e87dd 100644 --- a/clause-handlers.js +++ b/clause-handlers.js @@ -31,13 +31,18 @@ module.exports = { columnParam(joinedBy, opts, expression, comparator, value) { opts = opts || {} - if (value === undefined) { + const expressionObject = expressionToObject(expression) + + if (comparator === undefined) { + if (opts.like) { + throw new Error(`You can't use a "like" comparison without passing in a value`) + } + return Object.assign({ joinedBy }, expressionObject) + } else if (value === undefined) { value = comparator comparator = undefined } - const expressionObject = expressionToObject(expression) - const valueIsObject = (value && typeof value === `object` && value.params) const valueParams = valueIsObject diff --git a/test.js b/test.js index 8d165e9..4f73909 100644 --- a/test.js +++ b/test.js @@ -432,12 +432,13 @@ test(`no where value`, t => { const result = q.select(`myColumn`) .from(`table1`) .where(q`MATCH(myColumn) AGAINST(${ input })`) + .where(`someOtherColumn = somethingUnrelated`) .build() t.equal(result.sql, [ `SELECT myColumn`, `FROM table1`, - `WHERE MATCH(myColumn) AGAINST(?)`, + `WHERE MATCH(myColumn) AGAINST(?) AND someOtherColumn = somethingUnrelated`, ].join(`\n`)) t.deepEqual(result.values, [ `whatever` ]) @@ -445,6 +446,14 @@ test(`no where value`, t => { t.end() }) +test(`Throws an error if you call whereLike without a value`, t => { + t.throws(() => { + q.whereLike(`nuthin`) + }, /like/i) + + t.end() +}) + test(`toString`, t => { const result = q.select(`myColumn`) .from(`table1`) From 3840b96fab840be47df8c6193bf7cf667e18a51f Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Fri, 21 Jun 2019 10:51:41 -0500 Subject: [PATCH 2/4] Document that values are optional for where/having clauses --- readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 7e22879..46a960b 100644 --- a/readme.md +++ b/readme.md @@ -121,12 +121,12 @@ If you need a clause added that is not implemented yet, feel free to open a pull - `q.from(tablename | subquery, alias)` - `q.join(tablename | subquery, [alias], on)` - `q.leftJoin(tablename | subquery, [alias], on)` -- `q.where(expression, [comparitor], value)` -- `q.orWhere(expression, [comparitor], value)` +- `q.where(expression, [comparitor, [value]])` +- `q.orWhere(expression, [comparitor, [value]])` - `q.whereLike(expression, value)` - `q.orWhereLike(expression, value)` -- `q.having(expression, [comparitor], value)` -- `q.orHaving(expression, [comparitor], value)` +- `q.having(expression, [comparitor, [value]])` +- `q.orHaving(expression, [comparitor, [value]])` - `q.groupBy(expression1, expression2, etc)` - `q.orderBy(expression1, expression2, etc)` - `q.limit(offset)` From 2be854a431a5c3893437100362b584992b97084b Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Fri, 21 Jun 2019 10:58:29 -0500 Subject: [PATCH 3/4] Test on node 12 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 0610b87..11493b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,4 @@ node_js: - "6" - "8" - "10" + - "12" From c904576280c4198f375b997d888738ef1086c954 Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Fri, 21 Jun 2019 20:30:12 -0500 Subject: [PATCH 4/4] Fix comparitor->comparator typo --- readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 46a960b..2bce4a0 100644 --- a/readme.md +++ b/readme.md @@ -121,12 +121,12 @@ If you need a clause added that is not implemented yet, feel free to open a pull - `q.from(tablename | subquery, alias)` - `q.join(tablename | subquery, [alias], on)` - `q.leftJoin(tablename | subquery, [alias], on)` -- `q.where(expression, [comparitor, [value]])` -- `q.orWhere(expression, [comparitor, [value]])` +- `q.where(expression, [comparator, [value]])` +- `q.orWhere(expression, [comparator, [value]])` - `q.whereLike(expression, value)` - `q.orWhereLike(expression, value)` -- `q.having(expression, [comparitor, [value]])` -- `q.orHaving(expression, [comparitor, [value]])` +- `q.having(expression, [comparator, [value]])` +- `q.orHaving(expression, [comparator, [value]])` - `q.groupBy(expression1, expression2, etc)` - `q.orderBy(expression1, expression2, etc)` - `q.limit(offset)`