Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/enable where with multiples in conditions #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
.DS_Store
*.swp

.idea
137 changes: 87 additions & 50 deletions browser/sql-parser.js

Large diffs are not rendered by default.

102 changes: 54 additions & 48 deletions lib/compiled_parser.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions lib/grammar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion lib/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "Lexer and Parser for SQL Syntax",
"version": "0.5.5",
"scripts": {
"test": "./node_modules/.bin/cake build && ./node_modules/.bin/mocha --require should --compilers coffee:coffee-script/register"
"test": "cake build && mocha --require should --compilers coffee:coffee-script/register",
"build": "cake build"
},
"author": {
"name": "Andy Kent and Marcelo Camargo",
Expand Down
11 changes: 11 additions & 0 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ grammar =

WhereClause: [
o 'WHERE Expression', -> new Where($2)
o 'WHERE InConditions', -> new Where($2)
]

LimitClause: [
Expand Down Expand Up @@ -168,6 +169,16 @@ grammar =
o 'Value'
]

InConditions: [
o 'ExpressionList IN ExpressionList', -> new InConditions($1, $3)
]

ExpressionList: [
o 'Expression', -> [$1]
o 'ExpressionList SEPARATOR Expression', -> new ExpressionList($1, $3)
o 'LEFT_PAREN ExpressionList RIGHT_PAREN', -> $2
]

CaseBodies: [
o 'CaseBody'
o 'CaseBodies CaseBody', -> $1.concat $2
Expand Down
3 changes: 2 additions & 1 deletion src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class Lexer
@tokenizeFromWord('THEN') or
@tokenizeFromWord('ELSE') or
@tokenizeFromWord('END') or
@tokenizeFromWord('TOP')
@tokenizeFromWord('TOP') or
@tokenizeFromWord('IN')


dotToken: -> @tokenizeFromWord('DOT', '.')
Expand Down
10 changes: 10 additions & 0 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,13 @@ exports.Star = class Star
constructor: () -> null
toString: -> '*'
star: true

exports.ExpressionList = class ExpressionList
constructor: (@expressions, @expression) -> null
toString: ->
console.log(@expressions.toString && @expressions.toString())
return "(#{@expressions}, #{@expression})"

exports.InConditions = class InConditions
constructor: (@expression, @conditions) -> null
toString: -> "(#{@expression} IN #{@conditions})"
28 changes: 28 additions & 0 deletions test/grammar.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ describe "SQL Grammar", ->
FROM `a` AS `b`
"""

it "parses aliased field", ->
parse("""select a b from t""").toString().should.eql """
SELECT `a` AS `b`
FROM `t`
"""

describe "STARS", ->
it "parses stars as multiplication", ->
parse('SELECT * FROM foo WHERE a = 1*2').toString().should.eql """
Expand Down Expand Up @@ -452,3 +458,25 @@ describe "SQL Grammar", ->
SELECT `x`
FROM Y()
"""

describe "WHERE", ->
it "parses simple where condition", ->
parse("select a from b where a = 10").toString().should.eql """
SELECT `a`
FROM `b`
WHERE (`a` = 10)
"""

it "parses simple where condition with in clause", ->
parse("select a from b where a in (10, 'ten')").toString().should.eql """
SELECT `a`
FROM `b`
WHERE (`a` IN (10, 'ten'))
"""

it "parses where condition with multiple in clauses", ->
parse("select value, type from x where (value, type) in ((10, 'ten'), ('number', 'number in full'))").toString().should.eql """
SELECT `value`, `type`
FROM `x`
WHERE (`value` IN (10 'ten') AND (`type` IN ('number', 'number in full')))
"""