Skip to content

Commit 4e0c732

Browse files
committed
support multiple order directions
1 parent 6740dc6 commit 4e0c732

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

lib/grammar.coffee

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ grammar =
4444
]
4545

4646
LimitClause: [
47-
o 'LIMIT Number', -> new Limit($2)
47+
o 'LIMIT Number', -> new Limit($2)
4848
]
4949

50-
# TODO: order by can take mulitple sorts and also the direction is optional
5150
OrderClause: [
52-
o 'ORDER BY Value DIRECTION', -> new Order($3, $4)
51+
o 'ORDER BY OrderArgs', -> new Order($3)
52+
]
53+
54+
OrderArgs: [
55+
o 'OrderArg', -> [$1]
56+
o 'OrderArgs SEPARATOR OrderArg', -> $1.concat($3)
57+
]
58+
59+
OrderArg: [
60+
o 'Value', -> new OrderArgument($1, 'ASC')
61+
o 'Value DIRECTION', -> new OrderArgument($1, $2)
5362
]
5463

5564
GroupClause: [

lib/lexer.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class Lexer
6565

6666
starToken: -> @tokenizeFromRegex('STAR', STAR)
6767
seperatorToken: -> @tokenizeFromRegex('SEPARATOR', SEPARATOR)
68-
# whitespaceToken: -> @tokenizeFromRegex('WHITESPACE', WHITESPACE, 0, 0, @preserveWhitespace)
6968
literalToken: -> @tokenizeFromRegex('LITERAL', LITERAL, 1, 0)
7069
numberToken: -> @tokenizeFromRegex('NUMBER', NUMBER)
7170
stringToken: -> @tokenizeFromRegex('STRING', STRING, 1, 0)

lib/nodes.coffee

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ exports.NumberValue = class LiteralValue
2626
toString: -> @value.toString()
2727

2828
exports.Order = class Order
29+
constructor: (@orderings) ->
30+
toString: -> "ORDER BY #{@orderings.join(', ')}"
31+
32+
exports.OrderArgument = class OrderArgument
2933
constructor: (@value, @direction='ASC') -> null
30-
toString: -> "ORDER BY #{@value} #{@direction}"
34+
toString: -> "#{@value} #{@direction}"
3135

3236
exports.Limit = class Limit
3337
constructor: (@value) -> null

spec/grammar.spec.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ describe "SQL Grammer", ->
3535
"""
3636

3737
it "parses WHERE with ORDER BY clauses", ->
38+
expect(parse("SELECT * FROM my_table WHERE x > 1 ORDER BY y").toString()).toEqual """
39+
SELECT *
40+
FROM `my_table`
41+
WHERE `x` > 1
42+
ORDER BY `y` ASC
43+
"""
44+
45+
it "parses WHERE with multiple ORDER BY clauses", ->
46+
expect(parse("SELECT * FROM my_table WHERE x > 1 ORDER BY x, y DESC").toString()).toEqual """
47+
SELECT *
48+
FROM `my_table`
49+
WHERE `x` > 1
50+
ORDER BY `x` ASC, `y` DESC
51+
"""
52+
53+
it "parses WHERE with ORDER BY clauses with direction", ->
3854
expect(parse("SELECT * FROM my_table WHERE x > 1 ORDER BY y ASC").toString()).toEqual """
3955
SELECT *
4056
FROM `my_table`

0 commit comments

Comments
 (0)