Skip to content

Commit 6740dc6

Browse files
committed
added support for LIMIT in queries
1 parent b316798 commit 6740dc6

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

lib/grammar.coffee

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ grammar =
1515
]
1616

1717
Query: [
18+
o "SelectWithLimitQuery"
1819
o "SelectQuery"
1920
]
2021

2122
SelectQuery: [
2223
o 'Select'
23-
o 'Select OrderClause', -> $1.order = $2; $1
24-
o 'Select GroupClause', -> $1.group = $2; $1
25-
o 'Select GroupClause OrderClause', -> $1.group = $2; $1.order = $3; $1
24+
o 'Select OrderClause', -> $1.order = $2; $1
25+
o 'Select GroupClause', -> $1.group = $2; $1
26+
o 'Select GroupClause OrderClause', -> $1.group = $2; $1.order = $3; $1
27+
]
28+
29+
SelectWithLimitQuery: [
30+
o 'SelectQuery LimitClause', -> $1.limit = $2; $1
2631
]
2732

2833
Select: [
2934
o 'SelectClause'
30-
o 'SelectClause WhereClause', -> $1.where = $2; $1
35+
o 'SelectClause WhereClause', -> $1.where = $2; $1
3136
]
3237

3338
SelectClause: [
@@ -38,6 +43,10 @@ grammar =
3843
o 'WHERE Conditions', -> new Where($2)
3944
]
4045

46+
LimitClause: [
47+
o 'LIMIT Number', -> new Limit($2)
48+
]
49+
4150
# TODO: order by can take mulitple sorts and also the direction is optional
4251
OrderClause: [
4352
o 'ORDER BY Value DIRECTION', -> new Order($3, $4)
@@ -68,9 +77,17 @@ grammar =
6877

6978
Value: [
7079
o 'Literal'
80+
o 'Number'
81+
o 'String'
82+
o 'Function'
83+
]
84+
85+
Number: [
7186
o 'NUMBER', -> new NumberValue($1)
87+
]
88+
89+
String: [
7290
o 'STRING', -> new StringValue($1)
73-
o 'Function'
7491
]
7592

7693
Literal: [

lib/lexer.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Lexer
5656
@tokenizeFromWord('ORDER') or
5757
@tokenizeFromWord('BY') or
5858
@tokenizeFromWord('HAVING') or
59+
@tokenizeFromWord('LIMIT') or
5960
@tokenizeFromWord('AS')
6061
operatorToken: -> @tokenizeFromList('OPERATOR', SQL_OPERATORS)
6162
conditionalToken: -> @tokenizeFromList('CONDITIONAL', SQL_CONDITIONALS)

lib/nodes.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ exports.Select = class Select
33
@order = null
44
@group = null
55
@where = null
6+
@limit = null
67
toString: ->
78
ret = ["SELECT #{@fields.join(', ')}"]
89
ret.push "FROM #{@source}"
910
ret.push @where.toString() if @where
1011
ret.push @group.toString() if @group
1112
ret.push @order.toString() if @order
13+
ret.push @limit.toString() if @limit
1214
ret.join("\n ")
1315

1416
exports.LiteralValue = class LiteralValue
@@ -27,6 +29,10 @@ exports.Order = class Order
2729
constructor: (@value, @direction='ASC') -> null
2830
toString: -> "ORDER BY #{@value} #{@direction}"
2931

32+
exports.Limit = class Limit
33+
constructor: (@value) -> null
34+
toString: -> "LIMIT #{@value}"
35+
3036
exports.Group = class Group
3137
constructor: (@values) ->
3238
@having = null

spec/grammar.spec.coffee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ describe "SQL Grammer", ->
2020
GROUP BY `x`, `y`
2121
"""
2222

23+
it "parses LIMIT clauses", ->
24+
expect(parse("SELECT * FROM my_table LIMIT 10").toString()).toEqual """
25+
SELECT *
26+
FROM `my_table`
27+
LIMIT 10
28+
"""
29+
2330
it "parses WHERE clauses", ->
2431
expect(parse("SELECT * FROM my_table WHERE x > 1 AND y = 'foo'").toString()).toEqual """
2532
SELECT *

0 commit comments

Comments
 (0)