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" 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/readme.md b/readme.md index 7e22879..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)` 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`)