Skip to content

Commit

Permalink
Switch columns order
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed Apr 19, 2017
1 parent f1b8783 commit 015fea3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/common/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { entries } = Object
const { isArray } = Array
const { pluck, list } = require('./util')
const { pluck } = require('./util')

class Query {
get hasParams() {
Expand Down Expand Up @@ -32,6 +32,7 @@ class Select extends Query {

this.isNegated = false
this.isDistinct = false
this.params = {}

this.col = {}
this.src = []
Expand Down Expand Up @@ -139,14 +140,14 @@ class Select extends Query {
return (
`SELECT${this.isDistinct ? ' DISTINCT' : ''} ${
entries(this.col)
.map(([n, a]) => n === a ? n : `${n} AS ${a}`)
.map(([a, n]) => n === a ? n : `${n} AS ${a}`)
.join(', ') || '*'
}`
)
}

get FROM() {
return `FROM ${this.src.join(', ')}`
return `FROM ${this.src.join(' ')}`
}

get WHERE() {
Expand All @@ -164,5 +165,6 @@ module.exports = {
Query,
Select,

select(...args) { return new Select(...args) }
select(...args) { return new Select(...args) },
union(...args) { return args.join(' UNION ') }
}
25 changes: 20 additions & 5 deletions test/common/query_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ describe('Query Builder', () => {

it('alias', () => {
expect(select({ a: 'a', foo: 'b' }).query)
.to.match(/SELECT a, foo AS b/)
.to.match(/SELECT a, b AS foo/)
expect(select('a', { b: 'c' }, 'd').query)
.to.match(/SELECT a, b AS c, d/)
.to.match(/SELECT a, c AS b, d/)
})

it('distinct', () =>
expect(select('a').distinct.query).to.match(/SELECT DISTINCT a/))

it('scoped', () => {
expect(select('t.a', { 't.b': 'c' }, 't.*').query)
expect(select('t.a', { c: 't.b' }, 't.*').query)
.to.match(/^SELECT t\.a, t\.b AS c, t\.\*/)
})

Expand All @@ -43,15 +43,30 @@ describe('Query Builder', () => {
).to.match(/SELECT a, b FROM b ORDER BY b, a DESC/)
})

it('params', () => {
it('null', () => {
let q = select()

q.where({ a: null })
expect(select().where({ a: null }).WHERE).to.eql('WHERE a IS NULL')
expect(q.WHERE).to.eql('WHERE a IS NULL')

q.not.where({ a: null })
expect(q.WHERE).to.eql('WHERE a IS NULL AND a IS NOT NULL')
})

it('lists', () => {
expect(select().where({ a: [1, 2, 3] }).WHERE)
.to.eql('WHERE a IN (1, 2, 3)')

expect(select().unless({ a: [1, 2, 3] }).WHERE)
.to.eql('WHERE a NOT IN (1, 2, 3)')
})

it('params', () => {
let q = select().where({ a: 1, b: null, c: 'foo' })

expect(q.WHERE).to.eql('WHERE a = $a AND b IS NULL AND c = $c')
expect(q.params).to.eql({ $a: 1, $c: 'foo' })
})

})
})

0 comments on commit 015fea3

Please sign in to comment.