Skip to content

Commit 94830e4

Browse files
committed
support for more select variations in grammer
1 parent 5e46b2e commit 94830e4

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/grammar.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ grammar =
2323
o 'SelectFrom OrderClause', -> $1.order = $2; $1
2424
o 'SelectFrom GroupClause', -> $1.group = $2; $1
2525
o 'SelectFrom WhereClause', -> $1.where = $2; $1
26-
o 'SelectFrom WhereClause OrderClause', -> "#{$1} #{$2} #{$3}"
27-
o 'SelectFrom WhereClause GroupClause', -> "#{$1} #{$2} #{$3}"
28-
o 'SelectFrom WhereClause GroupClause OrderClause', -> "#{$1} #{$2} #{$3} #{$4}"
26+
o 'SelectFrom WhereClause OrderClause', -> $1.where = $2; $1.order = $3; $1
27+
o 'SelectFrom WhereClause GroupClause', -> $1.where = $2; $1.group = $3; $1
28+
o 'SelectFrom WhereClause GroupClause OrderClause', -> $1.where = $2; $1.group = $3; $1.order = $4; $1
2929
]
3030

3131
SelectFrom: [

lib/nodes.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ exports.Select = class Select
66
ret = ["SELECT #{@fields.join(', ')}"]
77
ret.push "FROM #{@source}"
88
ret.push @where.toString() if @where
9-
ret.push @order.toString() if @order
109
ret.push @group.toString() if @group
10+
ret.push @order.toString() if @order
1111
ret.join("\n ")
1212

1313
exports.LiteralValue = class LiteralValue

spec/grammar.spec.coffee

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,28 @@ describe "SQL Grammer", ->
2626
FROM `my_table`
2727
WHERE `x` > 1 AND `y` = 'foo'
2828
"""
29+
30+
it "parses WHERE with ORDER BY clauses", ->
31+
expect(parse("SELECT * FROM my_table WHERE x > 1 ORDER BY y ASC").toString()).toEqual """
32+
SELECT *
33+
FROM `my_table`
34+
WHERE `x` > 1
35+
ORDER BY `y` ASC
36+
"""
37+
38+
it "parses WHERE with GROUP BY clauses", ->
39+
expect(parse("SELECT * FROM my_table WHERE x > 1 GROUP BY x, y").toString()).toEqual """
40+
SELECT *
41+
FROM `my_table`
42+
WHERE `x` > 1
43+
GROUP BY `x`, `y`
44+
"""
45+
46+
it "parses WHERE with GROUP BY and ORDER BY clauses", ->
47+
expect(parse("SELECT * FROM my_table WHERE x > 1 GROUP BY x, y ORDER BY COUNT(y) ASC").toString()).toEqual """
48+
SELECT *
49+
FROM `my_table`
50+
WHERE `x` > 1
51+
GROUP BY `x`, `y`
52+
ORDER BY COUNT(`y`) ASC
53+
"""

0 commit comments

Comments
 (0)