Skip to content

Commit

Permalink
Merge pull request #22 from TehShrike/issue-20
Browse files Browse the repository at this point in the history
Support expressions without values
  • Loading branch information
TehShrike authored Jun 22, 2019
2 parents 792c6af + c904576 commit 52ee0ff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_js:
- "6"
- "8"
- "10"
- "12"
11 changes: 8 additions & 3 deletions clause-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
11 changes: 10 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,19 +432,28 @@ 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` ])

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`)
Expand Down

0 comments on commit 52ee0ff

Please # to comment.