Skip to content

Instantly share code, notes, and snippets.

@4mitch
Last active July 14, 2017 13:01
Show Gist options
  • Save 4mitch/9213fc3b6e7b0c9b2e157a1e6ed06638 to your computer and use it in GitHub Desktop.
Save 4mitch/9213fc3b6e7b0c9b2e157a1e6ed06638 to your computer and use it in GitHub Desktop.
PEG.JS SQL insert
start = sql
sql = statement
statement = _ insert:insert
{ return {insert} }
insert = _ head:head _ M_WITH_ROWLOCK _
"(" _ column_list:(column_list) _ ")"
_ select:(select)
_ from:(from)?
_ where:(where)
_ select_hints:select_hints _
{ return {head, column_list, select, from, where, select_hints } }
column_list = column_list:column+ { return column_list }
column = ","? _ f:field _ { return f }
M_WITH_ROWLOCK = h:"M_WITH_ROWLOCK" { return text(h) }
select = _ "select"i _
select:("*" / select_list)
{ return select }
select_list = sl:line+ { let select_list = {};
select_list = Object.assign(select_list, ...sl); return {select_list} }
line = ","* _ t:target _ "=" _ s:source _ { let a = {}; a[t.valueOf()]= s; return a }
target = ","? _ f:field { return f }
source = tar_name { return text() }
tar_name = String / Numeric { return text() }
from = "from " table_list:table_list { return table_list }
table_list = from_table+
from_table = tname:tname _ index:M_NOLOCK_INDEX { return {tname,index} }
M_NOLOCK_INDEX = "M_NOLOCK_INDEX" _ "(" _ idxname _ ")" { return text() }
where = "where" _ where:(and_list)? { return where }
and_list = and_list_line+
and_list_line = _ "and"* _ target:target _ "=" _ source:source _ { return {target, source} }
select_hints = "M_KEEPPLAN"
head = "insert" _ t:tname { return t }
field = String {return text()}
name = String
tname = String {return text()}
idxname = StringNum {return text()}
Numeric = Integer
StringNum = [_a-z]i [_a-z0-9]i*
String = [_a-z]i+
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }
_ "whitespace"
= [ \t\n\r]* {return ""}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment