diff --git a/src/alasqlparser.jison b/src/alasqlparser.jison index a23638580..f7efd8d63 100755 --- a/src/alasqlparser.jison +++ b/src/alasqlparser.jison @@ -1,3048 +1,3039 @@ -/* -// -// alasqlparser.jison -// SQL Parser for AlaSQL -// (c) 2014-2015, Andrey Gershun -// -// -*/ - -%lex -%options case-insensitive - -%% - -/* -\$\$(.+?)\$\$ return 'DOLLARSTRING' -*/ - -\`\`([^\`])+\`\` return 'JAVASCRIPT' -\[\?\] return 'BRAQUESTION' -'@[' return 'ATLBRA' -'ARRAY[' return 'ARRAYLBRA' -\[([^\]'])*?\] return 'BRALITERAL' -\`([^\`'])*?\` return 'BRALITERAL' - -N(['](\\.|[^']|\\\')*?['])+ return 'NSTRING' -X(['](\\.|[^']|\\\')*?['])+ return 'NSTRING' -(['](\\.|[^']|\\\')*?['])+ return 'STRING' -(["](\\.|[^"]|\\\")*?["])+ return 'STRING' - - -"--"(.*?)($|\r\n|\r|\n) return /* its a COMMENT */ - -\s+ /* skip whitespace */ -'||' return 'BARBAR' -'|' return 'BAR' -/* '&&' return 'AMPERSANDAMPERSAND' */ - -VALUE\s+OF\s+SEARCH yytext = 'VALUE';return 'SEARCH' -VALUE\s+OF\s+SELECT yytext = 'VALUE';return 'SELECT' -ROW\s+OF\s+SELECT yytext = 'ROW';return 'SELECT' -COLUMN\s+OF\s+SELECT yytext = 'COLUMN';return 'SELECT' -MATRIX\s+OF\s+SELECT yytext = 'MATRIX';return 'SELECT' -INDEX\s+OF\s+SELECT yytext = 'INDEX';return 'SELECT' -RECORDSET\s+OF\s+SELECT yytext = 'RECORDSET';return 'SELECT' -TEXT\s+OF\s+SELECT yytext = 'TEXT';return 'SELECT' - -'SELECT' yytext = 'SELECT';return 'SELECT' - -'ABSOLUTE' return 'ABSOLUTE' -'ACTION' return 'ACTION' -'ADD' return 'ADD' -'AFTER' return 'AFTER' -'AGGR' return 'AGGR' -'AGGREGATE' return 'AGGREGATE' -'AGGREGATOR' return 'AGGREGATE' -'ALL' return 'ALL' -'ALTER' return 'ALTER' -'AND' return 'AND' -'ANTI' return 'ANTI' -'ANY' return 'ANY' -'APPLY' return 'APPLY' -'ARRAY' return 'ARRAY' -'AS' return 'AS' -'ASSERT' return 'ASSERT' -'ASC' return 'DIRECTION' -'ATTACH' return 'ATTACH' -AUTO(_)?INCREMENT return 'IDENTITY' -'AVG' return 'AVG' - -'BEFORE' return 'BEFORE' -'BEGIN' return 'BEGIN' -'BETWEEN' return 'BETWEEN' -'BREAK' return 'BREAK' -NOT\s+BETWEEN return 'NOT_BETWEEN' -NOT\s+LIKE return 'NOT_LIKE' -'BY' return 'BY' - -/* Postgres aliases */ -'~~*' return 'LIKE' -'!~~*' return 'NOT_LIKE' -'~~' return 'LIKE' -'!~~' return 'NOT_LIKE' -'ILIKE' return 'LIKE' -NOT\s+ILIKE return 'NOT_LIKE' - -'CALL' return 'CALL' -'CASE' return 'CASE' -'CAST' return 'CAST' -'CHECK' return 'CHECK' -'CLASS' return 'CLASS' -'CLOSE' return 'CLOSE' -'COLLATE' return 'COLLATE' -COLUMN return 'COLUMN' -COLUMNS return 'COLUMN' -"COMMIT" return 'COMMIT' -"CONSTRAINT" return 'CONSTRAINT' -"CONTENT" return 'CONTENT' -"CONTINUE" return 'CONTINUE' -"CONVERT" return 'CONVERT' -"CORRESPONDING" return 'CORRESPONDING' -"COUNT" return 'COUNT' -'CREATE' return 'CREATE' -"CROSS" return 'CROSS' -'CUBE' return 'CUBE' -"CURRENT_TIMESTAMP" return 'CURRENT_TIMESTAMP' -"CURRENT_DATE" return 'CURRENT_DATE' -"CURDATE" return 'CURRENT_DATE' -"CURSOR" return 'CURSOR' -DATABASE(S)? return 'DATABASE' -'DATEADD' return 'DATEADD' -'DATEDIFF' return 'DATEDIFF' -'TIMESTAMPDIFF' return 'TIMESTAMPDIFF' -'DECLARE' return 'DECLARE' -'DEFAULT' return 'DEFAULT' -'DELETE' return 'DELETE' -'DELETED' return 'DELETED' -'DESC' return 'DIRECTION' -'DETACH' return 'DETACH' -'DISTINCT' return 'DISTINCT' -/* DOUBLE\s+PRECISION return 'LITERAL' */ -'DROP' return 'DROP' -'ECHO' return 'ECHO' -'EDGE' return 'EDGE' -'END' return 'END' -'ENUM' return 'ENUM' -'ELSE' return 'ELSE' -'ESCAPE' return 'ESCAPE' -'EXCEPT' return 'EXCEPT' -'EXEC' return 'CALL' -'EXECUTE' return 'CALL' -'EXISTS' return 'EXISTS' -'EXPLAIN' return 'EXPLAIN' -'FALSE' return 'FALSE' -'FETCH' return 'FETCH' -'FIRST' return 'FIRST' -'FOR' return 'FOR' -'FOREIGN' return 'FOREIGN' -'FROM' return 'FROM' -'FULL' return 'FULL' -'FUNCTION' return 'FUNCTION' -'GLOB' return 'GLOB' -'GO' return 'GO' -'GRAPH' return 'GRAPH' -'GROUP' return 'GROUP' -'GROUPING' return 'GROUPING' -'HAVING' return 'HAVING' -/*'HELP' return 'HELP'*/ -'IF' return 'IF' -'IDENTITY' return 'IDENTITY' -'IS' return 'IS' -'IN' return 'IN' -'INDEX' return 'INDEX' -'INDEXED' return 'INDEXED' -'INNER' return 'INNER' -'INSTEAD' return 'INSTEAD' -'INSERT' return 'INSERT' -'INSERTED' return 'INSERTED' -'INTERSECT' return 'INTERSECT' -'INTERVAL' return 'INTERVAL' -'INTO' return 'INTO' -'JOIN' return 'JOIN' -'KEY' return 'KEY' -'LAST' return 'LAST' -'LET' return 'LET' -'LEFT' return 'LEFT' -'LIKE' return 'LIKE' -'LIMIT' return 'LIMIT' -'MATCHED' return 'MATCHED' -'MATRIX' return 'MATRIX' - -/*"MAX" return 'MAX'*/ -/*"MIN" return 'MIN'*/ - -'MAX'(\s+)?/'(' return 'MAX' -'MAX'(\s+)?/(','|')') return 'MAXNUM' -'MIN'(\s+)?/'(' return 'MIN' - -"MERGE" return 'MERGE' -"MINUS" return 'EXCEPT' -"MODIFY" return 'MODIFY' -'NATURAL' return 'NATURAL' -'NEXT' return 'NEXT' -'NEW' return 'NEW' -'NOCASE' return 'NOCASE' -'NO' return 'NO' -'NOT' return 'NOT' -'NULL' return 'NULL' -'NULLS' return 'NULLS' -'OFF' return 'OFF' -'ON' return 'ON' -'ONLY' return 'ONLY' -'OF' return 'OF' -'OFFSET' return 'OFFSET' -'OPEN' return 'OPEN' -'OPTION' return 'OPTION' -'OR' return 'OR' -'ORDER' return 'ORDER' -'OUTER' return 'OUTER' -'OVER' return 'OVER' -'PATH' return 'PATH' -'PARTITION' return 'PARTITION' -'PERCENT' return 'PERCENT' -'PIVOT' return 'PIVOT' -'PLAN' return 'PLAN' -'PRIMARY' return 'PRIMARY' -'PRINT' return 'PRINT' -'PRIOR' return 'PRIOR' -'QUERY' return 'QUERY' -'READ' return 'READ' -'RECORDSET' return 'RECORDSET' -'REDUCE' return 'REDUCE' -'REFERENCES' return 'REFERENCES' -'REGEXP' return 'REGEXP' -'REINDEX' return 'REINDEX' -'RELATIVE' return 'RELATIVE' -'REMOVE' return 'REMOVE' -'RENAME' return 'RENAME' -'REPEAT' return 'REPEAT' -'REPLACE' return 'REPLACE' -'REQUIRE' return 'REQUIRE' -'RESTORE' return 'RESTORE' -'RETURN' return 'RETURN' -'RETURNS' return 'RETURN' -'RIGHT' return 'RIGHT' -'ROLLBACK' return 'ROLLBACK' -'ROLLUP' return 'ROLLUP' -'ROW' return 'ROW' -'ROWS' return 'ROWS' -SCHEMA(S)? return 'DATABASE' -'SEARCH' return 'SEARCH' - -'SEMI' return 'SEMI' -SET return 'SET' -SETS return 'SET' -'SHOW' return 'SHOW' -'SOME' return 'SOME' -'SOURCE' return 'SOURCE' -'STRATEGY' return 'STRATEGY' -'STORE' return 'STORE' -'SUM' return 'SUM' -'TOTAL' return 'TOTAL' -'TABLE' return 'TABLE' -'TABLES' return 'TABLE' -'TARGET' return 'TARGET' -'TEMP' return 'TEMP' -'TEMPORARY' return 'TEMP' -'TEXTSTRING' return 'TEXTSTRING' -'THEN' return 'THEN' -'TIMEOUT' return 'TIMEOUT' -'TO' return 'TO' -'TOP' return 'TOP' -'TRAN' return 'TRANSACTION' -'TRANSACTION' return 'TRANSACTION' -'TRIGGER' return 'TRIGGER' -'TRUE' return 'TRUE' -'TRUNCATE' return 'TRUNCATE' -'UNION' return 'UNION' -'UNIQUE' return 'UNIQUE' -'UNPIVOT' return 'UNPIVOT' -'UPDATE' return 'UPDATE' -'USE' return 'USE' -/* 'USER' return 'USER' */ -'USING' return 'USING' -'VALUE' return 'VALUE' -'VALUES' return 'VALUES' -'VERTEX' return 'VERTEX' -'VIEW' return 'VIEW' -'WHEN' return 'WHEN' -'WHERE' return 'WHERE' -'WHILE' return 'WHILE' -'WITH' return 'WITH' -'WORK' return 'TRANSACTION' /* Is this keyword required? */ - -(\d*[.])?\d+[eE]\d+ return 'NUMBER' -(\d*[.])?\d+ return 'NUMBER' - -'->' return 'ARROW' -'#' return 'SHARP' -'+' return 'PLUS' -'-' return 'MINUS' -'*' return 'STAR' -'/' return 'SLASH' -'%' return 'MODULO' -'!===' return 'NEEQEQEQ' -'===' return 'EQEQEQ' -'!==' return 'NEEQEQ' -'==' return 'EQEQ' -'>=' return 'GE' -'&' return 'AMPERSAND' -'|' return 'BAR' -'<<' return 'LTLT' -'>>' return 'GTGT' -'>' return 'GT' -'<=' return 'LE' -'<>' return 'NE' -'<' return 'LT' -'=' return 'EQ' -'!=' return 'NE' -'(' return 'LPAR' -')' return 'RPAR' -'@' return 'AT' -'{' return 'LCUR' -'}' return 'RCUR' - -']' return 'RBRA' - -':-' return 'COLONDASH' -'?-' return 'QUESTIONDASH' -'..' return 'DOTDOT' -'.' return 'DOT' -',' return 'COMMA' -'::' return 'DOUBLECOLON' -':' return 'COLON' -';' return 'SEMICOLON' -'$' return 'DOLLAR' -'?' return 'QUESTION' -'!' return 'EXCLAMATION' -'^' return 'CARET' - -'~' return 'TILDA' - -[0-9]*[a-zA-Z_]+[a-zA-Z_0-9]* return 'LITERAL' -<> return 'EOF' -. return 'INVALID' - -/lex -%left COMMA -%left DOUBLECOLON -%left OR -/* %left AND */ -%left AND BETWEEN NOT_BETWEEN -/*%left AND*/ -%left IN -%left NOT -%left GT GE LT LE EQ NE EQEQ NEEQEQ EQEQEQ NEEQEQEQ -%left IS -%left LIKE NOT_LIKE REGEXP GLOB -%left GTGT LTLT AMPERSAND BAR -%left PLUS MINUS -%left STAR SLASH MODULO -%left CARET -%left DOT ARROW EXCLAMATION -%left TILDA -%left SHARP -%left BARBAR - -%ebnf -%start main -%% - -Literal - : LITERAL - { - if (alasql.options.casesensitive) $$ = $1; - else $$ = $1.toLowerCase(); - } - | BRALITERAL - { $$ = doubleq($1.substr(1,$1.length-2)); } - | error NonReserved - { $$ = $2.toLowerCase() } - ; - -LiteralWithSpaces - : LITERAL - { $$ = $1 } - | LiteralWithSpaces LITERAL - { $$ = $2 ? $1 + ' ' + $2 : $1 } - ; - -main - : Statements EOF - { return new yy.Statements({statements:$1}); } - ; - -Statements - : Statements (SEMICOLON|GO) AStatement - { $$ = $1; if($3) $1.push($3); } - | AStatement - { $$ = [$1]; } - | ExplainStatement - { $$ = [$1]; } - ; - -ExplainStatement - : EXPLAIN AStatement - { $$ = $2; $2.explain = true; } - | EXPLAIN QUERY PLAN AStatement - { $$ = $4; $4.explain = true;} - ; - -AStatement - : Statement - { - $$ = $1; - - // TODO combine exists and queries - if(yy.exists) $$.exists = yy.exists; - delete yy.exists; - if(yy.queries) $$.queries = yy.queries; - delete yy.queries; - } - ; - -Statement - : { $$ = undefined; } - | AlterTable - | AttachDatabase - | Call - | CreateDatabase - | CreateIndex - | CreateGraph - | CreateTable - | CreateView - | CreateEdge - | CreateVertex - | Declare - | Delete - | DetachDatabase - | DropDatabase - | DropIndex - | DropTable - | DropView - | If - | Insert - | Merge - | Reindex - | RenameTable - | Select - | ShowCreateTable - | ShowColumns - | ShowDatabases - | ShowIndex - | ShowTables - | TruncateTable - | WithSelect - - | CreateTrigger - | DropTrigger - - | BeginTransaction - | CommitTransaction - | RollbackTransaction - | EndTransaction - | UseDatabase - | Update - /*| Help*/ - | JavaScript - - | Source - | Assert - | While - | Continue - | Break - | BeginEnd - | Print - | Require - | SetVariable - | ExpressionStatement - | AddRule - | Query - -/* PLugins */ - - | Echo - -/* - | Store - | Restore - - | DeclareCursor - | OpenCursor - | FetchCursor - | CloseCursor - - | SavePoint - | StoreDatabase - | StoreTable - | RestoreDatabase - | RestoreTable - - | While - | BulkInsert - - | CreateFunction - | CreateProcedure - | Loop - | ForLoop -*/ - - | CreateFunction - | CreateAggregate - ; - -/* WITH */ - -WithSelect - : WITH WithTablesList Select - { $$ = new yy.WithSelect({withs: $2, select:$3}); } - ; - -WithTablesList - : WithTablesList COMMA WithTable - { $1.push($3); $$=$1; } - | WithTable - { $$ = [$1]; } - ; - -WithTable - : Literal AS LPAR Select RPAR - { $$ = {name:$1, select:$4}; } - ; - -/* SELECT */ - -Select - : SelectClause RemoveClause? IntoClause FromClause PivotClause? WhereClause GroupClause OrderClause LimitClause UnionClause - { - yy.extend($$,$1); yy.extend($$,$2); yy.extend($$,$3); yy.extend($$,$4); - yy.extend($$,$5); yy.extend($$,$6);yy.extend($$,$7); - yy.extend($$,$8); yy.extend($$,$9); yy.extend($$,$10); - $$ = $1; - if(yy.exists) $$.exists = yy.exists.slice(); -/* if(yy.queries) $$.queries = yy.queries; - delete yy.queries; -*/ } - | SEARCH SearchSelector* IntoClause SearchFrom? - /* SearchLimit? SearchStrategy? SearchTimeout? */ - { - $$ = new yy.Search({selectors:$2, from:$4}); - yy.extend($$,$3); - } - ; - -PivotClause - : PIVOT LPAR Expression FOR Literal PivotClause2? RPAR AsLiteral? - { $$ = {pivot:{expr:$3, columnid:$5, inlist:$6, as:$8}}; } - | UNPIVOT LPAR Literal FOR Literal IN LPAR ColumnsList RPAR RPAR AsLiteral? - { $$ = {unpivot:{tocolumnid:$3, forcolumnid:$5, inlist:$8, as:$11}}; } - ; - -PivotClause2 - : IN LPAR AsList RPAR - { $$ = $3; } - ; - -AsLiteral - : AS Literal - { $$ = $2; } - | Literal - { $$ = $1; } - ; - -AsList - : AsList COMMA AsPart - { $$ = $1; $$.push($3); } - | AsPart - { $$ = [$1]; } - ; - -AsPart - : Expression - { $$ = {expr:$1}; } - | Expression AS Literal - { $$ = {expr:$1,as:$3}; } - ; - -RemoveClause - : REMOVE COLUMN? RemoveColumnsList - { $$ = {removecolumns:$3}; } - ; - -RemoveColumnsList - : RemoveColumnsList COMMA RemoveColumn - { $$ = $1; $$.push($3); } - | RemoveColumn - { $$ = [$1]; } - ; - -RemoveColumn - : Column - { $$ = $1; } - | LIKE StringValue - { $$ = {like:$2}; } - ; - -ArrowDot - : ARROW - | DOT - ; - -SearchSelector - : Literal - { $$ = {srchid:"PROP", args: [$1]}; } - - | ORDER BY LPAR OrderExpressionsList RPAR - { $$ = {srchid:"ORDERBY", args: $4}; } - | ORDER BY LPAR DIRECTION? RPAR - { - var dir = $4; - if(!dir) dir = 'ASC'; - $$ = {srchid:"ORDERBY", args: [{expression: new yy.Column({columnid:'_'}), direction:dir}]}; - } - - | DOTDOT - { $$ = {srchid:"PARENT"}; } - | ArrowDot Literal - { $$ = {srchid:"APROP", args: [$2]}; } - | CARET - { $$ = {selid:"ROOT"};} - | EQ Expression - { $$ = {srchid:"EQ", args: [$2]}; } - | LIKE Expression - { $$ = {srchid:"LIKE", args: [$2]}; } - | LPAR SearchSelector+ RPAR - { $$ = {selid:"WITH", args: $2}; } - | WITH LPAR SearchSelector+ RPAR - { $$ = {selid:"WITH", args: $3}; } - | Literal LPAR ExprList? RPAR - { $$ = {srchid:$1.toUpperCase(), args:$3}; } - | WHERE LPAR Expression RPAR - { $$ = {srchid:"WHERE", args:[$3]}; } - | OF LPAR Expression RPAR - { $$ = {selid:"OF", args:[$3]}; } - | CLASS LPAR Literal RPAR - { $$ = {srchid:"CLASS", args:[$3]}; } - | NUMBER - { $$ = {srchid:"PROP", args: [$1]}; } - | STRING - { $$ = {srchid:"NAME", args: [$1.substr(1,$1.length-2)]}; } - | SLASH - { $$ = {srchid:"CHILD"}; } - | VERTEX - { $$ = {srchid:"VERTEX"}; } - | EDGE - { $$ = {srchid:"EDGE"}; } - | EXCLAMATION - { $$ = {srchid:"REF"}; } - | SHARP Literal - { $$ = {srchid:"SHARP", args:[$2]}; } - | MODULO Literal - { $$ = {srchid:"ATTR", args:((typeof $2 == 'undefined')?undefined:[$2])}; } - | MODULO SLASH - { $$ = {srchid:"ATTR"}; } - | GT - { $$ = {srchid:"OUT"}; } - | LT - { $$ = {srchid:"IN"}; } - | GTGT - { $$ = {srchid:"OUTOUT"}; } - | LTLT - { $$ = {srchid:"ININ"}; } - | DOLLAR - { $$ = {srchid:"CONTENT"}; } /* TODO Decide! */ -/* | DELETE LPAR RPAR - { $$ = {srchid:"DELETE"}; } -*/ | Json - { $$ = {srchid:"EX",args:[new yy.Json({value:$1})]}; } - | AT Literal - { $$ = {srchid:"AT", args:[$2]}; } - | AS AT Literal - { $$ = {srchid:"AS", args:[$3]}; } - | SET LPAR SetColumnsList RPAR - { $$ = {srchid:"SET", args:$3}; } - - | TO AT Literal - { $$ = {selid:"TO", args:[$3]}; } - | VALUE - { $$ = {srchid:"VALUE"}; } - | ROW LPAR ExprList RPAR - { $$ = {srchid:"ROW", args:$3}; } - | COLON Literal - { $$ = {srchid:"CLASS", args:[$2]}; } - | SearchSelector PlusStar - { $$ = {selid:$2,args:[$1] }; } - - | NOT LPAR SearchSelector* RPAR - { $$ = {selid:"NOT",args:$3 }; } - | IF LPAR SearchSelector* RPAR - { $$ = {selid:"IF",args:$3 }; } - | Aggregator LPAR SearchSelector* RPAR - { $$ = {selid:$1,args:$3 }; } - | (DISTINCT|UNIQUE) LPAR SearchSelector* RPAR - { $$ = {selid:'DISTINCT',args:$3 }; } - | UNION LPAR SearchSelectorList RPAR - { $$ = {selid:'UNION',args:$3 }; } - | UNION ALL LPAR SearchSelectorList RPAR - { $$ = {selid:'UNIONALL',args:$4 }; } - | ALL LPAR SearchSelector* RPAR - { $$ = {selid:'ALL',args:[$3] }; } - | ANY LPAR SearchSelector* RPAR - { $$ = {selid:'ANY',args:[$3] }; } - | INTERSECT LPAR SearchSelectorList RPAR - { $$ = {selid:'INTERSECT',args:$3 }; } - | EXCEPT LPAR SearchSelectorList RPAR - { $$ = {selid:'EXCEPT',args:$3 }; } - | AND LPAR SearchSelectorList RPAR - { $$ = {selid:'AND',args:$3 }; } - | OR LPAR SearchSelectorList RPAR - { $$ = {selid:'OR',args:$3 }; } - | PATH LPAR SearchSelector RPAR - { $$ = {selid:'PATH',args:[$3] }; } - | RETURN LPAR ResultColumns RPAR - { $$ = {srchid:'RETURN',args:$3 }; } - | REPEAT LPAR SearchSelector* COMMA ExprList RPAR - { $$ = {selid:'REPEAT',sels:$3, args:$5 }; } - ; - -SearchSelectorList - : SearchSelectorList COMMA SearchSelector* - { $$ = $1; $$.push($3);} - | SearchSelector* - { $$ = [$1]; } - ; - -PlusStar - : PLUS - { $$ = "PLUS"; } - | STAR - { $$ = "STAR"; } - | QUESTION - { $$ = "QUESTION"; } - ; - -SearchFrom - : FROM Expression - { $$ = $2; } - ; - -/* -SearchLet - : LET - ; - -SearchWhile - : WHILE Expression - ; -SearchLimit - : LIMIT Expression - ; - -SearchStrategy - : STRATEGY Literal - ; - -SearchTimeout - : TIMEOUT Expression - ; - -*/ - -SelectClause - : - /* - - { $$ = new yy.Select({ columns:new yy.Column({columnid:'_'}), modifier: 'COLUMN' }); } - | -*/ - - SelectModifier DISTINCT TopClause ResultColumns - { $$ = new yy.Select({ columns:$4, distinct: true }); yy.extend($$, $1); yy.extend($$, $3); } - | SelectModifier UNIQUE TopClause ResultColumns - { $$ = new yy.Select({ columns:$4, distinct: true }); yy.extend($$, $1);yy.extend($$, $3); } - | SelectModifier ALL TopClause ResultColumns - { $$ = new yy.Select({ columns:$4, all:true }); yy.extend($$, $1);yy.extend($$, $3); } - | SelectModifier TopClause ResultColumns? - { - if(!$3) { - $$ = new yy.Select({columns:[new yy.Column({columnid:'_',})], modifier:'COLUMN'}); - } else { - $$ = new yy.Select({ columns:$3 }); yy.extend($$, $1);yy.extend($$, $2); - } - } -/* | - { $$ = new yy.Select({columns:[new yy.Column({columnid:'_', modifier:'COLUMN'})]});} -*/ ; - -SelectModifier - : SELECT - { if($1=='SELECT') $$ = undefined; else $$ = {modifier: $1}; } - | SELECT VALUE - { $$ = {modifier:'VALUE'}} - | SELECT ROW - { $$ = {modifier:'ROW'}} - | SELECT COLUMN - { $$ = {modifier:'COLUMN'}} - | SELECT MATRIX - { $$ = {modifier:'MATRIX'}} - | SELECT TEXTSTRING - { $$ = {modifier:'TEXTSTRING'}} - | SELECT INDEX - { $$ = {modifier:'INDEX'}} - | SELECT RECORDSET - { $$ = {modifier:'RECORDSET'}} - ; - -TopClause - : TOP NumValue PERCENT? - { $$ = {top: $2, percent:(typeof $3 != 'undefined'?true:undefined)}; } - | TOP LPAR NumValue RPAR - { $$ = {top: $3}; } - | { $$ = undefined; } - ; - -IntoClause - : {$$ = undefined; } - | INTO Table - {$$ = {into: $2} } - | INTO FuncValue - {$$ = {into: $2} } - | INTO ParamValue - {$$ = {into: $2} } - | INTO VarValue - {$$ = {into: $2} } - | INTO STRING - { - var s = $2; - s = s.substr(1,s.length-2); - var x3 = s.substr(-3).toUpperCase(); - var x4 = s.substr(-4).toUpperCase(); - if(s[0] == '#') { - $$ = {into: new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; - } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { - $$ = {into: new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; - } else if(x4=='XLSX' || x4 == 'JSON') { - $$ = {into: new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; - } - } - ; - -FromClause - : FROM FromTablesList - { $$ = { from: $2 }; } -/* | FROM FromTable JoinTablesList - { $$ = { from: [$2], joins: $3 }; } -*/ | FROM FromTablesList JoinTablesList - { $$ = { from: $2, joins: $3 }; } -/* | FROM LPAR FromTable JoinTablesList RPAR - { $$ = { from: [$3], joins: $4 }; } -*/ | FROM LPAR FromTablesList JoinTablesList RPAR - { $$ = { from: $3, joins: $4 }; } - | - { $$ = undefined; } - ; - -ApplyClause - : CROSS APPLY LPAR Select RPAR Literal - { $$ = new yy.Apply({select: $4, applymode:'CROSS', as:$6}); } - | CROSS APPLY LPAR Select RPAR AS Literal - { $$ = new yy.Apply({select: $4, applymode:'CROSS', as:$7}); } -/* { - if(!yy.exists) yy.exists = []; - $$ = new yy.Apply({select: $4, applymode:'CROSS', as:$7,existsidx:yy.exists.length}); - yy.exists.push($3); - - } -*/ | OUTER APPLY LPAR Select RPAR Literal - { $$ = new yy.Apply({select: $4, applymode:'OUTER', as:$6}); } - | OUTER APPLY LPAR Select RPAR AS Literal - { $$ = new yy.Apply({select: $4, applymode:'OUTER', as:$7}); } - ; - -FromTablesList - : FromTable - { $$ = [$1]; } - | FromTablesList COMMA FromTable - { $$ = $1; $1.push($3); } - ; - -FromTable - : LPAR Select RPAR Literal - { $$ = $2; $$.as = $4 } - | LPAR Select RPAR AS Literal - { $$ = $2; $$.as = $5 } - | LPAR Select RPAR /* default alias */ - { $$ = $2; $$.as = 'default' } - - | Json AS? Literal? - { $$ = new yy.Json({value:$1}); $1.as = $3 } - - | Table Literal - { $$ = $1; $1.as = $2 } - | Table AS Literal - { $$ = $1; $1.as = $3 } - | Table - { $$ = $1; } - | Table NOT INDEXED - { $$ = $1; } - | ParamValue Literal - { $$ = $1; $1.as = $2; } - | ParamValue AS Literal - { $$ = $1; $1.as = $3; } - | ParamValue - { $$ = $1; $1.as = 'default'; } - - | FuncValue - { $$ = $1; $1.as = 'default'; } - | FuncValue Literal - { $$ = $1; $1.as = $2; } - | FuncValue AS Literal - { $$ = $1; $1.as = $3; } - - | INSERTED - { $$ = {inserted:true}; } - - | VarValue - { $$ = $1; $1.as = 'default'; } - | VarValue Literal - { $$ = $1; $1.as = $2; } - | VarValue AS Literal - { $$ = $1; $1.as = $3; } - - | FromString - { $$ = $1; $1.as = 'default'; } - | FromString Literal - { $$ = $1; $1.as = $2; } - | FromString AS Literal - { $$ = $1; $1.as = $3; } - ; - -FromString - : STRING - { - var s = $1; - s = s.substr(1,s.length-2); - var x3 = s.substr(-3).toUpperCase(); - var x4 = s.substr(-4).toUpperCase(); - var r; - if(s[0] == '#') { - r = new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); - } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { - r = new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); - } else if(x4=='XLSX' || x4 == 'JSON') { - r = new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); - } else { - throw new Error('Unknown string in FROM clause'); - }; - $$ = r; - } - ; - -Table - : Literal DOT Literal - { - if($1 == 'INFORMATION_SCHEMA') { - $$ = new yy.FuncValue({funcid: $1, args:[new yy.StringValue({value:$3})]}); - } else { - $$ = new yy.Table({databaseid: $1, tableid:$3}); - } - } - | Literal - { $$ = new yy.Table({tableid: $1});} - ; - -JoinTablesList - : JoinTablesList JoinTable - { $$ = $1; $1.push($2); } - | JoinTablesList ApplyClause - { $$ = $1; $1.push($2); } - | JoinTable - { $$ = [$1]; } - | ApplyClause - { $$ = [$1]; } - ; - -JoinTable - : JoinMode JoinTableAs OnClause - { $$ = new yy.Join($1); yy.extend($$, $2); yy.extend($$, $3); } - ; - -JoinTableAs - : Table - { $$ = {table: $1}; } - | Table Literal - { $$ = {table: $1, as: $2 } ; } - | Table AS Literal - { $$ = {table: $1, as: $3 } ; } - | Json AS? Literal? - { $$ = {json:new yy.Json({value:$1,as:$3})}; } - | ParamValue Literal - { $$ = {param: $1, as: $2 } ; } - | ParamValue AS Literal - { $$ = {param: $1, as: $3 } ; } - | LPAR Select RPAR Literal - { $$ = {select: $2, as: $4} ; } - | LPAR Select RPAR AS Literal - { $$ = {select: $2, as: $5 } ; } - | FuncValue - { $$ = {func:$1, as:'default'}; } - | FuncValue Literal - { $$ = {func:$1, as: $2}; } - | FuncValue AS Literal - { $$ = {func:$1, as: $3}; } - - | VarValue - { $$ = {variable:$1,as:'default'}; } - | VarValue Literal - { $$ = {variable:$1,as:$2}; } - | VarValue AS Literal - { $$ = {variable:$1,as:$3} } - ; - -JoinMode - : JoinModeMode - { $$ = { joinmode: $1 } ; } - | NATURAL JoinModeMode - { $$ = {joinmode: $1, natural:true} ; } - ; - -JoinModeMode - : JOIN - { $$ = "INNER"; } - | INNER JOIN - { $$ = "INNER"; } - | LEFT JOIN - { $$ = "LEFT"; } - | LEFT OUTER JOIN - { $$ = "LEFT"; } - | RIGHT JOIN - { $$ = "RIGHT"; } - | RIGHT OUTER JOIN - { $$ = "RIGHT"; } - | OUTER JOIN - { $$ = "OUTER"; } - | FULL OUTER JOIN - { $$ = "OUTER"; } - | SEMI JOIN - { $$ = "SEMI"; } - | ANTI JOIN - { $$ = "ANTI"; } - | CROSS JOIN - { $$ = "CROSS"; } - ; - -OnClause - : ON Expression - { $$ = {on: $2}; } - | USING ColumnsList - { $$ = {using: $2}; } - | - { $$ = undefined; } - ; - -WhereClause - : { $$ = undefined; } - | WHERE Expression - { $$ = {where: new yy.Expression({expression:$2})}; } - ; - -GroupClause - : { $$ = undefined; } - | GROUP BY GroupExpressionsList HavingClause - { $$ = {group:$3}; yy.extend($$,$4); } - ; - -GroupExpressionsList - : GroupExpression - { $$ = [$1]; } - | GroupExpressionsList COMMA GroupExpression - { $$ = $1; $1.push($3); } - ; - -GroupExpression - : GROUPING SET LPAR GroupExpressionsList RPAR - { $$ = new yy.GroupExpression({type:'GROUPING SETS', group: $4}); } - | ROLLUP LPAR GroupExpressionsList RPAR - { $$ = new yy.GroupExpression({type:'ROLLUP', group: $3}); } - | CUBE LPAR GroupExpressionsList RPAR - { $$ = new yy.GroupExpression({type:'CUBE', group: $3}); } - | Expression - { $$ = $1; } - ; - - -HavingClause - : { $$ = undefined; } - | HAVING Expression - { $$ = {having:$2}} - ; - -UnionClause - : { $$ = undefined; } - | UNION Select - { $$ = {union: $2} ; } - | UNION ALL Select - { $$ = {unionall: $3} ; } - | EXCEPT Select - { $$ = {except: $2} ; } - | INTERSECT Select - { $$ = {intersect: $2} ; } - | UNION CORRESPONDING Select - { $$ = {union: $3, corresponding:true} ; } - | UNION ALL CORRESPONDING Select - { $$ = {unionall: $4, corresponding:true} ; } - | EXCEPT CORRESPONDING Select - { $$ = {except: $3, corresponding:true} ; } - | INTERSECT CORRESPONDING Select - { $$ = {intersect: $3, corresponding:true} ; } - ; - -OrderClause - : { $$ = undefined; } - | ORDER BY OrderExpressionsList - { $$ = {order:$3}} - ; - -OrderExpressionsList - : OrderExpression - { $$ = [$1]; } - | OrderExpressionsList COMMA OrderExpression - { $$ = $1; $1.push($3)} - ; - -NullsOrder - : NULLS FIRST - { $$ = {nullsOrder: 'FIRST'}; } - | NULLS LAST - { $$ = {nullsOrder: 'LAST'}; } - ; - -OrderExpression - : Expression - { $$ = new yy.Expression({expression: $1, direction:'ASC'}) } - | Expression DIRECTION - { $$ = new yy.Expression({expression: $1, direction:$2.toUpperCase()}) } - | Expression DIRECTION NullsOrder - { $$ = new yy.Expression({expression: $1, direction:$2.toUpperCase()}); yy.extend($$, $3) } - | Expression COLLATE NOCASE - { $$ = new yy.Expression({expression: $1, direction:'ASC', nocase:true}) } - | Expression COLLATE NOCASE DIRECTION - { $$ = new yy.Expression({expression: $1, direction:$4.toUpperCase(), nocase:true}) } - ; - -LimitClause - : { $$ = undefined; } - | LIMIT NumValue OffsetClause - { $$ = {limit:$2}; yy.extend($$, $3); } - | OFFSET NumValue ROWS? FETCH NEXT? NumValue ROWS? ONLY? - { $$ = {limit:$6,offset:$2}; } - ; - -OffsetClause - : { $$ = undefined; } - | OFFSET NumValue - { $$ = {offset:$2}; } - ; - - -ResultColumns - : ResultColumns COMMA ResultColumn - { $1.push($3); $$ = $1; } - | ResultColumn - { $$ = [$1]; } - ; - -ResultColumn - : Expression AS Literal - { $1.as = $3; $$ = $1;} - | Expression Literal - { $1.as = $2; $$ = $1;} - | Expression AS NUMBER - { $1.as = $3; $$ = $1;} - | Expression NUMBER - { $1.as = $2; $$ = $1;} - | Expression AS StringValue - { $1.as = $3; $$ = $1;} - | Expression StringValue - { $1.as = $2; $$ = $1;} - | Expression - { $$ = $1; } - ; - -Star - : Literal DOT Literal DOT STAR - { $$ = new yy.Column({columid: $5, tableid: $3, databaseid:$1}); } - | Literal DOT STAR - { $$ = new yy.Column({columnid: $3, tableid: $1}); } - | STAR - { $$ = new yy.Column({columnid:$1}); } - ; - -Column - : Literal DOT Literal DOT Literal - { $$ = new yy.Column({columnid: $5, tableid: $3, databaseid:$1});} - | Literal DOT Literal - { $$ = new yy.Column({columnid: $3, tableid: $1});} - | Literal DOT VALUE - { $$ = new yy.Column({columnid: $3, tableid: $1});} - | Literal - { $$ = new yy.Column({columnid: $1});} - ; - -Expression - : AggrValue - { $$ = $1; } - | FuncValue - { $$ = $1; } - | Op - { $$ = $1; } - | Column - { $$ = $1; } - | Star - { $$ = $1; } - | NumValue - { $$ = $1; } - | LogicValue - { $$ = $1; } - | StringValue - { $$ = $1; } - | NullValue - { $$ = $1; } - | ParamValue - { $$ = $1; } - | VarValue - { $$ = $1; } - | ExistsValue - { $$ = $1; } - | CaseValue - { $$ = $1; } - | CastClause - { $$ = $1; } - | VALUE - { $$ = new yy.DomainValueValue(); } - | Json - { $$ = new yy.Json({value:$1}); } - | ArrayValue - { $$ = $1; } -/* | ATLBRA JsonArray - { $$ = new yy.Json({value:$2}); } -*/ | NewClause - { $$ = $1; } -/* | AT LPAR Expression RPAR - { $$ = new yy.FuncValue({funcid: 'CLONEDEEP', args:[$3]}); } -*/ -/* | AT LPAR Json RPAR - { $$ = new yy.Json({value:$3}); } -*/ | LPAR Select RPAR - { - if(!yy.queries) yy.queries = []; - yy.queries.push($2); - $2.queriesidx = yy.queries.length; - $$ = $2; - } - | LPAR Insert RPAR - { - if(!yy.queries) yy.queries = []; - yy.queries.push($2); - $2.queriesidx = yy.queries.length; - $$ = $2; - } - | LPAR (CreateVertex|CreateEdge) RPAR - { - if(!yy.queries) yy.queries = []; - yy.queries.push($2); - $2.queriesidx = yy.queries.length; - $$ = $2; - } - - | JavaScript - {$$ = $1} - | CURRENT_TIMESTAMP - { $$ = new yy.FuncValue({funcid:'CURRENT_TIMESTAMP'});} - | CURRENT_DATE - { $$ = new yy.FuncValue({funcid:'CURRENT_DATE'});} -/* | USER - { $$ = new yy.FuncValue({funcid:'USER'});} -*/ ; - -JavaScript - : JAVASCRIPT - { $$ = new yy.JavaScript({value:$1.substr(2,$1.length-4)}); } - ; - -CreateFunction - : CREATE FUNCTION LITERAL AS JAVASCRIPT - { $$ = new yy.JavaScript({value:'alasql.fn["'+$3+'"] = '+$5.substr(2,$5.length-4)}); } - ; - -CreateAggregate - : CREATE AGGREGATE LITERAL AS JAVASCRIPT - { $$ = new yy.JavaScript({value:'alasql.aggr["'+$3+'"] = '+$5.substr(2,$5.length-4)}); } - ; - - -NewClause - : NEW Literal - { $$ = new yy.FuncValue({funcid:$2, newid:true}); } - | NEW FuncValue - { $$ = $2; yy.extend($$,{newid:true}); } - ; - - -CastClause - : CAST LPAR Expression AS ColumnType RPAR - { $$ = new yy.Convert({expression:$3}) ; yy.extend($$,$5) ; } - | CAST LPAR Expression AS ColumnType COMMA NUMBER RPAR - { $$ = new yy.Convert({expression:$3, style:$7}) ; yy.extend($$,$5) ; } - | CONVERT LPAR ColumnType COMMA Expression RPAR - { $$ = new yy.Convert({expression:$5}) ; yy.extend($$,$3) ; } - | CONVERT LPAR ColumnType COMMA Expression COMMA NUMBER RPAR - { $$ = new yy.Convert({expression:$5, style:$7}) ; yy.extend($$,$3) ; } - ; - -PrimitiveValue - : NumValue - { $$ = $1; } - | StringValue - { $$ = $1; } - | LogicValue - { $$ = $1; } - | NullValue - { $$ = $1; } - | ParamValue - { $$ = $1; } - | FuncValue - { $$ = $1; } - | CURRENT_TIMESTAMP - { $$ = new yy.FuncValue({funcid:'CURRENT_TIMESTAMP'}); } - | CURRENT_DATE - { $$ = new yy.FuncValue({funcid:'CURRENT_DATE'}); } -/* | USER - { $$ = new yy.FuncValue({funcid:'USER'}); } -*/ ; - - -AggrValue - : Aggregator LPAR ExprList RPAR OverClause - { - if($3.length > 1 && ($1.toUpperCase() == 'MAX' || $1.toUpperCase() == 'MIN')) { - $$ = new yy.FuncValue({funcid:$1,args:$3}); - } else { - $$ = new yy.AggrValue({aggregatorid: $1.toUpperCase(), expression: $3.pop(), over:$5}); - } - } - | Aggregator LPAR DISTINCT Expression RPAR OverClause - { $$ = new yy.AggrValue({aggregatorid: $1.toUpperCase(), expression: $4, distinct:true, over:$6}); } - | Aggregator LPAR ALL Expression RPAR OverClause - { $$ = new yy.AggrValue({aggregatorid: $1.toUpperCase(), expression: $4, - over:$6}); } - ; - -OverClause - : - {$$ = undefined; } - | OVER LPAR OverPartitionClause RPAR - { $$ = new yy.Over(); yy.extend($$,$3); } - | OVER LPAR OverOrderByClause RPAR - { $$ = new yy.Over(); yy.extend($$,$3); } - | OVER LPAR OverPartitionClause OverOrderByClause RPAR - { $$ = new yy.Over(); yy.extend($$,$3); yy.extend($$,$4);} - ; - -OverPartitionClause - : PARTITION BY GroupExpressionsList - { $$ = {partition:$3}; } - ; -OverOrderByClause - : ORDER BY OrderExpressionsList - { $$ = {order:$3}; } - ; -Aggregator - : SUM { $$ = "SUM"; } - | TOTAL { $$ = "TOTAL"; } - | COUNT { $$ = "COUNT"; } - | MIN { $$ = "MIN"; } - | MAX { $$ = "MAX"; } - | AVG { $$ = "AVG"; } - | FIRST { $$ = "FIRST"; } - | LAST { $$ = "LAST"; } - | AGGR { $$ = "AGGR"; } - | ARRAY { $$ = "ARRAY"; } -/* | REDUCE { $$ = "REDUCE"; } */ - ; - -FuncValue - : Literal LPAR (DISTINCT|ALL)? ExprList RPAR - { - var funcid = $1; - var exprlist = $4; - if(exprlist.length > 1 && (funcid.toUpperCase() == 'MIN' || funcid.toUpperCase() == 'MAX')) { - $$ = new yy.FuncValue({funcid: funcid, args: exprlist}); - } else if(alasql.aggr[$1]) { - $$ = new yy.AggrValue({aggregatorid: 'REDUCE', - funcid: funcid, expression: exprlist.pop(),distinct:($3=='DISTINCT') }); - } else { - $$ = new yy.FuncValue({funcid: funcid, args: exprlist}); - }; - } - | Literal LPAR RPAR - { $$ = new yy.FuncValue({ funcid: $1 }) } - | IF LPAR ExprList RPAR - { $$ = new yy.FuncValue({ funcid: 'IIF', args:$3 }) } - | REPLACE LPAR ExprList RPAR - { $$ = new yy.FuncValue({ funcid: 'REPLACE', args:$3 }) } - | CURRENT_DATE LPAR RPAR - { $$ = new yy.FuncValue({ funcid: $1 }) } - | DATEADD LPAR Literal COMMA Expression COMMA Expression RPAR - { $$ = new yy.FuncValue({ funcid: 'DATEADD', args:[new yy.StringValue({value:$3}),$5,$7]}) } - | DATEADD LPAR STRING COMMA Expression COMMA Expression RPAR - { $$ = new yy.FuncValue({ funcid: 'DATEADD', args:[$3,$5,$7]}) } - | DATEDIFF LPAR Literal COMMA Expression COMMA Expression RPAR - { $$ = new yy.FuncValue({ funcid: 'DATEDIFF', args:[new yy.StringValue({value:$3}),$5,$7]}) } - | DATEDIFF LPAR STRING COMMA Expression COMMA Expression RPAR - { $$ = new yy.FuncValue({ funcid: 'DATEDIFF', args:[$3,$5,$7]}) } - | TIMESTAMPDIFF LPAR Expression COMMA Expression COMMA Expression RPAR - { $$ = new yy.FuncValue({ funcid: 'TIMESTAMPDIFF', args:[new yy.StringValue({value:$3}),$5,$7]}) } - | INTERVAL Expression Literal - { $$ = new yy.FuncValue({ funcid: 'INTERVAL', args:[$2,new yy.StringValue({value:($3).toLowerCase()})]}); } - ; - -ExprList - : Expression - { $$ = [$1]; } - | ExprList COMMA Expression - { $1.push($3); $$ = $1 } - ; - -NumValue - : NUMBER - { $$ = new yy.NumValue({value:+$1}); } - ; - -LogicValue - : TRUE - { $$ = new yy.LogicValue({value:true}); } - | FALSE - { $$ = new yy.LogicValue({value:false}); } - ; - -StringValue - : STRING - { $$ = new yy.StringValue({value: $1.substr(1,$1.length-2).replace(/(\\\')/g,"'").replace(/(\'\')/g,"'")}); } - | NSTRING - { $$ = new yy.StringValue({value: $1.substr(2,$1.length-3).replace(/(\\\')/g,"'").replace(/(\'\')/g,"'")}); } - ; - -NullValue - : NULL - { $$ = new yy.NullValue({value:undefined}); } - ; - -VarValue - : AT Literal - { $$ = new yy.VarValue({variable:$2}); } - ; - -ExistsValue - : EXISTS LPAR Select RPAR - { - if(!yy.exists) yy.exists = []; - $$ = new yy.ExistsValue({value:$3, existsidx:yy.exists.length}); - yy.exists.push($3); - } - ; - -ArrayValue - : ARRAYLBRA ExprList RBRA - { $$ = new yy.ArrayValue({value:$2}); } - ; - -ParamValue - : DOLLAR (Literal|NUMBER) - { $$ = new yy.ParamValue({param: $2}); } -/* | DOLLAR NUMBER - { $$ = new yy.ParamValue({param: $2}); } -*/ | COLON Literal - { $$ = new yy.ParamValue({param: $2}); } - | QUESTION - { - if(typeof yy.question == 'undefined') yy.question = 0; - $$ = new yy.ParamValue({param: yy.question++}); - } - | BRAQUESTION - { - if(typeof yy.question == 'undefined') yy.question = 0; - $$ = new yy.ParamValue({param: yy.question++, array:true}); - } - ; - - -CaseValue - : CASE Expression WhensList ElseClause END - { $$ = new yy.CaseValue({expression:$2, whens: $3, elses: $4}); } - | CASE WhensList ElseClause END - { $$ = new yy.CaseValue({whens: $2, elses: $3}); } - ; - -WhensList - : WhensList When - { $$ = $1; $$.push($2); } - | When - { $$ = [$1]; } - ; - -When - : WHEN Expression THEN Expression - { $$ = {when: $2, then: $4 }; } - ; - -ElseClause - : ELSE Expression - { $$ = $2; } - | - { $$ = undefined; } - ; - -Op - : Expression REGEXP Expression - { $$ = new yy.Op({left:$1, op:'REGEXP', right:$3}); } - | Expression TILDA Expression - { $$ = new yy.Op({left:$1, op:'REGEXP', right:$3}); } - | Expression GLOB Expression - { $$ = new yy.Op({left:$1, op:'GLOB', right:$3}); } - | Expression LIKE Expression - { $$ = new yy.Op({left:$1, op:'LIKE', right:$3}); } - | Expression LIKE Expression ESCAPE Expression - { $$ = new yy.Op({left:$1, op:'LIKE', right:$3, escape:$5}); } - | Expression NOT_LIKE Expression - { $$ = new yy.Op({left:$1, op:'NOT LIKE', right:$3 }); } - | Expression NOT_LIKE Expression ESCAPE Expression - { $$ = new yy.Op({left:$1, op:'NOT LIKE', right:$3, escape:$5 }); } - | Expression BARBAR Expression - { $$ = new yy.Op({left:$1, op:'||', right:$3}); } - | Expression PLUS Expression - { $$ = new yy.Op({left:$1, op:'+', right:$3}); } - | Expression MINUS Expression - { $$ = new yy.Op({left:$1, op:'-', right:$3}); } - | Expression STAR Expression - { $$ = new yy.Op({left:$1, op:'*', right:$3}); } - | Expression SLASH Expression - { $$ = new yy.Op({left:$1, op:'/', right:$3}); } - | Expression MODULO Expression - { $$ = new yy.Op({left:$1, op:'%', right:$3}); } - | Expression CARET Expression - { $$ = new yy.Op({left:$1, op:'^', right:$3}); } - - | Expression GTGT Expression - { $$ = new yy.Op({left:$1, op:'>>', right:$3}); } - | Expression LTLT Expression - { $$ = new yy.Op({left:$1, op:'<<', right:$3}); } - | Expression AMPERSAND Expression - { $$ = new yy.Op({left:$1, op:'&', right:$3}); } - | Expression BAR Expression - { $$ = new yy.Op({left:$1, op:'|', right:$3}); } - - | Expression ArrowDot Literal - { $$ = new yy.Op({left:$1, op:'->' , right:$3}); } - | Expression ArrowDot NumValue - { $$ = new yy.Op({left:$1, op:'->' , right:$3}); } - | Expression ArrowDot LPAR Expression RPAR - { $$ = new yy.Op({left:$1, op:'->' , right:$4}); } - | Expression ArrowDot FuncValue - { $$ = new yy.Op({left:$1, op:'->' , right:$3}); } - - | Expression EXCLAMATION Literal - { $$ = new yy.Op({left:$1, op:'!' , right:$3}); } - | Expression EXCLAMATION NumValue - { $$ = new yy.Op({left:$1, op:'!' , right:$3}); } - | Expression EXCLAMATION LPAR Expression RPAR - { $$ = new yy.Op({left:$1, op:'!' , right:$4}); } - | Expression EXCLAMATION FuncValue - { $$ = new yy.Op({left:$1, op:'!' , right:$3}); } - - - - - | Expression GT Expression - { $$ = new yy.Op({left:$1, op:'>' , right:$3}); } - | Expression GE Expression - { $$ = new yy.Op({left:$1, op:'>=' , right:$3}); } - | Expression LT Expression - { $$ = new yy.Op({left:$1, op:'<' , right:$3}); } - | Expression LE Expression - { $$ = new yy.Op({left:$1, op:'<=' , right:$3}); } - | Expression EQ Expression - { $$ = new yy.Op({left:$1, op:'=' , right:$3}); } - | Expression EQEQ Expression - { $$ = new yy.Op({left:$1, op:'==' , right:$3}); } - | Expression EQEQEQ Expression - { $$ = new yy.Op({left:$1, op:'===' , right:$3}); } - | Expression NE Expression - { $$ = new yy.Op({left:$1, op:'!=' , right:$3}); } - | Expression NEEQEQ Expression - { $$ = new yy.Op({left:$1, op:'!==' , right:$3}); } - | Expression NEEQEQEQ Expression - { $$ = new yy.Op({left:$1, op:'!===' , right:$3}); } - - | Expression CondOp AllSome LPAR Select RPAR - { - if(!yy.queries) yy.queries = []; - $$ = new yy.Op({left:$1, op:$2 , allsome:$3, right:$5, queriesidx: yy.queries.length}); - yy.queries.push($5); - } - - | Expression CondOp AllSome LPAR ExprList RPAR - { - $$ = new yy.Op({left:$1, op:$2 , allsome:$3, right:$5}); - } - - | Expression AND Expression - { - if($1.op == 'BETWEEN1') { - - if($1.left.op == 'AND') { - $$ = new yy.Op({left:$1.left.left,op:'AND',right: - new yy.Op({left:$1.left.right, op:'BETWEEN', - right1:$1.right, right2:$3}) - }); - } else { - $$ = new yy.Op({left:$1.left, op:'BETWEEN', - right1:$1.right, right2:$3}); - } - - } else if($1.op == 'NOT BETWEEN1') { - if($1.left.op == 'AND') { - $$ = new yy.Op({left:$1.left.left,op:'AND',right: - new yy.Op({left:$1.left.right, op:'NOT BETWEEN', - right1:$1.right, right2:$3}) - }); - } else { - $$ = new yy.Op({left:$1.left, op:'NOT BETWEEN', - right1:$1.right, right2:$3}); - } - } else { - $$ = new yy.Op({left:$1, op:'AND', right:$3}); - } - - - } - | Expression OR Expression - { $$ = new yy.Op({left:$1, op:'OR' , right:$3}); } - | NOT Expression - { $$ = new yy.UniOp({op:'NOT' , right:$2}); } - | MINUS Expression - { $$ = new yy.UniOp({op:'-' , right:$2}); } - | PLUS Expression - { $$ = new yy.UniOp({op:'+' , right:$2}); } - | TILDA Expression - { $$ = new yy.UniOp({op:'~' , right:$2}); } - | SHARP Expression - { $$ = new yy.UniOp({op:'#' , right:$2}); } - | LPAR Expression RPAR - { $$ = new yy.UniOp({right: $2}); } - - | Expression IN LPAR Select RPAR - { - if(!yy.queries) yy.queries = []; - $$ = new yy.Op({left: $1, op:'IN', right:$4, queriesidx: yy.queries.length}); - yy.queries.push($4); - } - - | Expression NOT IN LPAR Select RPAR - { - if(!yy.queries) yy.queries = []; - $$ = new yy.Op({left: $1, op:'NOT IN', right:$5, queriesidx: yy.queries.length}); - yy.queries.push($5); - } - - | Expression IN LPAR ExprList RPAR - { $$ = new yy.Op({left: $1, op:'IN', right:$4}); } - - | Expression NOT IN LPAR ExprList RPAR - { $$ = new yy.Op({left: $1, op:'NOT IN', right:$5}); } - - | Expression IN LPAR RPAR - { $$ = new yy.Op({left: $1, op:'IN', right:[]}); } - - | Expression NOT IN LPAR RPAR - { $$ = new yy.Op({left: $1, op:'NOT IN', right:[]}); } - - | Expression IN ColFunc - { $$ = new yy.Op({left: $1, op:'IN', right:$3}); } - - | Expression NOT IN ColFunc - { $$ = new yy.Op({left: $1, op:'NOT IN', right:$4}); } - - | Expression IN VarValue - { $$ = new yy.Op({left: $1, op:'IN', right:$3}); } - - | Expression NOT IN VarValue - { $$ = new yy.Op({left: $1, op:'NOT IN', right:$4}); } - - /* - Hack - it impossimle to parse BETWEEN AND and AND expressions with grammar. - At least, I do not know how. - */ - | Expression BETWEEN Expression - { -/* var expr = $3; - if(expr.left && expr.left.op == 'AND') { - $$ = new yy.Op({left:new yy.Op({left:$1, op:'BETWEEN', right:expr.left}), op:'AND', right:expr.right }); - } else { -*/ - $$ = new yy.Op({left:$1, op:'BETWEEN1', right:$3 }); -// } - } - | Expression NOT_BETWEEN Expression - { -// var expr = $3; -// if(expr.left && expr.left.op == 'AND') { -// $$ = new yy.Op({left:new yy.Op({left:$1, op:'NOT BETWEEN', right:expr.left}), op:'AND', right:expr.right }); -// } else { - $$ = new yy.Op({left:$1, op:'NOT BETWEEN1', right:$3 }); -// } - } - | Expression IS Expression - { $$ = new yy.Op({op:'IS' , left:$1, right:$3}); } - | Expression NOT NULL - { - $$ = new yy.Op({ - op:'IS', - left:$1, - right: new yy.UniOp({ - op:'NOT', - right:new yy.NullValue({value:undefined}) - }) - }); - } - | Expression DOUBLECOLON ColumnType - { $$ = new yy.Convert({expression:$1}) ; yy.extend($$,$3) ; } - ; - -ColFunc - : Column - { $$ = $1;} - | FuncValue - { $$ = $1;} - | AT LPAR Expression RPAR - { $$ = $3;} - ; - -CondOp - : GT { $$ = $1; } - | GE { $$ = $1; } - | LT { $$ = $1; } - | LE { $$ = $1; } - | EQ { $$ = $1; } - | NE { $$ = $1; } - ; - -AllSome - : ALL - { $$ = 'ALL'; } - | SOME - { $$ = 'SOME'; } - | ANY - { $$ = 'ANY'; } - ; - -/* PART TWO */ - -/* UPDATE */ - -Update - : UPDATE Table SET SetColumnsList WHERE Expression - { $$ = new yy.Update({table:$2, columns:$4, where:$6}); } - | UPDATE Table SET SetColumnsList - { $$ = new yy.Update({table:$2, columns:$4}); } - ; - -SetColumnsList - : SetColumn - { $$ = [$1]; } - | SetColumnsList COMMA SetColumn - { $$ = $1; $1.push($3); } - ; - -SetColumn - : Column EQ Expression -/* TODO Replace columnid with column */ - { $$ = new yy.SetColumn({column:$1, expression:$3})} - | (AT|DOLLAR) Literal EQ Expression - { $$ = new yy.SetColumn({variable:$2, expression:$4, method:$1})} - ; - -/* DELETE */ - -Delete - : DELETE FROM Table WHERE Expression - { $$ = new yy.Delete({table:$3, where:$5});} - | DELETE FROM Table - { $$ = new yy.Delete({table:$3});} - ; - -/* INSERT */ - -Insert - : INSERT Into Table Values ValuesListsList - { $$ = new yy.Insert({into:$3, values: $5}); } - | INSERT Into Table ValuesListsList - { $$ = new yy.Insert({into:$3, values: $4}); } - | INSERT OR REPLACE Into Table Values ValuesListsList - { $$ = new yy.Insert({into:$5, values: $7, orreplace:true}); } - | INSERT OR REPLACE Into Table ValuesListsList - { $$ = new yy.Insert({into:$5, values: $6, orreplace:true}); } - | REPLACE Into Table Values ValuesListsList - { $$ = new yy.Insert({into:$3, values: $5, orreplace:true}); } - | REPLACE Into Table ValuesListsList - { $$ = new yy.Insert({into:$3, values: $4, orreplace:true}); } - | INSERT Into Table DEFAULT Values - { $$ = new yy.Insert({into:$3, "default": true}) ; } - | INSERT Into Table LPAR ColumnsList RPAR Values ValuesListsList - { $$ = new yy.Insert({into:$3, columns: $5, values: $8}); } - | INSERT Into Table LPAR ColumnsList RPAR ValuesListsList - { $$ = new yy.Insert({into:$3, columns: $5, values: $7}); } - | INSERT Into Table Select - { $$ = new yy.Insert({into:$3, select: $4}); } - | INSERT OR REPLACE Into Table Select - { $$ = new yy.Insert({into:$5, select: $6, orreplace:true}); } - | INSERT Into Table LPAR ColumnsList RPAR Select - { $$ = new yy.Insert({into:$3, columns: $5, select: $7}); } - ; - -Values - : VALUES - | VALUE - ; - -Into - : - | INTO - ; -/* -TableParamFunc - : Table - { $$ = $1; } - | ParamValue - { $$ = $1; } - | FuncValue - { $$ = $1; } - ; -*/ - -ValuesListsList - : LPAR ValuesList RPAR - { $$ = [$2]; } - | Json - { $$ = [$1]; } - | ParamValue - { $$ = [$1]; } - | ValuesListsList COMMA LPAR ValuesList RPAR - {$$ = $1; $1.push($4)} - | ValuesListsList COMMA Json - {$$ = $1; $1.push($3)} - | ValuesListsList COMMA ParamValue - {$$ = $1; $1.push($3)} - ; - -ValuesList - : Expression - { $$ = [$1]; } - | ValuesList COMMA Expression - {$$ = $1; $1.push($3)} - ; - -Value - : NumValue - | StringValue - | LogicValue - | NullValue - | DateValue - | ParamValue - ; - -ColumnsList - : Column - { $$ = [$1]; } - | ColumnsList COMMA Column - {$$ = $1; $1.push($3)} - ; - -/* CREATE TABLE */ - -CreateTable - : CREATE TemporaryClause TableClass IfNotExists Table LPAR CreateTableDefClause RPAR CreateTableOptionsClause - { - $$ = new yy.CreateTable({table:$5}); - yy.extend($$,$2); - yy.extend($$,$3); - yy.extend($$,$4); - yy.extend($$,$7); - yy.extend($$,$9); - } - | CREATE TemporaryClause TableClass IfNotExists Table - { - $$ = new yy.CreateTable({table:$5}); - yy.extend($$,$2); - yy.extend($$,$3); - yy.extend($$,$4); - } -; - -TableClass - : TABLE - { $$ = undefined; } - | CLASS - { $$ = {class:true}; } - ; - -CreateTableOptionsClause - : - | CreateTableOptions - ; - -CreateTableOptions - : CreateTableOptions CreateTableOption - | CreateTableOption - ; - -/* TODO: Remove this section */ -CreateTableOption - : DEFAULT - | LITERAL EQ Literal - | IDENTITY EQ NumValue - | COLLATE EQ Literal - ; - -TemporaryClause - : { $$ = undefined; } - | TEMP - { $$ = {temporary:true}; } - ; - -IfNotExists - : { $$ = undefined; } - | IF NOT EXISTS - { $$ = {ifnotexists: true}; } - ; - -CreateTableDefClause - : ColumnDefsList COMMA ConstraintsList - { $$ = {columns: $1, constraints: $3}; } - | ColumnDefsList - { $$ = {columns: $1}; } - | AS Select - { $$ = {as: $2} } - ; - -ConstraintsList - : Constraint - { $$ = [$1];} - | ConstraintsList COMMA Constraint - { $1.push($3); $$ = $1; } - ; - -Constraint - : ConstraintName PrimaryKey - { $2.constraintid = $1; $$ = $2; } - | ConstraintName ForeignKey - { $2.constraintid = $1; $$ = $2; } - | ConstraintName UniqueKey - { $2.constraintid = $1; $$ = $2; } - | ConstraintName IndexKey - { $2.constraintid = $1; $$ = $2; } - | ConstraintName Check - { $2.constraintid = $1; $$ = $2; } - ; - -ConstraintName - : { $$ = undefined; } - | CONSTRAINT Literal - { $$ = $2; } - ; - -Check - : CHECK LPAR Expression RPAR - { $$ = {type: 'CHECK', expression: $3}; } - ; - -PrimaryKey - : PRIMARY KEY Literal? LPAR ColsList RPAR - { $$ = {type: 'PRIMARY KEY', columns: $5, clustered:($3+'').toUpperCase()}; } - ; - -ForeignKey - : FOREIGN KEY LPAR ColsList RPAR REFERENCES Table ParColsList? - OnForeignKeyClause - { $$ = {type: 'FOREIGN KEY', columns: $4, fktable: $7, fkcolumns: $8}; } - ; - -ParColsList - : LPAR ColsList RPAR - { $$ = $2; } - ; - -OnForeignKeyClause - : - { $$ = undefined; } - | OnDeleteClause OnUpdateClause - { $$ = undefined; } - ; - -OnDeleteClause - : ON DELETE NO ACTION - {$$ = undefined; } - ; -OnUpdateClause - : ON UPDATE NO ACTION - {$$ = undefined; } - ; - -UniqueKey - : UNIQUE KEY? Literal? LPAR ColumnsList RPAR - { - $$ = {type: 'UNIQUE', columns: $5, clustered:($3+'').toUpperCase()}; - } - ; - -IndexKey - : INDEX Literal LPAR ColumnsList RPAR - | KEY Literal LPAR ColumnsList RPAR - ; -ColsList - : Literal - { $$ = [$1]; } - | STRING - { $$ = [$1]; } - | ColsList COMMA Literal - { $$ = $1; $1.push($3); } - | ColsList COMMA STRING - { $$ = $1; $1.push($3); } - ; - -/* -OrderedColsList - : Literal - { $$ = [$1]; } - | STRING - { $$ = [$1]; } - | OrderedColsList COMMA Literal - { $$ = $1; $1.push($3); } - | OrderedColsList COMMA STRING - { $$ = $1; $1.push($3); } - ; -*/ -ColumnDefsList - : ColumnDef - { $$ = [$1];} - | ColumnDefsList COMMA ColumnDef - { $1.push($3); $$ = $1; } - ; - -ColumnDef - : Literal ColumnType ColumnConstraintsClause - { $$ = new yy.ColumnDef({columnid:$1}); yy.extend($$,$2); yy.extend($$,$3);} - | Literal ColumnConstraints - { $$ = new yy.ColumnDef({columnid:$1}); yy.extend($$,$2); } - | Literal - { $$ = new yy.ColumnDef({columnid:$1, dbtypeid: ''}); } - ; - -/* -ColumnType - : LITERAL LPAR NumberMax COMMA NUMBER RPAR - { $$ = {dbtypeid: $1, dbsize: $3, dbprecision: +$5} } - | LITERAL LPAR NumberMax RPAR - { $$ = {dbtypeid: $1, dbsize: $3} } - | LITERAL - { $$ = {dbtypeid: $1} } - | ENUM LPAR ValuesList RPAR - { $$ = {dbtypeid: 'ENUM', enumvalues: $3} } - ; -*/ -SingularColumnType - : LiteralWithSpaces LPAR NumberMax COMMA NUMBER RPAR - { $$ = {dbtypeid: $1, dbsize: $3, dbprecision: +$5} } - | LiteralWithSpaces LPAR NumberMax RPAR - { $$ = {dbtypeid: $1, dbsize: $3} } - | LiteralWithSpaces - { $$ = {dbtypeid: $1} } - | ENUM LPAR ValuesList RPAR - { $$ = {dbtypeid: 'ENUM', enumvalues: $3} } - ; - -ColumnType - : SingularColumnType BRALITERAL /* text[] */ - { $$ = $1; $1.dbtypeid += '[' + $2 + ']'; } - | SingularColumnType - { $$ = $1; } - ; - - -NumberMax - : NUMBER - { $$ = +$1; } - | MAXNUM - { $$ = "MAX"; } - ; - -ColumnConstraintsClause - : {$$ = undefined} - | ColumnConstraintsList - { $$ = $1; } - ; - - -ColumnConstraintsList - : ColumnConstraintsList ColumnConstraint - { - yy.extend($1,$2); $$ = $1; - } - | ColumnConstraint - { $$ = $1; } - ; - -ParLiteral - : LPAR Literal RPAR - { $$ = $2; } - ; - -ColumnConstraint - : PRIMARY KEY - {$$ = {primarykey:true};} - | FOREIGN KEY REFERENCES Table ParLiteral? - {$$ = {foreignkey:{table:$4, columnid: $5}};} - | REFERENCES Table ParLiteral? - {$$ = {foreignkey:{table:$2, columnid: $3}};} - | IDENTITY LPAR NumValue COMMA NumValue RPAR - { $$ = {identity: {value:$3,step:$5}} } - | IDENTITY - { $$ = {identity: {value:1,step:1}} } - | DEFAULT PrimitiveValue - {$$ = {"default":$2};} - | DEFAULT LPAR Expression RPAR - {$$ = {"default":$3};} - | DEFAULT FuncValue - {$$ = {"default":$2};} - | NULL - {$$ = {null:true}; } - | NOT NULL - {$$ = {notnull:true}; } - | Check - {$$ = {check:$1}; } - | UNIQUE - {$$ = {unique:true}; } - | ON UPDATE PrimitiveValue - {$$ = {"onupdate":$3};} - | ON UPDATE LPAR Expression RPAR - {$$ = {"onupdate":$4};} - ; - -/* DROP TABLE */ - -DropTable - : DROP (TABLE|CLASS) IfExists TablesList - { $$ = new yy.DropTable({tables:$4,type:$2}); yy.extend($$, $3); } - ; - -TablesList - : TablesList COMMA Table - { $1.push($3); $$=$1; } - | Table - { $$ = [$1]; } - ; - - -IfExists - : { $$ = undefined; } - | IF EXISTS - { $$ = {ifexists: true};} - ; - -/* ALTER TABLE */ - -AlterTable - : ALTER TABLE Table RENAME TO Literal - { $$ = new yy.AlterTable({table:$3, renameto: $6});} - | ALTER TABLE Table ADD COLUMN ColumnDef - { $$ = new yy.AlterTable({table:$3, addcolumn: $6});} - | ALTER TABLE Table MODIFY COLUMN ColumnDef - { $$ = new yy.AlterTable({table:$3, modifycolumn: $6});} - | ALTER TABLE Table RENAME COLUMN Literal TO Literal - { $$ = new yy.AlterTable({table:$3, renamecolumn: $6, to: $8});} - | ALTER TABLE Table DROP COLUMN Literal - { $$ = new yy.AlterTable({table:$3, dropcolumn: $6});} - ; - -RenameTable - : RENAME TABLE Table TO Literal - { $$ = new yy.AlterTable({table:$3, renameto: $5});} - ; - -/* DATABASES */ - -AttachDatabase - : ATTACH Literal DATABASE Literal - { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase() });} - | ATTACH Literal DATABASE Literal LPAR ExprList RPAR - { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase(), args:$6 });} - | ATTACH Literal DATABASE Literal AS Literal - { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase(), as:$6 });} - | ATTACH Literal DATABASE Literal LPAR ExprList RPAR AS Literal - { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase(), as:$9, args:$6});} - ; - -DetachDatabase - : DETACH DATABASE Literal - { $$ = new yy.DetachDatabase({databaseid:$3});} - ; - -CreateDatabase - : CREATE DATABASE IfNotExists Literal - { $$ = new yy.CreateDatabase({databaseid:$4 }); yy.extend($$,$4); } - | CREATE Literal DATABASE IfNotExists Literal AsClause - { $$ = new yy.CreateDatabase({engineid:$2.toUpperCase(), databaseid:$5, as:$6 }); yy.extend($$,$4); } - | CREATE Literal DATABASE IfNotExists Literal LPAR ExprList RPAR AsClause - { $$ = new yy.CreateDatabase({engineid:$2.toUpperCase(), databaseid:$5, args:$7, as:$9 }); yy.extend($$,$4); } - | CREATE Literal DATABASE IfNotExists StringValue AsClause - { $$ = new yy.CreateDatabase({engineid:$2.toUpperCase(), - as:$6, args:[$5] }); yy.extend($$,$4); } - ; - -AsClause - : - {$$ = undefined;} - | AS Literal - { $$ = $1; } - ; - -UseDatabase - : USE DATABASE Literal - { $$ = new yy.UseDatabase({databaseid: $3 });} - | USE Literal - { $$ = new yy.UseDatabase({databaseid: $2 });} - ; - -DropDatabase - : DROP DATABASE IfExists Literal - { $$ = new yy.DropDatabase({databaseid: $4 }); yy.extend($$,$3); } - | DROP Literal DATABASE IfExists Literal - { $$ = new yy.DropDatabase({databaseid: $5, engineid:$2.toUpperCase() }); yy.extend($$,$4); } - | DROP Literal DATABASE IfExists StringValue - { $$ = new yy.DropDatabase({databaseid: $5, engineid:$2.toUpperCase() }); yy.extend($$,$4); } - ; - -/* INDEXES */ - -CreateIndex - : - CREATE INDEX Literal ON Table LPAR OrderExpressionsList RPAR - { $$ = new yy.CreateIndex({indexid:$3, table:$5, columns:$7})} - | - - CREATE UNIQUE INDEX Literal ON Table LPAR OrderExpressionsList RPAR - { $$ = new yy.CreateIndex({indexid:$4, table:$6, columns:$8, unique:true})} - ; - -DropIndex - : DROP INDEX Literal - { $$ = new yy.DropIndex({indexid:$3});} - ; - -/* SHOW COMMAND */ - -ShowDatabases - : SHOW DATABASE - { $$ = new yy.ShowDatabases();} - | SHOW DATABASE LIKE StringValue - { $$ = new yy.ShowDatabases({like:$4});} - | SHOW Literal DATABASE - { $$ = new yy.ShowDatabases({engineid:$2.toUpperCase() });} - | SHOW Literal DATABASE LIKE StringValue - { $$ = new yy.ShowDatabases({engineid:$2.toUpperCase() , like:$5});} - ; - -ShowTables - : SHOW TABLE - { $$ = new yy.ShowTables();} - | SHOW TABLE LIKE StringValue - { $$ = new yy.ShowTables({like:$4});} - | SHOW TABLE FROM Literal - { $$ = new yy.ShowTables({databaseid: $4});} - | SHOW TABLE FROM Literal LIKE StringValue - { $$ = new yy.ShowTables({like:$6, databaseid: $4});} - ; - -ShowColumns - : SHOW COLUMN FROM Table - { $$ = new yy.ShowColumns({table: $4});} - | SHOW COLUMN FROM Table FROM Literal - { $$ = new yy.ShowColumns({table: $4, databaseid:$6});} - ; - -ShowIndex - : SHOW INDEX FROM Table - { $$ = new yy.ShowIndex({table: $4});} - | SHOW INDEX FROM Table FROM Literal - { $$ = new yy.ShowIndex({table: $4, databaseid: $6});} - ; - -ShowCreateTable - : SHOW CREATE TABLE Table - { $$ = new yy.ShowCreateTable({table: $4});} - | SHOW CREATE TABLE Table FROM Literal - { $$ = new yy.ShowCreateTable({table: $4, databaseid:$6});} - ; - -CreateView - : CREATE TemporaryClause VIEW IfNotExists Table LPAR ColumnsList RPAR AS Select SubqueryRestriction? - { - $$ = new yy.CreateTable({table:$5,view:true,select:$10,viewcolumns:$7}); - yy.extend($$,$2); - yy.extend($$,$4); - } - | CREATE TemporaryClause VIEW IfNotExists Table AS Select SubqueryRestriction? - { - $$ = new yy.CreateTable({table:$5,view:true,select:$7}); - yy.extend($$,$2); - yy.extend($$,$4); - } - ; - -SubqueryRestriction - : WITH READ ONLY - | WITH CHECK OPTION - | WITH CHECK OPTION CONSTRAINT Constraint - ; - - -DropView - : DROP VIEW IfExists TablesList - { $$ = new yy.DropTable({tables:$4, view:true}); yy.extend($$, $3); } - ; -/* -DeclareCursor - : DECLARE Literal CURSOR FOR Select - { $$ = new yy.DeclareCursor({cursorid:$2, select:$5}); } - ; - -OpenCursor - : OPEN Literal - { $$ = new yy.OpenCursor({cursorid:$2}); } - ; - -CloseCursor - : CLOSE Literal - { $$ = new yy.CloseCursor({cursorid:$2}); } - ; - -FetchCursor - : FETCH FetchDirection FROM Literal - { $$ = new yy.FetchCursor({cursorid:$4}); yy.extend($$,$2); } - ; - -FetchDirection - : NEXT - { $$ = {direction: 'NEXT'}; } - | PRIOR - { $$ = {direction: 'PRIOR'}; } - | FIRST - { $$ = {direction: 'FIRST'}; } - | LAST - { $$ = {direction: 'LAST'}; } - | ABSOLUTE NumValue - { $$ = {direction: 'ABSOLUTE', num:$2}; } - | RELATIVE NumValue - { $$ = {direction: 'RELATIVE', num:$2}; } - ; -*/ - -/* -Help - : HELP StringValue - { $$ = new yy.Help({subject:$2.value.toUpperCase()} ) ; } - | HELP - { $$ = new yy.Help() ; } - ; -*/ - -ExpressionStatement - : EQ Expression - { $$ = new yy.ExpressionStatement({expression:$2}); } - ; - -Source - : SOURCE StringValue - { $$ = new yy.Source({url:$2.value}); } - ; - -Assert - : ASSERT Json - { $$ = new yy.Assert({value:$2}); } - | ASSERT PrimitiveValue - { $$ = new yy.Assert({value:$2.value}); } - | ASSERT STRING COMMA Json - { $$ = new yy.Assert({value:$4, message:$2}); } - ; - -Json - : AT LPAR Expression RPAR - { $$ = $3; } - | AT StringValue - { $$ = $2.value; } - | AT NumValue - { $$ = +$2.value; } - | AT LogicValue - { $$ = (!!$2.value); } - | AT ParamValue - { $$ = $2; } - | JsonObject - { $$ = $1; } - | AT JsonObject - { $$ = $2; } - | ATLBRA JsonArray - { $$ = $2; } - ; - -JsonValue - : Json - { $$ = $1; } - | JsonPrimitiveValue - { $$ = $1; } - ; - -JsonPrimitiveValue - : NumValue - { $$ = +$1.value; } - | StringValue - { $$ = ""+$1.value; } - | LogicValue - { $$ = $1.value; } - | Column - { $$ = $1; } - | NullValue - { $$ = $1.value; } - | ParamValue - { $$ = $1; } - | FuncValue - { $$ = $1; } - | LPAR Expression RPAR - { $$ = $2; } - ; - - -JsonObject - : LCUR JsonPropertiesList RCUR - { $$ = $2; } - | LCUR JsonPropertiesList COMMA RCUR - { $$ = $2; } - | LCUR RCUR - { $$ = {}; } - ; - -JsonArray - : JsonElementsList RBRA - { $$ = $1; } - | JsonElementsList COMMA RBRA - { $$ = $1; } - | RBRA - { $$ = []; } - ; - -JsonPropertiesList - : JsonPropertiesList COMMA JsonProperty - { yy.extend($1,$3); $$ = $1; } - | JsonProperty - { $$ = $1; } - ; - -JsonProperty - : STRING COLON JsonValue - { $$ = {}; $$[$1.substr(1,$1.length-2)] = $3; } - | NUMBER COLON JsonValue - { $$ = {}; $$[$1] = $3; } - | Literal COLON JsonValue - { $$ = {}; $$[$1] = $3; } -/* | STRING COLON ParamValue - { $$ = {}; $$[$1.substr(1,$1.length-2)] = $3; } - | NUMBER COLON ParamValue - { $$ = {}; $$[$1] = $3; } - | LITERAL COLON ParamValue - { $$ = {}; $$[$1] = $3; } -*/ ; - -JsonElementsList - : JsonElementsList COMMA JsonValue - { $1.push($3); $$ = $1; } - | JsonValue - { $$ = [$1]; } - ; - -SetVariable - : SET Literal EQ OnOff - { $$ = new yy.SetVariable({variable:$2.toLowerCase(), value:$4});} - | SET Literal OnOff - { $$ = new yy.SetVariable({variable:$2.toLowerCase(), value:$3});} - | SET Literal EQ Expression - { $$ = new yy.SetVariable({variable:$2, expression:$4});} - | SET Literal SetPropsList EQ Expression - { $$ = new yy.SetVariable({variable:$2, props: $3, expression:$5});} - | SET AtDollar Literal EQ Expression - { $$ = new yy.SetVariable({variable:$3, expression:$5, method:$2});} - | SET AtDollar Literal SetPropsList EQ Expression - { $$ = new yy.SetVariable({variable:$3, props: $4, expression:$6, method:$2});} - ; - -AtDollar - : AT - {$$ = '@'; } - | DOLLAR - {$$ = '$'; } - ; - -SetPropsList - : SetPropsList ArrowDot SetProp - { $1.push($3); $$ = $1; } - | ArrowDot SetProp - { $$ = [$2]; } - ; - -SetProp - : Literal - { $$ = $1; } - | NUMBER - { $$ = $1; } - | LPAR Expression RPAR - { $$ = $2; } - ; - -OnOff - : ON - { $$ = true; } - | OFF - { $$ = false; } - ; - -CommitTransaction - : COMMIT TRANSACTION - { $$ = new yy.CommitTransaction(); } - ; - -RollbackTransaction - : ROLLBACK TRANSACTION - { $$ = new yy.RollbackTransaction(); } - ; - -BeginTransaction - : BEGIN TRANSACTION - { $$ = new yy.BeginTransaction(); } - ; - -/* -Store - : STORE - { $$ = new yy.Store(); } - | STORE Literal - { $$ = new yy.Store({databaseid: $2}); } - ; - -Restore - : RESTORE - { $$ = new yy.Restore(); } - | RESTORE Literal - { $$ = new yy.Restore({databaseid: $2}); } - ; -*/ - -If - : -/* IF Expression AStatement - { $$ = new yy.If({expression:$2,thenstat:$3}); - if($3.exists) $$.exists = $3.exists; - if($3.queries) $$.queries = $3.queries; - } - | - -*/ - IF Expression AStatement ElseStatement - { $$ = new yy.If({expression:$2,thenstat:$3, elsestat:$4}); - if($3.exists) $$.exists = $3.exists; - if($3.queries) $$.queries = $3.queries; - } - - | IF Expression AStatement - { - $$ = new yy.If({expression:$2,thenstat:$3}); - if($3.exists) $$.exists = $3.exists; - if($3.queries) $$.queries = $3.queries; - } - ; - -ElseStatement - : ELSE AStatement - {$$ = $2;} - ; - -While - : WHILE Expression AStatement - { $$ = new yy.While({expression:$2,loopstat:$3}); - if($3.exists) $$.exists = $3.exists; - if($3.queries) $$.queries = $3.queries; - } - ; - -Continue - : CONTINUE - { $$ = new yy.Continue(); } - ; - -Break - : BREAK - { $$ = new yy.Break(); } - ; - -BeginEnd - : BEGIN Statements END - { $$ = new yy.BeginEnd({statements:$2}); } - ; - -Print - : PRINT ExprList - { $$ = new yy.Print({exprs:$2});} - | PRINT Select - { $$ = new yy.Print({select:$2});} - ; - -Require - : REQUIRE StringValuesList - { $$ = new yy.Require({paths:$2}); } - | REQUIRE PluginsList - { $$ = new yy.Require({plugins:$2}); } - ; - -/* For test plugin system */ - -Plugin - : ECHO {$$ = $1.toUpperCase(); } - | Literal {$$ = $1.toUpperCase(); } - ; - -Echo - : ECHO Expression - { $$ = new yy.Echo({expr:$2}); } - ; - - -StringValuesList - : StringValuesList COMMA StringValue - { $1.push($3); $$ = $1; } - | StringValue - { $$ = [$1]; } - ; - -PluginsList - : PluginsList COMMA Plugin - { $1.push($3); $$ = $1; } - | Plugin - { $$ = [$1]; } - ; - - -Declare - : DECLARE DeclaresList - { $$ = new yy.Declare({declares:$2}); } - ; - -DeclaresList - : DeclareItem - { $$ = [$1]; } - | DeclaresList COMMA DeclareItem - { $1.push($3); $$ = $1; } - ; - -DeclareItem - : AT Literal ColumnType - { $$ = {variable: $2}; yy.extend($$,$3); } - | AT Literal AS ColumnType - { $$ = {variable: $2}; yy.extend($$,$4); } - | AT Literal ColumnType EQ Expression - { $$ = {variable: $2, expression:$5}; yy.extend($$,$3);} - | AT Literal AS ColumnType EQ Expression - { $$ = {variable: $2, expression:$6}; yy.extend($$,$4);} - ; - -TruncateTable - : TRUNCATE TABLE Table - { $$ = new yy.TruncateTable({table:$3});} - ; - -Merge - : MERGE MergeInto MergeUsing MergeOn MergeMatchedList OutputClause - { - $$ = new yy.Merge(); yy.extend($$,$2); yy.extend($$,$3); - yy.extend($$,$4); - yy.extend($$,{matches:$5});yy.extend($$,$6); - } - ; - -MergeInto - : FromTable - { $$ = {into: $1}; } - | INTO FromTable - { $$ = {into: $2}; } - ; - -MergeUsing - : USING FromTable - { $$ = {using: $2}; } - ; - -MergeOn - : ON Expression - { $$ = {on:$2}; } - ; - -MergeMatchedList - : MergeMatchedList MergeMatched - { $$ = $1; $$.push($2); } - | MergeMatchedList MergeNotMatched - { $$ = $1; $$.push($2); } - | MergeMatched - { $$ = [$1]; } - | MergeNotMatched - { $$ = [$1]; } - ; - -MergeMatched - : WHEN MATCHED THEN MergeMatchedAction - { $$ = {matched:true, action:$4} } - | WHEN MATCHED AND Expression THEN MergeMatchedAction - { $$ = {matched:true, expr: $4, action:$6} } - ; - -MergeMatchedAction - : DELETE - { $$ = {delete:true}; } - | UPDATE SET SetColumnsList - { $$ = {update:$3}; } - ; - -MergeNotMatched - : WHEN NOT MATCHED THEN MergeNotMatchedAction - { $$ = {matched:false, bytarget: true, action:$5} } - | WHEN NOT MATCHED BY TARGET THEN MergeNotMatchedAction - { $$ = {matched:false, bytarget: true, action:$7} } - | WHEN NOT MATCHED AND Expression THEN MergeNotMatchedAction - { $$ = {matched:false, bytarget: true, expr:$5, action:$7} } - | WHEN NOT MATCHED BY TARGET AND Expression THEN MergeNotMatchedAction - { $$ = {matched:false, bytarget: true, expr:$7, action:$9} } - | WHEN NOT MATCHED BY SOURCE THEN MergeNotMatchedAction - { $$ = {matched:false, bysource: true, action:$7} } - | WHEN NOT MATCHED BY SOURCE AND Expression THEN MergeMatchedAction - { $$ = {matched:false, bysource: true, expr:$7, action:$9} } - ; - -MergeNotMatchedAction - : INSERT VALUES ValuesListsList - { $$ = {insert:true, values:$3}; } - | INSERT LPAR ColumnsList RPAR VALUES ValuesListsList - { $$ = {insert:true, values:$6, columns:$3}; } - | INSERT DEFAULT VALUES - { $$ = {insert:true, defaultvalues:true}; } - | INSERT LPAR ColumnsList RPAR DEFAULT VALUES - { $$ = {insert:true, defaultvalues:true, columns:$3}; } - ; - -OutputClause - : - | OUTPUT ResultColumns - { $$ = {output:{columns:$2}} } - | OUTPUT ResultColumns INTO AtDollar Literal - { $$ = {output:{columns:$2, intovar: $5, method:$4}} } - | OUTPUT ResultColumns INTO Table - { $$ = {output:{columns:$2, intotable: $4}} } - | OUTPUT ResultColumns INTO Table LPAR ColumnsList RPAR - { $$ = {output:{columns:$2, intotable: $4, intocolumns:$6}} } - ; - -/* -CreateVertex - : CREATE VERTEX - { $$ = new yy.CreateVertex(); } - | CREATE VERTEX SET SetColumnsList - { $$ = new yy.CreateVertex({set: $4}); } - | CREATE VERTEX Literal SET SetColumnsList - { $$ = new yy.CreateVertex({class:$3, set: $5}); } - | CREATE VERTEX CONTENT ExprList - { $$ = new yy.CreateVertex({content: $4}); } - | CREATE VERTEX Literal CONTENT ExprList - { $$ = new yy.CreateVertex({class:$3, content: $5}); } - | CREATE VERTEX Literal Select - { $$ = new yy.CreateVertex({class:$3, select:$4}); } - | CREATE VERTEX Select - { $$ = new yy.CreateVertex({select:$4}); } - ; -*/ -CreateVertex - : CREATE VERTEX Literal? SharpValue? StringValue? CreateVertexSet - { - $$ = new yy.CreateVertex({class:$3,sharp:$4, name:$5}); - yy.extend($$,$6); - } - ; - -SharpValue - : SHARP Literal - { $$ = $2; } - ; - -CreateVertexSet - : - {$$ = undefined; } - | SET SetColumnsList - { $$ = {sets:$2}; } - | CONTENT ExprList - { $$ = {content:$2}; } - | Select - { $$ = {select:$1}; } - ; - -CreateEdge - : CREATE EDGE StringValue? FROM Expression TO Expression CreateVertexSet - { - $$ = new yy.CreateEdge({from:$5,to:$7,name:$3}); - yy.extend($$,$8); - } -/* | CREATE EDGE StringValue? FROM Expression TO Expression - { - $$ = new yy.CreateEdge({from:$5,to:$7,name:$3}); - } -*/ ; - - -/* -CreateEdge - : CREATE EDGE Literal? - FROM Expression - TO Expression - (SET SetColumnsList | CONTENT Expression)? - - { - $$ = new yy.CreateEdge({class:$3, from:$5, to:$7}); - if(typeof $8 != 'undefined') { - $$.type = $8; - $$.expre = $9; - } - } - - ; -*/ - -CreateGraph - : CREATE GRAPH GraphList - { $$ = new yy.CreateGraph({graph:$3}); } - | CREATE GRAPH FROM Expression - { $$ = new yy.CreateGraph({from:$4}); } - ; - -GraphList - : GraphList COMMA GraphVertexEdge - { $$ = $1; $$.push($3); } - | GraphVertexEdge - { $$ = [$1]; } - ; - -GraphVertexEdge - : GraphElement Json? GraphAsClause? - { - $$ = $1; - if($2) $$.json = new yy.Json({value:$2}); - if($3) $$.as = $3; - } - | GraphElementVar GT GraphElement Json? GraphAsClause? GT GraphElementVar - { - $$ = {source:$1, target: $7}; - if($4) $$.json = new yy.Json({value:$4}); - if($5) $$.as = $5; - yy.extend($$,$3); - } - | GraphElementVar GT Json GraphAsClause? GT GraphElementVar - { - $$ = {source:$1, target: $6}; - if($4) $$.json = new yy.Json({value:$3}); - if($5) $$.as = $4; - } - | GraphElementVar GTGT GraphElementVar - { - $$ = {source:$1, target: $3}; - } - | Literal LPAR GraphList RPAR - ; - -GraphElementVar - : GraphElement { $$ = $1; } - | GraphVar { $$ = $1; } - ; - -GraphVar - : AtDollar Literal - { $$ = {vars:$2, method:$1}; } - ; - -GraphAsClause - : AS AtDollar Literal - { $$ = $3; } - ; - -GraphAtClause - : AtDollar Literal - { $$ = $2; } - ; - -GraphElement2 - : Literal? SharpLiteral? STRING? ColonLiteral? - { - var s3 = $3; - $$ = {prop:$1, sharp:$2, name:(typeof s3 == 'undefined')?undefined:s3.substr(1,s3.length-2), class:$4}; - } - ; - -GraphElement - : Literal SharpLiteral? STRING? ColonLiteral? - { - var s3 = $3; - $$ = {prop:$1, sharp:$2, name:(typeof s3 == 'undefined')?undefined:s3.substr(1,s3.length-2), class:$4}; - } - | SharpLiteral STRING? ColonLiteral? - { - var s2 = $2; - $$ = {sharp:$1, name:(typeof s2 == 'undefined')?undefined:s2.substr(1,s2.length-2), class:$3}; - } - | STRING ColonLiteral? - { - var s1 = $1; - $$ = {name:(typeof s1 == 'undefined')?undefined:s1.substr(1,s1.length-2), class:$2}; - } - | ColonLiteral - { - $$ = {class:$1}; - } - ; - - - -ColonLiteral - : COLON Literal - { $$ = $2; } - ; - -SharpLiteral - : SHARP Literal - { $$ = $2; } - | SHARP NUMBER - { $$ = +$2; } - ; - -DeleteVertex - : DELETE VERTEX Expression (WHERE Expression)? - ; - -DeleteEdge - : DELETE EDGE Expression (FROM Expression)? (TO Expression)? (WHERE Expression)? - ; - -AddRule - : Term COLONDASH TermsList - { $$ = new yy.AddRule({left:$1, right:$3}); } - | COLONDASH TermsList - { $$ = new yy.AddRule({right:$2}); } - ; - -TermsList - : TermsList COMMA Term - { $$ = $1; $$.push($3); } - | Term - { $$ = [$1]; } - ; - -Term - : Literal - { $$ = {termid: $1}; } - | Literal LPAR TermsList RPAR - { $$ = {termid:$1, args:$3}; } - ; -Query - : QUESTIONDASH FuncValue - ; - -Call - : CALL FuncValue - //{ $$ = $2; } - { $$ = new yy.ExpressionStatement({expression:$2}); } - ; - -CreateTrigger - : CREATE TRIGGER Literal BeforeAfter InsertDeleteUpdate ON Table AS? AStatement - { - $$ = new yy.CreateTrigger({trigger:$3, when:$4, action:$5, table:$7, statement:$9}); - if($9.exists) $$.exists = $9.exists; - if($9.queries) $$.queries = $9.queries; - } - | CREATE TRIGGER Literal BeforeAfter InsertDeleteUpdate ON Table Literal - { - $$ = new yy.CreateTrigger({trigger:$3, when:$4, action:$5, table:$7, funcid:$8}); - } - | CREATE TRIGGER Literal ON Table BeforeAfter InsertDeleteUpdate AS? AStatement - { - $$ = new yy.CreateTrigger({trigger:$3, when:$5, action:$6, table:$4, statement:$9}); - if($9.exists) $$.exists = $9.exists; - if($9.queries) $$.queries = $9.queries; - } - ; - -BeforeAfter - : - { $$ = 'AFTER'; } - | FOR - { $$ = 'AFTER'; } - | BEFORE - { $$ = 'BEFORE'; } - | AFTER - { $$ = 'AFTER'; } - | INSTEAD OF - { $$ = 'INSTEADOF'; } - ; - -InsertDeleteUpdate - : INSERT { $$ = 'INSERT'; } - | DELETE { $$ = 'DELETE'; } - | UPDATE { $$ = 'UPDATE'; } - ; - -DropTrigger - : DROP TRIGGER Literal - { $$ = new yy.DropTrigger({trigger:$3}); } - ; - -Reindex - : REINDEX Literal - { $$ = new yy.Reindex({indexid:$2});} - ; - -NonReserved - : A|ABSENT|ABSOLUTE|ACCORDING|ACTION|ADA|ADD|ADMIN|AFTER|ALWAYS|ASC|ASSERTION|ASSIGNMENT|ATTRIBUTE|ATTRIBUTES|BASE64|BEFORE|BERNOULLI|BLOCKED|BOM|BREADTH|C|CASCADE|CATALOG|CATALOG_NAME|CHAIN|CHARACTERISTICS|CHARACTERS|CHARACTER_SET_CATALOG|CHARACTER_SET_NAME|CHARACTER_SET_SCHEMA|CLASS_ORIGIN|COBOL|COLLATION|COLLATION_CATALOG|COLLATION_NAME|COLLATION_SCHEMA|COLUMNS|COLUMN_NAME|COMMAND_FUNCTION|COMMAND_FUNCTION_CODE|COMMITTED|CONDITION_NUMBER|CONNECTION|CONNECTION_NAME|CONSTRAINTS|CONSTRAINT_CATALOG|CONSTRAINT_NAME|CONSTRAINT_SCHEMA|CONSTRUCTOR|CONTENT|CONTINUE|CONTROL|CURSOR_NAME|DATA|DATETIME_INTERVAL_CODE|DATETIME_INTERVAL_PRECISION|DB|DEFAULTS|DEFERRABLE|DEFERRED|DEFINED|DEFINER|DEGREE|DEPTH|DERIVED|DESC|DESCRIPTOR|DIAGNOSTICS|DISPATCH|DOCUMENT|DOMAIN|DYNAMIC_FUNCTION|DYNAMIC_FUNCTION_CODE|EMPTY|ENCODING|ENFORCED|EXCLUDE|EXCLUDING|EXPRESSION|FILE|FINAL|FIRST|FLAG|FOLLOWING|FORTRAN|FOUND|FS|G|GENERAL|GENERATED|GO|GOTO|GRANTED|HEX|HIERARCHY|ID|IGNORE|IMMEDIATE|IMMEDIATELY|IMPLEMENTATION|INCLUDING|INCREMENT|INDENT|INITIALLY|INPUT|INSTANCE|INSTANTIABLE|INSTEAD|INTEGRITY|INVOKER|ISOLATION|K|KEY|KEY_MEMBER|KEY_TYPE|LAST|LENGTH|LEVEL|LIBRARY|LIMIT|LINK|LOCATION|LOCATOR|M|MAP|MAPPING|MATCHED|MAXVALUE|MESSAGE_LENGTH|MESSAGE_OCTET_LENGTH|MESSAGE_TEXT|MINVALUE|MORE|MUMPS|NAME|NAMES|NAMESPACE|NESTING|NEXT|NFC|NFD|NFKC|NFKD|NIL|NORMALIZED|NULLABLE|NULLS|NUMBER|OBJECT|OCTETS|OFF|OPTION|OPTIONS|ORDERING|ORDINALITY|OTHERS|OUTPUT|OVERRIDING|P|PAD|PARAMETER_MODE|PARAMETER_NAME|PARAMETER_ORDINAL_POSITION|PARAMETER_SPECIFIC_CATALOG|PARAMETER_SPECIFIC_NAME|PARAMETER_SPECIFIC_SCHEMA|PARTIAL|PASCAL|PASSING|PASSTHROUGH|PATH|PERMISSION|PLACING|PLI|PRECEDING|PRESERVE|PRIOR|PRIVILEGES|PUBLIC|READ|RECOVERY|RELATIVE|REPEATABLE|REQUIRING|RESPECT|RESTART|RESTORE|RESTRICT|RETURNED_CARDINALITY|RETURNED_LENGTH|RETURNED_OCTET_LENGTH|RETURNED_SQLSTATE|RETURNING|ROLE|ROUTINE|ROUTINE_CATALOG|ROUTINE_NAME|ROUTINE_SCHEMA|ROW_COUNT|SCALE|SCHEMA|SCHEMA_NAME|SCOPE_CATALOG|SCOPE_NAME|SCOPE_SCHEMA|SECTION|SECURITY|SELECTIVE|SELF|SEQUENCE|SERIALIZABLE|SERVER|SERVER_NAME|SESSION|SETS|SIMPLE|SIZE|SOURCE|SPACE|SPECIFIC_NAME|STANDALONE|STATE|STATEMENT|STRIP|STRUCTURE|STYLE|SUBCLASS_ORIGIN|T|TABLE_NAME|TEMPORARY|TIES|TOKEN|TOP_LEVEL_COUNT|TRANSACTION|TRANSACTIONS_COMMITTED|TRANSACTIONS_ROLLED_BACK|TRANSACTION_ACTIVE|TRANSFORM|TRANSFORMS|TRIGGER_CATALOG|TRIGGER_NAME|TRIGGER_SCHEMA|TYPE|UNBOUNDED|UNCOMMITTED|UNDER|UNLINK|UNNAMED|UNTYPED|URI|USAGE|USER_DEFINED_TYPE_CATALOG|USER_DEFINED_TYPE_CODE|USER_DEFINED_TYPE_NAME|USER_DEFINED_TYPE_SCHEMA|VALID|VERSION|VIEW|WHITESPACE|WORK|WRAPPER|WRITE|XMLDECLARATION|XMLSCHEMA|YES|ZONE; - -%% - -// from https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html -// JSON.stringify([].slice.call(document.querySelectorAll('tr')).filter(x => x.children.length == 5 && x.children[2].innerText == 'reserved').map(x => x.children[0].innerText)) - -var nonReserved = ["A","ABSENT","ABSOLUTE","ACCORDING","ACTION","ADA","ADD","ADMIN","AFTER","ALWAYS","ASC","ASSERTION","ASSIGNMENT","ATTRIBUTE","ATTRIBUTES","BASE64","BEFORE","BERNOULLI","BLOCKED","BOM","BREADTH","C","CASCADE","CATALOG","CATALOG_NAME","CHAIN","CHARACTERISTICS","CHARACTERS","CHARACTER_SET_CATALOG","CHARACTER_SET_NAME","CHARACTER_SET_SCHEMA","CLASS_ORIGIN","COBOL","COLLATION","COLLATION_CATALOG","COLLATION_NAME","COLLATION_SCHEMA","COLUMNS","COLUMN_NAME","COMMAND_FUNCTION","COMMAND_FUNCTION_CODE","COMMITTED","CONDITION_NUMBER","CONNECTION","CONNECTION_NAME","CONSTRAINTS","CONSTRAINT_CATALOG","CONSTRAINT_NAME","CONSTRAINT_SCHEMA","CONSTRUCTOR","CONTENT","CONTINUE","CONTROL","CURSOR_NAME","DATA","DATETIME_INTERVAL_CODE","DATETIME_INTERVAL_PRECISION","DB","DEFAULTS","DEFERRABLE","DEFERRED","DEFINED","DEFINER","DEGREE","DEPTH","DERIVED","DESC","DESCRIPTOR","DIAGNOSTICS","DISPATCH","DOCUMENT","DOMAIN","DYNAMIC_FUNCTION","DYNAMIC_FUNCTION_CODE","EMPTY","ENCODING","ENFORCED","EXCLUDE","EXCLUDING","EXPRESSION","FILE","FINAL","FIRST","FLAG","FOLLOWING","FORTRAN","FOUND","FS","G","GENERAL","GENERATED","GO","GOTO","GRANTED","HEX","HIERARCHY","ID","IGNORE","IMMEDIATE","IMMEDIATELY","IMPLEMENTATION","INCLUDING","INCREMENT","INDENT","INITIALLY","INPUT","INSTANCE","INSTANTIABLE","INSTEAD","INTEGRITY","INVOKER","ISOLATION","K","KEY","KEY_MEMBER","KEY_TYPE","LAST","LENGTH","LEVEL","LIBRARY","LIMIT","LINK","LOCATION","LOCATOR","M","MAP","MAPPING","MATCHED","MAXVALUE","MESSAGE_LENGTH","MESSAGE_OCTET_LENGTH","MESSAGE_TEXT","MINVALUE","MORE","MUMPS","NAME","NAMES","NAMESPACE","NESTING","NEXT","NFC","NFD","NFKC","NFKD","NIL","NORMALIZED","NULLABLE","NULLS","NUMBER","OBJECT","OCTETS","OFF","OPTION","OPTIONS","ORDERING","ORDINALITY","OTHERS","OUTPUT","OVERRIDING","P","PAD","PARAMETER_MODE","PARAMETER_NAME","PARAMETER_ORDINAL_POSITION","PARAMETER_SPECIFIC_CATALOG","PARAMETER_SPECIFIC_NAME","PARAMETER_SPECIFIC_SCHEMA","PARTIAL","PASCAL","PASSING","PASSTHROUGH","PATH","PERMISSION","PLACING","PLI","PRECEDING","PRESERVE","PRIOR","PRIVILEGES","PUBLIC","READ","RECOVERY","RELATIVE","REPEATABLE","REQUIRING","RESPECT","RESTART","RESTORE","RESTRICT","RETURNED_CARDINALITY","RETURNED_LENGTH","RETURNED_OCTET_LENGTH","RETURNED_SQLSTATE","RETURNING","ROLE","ROUTINE","ROUTINE_CATALOG","ROUTINE_NAME","ROUTINE_SCHEMA","ROW_COUNT","SCALE","SCHEMA","SCHEMA_NAME","SCOPE_CATALOG","SCOPE_NAME","SCOPE_SCHEMA","SECTION","SECURITY","SELECTIVE","SELF","SEQUENCE","SERIALIZABLE","SERVER","SERVER_NAME","SESSION","SETS","SIMPLE","SIZE","SOURCE","SPACE","SPECIFIC_NAME","STANDALONE","STATE","STATEMENT","STRIP","STRUCTURE","STYLE","SUBCLASS_ORIGIN","T","TABLE_NAME","TEMPORARY","TIES","TOKEN","TOP_LEVEL_COUNT","TRANSACTION","TRANSACTIONS_COMMITTED","TRANSACTIONS_ROLLED_BACK","TRANSACTION_ACTIVE","TRANSFORM","TRANSFORMS","TRIGGER_CATALOG","TRIGGER_NAME","TRIGGER_SCHEMA","TYPE","UNBOUNDED","UNCOMMITTED","UNDER","UNLINK","UNNAMED","UNTYPED","URI","USAGE","USER_DEFINED_TYPE_CATALOG","USER_DEFINED_TYPE_CODE","USER_DEFINED_TYPE_NAME","USER_DEFINED_TYPE_SCHEMA","VALID","VERSION","VIEW","WHITESPACE","WORK","WRAPPER","WRITE","XMLDECLARATION","XMLSCHEMA","YES","ZONE"] - -parser.parseError = function(str, hash) { - if (hash.expected && hash.expected.indexOf("'LITERAL'") > -1 && /[a-zA-Z_][a-zA-Z_0-9]*/.test(hash.token) && nonReserved.indexOf(hash.token) > -1) { - return - } - throw new SyntaxError(str) -} +/* +// +// alasqlparser.jison +// SQL Parser for AlaSQL +// (c) 2014-2015, Andrey Gershun +// +// +*/ + +%lex +%options case-insensitive + +%% + +/* +\$\$(.+?)\$\$ return 'DOLLARSTRING' +*/ + +\`\`([^\`])+\`\` return 'JAVASCRIPT' +\[\?\] return 'BRAQUESTION' +'@[' return 'ATLBRA' +'ARRAY[' return 'ARRAYLBRA' +\[([^\]'])*?\] return 'BRALITERAL' +\`([^\`'])*?\` return 'BRALITERAL' + +N(['](\\.|[^']|\\\')*?['])+ return 'NSTRING' +X(['](\\.|[^']|\\\')*?['])+ return 'NSTRING' +(['](\\.|[^']|\\\')*?['])+ return 'STRING' +(["](\\.|[^"]|\\\")*?["])+ return 'STRING' + + +"--"(.*?)($|\r\n|\r|\n) return /* its a COMMENT */ + +\s+ /* skip whitespace */ +'||' return 'BARBAR' +'|' return 'BAR' +/* '&&' return 'AMPERSANDAMPERSAND' */ + +VALUE\s+OF\s+SEARCH yytext = 'VALUE';return 'SEARCH' +VALUE\s+OF\s+SELECT yytext = 'VALUE';return 'SELECT' +ROW\s+OF\s+SELECT yytext = 'ROW';return 'SELECT' +COLUMN\s+OF\s+SELECT yytext = 'COLUMN';return 'SELECT' +MATRIX\s+OF\s+SELECT yytext = 'MATRIX';return 'SELECT' +INDEX\s+OF\s+SELECT yytext = 'INDEX';return 'SELECT' +RECORDSET\s+OF\s+SELECT yytext = 'RECORDSET';return 'SELECT' +TEXT\s+OF\s+SELECT yytext = 'TEXT';return 'SELECT' + +'SELECT' yytext = 'SELECT';return 'SELECT' + +'ABSOLUTE' return 'ABSOLUTE' +'ACTION' return 'ACTION' +'ADD' return 'ADD' +'AFTER' return 'AFTER' +'AGGR' return 'AGGR' +'AGGREGATE' return 'AGGREGATE' +'AGGREGATOR' return 'AGGREGATE' +'ALL' return 'ALL' +'ALTER' return 'ALTER' +'AND' return 'AND' +'ANTI' return 'ANTI' +'ANY' return 'ANY' +'APPLY' return 'APPLY' +'ARRAY' return 'ARRAY' +'AS' return 'AS' +'ASSERT' return 'ASSERT' +'ASC' return 'DIRECTION' +'ATTACH' return 'ATTACH' +AUTO(_)?INCREMENT return 'IDENTITY' +'AVG' return 'AVG' + +'BEFORE' return 'BEFORE' +'BEGIN' return 'BEGIN' +'BETWEEN' return 'BETWEEN' +'BREAK' return 'BREAK' +NOT\s+BETWEEN return 'NOT_BETWEEN' +NOT\s+LIKE return 'NOT_LIKE' +'BY' return 'BY' + +/* Postgres aliases */ +'~~*' return 'LIKE' +'!~~*' return 'NOT_LIKE' +'~~' return 'LIKE' +'!~~' return 'NOT_LIKE' +'ILIKE' return 'LIKE' +NOT\s+ILIKE return 'NOT_LIKE' + +'CALL' return 'CALL' +'CASE' return 'CASE' +'CAST' return 'CAST' +'CHECK' return 'CHECK' +'CLASS' return 'CLASS' +'CLOSE' return 'CLOSE' +'COLLATE' return 'COLLATE' +COLUMN return 'COLUMN' +COLUMNS return 'COLUMN' +"COMMIT" return 'COMMIT' +"CONSTRAINT" return 'CONSTRAINT' +"CONTENT" return 'CONTENT' +"CONTINUE" return 'CONTINUE' +"CONVERT" return 'CONVERT' +"CORRESPONDING" return 'CORRESPONDING' +"COUNT" return 'COUNT' +'CREATE' return 'CREATE' +"CROSS" return 'CROSS' +'CUBE' return 'CUBE' +"CURRENT_TIMESTAMP" return 'CURRENT_TIMESTAMP' +"CURRENT_DATE" return 'CURRENT_DATE' +"CURDATE" return 'CURRENT_DATE' +"CURSOR" return 'CURSOR' +DATABASE(S)? return 'DATABASE' +'DATEADD' return 'DATEADD' +'DATEDIFF' return 'DATEDIFF' +'TIMESTAMPDIFF' return 'TIMESTAMPDIFF' +'DECLARE' return 'DECLARE' +'DEFAULT' return 'DEFAULT' +'DELETE' return 'DELETE' +'DELETED' return 'DELETED' +'DESC' return 'DIRECTION' +'DETACH' return 'DETACH' +'DISTINCT' return 'DISTINCT' +/* DOUBLE\s+PRECISION return 'LITERAL' */ +'DROP' return 'DROP' +'ECHO' return 'ECHO' +'EDGE' return 'EDGE' +'END' return 'END' +'ENUM' return 'ENUM' +'ELSE' return 'ELSE' +'ESCAPE' return 'ESCAPE' +'EXCEPT' return 'EXCEPT' +'EXEC' return 'CALL' +'EXECUTE' return 'CALL' +'EXISTS' return 'EXISTS' +'EXPLAIN' return 'EXPLAIN' +'FALSE' return 'FALSE' +'FETCH' return 'FETCH' +'FIRST' return 'FIRST' +'FOR' return 'FOR' +'FOREIGN' return 'FOREIGN' +'FROM' return 'FROM' +'FULL' return 'FULL' +'FUNCTION' return 'FUNCTION' +'GLOB' return 'GLOB' +'GO' return 'GO' +'GRAPH' return 'GRAPH' +'GROUP' return 'GROUP' +'GROUPING' return 'GROUPING' +'HAVING' return 'HAVING' +/*'HELP' return 'HELP'*/ +'IF' return 'IF' +'IDENTITY' return 'IDENTITY' +'IS' return 'IS' +'IN' return 'IN' +'INDEX' return 'INDEX' +'INDEXED' return 'INDEXED' +'INNER' return 'INNER' +'INSTEAD' return 'INSTEAD' +'INSERT' return 'INSERT' +'INSERTED' return 'INSERTED' +'INTERSECT' return 'INTERSECT' +'INTERVAL' return 'INTERVAL' +'INTO' return 'INTO' +'JOIN' return 'JOIN' +'KEY' return 'KEY' +'LAST' return 'LAST' +'LET' return 'LET' +'LEFT' return 'LEFT' +'LIKE' return 'LIKE' +'LIMIT' return 'LIMIT' +'MATCHED' return 'MATCHED' +'MATRIX' return 'MATRIX' +'MAX'\s*/'(' return 'MAX' +'MAX'\s*/(','|')') return 'MAXNUM' +'MIN'\s*/'(' return 'MIN' +"MERGE" return 'MERGE' +"MINUS" return 'EXCEPT' +"MODIFY" return 'MODIFY' +'NATURAL' return 'NATURAL' +'NEXT' return 'NEXT' +'NEW' return 'NEW' +'NOCASE' return 'NOCASE' +'NO' return 'NO' +'NOT' return 'NOT' +'NULL' return 'NULL' +'NULLS' return 'NULLS' +'OFF' return 'OFF' +'ON' return 'ON' +'ONLY' return 'ONLY' +'OF' return 'OF' +'OFFSET' return 'OFFSET' +'OPEN' return 'OPEN' +'OPTION' return 'OPTION' +'OR' return 'OR' +'ORDER' return 'ORDER' +'OUTER' return 'OUTER' +'OVER' return 'OVER' +'PATH' return 'PATH' +'PARTITION' return 'PARTITION' +'PERCENT' return 'PERCENT' +'PIVOT' return 'PIVOT' +'PLAN' return 'PLAN' +'PRIMARY' return 'PRIMARY' +'PRINT' return 'PRINT' +'PRIOR' return 'PRIOR' +'QUERY' return 'QUERY' +'READ' return 'READ' +'RECORDSET' return 'RECORDSET' +'REDUCE' return 'REDUCE' +'REFERENCES' return 'REFERENCES' +'REGEXP' return 'REGEXP' +'REINDEX' return 'REINDEX' +'RELATIVE' return 'RELATIVE' +'REMOVE' return 'REMOVE' +'RENAME' return 'RENAME' +'REPEAT' return 'REPEAT' +'REPLACE' return 'REPLACE' +'REQUIRE' return 'REQUIRE' +'RESTORE' return 'RESTORE' +'RETURN' return 'RETURN' +'RETURNS' return 'RETURN' +'RIGHT' return 'RIGHT' +'ROLLBACK' return 'ROLLBACK' +'ROLLUP' return 'ROLLUP' +'ROW' return 'ROW' +'ROWS' return 'ROWS' +SCHEMA(S)? return 'DATABASE' +'SEARCH' return 'SEARCH' + +'SEMI' return 'SEMI' +SET return 'SET' +SETS return 'SET' +'SHOW' return 'SHOW' +'SOME' return 'SOME' +'SOURCE' return 'SOURCE' +'STRATEGY' return 'STRATEGY' +'STORE' return 'STORE' +'SUM' return 'SUM' +'TOTAL' return 'TOTAL' +'TABLE' return 'TABLE' +'TABLES' return 'TABLE' +'TARGET' return 'TARGET' +'TEMP' return 'TEMP' +'TEMPORARY' return 'TEMP' +'TEXTSTRING' return 'TEXTSTRING' +'THEN' return 'THEN' +'TIMEOUT' return 'TIMEOUT' +'TO' return 'TO' +'TOP' return 'TOP' +'TRAN' return 'TRANSACTION' +'TRANSACTION' return 'TRANSACTION' +'TRIGGER' return 'TRIGGER' +'TRUE' return 'TRUE' +'TRUNCATE' return 'TRUNCATE' +'UNION' return 'UNION' +'UNIQUE' return 'UNIQUE' +'UNPIVOT' return 'UNPIVOT' +'UPDATE' return 'UPDATE' +'USE' return 'USE' +/* 'USER' return 'USER' */ +'USING' return 'USING' +'VALUE' return 'VALUE' +'VALUES' return 'VALUES' +'VERTEX' return 'VERTEX' +'VIEW' return 'VIEW' +'WHEN' return 'WHEN' +'WHERE' return 'WHERE' +'WHILE' return 'WHILE' +'WITH' return 'WITH' +'WORK' return 'TRANSACTION' /* Is this keyword required? */ +(\d+\.?\d*|\.\d+)([eE][+-]?\d+)? return 'NUMBER' +'->' return 'ARROW' +'#' return 'SHARP' +'+' return 'PLUS' +'-' return 'MINUS' +'*' return 'STAR' +'/' return 'SLASH' +'%' return 'MODULO' +'!===' return 'NEEQEQEQ' +'===' return 'EQEQEQ' +'!==' return 'NEEQEQ' +'==' return 'EQEQ' +'>=' return 'GE' +'&' return 'AMPERSAND' +'|' return 'BAR' +'<<' return 'LTLT' +'>>' return 'GTGT' +'>' return 'GT' +'<=' return 'LE' +'<>' return 'NE' +'<' return 'LT' +'=' return 'EQ' +'!=' return 'NE' +'(' return 'LPAR' +')' return 'RPAR' +'@' return 'AT' +'{' return 'LCUR' +'}' return 'RCUR' + +']' return 'RBRA' + +':-' return 'COLONDASH' +'?-' return 'QUESTIONDASH' +'..' return 'DOTDOT' +'.' return 'DOT' +',' return 'COMMA' +'::' return 'DOUBLECOLON' +':' return 'COLON' +';' return 'SEMICOLON' +'$' return 'DOLLAR' +'?' return 'QUESTION' +'!' return 'EXCLAMATION' +'^' return 'CARET' + +'~' return 'TILDA' + +[0-9]*[a-zA-Z_]+[a-zA-Z_0-9]* return 'LITERAL' +<> return 'EOF' +. return 'INVALID' + +/lex +%left COMMA +%left DOUBLECOLON +%left OR +/* %left AND */ +%left AND BETWEEN NOT_BETWEEN +/*%left AND*/ +%left IN +%left NOT +%left GT GE LT LE EQ NE EQEQ NEEQEQ EQEQEQ NEEQEQEQ +%left IS +%left LIKE NOT_LIKE REGEXP GLOB +%left GTGT LTLT AMPERSAND BAR +%left PLUS MINUS +%left STAR SLASH MODULO +%left CARET +%left DOT ARROW EXCLAMATION +%left TILDA +%left SHARP +%left BARBAR + +%ebnf +%start main +%% + +Literal + : LITERAL + { + if (alasql.options.casesensitive) $$ = $1; + else $$ = $1.toLowerCase(); + } + | BRALITERAL + { $$ = doubleq($1.substr(1,$1.length-2)); } + | error NonReserved + { $$ = $2.toLowerCase() } + ; + +LiteralWithSpaces + : LITERAL + { $$ = $1 } + | LiteralWithSpaces LITERAL + { $$ = $2 ? $1 + ' ' + $2 : $1 } + ; + +main + : Statements EOF + { return new yy.Statements({statements:$1}); } + ; + +Statements + : Statements (SEMICOLON|GO) AStatement + { $$ = $1; if($3) $1.push($3); } + | AStatement + { $$ = [$1]; } + | ExplainStatement + { $$ = [$1]; } + ; + +ExplainStatement + : EXPLAIN AStatement + { $$ = $2; $2.explain = true; } + | EXPLAIN QUERY PLAN AStatement + { $$ = $4; $4.explain = true;} + ; + +AStatement + : Statement + { + $$ = $1; + + // TODO combine exists and queries + if(yy.exists) $$.exists = yy.exists; + delete yy.exists; + if(yy.queries) $$.queries = yy.queries; + delete yy.queries; + } + ; + +Statement + : { $$ = undefined; } + | AlterTable + | AttachDatabase + | Call + | CreateDatabase + | CreateIndex + | CreateGraph + | CreateTable + | CreateView + | CreateEdge + | CreateVertex + | Declare + | Delete + | DetachDatabase + | DropDatabase + | DropIndex + | DropTable + | DropView + | If + | Insert + | Merge + | Reindex + | RenameTable + | Select + | ShowCreateTable + | ShowColumns + | ShowDatabases + | ShowIndex + | ShowTables + | TruncateTable + | WithSelect + + | CreateTrigger + | DropTrigger + + | BeginTransaction + | CommitTransaction + | RollbackTransaction + | EndTransaction + | UseDatabase + | Update + /*| Help*/ + | JavaScript + + | Source + | Assert + | While + | Continue + | Break + | BeginEnd + | Print + | Require + | SetVariable + | ExpressionStatement + | AddRule + | Query + +/* PLugins */ + + | Echo + +/* + | Store + | Restore + + | DeclareCursor + | OpenCursor + | FetchCursor + | CloseCursor + + | SavePoint + | StoreDatabase + | StoreTable + | RestoreDatabase + | RestoreTable + + | While + | BulkInsert + + | CreateFunction + | CreateProcedure + | Loop + | ForLoop +*/ + + | CreateFunction + | CreateAggregate + ; + +/* WITH */ + +WithSelect + : WITH WithTablesList Select + { $$ = new yy.WithSelect({withs: $2, select:$3}); } + ; + +WithTablesList + : WithTablesList COMMA WithTable + { $1.push($3); $$=$1; } + | WithTable + { $$ = [$1]; } + ; + +WithTable + : Literal AS LPAR Select RPAR + { $$ = {name:$1, select:$4}; } + ; + +/* SELECT */ + +Select + : SelectClause RemoveClause? IntoClause FromClause PivotClause? WhereClause GroupClause OrderClause LimitClause UnionClause + { + yy.extend($$,$1); yy.extend($$,$2); yy.extend($$,$3); yy.extend($$,$4); + yy.extend($$,$5); yy.extend($$,$6);yy.extend($$,$7); + yy.extend($$,$8); yy.extend($$,$9); yy.extend($$,$10); + $$ = $1; + if(yy.exists) $$.exists = yy.exists.slice(); +/* if(yy.queries) $$.queries = yy.queries; + delete yy.queries; +*/ } + | SEARCH SearchSelector* IntoClause SearchFrom? + /* SearchLimit? SearchStrategy? SearchTimeout? */ + { + $$ = new yy.Search({selectors:$2, from:$4}); + yy.extend($$,$3); + } + ; + +PivotClause + : PIVOT LPAR Expression FOR Literal PivotClause2? RPAR AsLiteral? + { $$ = {pivot:{expr:$3, columnid:$5, inlist:$6, as:$8}}; } + | UNPIVOT LPAR Literal FOR Literal IN LPAR ColumnsList RPAR RPAR AsLiteral? + { $$ = {unpivot:{tocolumnid:$3, forcolumnid:$5, inlist:$8, as:$11}}; } + ; + +PivotClause2 + : IN LPAR AsList RPAR + { $$ = $3; } + ; + +AsLiteral + : AS Literal + { $$ = $2; } + | Literal + { $$ = $1; } + ; + +AsList + : AsList COMMA AsPart + { $$ = $1; $$.push($3); } + | AsPart + { $$ = [$1]; } + ; + +AsPart + : Expression + { $$ = {expr:$1}; } + | Expression AS Literal + { $$ = {expr:$1,as:$3}; } + ; + +RemoveClause + : REMOVE COLUMN? RemoveColumnsList + { $$ = {removecolumns:$3}; } + ; + +RemoveColumnsList + : RemoveColumnsList COMMA RemoveColumn + { $$ = $1; $$.push($3); } + | RemoveColumn + { $$ = [$1]; } + ; + +RemoveColumn + : Column + { $$ = $1; } + | LIKE StringValue + { $$ = {like:$2}; } + ; + +ArrowDot + : ARROW + | DOT + ; + +SearchSelector + : Literal + { $$ = {srchid:"PROP", args: [$1]}; } + + | ORDER BY LPAR OrderExpressionsList RPAR + { $$ = {srchid:"ORDERBY", args: $4}; } + | ORDER BY LPAR DIRECTION? RPAR + { + var dir = $4; + if(!dir) dir = 'ASC'; + $$ = {srchid:"ORDERBY", args: [{expression: new yy.Column({columnid:'_'}), direction:dir}]}; + } + + | DOTDOT + { $$ = {srchid:"PARENT"}; } + | ArrowDot Literal + { $$ = {srchid:"APROP", args: [$2]}; } + | CARET + { $$ = {selid:"ROOT"};} + | EQ Expression + { $$ = {srchid:"EQ", args: [$2]}; } + | LIKE Expression + { $$ = {srchid:"LIKE", args: [$2]}; } + | LPAR SearchSelector+ RPAR + { $$ = {selid:"WITH", args: $2}; } + | WITH LPAR SearchSelector+ RPAR + { $$ = {selid:"WITH", args: $3}; } + | Literal LPAR ExprList? RPAR + { $$ = {srchid:$1.toUpperCase(), args:$3}; } + | WHERE LPAR Expression RPAR + { $$ = {srchid:"WHERE", args:[$3]}; } + | OF LPAR Expression RPAR + { $$ = {selid:"OF", args:[$3]}; } + | CLASS LPAR Literal RPAR + { $$ = {srchid:"CLASS", args:[$3]}; } + | NUMBER + { $$ = {srchid:"PROP", args: [$1]}; } + | STRING + { $$ = {srchid:"NAME", args: [$1.substr(1,$1.length-2)]}; } + | SLASH + { $$ = {srchid:"CHILD"}; } + | VERTEX + { $$ = {srchid:"VERTEX"}; } + | EDGE + { $$ = {srchid:"EDGE"}; } + | EXCLAMATION + { $$ = {srchid:"REF"}; } + | SHARP Literal + { $$ = {srchid:"SHARP", args:[$2]}; } + | MODULO Literal + { $$ = {srchid:"ATTR", args:((typeof $2 == 'undefined')?undefined:[$2])}; } + | MODULO SLASH + { $$ = {srchid:"ATTR"}; } + | GT + { $$ = {srchid:"OUT"}; } + | LT + { $$ = {srchid:"IN"}; } + | GTGT + { $$ = {srchid:"OUTOUT"}; } + | LTLT + { $$ = {srchid:"ININ"}; } + | DOLLAR + { $$ = {srchid:"CONTENT"}; } /* TODO Decide! */ +/* | DELETE LPAR RPAR + { $$ = {srchid:"DELETE"}; } +*/ | Json + { $$ = {srchid:"EX",args:[new yy.Json({value:$1})]}; } + | AT Literal + { $$ = {srchid:"AT", args:[$2]}; } + | AS AT Literal + { $$ = {srchid:"AS", args:[$3]}; } + | SET LPAR SetColumnsList RPAR + { $$ = {srchid:"SET", args:$3}; } + + | TO AT Literal + { $$ = {selid:"TO", args:[$3]}; } + | VALUE + { $$ = {srchid:"VALUE"}; } + | ROW LPAR ExprList RPAR + { $$ = {srchid:"ROW", args:$3}; } + | COLON Literal + { $$ = {srchid:"CLASS", args:[$2]}; } + | SearchSelector PlusStar + { $$ = {selid:$2,args:[$1] }; } + + | NOT LPAR SearchSelector* RPAR + { $$ = {selid:"NOT",args:$3 }; } + | IF LPAR SearchSelector* RPAR + { $$ = {selid:"IF",args:$3 }; } + | Aggregator LPAR SearchSelector* RPAR + { $$ = {selid:$1,args:$3 }; } + | (DISTINCT|UNIQUE) LPAR SearchSelector* RPAR + { $$ = {selid:'DISTINCT',args:$3 }; } + | UNION LPAR SearchSelectorList RPAR + { $$ = {selid:'UNION',args:$3 }; } + | UNION ALL LPAR SearchSelectorList RPAR + { $$ = {selid:'UNIONALL',args:$4 }; } + | ALL LPAR SearchSelector* RPAR + { $$ = {selid:'ALL',args:[$3] }; } + | ANY LPAR SearchSelector* RPAR + { $$ = {selid:'ANY',args:[$3] }; } + | INTERSECT LPAR SearchSelectorList RPAR + { $$ = {selid:'INTERSECT',args:$3 }; } + | EXCEPT LPAR SearchSelectorList RPAR + { $$ = {selid:'EXCEPT',args:$3 }; } + | AND LPAR SearchSelectorList RPAR + { $$ = {selid:'AND',args:$3 }; } + | OR LPAR SearchSelectorList RPAR + { $$ = {selid:'OR',args:$3 }; } + | PATH LPAR SearchSelector RPAR + { $$ = {selid:'PATH',args:[$3] }; } + | RETURN LPAR ResultColumns RPAR + { $$ = {srchid:'RETURN',args:$3 }; } + | REPEAT LPAR SearchSelector* COMMA ExprList RPAR + { $$ = {selid:'REPEAT',sels:$3, args:$5 }; } + ; + +SearchSelectorList + : SearchSelectorList COMMA SearchSelector* + { $$ = $1; $$.push($3);} + | SearchSelector* + { $$ = [$1]; } + ; + +PlusStar + : PLUS + { $$ = "PLUS"; } + | STAR + { $$ = "STAR"; } + | QUESTION + { $$ = "QUESTION"; } + ; + +SearchFrom + : FROM Expression + { $$ = $2; } + ; + +/* +SearchLet + : LET + ; + +SearchWhile + : WHILE Expression + ; +SearchLimit + : LIMIT Expression + ; + +SearchStrategy + : STRATEGY Literal + ; + +SearchTimeout + : TIMEOUT Expression + ; + +*/ + +SelectClause + : + /* + + { $$ = new yy.Select({ columns:new yy.Column({columnid:'_'}), modifier: 'COLUMN' }); } + | +*/ + + SelectModifier DISTINCT TopClause ResultColumns + { $$ = new yy.Select({ columns:$4, distinct: true }); yy.extend($$, $1); yy.extend($$, $3); } + | SelectModifier UNIQUE TopClause ResultColumns + { $$ = new yy.Select({ columns:$4, distinct: true }); yy.extend($$, $1);yy.extend($$, $3); } + | SelectModifier ALL TopClause ResultColumns + { $$ = new yy.Select({ columns:$4, all:true }); yy.extend($$, $1);yy.extend($$, $3); } + | SelectModifier TopClause ResultColumns? + { + if(!$3) { + $$ = new yy.Select({columns:[new yy.Column({columnid:'_',})], modifier:'COLUMN'}); + } else { + $$ = new yy.Select({ columns:$3 }); yy.extend($$, $1);yy.extend($$, $2); + } + } +/* | + { $$ = new yy.Select({columns:[new yy.Column({columnid:'_', modifier:'COLUMN'})]});} +*/ ; + +SelectModifier + : SELECT + { if($1=='SELECT') $$ = undefined; else $$ = {modifier: $1}; } + | SELECT VALUE + { $$ = {modifier:'VALUE'}} + | SELECT ROW + { $$ = {modifier:'ROW'}} + | SELECT COLUMN + { $$ = {modifier:'COLUMN'}} + | SELECT MATRIX + { $$ = {modifier:'MATRIX'}} + | SELECT TEXTSTRING + { $$ = {modifier:'TEXTSTRING'}} + | SELECT INDEX + { $$ = {modifier:'INDEX'}} + | SELECT RECORDSET + { $$ = {modifier:'RECORDSET'}} + ; + +TopClause + : TOP NumValue PERCENT? + { $$ = {top: $2, percent:(typeof $3 != 'undefined'?true:undefined)}; } + | TOP LPAR NumValue RPAR + { $$ = {top: $3}; } + | { $$ = undefined; } + ; + +IntoClause + : {$$ = undefined; } + | INTO Table + {$$ = {into: $2} } + | INTO FuncValue + {$$ = {into: $2} } + | INTO ParamValue + {$$ = {into: $2} } + | INTO VarValue + {$$ = {into: $2} } + | INTO STRING + { + var s = $2; + s = s.substr(1,s.length-2); + var x3 = s.substr(-3).toUpperCase(); + var x4 = s.substr(-4).toUpperCase(); + if(s[0] == '#') { + $$ = {into: new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; + } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { + $$ = {into: new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; + } else if(x4=='XLSX' || x4 == 'JSON') { + $$ = {into: new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; + } + } + ; + +FromClause + : FROM FromTablesList + { $$ = { from: $2 }; } +/* | FROM FromTable JoinTablesList + { $$ = { from: [$2], joins: $3 }; } +*/ | FROM FromTablesList JoinTablesList + { $$ = { from: $2, joins: $3 }; } +/* | FROM LPAR FromTable JoinTablesList RPAR + { $$ = { from: [$3], joins: $4 }; } +*/ | FROM LPAR FromTablesList JoinTablesList RPAR + { $$ = { from: $3, joins: $4 }; } + | + { $$ = undefined; } + ; + +ApplyClause + : CROSS APPLY LPAR Select RPAR Literal + { $$ = new yy.Apply({select: $4, applymode:'CROSS', as:$6}); } + | CROSS APPLY LPAR Select RPAR AS Literal + { $$ = new yy.Apply({select: $4, applymode:'CROSS', as:$7}); } +/* { + if(!yy.exists) yy.exists = []; + $$ = new yy.Apply({select: $4, applymode:'CROSS', as:$7,existsidx:yy.exists.length}); + yy.exists.push($3); + + } +*/ | OUTER APPLY LPAR Select RPAR Literal + { $$ = new yy.Apply({select: $4, applymode:'OUTER', as:$6}); } + | OUTER APPLY LPAR Select RPAR AS Literal + { $$ = new yy.Apply({select: $4, applymode:'OUTER', as:$7}); } + ; + +FromTablesList + : FromTable + { $$ = [$1]; } + | FromTablesList COMMA FromTable + { $$ = $1; $1.push($3); } + ; + +FromTable + : LPAR Select RPAR Literal + { $$ = $2; $$.as = $4 } + | LPAR Select RPAR AS Literal + { $$ = $2; $$.as = $5 } + | LPAR Select RPAR /* default alias */ + { $$ = $2; $$.as = 'default' } + + | Json AS? Literal? + { $$ = new yy.Json({value:$1}); $1.as = $3 } + + | Table Literal + { $$ = $1; $1.as = $2 } + | Table AS Literal + { $$ = $1; $1.as = $3 } + | Table + { $$ = $1; } + | Table NOT INDEXED + { $$ = $1; } + | ParamValue Literal + { $$ = $1; $1.as = $2; } + | ParamValue AS Literal + { $$ = $1; $1.as = $3; } + | ParamValue + { $$ = $1; $1.as = 'default'; } + + | FuncValue + { $$ = $1; $1.as = 'default'; } + | FuncValue Literal + { $$ = $1; $1.as = $2; } + | FuncValue AS Literal + { $$ = $1; $1.as = $3; } + + | INSERTED + { $$ = {inserted:true}; } + + | VarValue + { $$ = $1; $1.as = 'default'; } + | VarValue Literal + { $$ = $1; $1.as = $2; } + | VarValue AS Literal + { $$ = $1; $1.as = $3; } + + | FromString + { $$ = $1; $1.as = 'default'; } + | FromString Literal + { $$ = $1; $1.as = $2; } + | FromString AS Literal + { $$ = $1; $1.as = $3; } + ; + +FromString + : STRING + { + var s = $1; + s = s.substr(1,s.length-2); + var x3 = s.substr(-3).toUpperCase(); + var x4 = s.substr(-4).toUpperCase(); + var r; + if(s[0] == '#') { + r = new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); + } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { + r = new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); + } else if(x4=='XLSX' || x4 == 'JSON') { + r = new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); + } else { + throw new Error('Unknown string in FROM clause'); + }; + $$ = r; + } + ; + +Table + : Literal DOT Literal + { + if($1 == 'INFORMATION_SCHEMA') { + $$ = new yy.FuncValue({funcid: $1, args:[new yy.StringValue({value:$3})]}); + } else { + $$ = new yy.Table({databaseid: $1, tableid:$3}); + } + } + | Literal + { $$ = new yy.Table({tableid: $1});} + ; + +JoinTablesList + : JoinTablesList JoinTable + { $$ = $1; $1.push($2); } + | JoinTablesList ApplyClause + { $$ = $1; $1.push($2); } + | JoinTable + { $$ = [$1]; } + | ApplyClause + { $$ = [$1]; } + ; + +JoinTable + : JoinMode JoinTableAs OnClause + { $$ = new yy.Join($1); yy.extend($$, $2); yy.extend($$, $3); } + ; + +JoinTableAs + : Table + { $$ = {table: $1}; } + | Table Literal + { $$ = {table: $1, as: $2 } ; } + | Table AS Literal + { $$ = {table: $1, as: $3 } ; } + | Json AS? Literal? + { $$ = {json:new yy.Json({value:$1,as:$3})}; } + | ParamValue Literal + { $$ = {param: $1, as: $2 } ; } + | ParamValue AS Literal + { $$ = {param: $1, as: $3 } ; } + | LPAR Select RPAR Literal + { $$ = {select: $2, as: $4} ; } + | LPAR Select RPAR AS Literal + { $$ = {select: $2, as: $5 } ; } + | FuncValue + { $$ = {func:$1, as:'default'}; } + | FuncValue Literal + { $$ = {func:$1, as: $2}; } + | FuncValue AS Literal + { $$ = {func:$1, as: $3}; } + + | VarValue + { $$ = {variable:$1,as:'default'}; } + | VarValue Literal + { $$ = {variable:$1,as:$2}; } + | VarValue AS Literal + { $$ = {variable:$1,as:$3} } + ; + +JoinMode + : JoinModeMode + { $$ = { joinmode: $1 } ; } + | NATURAL JoinModeMode + { $$ = {joinmode: $1, natural:true} ; } + ; + +JoinModeMode + : JOIN + { $$ = "INNER"; } + | INNER JOIN + { $$ = "INNER"; } + | LEFT JOIN + { $$ = "LEFT"; } + | LEFT OUTER JOIN + { $$ = "LEFT"; } + | RIGHT JOIN + { $$ = "RIGHT"; } + | RIGHT OUTER JOIN + { $$ = "RIGHT"; } + | OUTER JOIN + { $$ = "OUTER"; } + | FULL OUTER JOIN + { $$ = "OUTER"; } + | SEMI JOIN + { $$ = "SEMI"; } + | ANTI JOIN + { $$ = "ANTI"; } + | CROSS JOIN + { $$ = "CROSS"; } + ; + +OnClause + : ON Expression + { $$ = {on: $2}; } + | USING ColumnsList + { $$ = {using: $2}; } + | + { $$ = undefined; } + ; + +WhereClause + : { $$ = undefined; } + | WHERE Expression + { $$ = {where: new yy.Expression({expression:$2})}; } + ; + +GroupClause + : { $$ = undefined; } + | GROUP BY GroupExpressionsList HavingClause + { $$ = {group:$3}; yy.extend($$,$4); } + ; + +GroupExpressionsList + : GroupExpression + { $$ = [$1]; } + | GroupExpressionsList COMMA GroupExpression + { $$ = $1; $1.push($3); } + ; + +GroupExpression + : GROUPING SET LPAR GroupExpressionsList RPAR + { $$ = new yy.GroupExpression({type:'GROUPING SETS', group: $4}); } + | ROLLUP LPAR GroupExpressionsList RPAR + { $$ = new yy.GroupExpression({type:'ROLLUP', group: $3}); } + | CUBE LPAR GroupExpressionsList RPAR + { $$ = new yy.GroupExpression({type:'CUBE', group: $3}); } + | Expression + { $$ = $1; } + ; + + +HavingClause + : { $$ = undefined; } + | HAVING Expression + { $$ = {having:$2}} + ; + +UnionClause + : { $$ = undefined; } + | UNION Select + { $$ = {union: $2} ; } + | UNION ALL Select + { $$ = {unionall: $3} ; } + | EXCEPT Select + { $$ = {except: $2} ; } + | INTERSECT Select + { $$ = {intersect: $2} ; } + | UNION CORRESPONDING Select + { $$ = {union: $3, corresponding:true} ; } + | UNION ALL CORRESPONDING Select + { $$ = {unionall: $4, corresponding:true} ; } + | EXCEPT CORRESPONDING Select + { $$ = {except: $3, corresponding:true} ; } + | INTERSECT CORRESPONDING Select + { $$ = {intersect: $3, corresponding:true} ; } + ; + +OrderClause + : { $$ = undefined; } + | ORDER BY OrderExpressionsList + { $$ = {order:$3}} + ; + +OrderExpressionsList + : OrderExpression + { $$ = [$1]; } + | OrderExpressionsList COMMA OrderExpression + { $$ = $1; $1.push($3)} + ; + +NullsOrder + : NULLS FIRST + { $$ = {nullsOrder: 'FIRST'}; } + | NULLS LAST + { $$ = {nullsOrder: 'LAST'}; } + ; + +OrderExpression + : Expression + { $$ = new yy.Expression({expression: $1, direction:'ASC'}) } + | Expression DIRECTION + { $$ = new yy.Expression({expression: $1, direction:$2.toUpperCase()}) } + | Expression DIRECTION NullsOrder + { $$ = new yy.Expression({expression: $1, direction:$2.toUpperCase()}); yy.extend($$, $3) } + | Expression COLLATE NOCASE + { $$ = new yy.Expression({expression: $1, direction:'ASC', nocase:true}) } + | Expression COLLATE NOCASE DIRECTION + { $$ = new yy.Expression({expression: $1, direction:$4.toUpperCase(), nocase:true}) } + ; + +LimitClause + : { $$ = undefined; } + | LIMIT NumValue OffsetClause + { $$ = {limit:$2}; yy.extend($$, $3); } + | OFFSET NumValue ROWS? FETCH NEXT? NumValue ROWS? ONLY? + { $$ = {limit:$6,offset:$2}; } + ; + +OffsetClause + : { $$ = undefined; } + | OFFSET NumValue + { $$ = {offset:$2}; } + ; + + +ResultColumns + : ResultColumns COMMA ResultColumn + { $1.push($3); $$ = $1; } + | ResultColumn + { $$ = [$1]; } + ; + +ResultColumn + : Expression AS Literal + { $1.as = $3; $$ = $1;} + | Expression Literal + { $1.as = $2; $$ = $1;} + | Expression AS NUMBER + { $1.as = $3; $$ = $1;} + | Expression NUMBER + { $1.as = $2; $$ = $1;} + | Expression AS StringValue + { $1.as = $3; $$ = $1;} + | Expression StringValue + { $1.as = $2; $$ = $1;} + | Expression + { $$ = $1; } + ; + +Star + : Literal DOT Literal DOT STAR + { $$ = new yy.Column({columid: $5, tableid: $3, databaseid:$1}); } + | Literal DOT STAR + { $$ = new yy.Column({columnid: $3, tableid: $1}); } + | STAR + { $$ = new yy.Column({columnid:$1}); } + ; + +Column + : Literal DOT Literal DOT Literal + { $$ = new yy.Column({columnid: $5, tableid: $3, databaseid:$1});} + | Literal DOT Literal + { $$ = new yy.Column({columnid: $3, tableid: $1});} + | Literal DOT VALUE + { $$ = new yy.Column({columnid: $3, tableid: $1});} + | Literal + { $$ = new yy.Column({columnid: $1});} + ; + +Expression + : AggrValue + { $$ = $1; } + | FuncValue + { $$ = $1; } + | Op + { $$ = $1; } + | Column + { $$ = $1; } + | Star + { $$ = $1; } + | NumValue + { $$ = $1; } + | LogicValue + { $$ = $1; } + | StringValue + { $$ = $1; } + | NullValue + { $$ = $1; } + | ParamValue + { $$ = $1; } + | VarValue + { $$ = $1; } + | ExistsValue + { $$ = $1; } + | CaseValue + { $$ = $1; } + | CastClause + { $$ = $1; } + | VALUE + { $$ = new yy.DomainValueValue(); } + | Json + { $$ = new yy.Json({value:$1}); } + | ArrayValue + { $$ = $1; } +/* | ATLBRA JsonArray + { $$ = new yy.Json({value:$2}); } +*/ | NewClause + { $$ = $1; } +/* | AT LPAR Expression RPAR + { $$ = new yy.FuncValue({funcid: 'CLONEDEEP', args:[$3]}); } +*/ +/* | AT LPAR Json RPAR + { $$ = new yy.Json({value:$3}); } +*/ | LPAR Select RPAR + { + if(!yy.queries) yy.queries = []; + yy.queries.push($2); + $2.queriesidx = yy.queries.length; + $$ = $2; + } + | LPAR Insert RPAR + { + if(!yy.queries) yy.queries = []; + yy.queries.push($2); + $2.queriesidx = yy.queries.length; + $$ = $2; + } + | LPAR (CreateVertex|CreateEdge) RPAR + { + if(!yy.queries) yy.queries = []; + yy.queries.push($2); + $2.queriesidx = yy.queries.length; + $$ = $2; + } + + | JavaScript + {$$ = $1} + | CURRENT_TIMESTAMP + { $$ = new yy.FuncValue({funcid:'CURRENT_TIMESTAMP'});} + | CURRENT_DATE + { $$ = new yy.FuncValue({funcid:'CURRENT_DATE'});} +/* | USER + { $$ = new yy.FuncValue({funcid:'USER'});} +*/ ; + +JavaScript + : JAVASCRIPT + { $$ = new yy.JavaScript({value:$1.substr(2,$1.length-4)}); } + ; + +CreateFunction + : CREATE FUNCTION LITERAL AS JAVASCRIPT + { $$ = new yy.JavaScript({value:'alasql.fn["'+$3+'"] = '+$5.substr(2,$5.length-4)}); } + ; + +CreateAggregate + : CREATE AGGREGATE LITERAL AS JAVASCRIPT + { $$ = new yy.JavaScript({value:'alasql.aggr["'+$3+'"] = '+$5.substr(2,$5.length-4)}); } + ; + + +NewClause + : NEW Literal + { $$ = new yy.FuncValue({funcid:$2, newid:true}); } + | NEW FuncValue + { $$ = $2; yy.extend($$,{newid:true}); } + ; + + +CastClause + : CAST LPAR Expression AS ColumnType RPAR + { $$ = new yy.Convert({expression:$3}) ; yy.extend($$,$5) ; } + | CAST LPAR Expression AS ColumnType COMMA NUMBER RPAR + { $$ = new yy.Convert({expression:$3, style:$7}) ; yy.extend($$,$5) ; } + | CONVERT LPAR ColumnType COMMA Expression RPAR + { $$ = new yy.Convert({expression:$5}) ; yy.extend($$,$3) ; } + | CONVERT LPAR ColumnType COMMA Expression COMMA NUMBER RPAR + { $$ = new yy.Convert({expression:$5, style:$7}) ; yy.extend($$,$3) ; } + ; + +PrimitiveValue + : NumValue + { $$ = $1; } + | StringValue + { $$ = $1; } + | LogicValue + { $$ = $1; } + | NullValue + { $$ = $1; } + | ParamValue + { $$ = $1; } + | FuncValue + { $$ = $1; } + | CURRENT_TIMESTAMP + { $$ = new yy.FuncValue({funcid:'CURRENT_TIMESTAMP'}); } + | CURRENT_DATE + { $$ = new yy.FuncValue({funcid:'CURRENT_DATE'}); } +/* | USER + { $$ = new yy.FuncValue({funcid:'USER'}); } +*/ ; + + +AggrValue + : Aggregator LPAR ExprList RPAR OverClause + { + if($3.length > 1 && ($1.toUpperCase() == 'MAX' || $1.toUpperCase() == 'MIN')) { + $$ = new yy.FuncValue({funcid:$1,args:$3}); + } else { + $$ = new yy.AggrValue({aggregatorid: $1.toUpperCase(), expression: $3.pop(), over:$5}); + } + } + | Aggregator LPAR DISTINCT Expression RPAR OverClause + { $$ = new yy.AggrValue({aggregatorid: $1.toUpperCase(), expression: $4, distinct:true, over:$6}); } + | Aggregator LPAR ALL Expression RPAR OverClause + { $$ = new yy.AggrValue({aggregatorid: $1.toUpperCase(), expression: $4, + over:$6}); } + ; + +OverClause + : + {$$ = undefined; } + | OVER LPAR OverPartitionClause RPAR + { $$ = new yy.Over(); yy.extend($$,$3); } + | OVER LPAR OverOrderByClause RPAR + { $$ = new yy.Over(); yy.extend($$,$3); } + | OVER LPAR OverPartitionClause OverOrderByClause RPAR + { $$ = new yy.Over(); yy.extend($$,$3); yy.extend($$,$4);} + ; + +OverPartitionClause + : PARTITION BY GroupExpressionsList + { $$ = {partition:$3}; } + ; +OverOrderByClause + : ORDER BY OrderExpressionsList + { $$ = {order:$3}; } + ; +Aggregator + : SUM { $$ = "SUM"; } + | TOTAL { $$ = "TOTAL"; } + | COUNT { $$ = "COUNT"; } + | MIN { $$ = "MIN"; } + | MAX { $$ = "MAX"; } + | AVG { $$ = "AVG"; } + | FIRST { $$ = "FIRST"; } + | LAST { $$ = "LAST"; } + | AGGR { $$ = "AGGR"; } + | ARRAY { $$ = "ARRAY"; } +/* | REDUCE { $$ = "REDUCE"; } */ + ; + +FuncValue + : Literal LPAR (DISTINCT|ALL)? ExprList RPAR + { + var funcid = $1; + var exprlist = $4; + if(exprlist.length > 1 && (funcid.toUpperCase() == 'MIN' || funcid.toUpperCase() == 'MAX')) { + $$ = new yy.FuncValue({funcid: funcid, args: exprlist}); + } else if(alasql.aggr[$1]) { + $$ = new yy.AggrValue({aggregatorid: 'REDUCE', + funcid: funcid, expression: exprlist.pop(),distinct:($3=='DISTINCT') }); + } else { + $$ = new yy.FuncValue({funcid: funcid, args: exprlist}); + }; + } + | Literal LPAR RPAR + { $$ = new yy.FuncValue({ funcid: $1 }) } + | IF LPAR ExprList RPAR + { $$ = new yy.FuncValue({ funcid: 'IIF', args:$3 }) } + | REPLACE LPAR ExprList RPAR + { $$ = new yy.FuncValue({ funcid: 'REPLACE', args:$3 }) } + | CURRENT_DATE LPAR RPAR + { $$ = new yy.FuncValue({ funcid: $1 }) } + | DATEADD LPAR Literal COMMA Expression COMMA Expression RPAR + { $$ = new yy.FuncValue({ funcid: 'DATEADD', args:[new yy.StringValue({value:$3}),$5,$7]}) } + | DATEADD LPAR STRING COMMA Expression COMMA Expression RPAR + { $$ = new yy.FuncValue({ funcid: 'DATEADD', args:[$3,$5,$7]}) } + | DATEDIFF LPAR Literal COMMA Expression COMMA Expression RPAR + { $$ = new yy.FuncValue({ funcid: 'DATEDIFF', args:[new yy.StringValue({value:$3}),$5,$7]}) } + | DATEDIFF LPAR STRING COMMA Expression COMMA Expression RPAR + { $$ = new yy.FuncValue({ funcid: 'DATEDIFF', args:[$3,$5,$7]}) } + | TIMESTAMPDIFF LPAR Expression COMMA Expression COMMA Expression RPAR + { $$ = new yy.FuncValue({ funcid: 'TIMESTAMPDIFF', args:[new yy.StringValue({value:$3}),$5,$7]}) } + | INTERVAL Expression Literal + { $$ = new yy.FuncValue({ funcid: 'INTERVAL', args:[$2,new yy.StringValue({value:($3).toLowerCase()})]}); } + ; + +ExprList + : Expression + { $$ = [$1]; } + | ExprList COMMA Expression + { $1.push($3); $$ = $1 } + ; + +NumValue + : NUMBER + { $$ = new yy.NumValue({value:+$1}); } + ; + +LogicValue + : TRUE + { $$ = new yy.LogicValue({value:true}); } + | FALSE + { $$ = new yy.LogicValue({value:false}); } + ; + +StringValue + : STRING + { $$ = new yy.StringValue({value: $1.substr(1,$1.length-2).replace(/(\\\')/g,"'").replace(/(\'\')/g,"'")}); } + | NSTRING + { $$ = new yy.StringValue({value: $1.substr(2,$1.length-3).replace(/(\\\')/g,"'").replace(/(\'\')/g,"'")}); } + ; + +NullValue + : NULL + { $$ = new yy.NullValue({value:undefined}); } + ; + +VarValue + : AT Literal + { $$ = new yy.VarValue({variable:$2}); } + ; + +ExistsValue + : EXISTS LPAR Select RPAR + { + if(!yy.exists) yy.exists = []; + $$ = new yy.ExistsValue({value:$3, existsidx:yy.exists.length}); + yy.exists.push($3); + } + ; + +ArrayValue + : ARRAYLBRA ExprList RBRA + { $$ = new yy.ArrayValue({value:$2}); } + ; + +ParamValue + : DOLLAR (Literal|NUMBER) + { $$ = new yy.ParamValue({param: $2}); } +/* | DOLLAR NUMBER + { $$ = new yy.ParamValue({param: $2}); } +*/ | COLON Literal + { $$ = new yy.ParamValue({param: $2}); } + | QUESTION + { + if(typeof yy.question == 'undefined') yy.question = 0; + $$ = new yy.ParamValue({param: yy.question++}); + } + | BRAQUESTION + { + if(typeof yy.question == 'undefined') yy.question = 0; + $$ = new yy.ParamValue({param: yy.question++, array:true}); + } + ; + + +CaseValue + : CASE Expression WhensList ElseClause END + { $$ = new yy.CaseValue({expression:$2, whens: $3, elses: $4}); } + | CASE WhensList ElseClause END + { $$ = new yy.CaseValue({whens: $2, elses: $3}); } + ; + +WhensList + : WhensList When + { $$ = $1; $$.push($2); } + | When + { $$ = [$1]; } + ; + +When + : WHEN Expression THEN Expression + { $$ = {when: $2, then: $4 }; } + ; + +ElseClause + : ELSE Expression + { $$ = $2; } + | + { $$ = undefined; } + ; + +Op + : Expression REGEXP Expression + { $$ = new yy.Op({left:$1, op:'REGEXP', right:$3}); } + | Expression TILDA Expression + { $$ = new yy.Op({left:$1, op:'REGEXP', right:$3}); } + | Expression GLOB Expression + { $$ = new yy.Op({left:$1, op:'GLOB', right:$3}); } + | Expression LIKE Expression + { $$ = new yy.Op({left:$1, op:'LIKE', right:$3}); } + | Expression LIKE Expression ESCAPE Expression + { $$ = new yy.Op({left:$1, op:'LIKE', right:$3, escape:$5}); } + | Expression NOT_LIKE Expression + { $$ = new yy.Op({left:$1, op:'NOT LIKE', right:$3 }); } + | Expression NOT_LIKE Expression ESCAPE Expression + { $$ = new yy.Op({left:$1, op:'NOT LIKE', right:$3, escape:$5 }); } + | Expression BARBAR Expression + { $$ = new yy.Op({left:$1, op:'||', right:$3}); } + | Expression PLUS Expression + { $$ = new yy.Op({left:$1, op:'+', right:$3}); } + | Expression MINUS Expression + { $$ = new yy.Op({left:$1, op:'-', right:$3}); } + | Expression STAR Expression + { $$ = new yy.Op({left:$1, op:'*', right:$3}); } + | Expression SLASH Expression + { $$ = new yy.Op({left:$1, op:'/', right:$3}); } + | Expression MODULO Expression + { $$ = new yy.Op({left:$1, op:'%', right:$3}); } + | Expression CARET Expression + { $$ = new yy.Op({left:$1, op:'^', right:$3}); } + + | Expression GTGT Expression + { $$ = new yy.Op({left:$1, op:'>>', right:$3}); } + | Expression LTLT Expression + { $$ = new yy.Op({left:$1, op:'<<', right:$3}); } + | Expression AMPERSAND Expression + { $$ = new yy.Op({left:$1, op:'&', right:$3}); } + | Expression BAR Expression + { $$ = new yy.Op({left:$1, op:'|', right:$3}); } + + | Expression ArrowDot Literal + { $$ = new yy.Op({left:$1, op:'->' , right:$3}); } + | Expression ArrowDot NumValue + { $$ = new yy.Op({left:$1, op:'->' , right:$3}); } + | Expression ArrowDot LPAR Expression RPAR + { $$ = new yy.Op({left:$1, op:'->' , right:$4}); } + | Expression ArrowDot FuncValue + { $$ = new yy.Op({left:$1, op:'->' , right:$3}); } + + | Expression EXCLAMATION Literal + { $$ = new yy.Op({left:$1, op:'!' , right:$3}); } + | Expression EXCLAMATION NumValue + { $$ = new yy.Op({left:$1, op:'!' , right:$3}); } + | Expression EXCLAMATION LPAR Expression RPAR + { $$ = new yy.Op({left:$1, op:'!' , right:$4}); } + | Expression EXCLAMATION FuncValue + { $$ = new yy.Op({left:$1, op:'!' , right:$3}); } + + + + + | Expression GT Expression + { $$ = new yy.Op({left:$1, op:'>' , right:$3}); } + | Expression GE Expression + { $$ = new yy.Op({left:$1, op:'>=' , right:$3}); } + | Expression LT Expression + { $$ = new yy.Op({left:$1, op:'<' , right:$3}); } + | Expression LE Expression + { $$ = new yy.Op({left:$1, op:'<=' , right:$3}); } + | Expression EQ Expression + { $$ = new yy.Op({left:$1, op:'=' , right:$3}); } + | Expression EQEQ Expression + { $$ = new yy.Op({left:$1, op:'==' , right:$3}); } + | Expression EQEQEQ Expression + { $$ = new yy.Op({left:$1, op:'===' , right:$3}); } + | Expression NE Expression + { $$ = new yy.Op({left:$1, op:'!=' , right:$3}); } + | Expression NEEQEQ Expression + { $$ = new yy.Op({left:$1, op:'!==' , right:$3}); } + | Expression NEEQEQEQ Expression + { $$ = new yy.Op({left:$1, op:'!===' , right:$3}); } + + | Expression CondOp AllSome LPAR Select RPAR + { + if(!yy.queries) yy.queries = []; + $$ = new yy.Op({left:$1, op:$2 , allsome:$3, right:$5, queriesidx: yy.queries.length}); + yy.queries.push($5); + } + + | Expression CondOp AllSome LPAR ExprList RPAR + { + $$ = new yy.Op({left:$1, op:$2 , allsome:$3, right:$5}); + } + + | Expression AND Expression + { + if($1.op == 'BETWEEN1') { + + if($1.left.op == 'AND') { + $$ = new yy.Op({left:$1.left.left,op:'AND',right: + new yy.Op({left:$1.left.right, op:'BETWEEN', + right1:$1.right, right2:$3}) + }); + } else { + $$ = new yy.Op({left:$1.left, op:'BETWEEN', + right1:$1.right, right2:$3}); + } + + } else if($1.op == 'NOT BETWEEN1') { + if($1.left.op == 'AND') { + $$ = new yy.Op({left:$1.left.left,op:'AND',right: + new yy.Op({left:$1.left.right, op:'NOT BETWEEN', + right1:$1.right, right2:$3}) + }); + } else { + $$ = new yy.Op({left:$1.left, op:'NOT BETWEEN', + right1:$1.right, right2:$3}); + } + } else { + $$ = new yy.Op({left:$1, op:'AND', right:$3}); + } + + + } + | Expression OR Expression + { $$ = new yy.Op({left:$1, op:'OR' , right:$3}); } + | NOT Expression + { $$ = new yy.UniOp({op:'NOT' , right:$2}); } + | MINUS Expression + { $$ = new yy.UniOp({op:'-' , right:$2}); } + | PLUS Expression + { $$ = new yy.UniOp({op:'+' , right:$2}); } + | TILDA Expression + { $$ = new yy.UniOp({op:'~' , right:$2}); } + | SHARP Expression + { $$ = new yy.UniOp({op:'#' , right:$2}); } + | LPAR Expression RPAR + { $$ = new yy.UniOp({right: $2}); } + + | Expression IN LPAR Select RPAR + { + if(!yy.queries) yy.queries = []; + $$ = new yy.Op({left: $1, op:'IN', right:$4, queriesidx: yy.queries.length}); + yy.queries.push($4); + } + + | Expression NOT IN LPAR Select RPAR + { + if(!yy.queries) yy.queries = []; + $$ = new yy.Op({left: $1, op:'NOT IN', right:$5, queriesidx: yy.queries.length}); + yy.queries.push($5); + } + + | Expression IN LPAR ExprList RPAR + { $$ = new yy.Op({left: $1, op:'IN', right:$4}); } + + | Expression NOT IN LPAR ExprList RPAR + { $$ = new yy.Op({left: $1, op:'NOT IN', right:$5}); } + + | Expression IN LPAR RPAR + { $$ = new yy.Op({left: $1, op:'IN', right:[]}); } + + | Expression NOT IN LPAR RPAR + { $$ = new yy.Op({left: $1, op:'NOT IN', right:[]}); } + + | Expression IN ColFunc + { $$ = new yy.Op({left: $1, op:'IN', right:$3}); } + + | Expression NOT IN ColFunc + { $$ = new yy.Op({left: $1, op:'NOT IN', right:$4}); } + + | Expression IN VarValue + { $$ = new yy.Op({left: $1, op:'IN', right:$3}); } + + | Expression NOT IN VarValue + { $$ = new yy.Op({left: $1, op:'NOT IN', right:$4}); } + + /* + Hack - it impossimle to parse BETWEEN AND and AND expressions with grammar. + At least, I do not know how. + */ + | Expression BETWEEN Expression + { +/* var expr = $3; + if(expr.left && expr.left.op == 'AND') { + $$ = new yy.Op({left:new yy.Op({left:$1, op:'BETWEEN', right:expr.left}), op:'AND', right:expr.right }); + } else { +*/ + $$ = new yy.Op({left:$1, op:'BETWEEN1', right:$3 }); +// } + } + | Expression NOT_BETWEEN Expression + { +// var expr = $3; +// if(expr.left && expr.left.op == 'AND') { +// $$ = new yy.Op({left:new yy.Op({left:$1, op:'NOT BETWEEN', right:expr.left}), op:'AND', right:expr.right }); +// } else { + $$ = new yy.Op({left:$1, op:'NOT BETWEEN1', right:$3 }); +// } + } + | Expression IS Expression + { $$ = new yy.Op({op:'IS' , left:$1, right:$3}); } + | Expression NOT NULL + { + $$ = new yy.Op({ + op:'IS', + left:$1, + right: new yy.UniOp({ + op:'NOT', + right:new yy.NullValue({value:undefined}) + }) + }); + } + | Expression DOUBLECOLON ColumnType + { $$ = new yy.Convert({expression:$1}) ; yy.extend($$,$3) ; } + ; + +ColFunc + : Column + { $$ = $1;} + | FuncValue + { $$ = $1;} + | AT LPAR Expression RPAR + { $$ = $3;} + ; + +CondOp + : GT { $$ = $1; } + | GE { $$ = $1; } + | LT { $$ = $1; } + | LE { $$ = $1; } + | EQ { $$ = $1; } + | NE { $$ = $1; } + ; + +AllSome + : ALL + { $$ = 'ALL'; } + | SOME + { $$ = 'SOME'; } + | ANY + { $$ = 'ANY'; } + ; + +/* PART TWO */ + +/* UPDATE */ + +Update + : UPDATE Table SET SetColumnsList WHERE Expression + { $$ = new yy.Update({table:$2, columns:$4, where:$6}); } + | UPDATE Table SET SetColumnsList + { $$ = new yy.Update({table:$2, columns:$4}); } + ; + +SetColumnsList + : SetColumn + { $$ = [$1]; } + | SetColumnsList COMMA SetColumn + { $$ = $1; $1.push($3); } + ; + +SetColumn + : Column EQ Expression +/* TODO Replace columnid with column */ + { $$ = new yy.SetColumn({column:$1, expression:$3})} + | (AT|DOLLAR) Literal EQ Expression + { $$ = new yy.SetColumn({variable:$2, expression:$4, method:$1})} + ; + +/* DELETE */ + +Delete + : DELETE FROM Table WHERE Expression + { $$ = new yy.Delete({table:$3, where:$5});} + | DELETE FROM Table + { $$ = new yy.Delete({table:$3});} + ; + +/* INSERT */ + +Insert + : INSERT Into Table Values ValuesListsList + { $$ = new yy.Insert({into:$3, values: $5}); } + | INSERT Into Table ValuesListsList + { $$ = new yy.Insert({into:$3, values: $4}); } + | INSERT OR REPLACE Into Table Values ValuesListsList + { $$ = new yy.Insert({into:$5, values: $7, orreplace:true}); } + | INSERT OR REPLACE Into Table ValuesListsList + { $$ = new yy.Insert({into:$5, values: $6, orreplace:true}); } + | REPLACE Into Table Values ValuesListsList + { $$ = new yy.Insert({into:$3, values: $5, orreplace:true}); } + | REPLACE Into Table ValuesListsList + { $$ = new yy.Insert({into:$3, values: $4, orreplace:true}); } + | INSERT Into Table DEFAULT Values + { $$ = new yy.Insert({into:$3, "default": true}) ; } + | INSERT Into Table LPAR ColumnsList RPAR Values ValuesListsList + { $$ = new yy.Insert({into:$3, columns: $5, values: $8}); } + | INSERT Into Table LPAR ColumnsList RPAR ValuesListsList + { $$ = new yy.Insert({into:$3, columns: $5, values: $7}); } + | INSERT Into Table Select + { $$ = new yy.Insert({into:$3, select: $4}); } + | INSERT OR REPLACE Into Table Select + { $$ = new yy.Insert({into:$5, select: $6, orreplace:true}); } + | INSERT Into Table LPAR ColumnsList RPAR Select + { $$ = new yy.Insert({into:$3, columns: $5, select: $7}); } + ; + +Values + : VALUES + | VALUE + ; + +Into + : + | INTO + ; +/* +TableParamFunc + : Table + { $$ = $1; } + | ParamValue + { $$ = $1; } + | FuncValue + { $$ = $1; } + ; +*/ + +ValuesListsList + : LPAR ValuesList RPAR + { $$ = [$2]; } + | Json + { $$ = [$1]; } + | ParamValue + { $$ = [$1]; } + | ValuesListsList COMMA LPAR ValuesList RPAR + {$$ = $1; $1.push($4)} + | ValuesListsList COMMA Json + {$$ = $1; $1.push($3)} + | ValuesListsList COMMA ParamValue + {$$ = $1; $1.push($3)} + ; + +ValuesList + : Expression + { $$ = [$1]; } + | ValuesList COMMA Expression + {$$ = $1; $1.push($3)} + ; + +Value + : NumValue + | StringValue + | LogicValue + | NullValue + | DateValue + | ParamValue + ; + +ColumnsList + : Column + { $$ = [$1]; } + | ColumnsList COMMA Column + {$$ = $1; $1.push($3)} + ; + +/* CREATE TABLE */ + +CreateTable + : CREATE TemporaryClause TableClass IfNotExists Table LPAR CreateTableDefClause RPAR CreateTableOptionsClause + { + $$ = new yy.CreateTable({table:$5}); + yy.extend($$,$2); + yy.extend($$,$3); + yy.extend($$,$4); + yy.extend($$,$7); + yy.extend($$,$9); + } + | CREATE TemporaryClause TableClass IfNotExists Table + { + $$ = new yy.CreateTable({table:$5}); + yy.extend($$,$2); + yy.extend($$,$3); + yy.extend($$,$4); + } +; + +TableClass + : TABLE + { $$ = undefined; } + | CLASS + { $$ = {class:true}; } + ; + +CreateTableOptionsClause + : + | CreateTableOptions + ; + +CreateTableOptions + : CreateTableOptions CreateTableOption + | CreateTableOption + ; + +/* TODO: Remove this section */ +CreateTableOption + : DEFAULT + | LITERAL EQ Literal + | IDENTITY EQ NumValue + | COLLATE EQ Literal + ; + +TemporaryClause + : { $$ = undefined; } + | TEMP + { $$ = {temporary:true}; } + ; + +IfNotExists + : { $$ = undefined; } + | IF NOT EXISTS + { $$ = {ifnotexists: true}; } + ; + +CreateTableDefClause + : ColumnDefsList COMMA ConstraintsList + { $$ = {columns: $1, constraints: $3}; } + | ColumnDefsList + { $$ = {columns: $1}; } + | AS Select + { $$ = {as: $2} } + ; + +ConstraintsList + : Constraint + { $$ = [$1];} + | ConstraintsList COMMA Constraint + { $1.push($3); $$ = $1; } + ; + +Constraint + : ConstraintName PrimaryKey + { $2.constraintid = $1; $$ = $2; } + | ConstraintName ForeignKey + { $2.constraintid = $1; $$ = $2; } + | ConstraintName UniqueKey + { $2.constraintid = $1; $$ = $2; } + | ConstraintName IndexKey + { $2.constraintid = $1; $$ = $2; } + | ConstraintName Check + { $2.constraintid = $1; $$ = $2; } + ; + +ConstraintName + : { $$ = undefined; } + | CONSTRAINT Literal + { $$ = $2; } + ; + +Check + : CHECK LPAR Expression RPAR + { $$ = {type: 'CHECK', expression: $3}; } + ; + +PrimaryKey + : PRIMARY KEY Literal? LPAR ColsList RPAR + { $$ = {type: 'PRIMARY KEY', columns: $5, clustered:($3+'').toUpperCase()}; } + ; + +ForeignKey + : FOREIGN KEY LPAR ColsList RPAR REFERENCES Table ParColsList? + OnForeignKeyClause + { $$ = {type: 'FOREIGN KEY', columns: $4, fktable: $7, fkcolumns: $8}; } + ; + +ParColsList + : LPAR ColsList RPAR + { $$ = $2; } + ; + +OnForeignKeyClause + : + { $$ = undefined; } + | OnDeleteClause OnUpdateClause + { $$ = undefined; } + ; + +OnDeleteClause + : ON DELETE NO ACTION + {$$ = undefined; } + ; +OnUpdateClause + : ON UPDATE NO ACTION + {$$ = undefined; } + ; + +UniqueKey + : UNIQUE KEY? Literal? LPAR ColumnsList RPAR + { + $$ = {type: 'UNIQUE', columns: $5, clustered:($3+'').toUpperCase()}; + } + ; + +IndexKey + : INDEX Literal LPAR ColumnsList RPAR + | KEY Literal LPAR ColumnsList RPAR + ; +ColsList + : Literal + { $$ = [$1]; } + | STRING + { $$ = [$1]; } + | ColsList COMMA Literal + { $$ = $1; $1.push($3); } + | ColsList COMMA STRING + { $$ = $1; $1.push($3); } + ; + +/* +OrderedColsList + : Literal + { $$ = [$1]; } + | STRING + { $$ = [$1]; } + | OrderedColsList COMMA Literal + { $$ = $1; $1.push($3); } + | OrderedColsList COMMA STRING + { $$ = $1; $1.push($3); } + ; +*/ +ColumnDefsList + : ColumnDef + { $$ = [$1];} + | ColumnDefsList COMMA ColumnDef + { $1.push($3); $$ = $1; } + ; + +ColumnDef + : Literal ColumnType ColumnConstraintsClause + { $$ = new yy.ColumnDef({columnid:$1}); yy.extend($$,$2); yy.extend($$,$3);} + | Literal ColumnConstraints + { $$ = new yy.ColumnDef({columnid:$1}); yy.extend($$,$2); } + | Literal + { $$ = new yy.ColumnDef({columnid:$1, dbtypeid: ''}); } + ; + +/* +ColumnType + : LITERAL LPAR NumberMax COMMA NUMBER RPAR + { $$ = {dbtypeid: $1, dbsize: $3, dbprecision: +$5} } + | LITERAL LPAR NumberMax RPAR + { $$ = {dbtypeid: $1, dbsize: $3} } + | LITERAL + { $$ = {dbtypeid: $1} } + | ENUM LPAR ValuesList RPAR + { $$ = {dbtypeid: 'ENUM', enumvalues: $3} } + ; +*/ +SingularColumnType + : LiteralWithSpaces LPAR NumberMax COMMA NUMBER RPAR + { $$ = {dbtypeid: $1, dbsize: $3, dbprecision: +$5} } + | LiteralWithSpaces LPAR NumberMax RPAR + { $$ = {dbtypeid: $1, dbsize: $3} } + | LiteralWithSpaces + { $$ = {dbtypeid: $1} } + | ENUM LPAR ValuesList RPAR + { $$ = {dbtypeid: 'ENUM', enumvalues: $3} } + ; + +ColumnType + : SingularColumnType BRALITERAL /* text[] */ + { $$ = $1; $1.dbtypeid += '[' + $2 + ']'; } + | SingularColumnType + { $$ = $1; } + ; + + +NumberMax + : NUMBER + { $$ = +$1; } + | MAXNUM + { $$ = "MAX"; } + ; + +ColumnConstraintsClause + : {$$ = undefined} + | ColumnConstraintsList + { $$ = $1; } + ; + + +ColumnConstraintsList + : ColumnConstraintsList ColumnConstraint + { + yy.extend($1,$2); $$ = $1; + } + | ColumnConstraint + { $$ = $1; } + ; + +ParLiteral + : LPAR Literal RPAR + { $$ = $2; } + ; + +ColumnConstraint + : PRIMARY KEY + {$$ = {primarykey:true};} + | FOREIGN KEY REFERENCES Table ParLiteral? + {$$ = {foreignkey:{table:$4, columnid: $5}};} + | REFERENCES Table ParLiteral? + {$$ = {foreignkey:{table:$2, columnid: $3}};} + | IDENTITY LPAR NumValue COMMA NumValue RPAR + { $$ = {identity: {value:$3,step:$5}} } + | IDENTITY + { $$ = {identity: {value:1,step:1}} } + | DEFAULT PrimitiveValue + {$$ = {"default":$2};} + | DEFAULT LPAR Expression RPAR + {$$ = {"default":$3};} + | DEFAULT FuncValue + {$$ = {"default":$2};} + | NULL + {$$ = {null:true}; } + | NOT NULL + {$$ = {notnull:true}; } + | Check + {$$ = {check:$1}; } + | UNIQUE + {$$ = {unique:true}; } + | ON UPDATE PrimitiveValue + {$$ = {"onupdate":$3};} + | ON UPDATE LPAR Expression RPAR + {$$ = {"onupdate":$4};} + ; + +/* DROP TABLE */ + +DropTable + : DROP (TABLE|CLASS) IfExists TablesList + { $$ = new yy.DropTable({tables:$4,type:$2}); yy.extend($$, $3); } + ; + +TablesList + : TablesList COMMA Table + { $1.push($3); $$=$1; } + | Table + { $$ = [$1]; } + ; + + +IfExists + : { $$ = undefined; } + | IF EXISTS + { $$ = {ifexists: true};} + ; + +/* ALTER TABLE */ + +AlterTable + : ALTER TABLE Table RENAME TO Literal + { $$ = new yy.AlterTable({table:$3, renameto: $6});} + | ALTER TABLE Table ADD COLUMN ColumnDef + { $$ = new yy.AlterTable({table:$3, addcolumn: $6});} + | ALTER TABLE Table MODIFY COLUMN ColumnDef + { $$ = new yy.AlterTable({table:$3, modifycolumn: $6});} + | ALTER TABLE Table RENAME COLUMN Literal TO Literal + { $$ = new yy.AlterTable({table:$3, renamecolumn: $6, to: $8});} + | ALTER TABLE Table DROP COLUMN Literal + { $$ = new yy.AlterTable({table:$3, dropcolumn: $6});} + ; + +RenameTable + : RENAME TABLE Table TO Literal + { $$ = new yy.AlterTable({table:$3, renameto: $5});} + ; + +/* DATABASES */ + +AttachDatabase + : ATTACH Literal DATABASE Literal + { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase() });} + | ATTACH Literal DATABASE Literal LPAR ExprList RPAR + { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase(), args:$6 });} + | ATTACH Literal DATABASE Literal AS Literal + { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase(), as:$6 });} + | ATTACH Literal DATABASE Literal LPAR ExprList RPAR AS Literal + { $$ = new yy.AttachDatabase({databaseid:$4, engineid:$2.toUpperCase(), as:$9, args:$6});} + ; + +DetachDatabase + : DETACH DATABASE Literal + { $$ = new yy.DetachDatabase({databaseid:$3});} + ; + +CreateDatabase + : CREATE DATABASE IfNotExists Literal + { $$ = new yy.CreateDatabase({databaseid:$4 }); yy.extend($$,$4); } + | CREATE Literal DATABASE IfNotExists Literal AsClause + { $$ = new yy.CreateDatabase({engineid:$2.toUpperCase(), databaseid:$5, as:$6 }); yy.extend($$,$4); } + | CREATE Literal DATABASE IfNotExists Literal LPAR ExprList RPAR AsClause + { $$ = new yy.CreateDatabase({engineid:$2.toUpperCase(), databaseid:$5, args:$7, as:$9 }); yy.extend($$,$4); } + | CREATE Literal DATABASE IfNotExists StringValue AsClause + { $$ = new yy.CreateDatabase({engineid:$2.toUpperCase(), + as:$6, args:[$5] }); yy.extend($$,$4); } + ; + +AsClause + : + {$$ = undefined;} + | AS Literal + { $$ = $1; } + ; + +UseDatabase + : USE DATABASE Literal + { $$ = new yy.UseDatabase({databaseid: $3 });} + | USE Literal + { $$ = new yy.UseDatabase({databaseid: $2 });} + ; + +DropDatabase + : DROP DATABASE IfExists Literal + { $$ = new yy.DropDatabase({databaseid: $4 }); yy.extend($$,$3); } + | DROP Literal DATABASE IfExists Literal + { $$ = new yy.DropDatabase({databaseid: $5, engineid:$2.toUpperCase() }); yy.extend($$,$4); } + | DROP Literal DATABASE IfExists StringValue + { $$ = new yy.DropDatabase({databaseid: $5, engineid:$2.toUpperCase() }); yy.extend($$,$4); } + ; + +/* INDEXES */ + +CreateIndex + : + CREATE INDEX Literal ON Table LPAR OrderExpressionsList RPAR + { $$ = new yy.CreateIndex({indexid:$3, table:$5, columns:$7})} + | + + CREATE UNIQUE INDEX Literal ON Table LPAR OrderExpressionsList RPAR + { $$ = new yy.CreateIndex({indexid:$4, table:$6, columns:$8, unique:true})} + ; + +DropIndex + : DROP INDEX Literal + { $$ = new yy.DropIndex({indexid:$3});} + ; + +/* SHOW COMMAND */ + +ShowDatabases + : SHOW DATABASE + { $$ = new yy.ShowDatabases();} + | SHOW DATABASE LIKE StringValue + { $$ = new yy.ShowDatabases({like:$4});} + | SHOW Literal DATABASE + { $$ = new yy.ShowDatabases({engineid:$2.toUpperCase() });} + | SHOW Literal DATABASE LIKE StringValue + { $$ = new yy.ShowDatabases({engineid:$2.toUpperCase() , like:$5});} + ; + +ShowTables + : SHOW TABLE + { $$ = new yy.ShowTables();} + | SHOW TABLE LIKE StringValue + { $$ = new yy.ShowTables({like:$4});} + | SHOW TABLE FROM Literal + { $$ = new yy.ShowTables({databaseid: $4});} + | SHOW TABLE FROM Literal LIKE StringValue + { $$ = new yy.ShowTables({like:$6, databaseid: $4});} + ; + +ShowColumns + : SHOW COLUMN FROM Table + { $$ = new yy.ShowColumns({table: $4});} + | SHOW COLUMN FROM Table FROM Literal + { $$ = new yy.ShowColumns({table: $4, databaseid:$6});} + ; + +ShowIndex + : SHOW INDEX FROM Table + { $$ = new yy.ShowIndex({table: $4});} + | SHOW INDEX FROM Table FROM Literal + { $$ = new yy.ShowIndex({table: $4, databaseid: $6});} + ; + +ShowCreateTable + : SHOW CREATE TABLE Table + { $$ = new yy.ShowCreateTable({table: $4});} + | SHOW CREATE TABLE Table FROM Literal + { $$ = new yy.ShowCreateTable({table: $4, databaseid:$6});} + ; + +CreateView + : CREATE TemporaryClause VIEW IfNotExists Table LPAR ColumnsList RPAR AS Select SubqueryRestriction? + { + $$ = new yy.CreateTable({table:$5,view:true,select:$10,viewcolumns:$7}); + yy.extend($$,$2); + yy.extend($$,$4); + } + | CREATE TemporaryClause VIEW IfNotExists Table AS Select SubqueryRestriction? + { + $$ = new yy.CreateTable({table:$5,view:true,select:$7}); + yy.extend($$,$2); + yy.extend($$,$4); + } + ; + +SubqueryRestriction + : WITH READ ONLY + | WITH CHECK OPTION + | WITH CHECK OPTION CONSTRAINT Constraint + ; + + +DropView + : DROP VIEW IfExists TablesList + { $$ = new yy.DropTable({tables:$4, view:true}); yy.extend($$, $3); } + ; +/* +DeclareCursor + : DECLARE Literal CURSOR FOR Select + { $$ = new yy.DeclareCursor({cursorid:$2, select:$5}); } + ; + +OpenCursor + : OPEN Literal + { $$ = new yy.OpenCursor({cursorid:$2}); } + ; + +CloseCursor + : CLOSE Literal + { $$ = new yy.CloseCursor({cursorid:$2}); } + ; + +FetchCursor + : FETCH FetchDirection FROM Literal + { $$ = new yy.FetchCursor({cursorid:$4}); yy.extend($$,$2); } + ; + +FetchDirection + : NEXT + { $$ = {direction: 'NEXT'}; } + | PRIOR + { $$ = {direction: 'PRIOR'}; } + | FIRST + { $$ = {direction: 'FIRST'}; } + | LAST + { $$ = {direction: 'LAST'}; } + | ABSOLUTE NumValue + { $$ = {direction: 'ABSOLUTE', num:$2}; } + | RELATIVE NumValue + { $$ = {direction: 'RELATIVE', num:$2}; } + ; +*/ + +/* +Help + : HELP StringValue + { $$ = new yy.Help({subject:$2.value.toUpperCase()} ) ; } + | HELP + { $$ = new yy.Help() ; } + ; +*/ + +ExpressionStatement + : EQ Expression + { $$ = new yy.ExpressionStatement({expression:$2}); } + ; + +Source + : SOURCE StringValue + { $$ = new yy.Source({url:$2.value}); } + ; + +Assert + : ASSERT Json + { $$ = new yy.Assert({value:$2}); } + | ASSERT PrimitiveValue + { $$ = new yy.Assert({value:$2.value}); } + | ASSERT STRING COMMA Json + { $$ = new yy.Assert({value:$4, message:$2}); } + ; + +Json + : AT LPAR Expression RPAR + { $$ = $3; } + | AT StringValue + { $$ = $2.value; } + | AT NumValue + { $$ = +$2.value; } + | AT LogicValue + { $$ = (!!$2.value); } + | AT ParamValue + { $$ = $2; } + | JsonObject + { $$ = $1; } + | AT JsonObject + { $$ = $2; } + | ATLBRA JsonArray + { $$ = $2; } + ; + +JsonValue + : Json + { $$ = $1; } + | JsonPrimitiveValue + { $$ = $1; } + ; + +JsonPrimitiveValue + : NumValue + { $$ = +$1.value; } + | StringValue + { $$ = ""+$1.value; } + | LogicValue + { $$ = $1.value; } + | Column + { $$ = $1; } + | NullValue + { $$ = $1.value; } + | ParamValue + { $$ = $1; } + | FuncValue + { $$ = $1; } + | LPAR Expression RPAR + { $$ = $2; } + ; + + +JsonObject + : LCUR JsonPropertiesList RCUR + { $$ = $2; } + | LCUR JsonPropertiesList COMMA RCUR + { $$ = $2; } + | LCUR RCUR + { $$ = {}; } + ; + +JsonArray + : JsonElementsList RBRA + { $$ = $1; } + | JsonElementsList COMMA RBRA + { $$ = $1; } + | RBRA + { $$ = []; } + ; + +JsonPropertiesList + : JsonPropertiesList COMMA JsonProperty + { yy.extend($1,$3); $$ = $1; } + | JsonProperty + { $$ = $1; } + ; + +JsonProperty + : STRING COLON JsonValue + { $$ = {}; $$[$1.substr(1,$1.length-2)] = $3; } + | NUMBER COLON JsonValue + { $$ = {}; $$[$1] = $3; } + | Literal COLON JsonValue + { $$ = {}; $$[$1] = $3; } +/* | STRING COLON ParamValue + { $$ = {}; $$[$1.substr(1,$1.length-2)] = $3; } + | NUMBER COLON ParamValue + { $$ = {}; $$[$1] = $3; } + | LITERAL COLON ParamValue + { $$ = {}; $$[$1] = $3; } +*/ ; + +JsonElementsList + : JsonElementsList COMMA JsonValue + { $1.push($3); $$ = $1; } + | JsonValue + { $$ = [$1]; } + ; + +SetVariable + : SET Literal EQ OnOff + { $$ = new yy.SetVariable({variable:$2.toLowerCase(), value:$4});} + | SET Literal OnOff + { $$ = new yy.SetVariable({variable:$2.toLowerCase(), value:$3});} + | SET Literal EQ Expression + { $$ = new yy.SetVariable({variable:$2, expression:$4});} + | SET Literal SetPropsList EQ Expression + { $$ = new yy.SetVariable({variable:$2, props: $3, expression:$5});} + | SET AtDollar Literal EQ Expression + { $$ = new yy.SetVariable({variable:$3, expression:$5, method:$2});} + | SET AtDollar Literal SetPropsList EQ Expression + { $$ = new yy.SetVariable({variable:$3, props: $4, expression:$6, method:$2});} + ; + +AtDollar + : AT + {$$ = '@'; } + | DOLLAR + {$$ = '$'; } + ; + +SetPropsList + : SetPropsList ArrowDot SetProp + { $1.push($3); $$ = $1; } + | ArrowDot SetProp + { $$ = [$2]; } + ; + +SetProp + : Literal + { $$ = $1; } + | NUMBER + { $$ = $1; } + | LPAR Expression RPAR + { $$ = $2; } + ; + +OnOff + : ON + { $$ = true; } + | OFF + { $$ = false; } + ; + +CommitTransaction + : COMMIT TRANSACTION + { $$ = new yy.CommitTransaction(); } + ; + +RollbackTransaction + : ROLLBACK TRANSACTION + { $$ = new yy.RollbackTransaction(); } + ; + +BeginTransaction + : BEGIN TRANSACTION + { $$ = new yy.BeginTransaction(); } + ; + +/* +Store + : STORE + { $$ = new yy.Store(); } + | STORE Literal + { $$ = new yy.Store({databaseid: $2}); } + ; + +Restore + : RESTORE + { $$ = new yy.Restore(); } + | RESTORE Literal + { $$ = new yy.Restore({databaseid: $2}); } + ; +*/ + +If + : +/* IF Expression AStatement + { $$ = new yy.If({expression:$2,thenstat:$3}); + if($3.exists) $$.exists = $3.exists; + if($3.queries) $$.queries = $3.queries; + } + | + +*/ + IF Expression AStatement ElseStatement + { $$ = new yy.If({expression:$2,thenstat:$3, elsestat:$4}); + if($3.exists) $$.exists = $3.exists; + if($3.queries) $$.queries = $3.queries; + } + + | IF Expression AStatement + { + $$ = new yy.If({expression:$2,thenstat:$3}); + if($3.exists) $$.exists = $3.exists; + if($3.queries) $$.queries = $3.queries; + } + ; + +ElseStatement + : ELSE AStatement + {$$ = $2;} + ; + +While + : WHILE Expression AStatement + { $$ = new yy.While({expression:$2,loopstat:$3}); + if($3.exists) $$.exists = $3.exists; + if($3.queries) $$.queries = $3.queries; + } + ; + +Continue + : CONTINUE + { $$ = new yy.Continue(); } + ; + +Break + : BREAK + { $$ = new yy.Break(); } + ; + +BeginEnd + : BEGIN Statements END + { $$ = new yy.BeginEnd({statements:$2}); } + ; + +Print + : PRINT ExprList + { $$ = new yy.Print({exprs:$2});} + | PRINT Select + { $$ = new yy.Print({select:$2});} + ; + +Require + : REQUIRE StringValuesList + { $$ = new yy.Require({paths:$2}); } + | REQUIRE PluginsList + { $$ = new yy.Require({plugins:$2}); } + ; + +/* For test plugin system */ + +Plugin + : ECHO {$$ = $1.toUpperCase(); } + | Literal {$$ = $1.toUpperCase(); } + ; + +Echo + : ECHO Expression + { $$ = new yy.Echo({expr:$2}); } + ; + + +StringValuesList + : StringValuesList COMMA StringValue + { $1.push($3); $$ = $1; } + | StringValue + { $$ = [$1]; } + ; + +PluginsList + : PluginsList COMMA Plugin + { $1.push($3); $$ = $1; } + | Plugin + { $$ = [$1]; } + ; + + +Declare + : DECLARE DeclaresList + { $$ = new yy.Declare({declares:$2}); } + ; + +DeclaresList + : DeclareItem + { $$ = [$1]; } + | DeclaresList COMMA DeclareItem + { $1.push($3); $$ = $1; } + ; + +DeclareItem + : AT Literal ColumnType + { $$ = {variable: $2}; yy.extend($$,$3); } + | AT Literal AS ColumnType + { $$ = {variable: $2}; yy.extend($$,$4); } + | AT Literal ColumnType EQ Expression + { $$ = {variable: $2, expression:$5}; yy.extend($$,$3);} + | AT Literal AS ColumnType EQ Expression + { $$ = {variable: $2, expression:$6}; yy.extend($$,$4);} + ; + +TruncateTable + : TRUNCATE TABLE Table + { $$ = new yy.TruncateTable({table:$3});} + ; + +Merge + : MERGE MergeInto MergeUsing MergeOn MergeMatchedList OutputClause + { + $$ = new yy.Merge(); yy.extend($$,$2); yy.extend($$,$3); + yy.extend($$,$4); + yy.extend($$,{matches:$5});yy.extend($$,$6); + } + ; + +MergeInto + : FromTable + { $$ = {into: $1}; } + | INTO FromTable + { $$ = {into: $2}; } + ; + +MergeUsing + : USING FromTable + { $$ = {using: $2}; } + ; + +MergeOn + : ON Expression + { $$ = {on:$2}; } + ; + +MergeMatchedList + : MergeMatchedList MergeMatched + { $$ = $1; $$.push($2); } + | MergeMatchedList MergeNotMatched + { $$ = $1; $$.push($2); } + | MergeMatched + { $$ = [$1]; } + | MergeNotMatched + { $$ = [$1]; } + ; + +MergeMatched + : WHEN MATCHED THEN MergeMatchedAction + { $$ = {matched:true, action:$4} } + | WHEN MATCHED AND Expression THEN MergeMatchedAction + { $$ = {matched:true, expr: $4, action:$6} } + ; + +MergeMatchedAction + : DELETE + { $$ = {delete:true}; } + | UPDATE SET SetColumnsList + { $$ = {update:$3}; } + ; + +MergeNotMatched + : WHEN NOT MATCHED THEN MergeNotMatchedAction + { $$ = {matched:false, bytarget: true, action:$5} } + | WHEN NOT MATCHED BY TARGET THEN MergeNotMatchedAction + { $$ = {matched:false, bytarget: true, action:$7} } + | WHEN NOT MATCHED AND Expression THEN MergeNotMatchedAction + { $$ = {matched:false, bytarget: true, expr:$5, action:$7} } + | WHEN NOT MATCHED BY TARGET AND Expression THEN MergeNotMatchedAction + { $$ = {matched:false, bytarget: true, expr:$7, action:$9} } + | WHEN NOT MATCHED BY SOURCE THEN MergeNotMatchedAction + { $$ = {matched:false, bysource: true, action:$7} } + | WHEN NOT MATCHED BY SOURCE AND Expression THEN MergeMatchedAction + { $$ = {matched:false, bysource: true, expr:$7, action:$9} } + ; + +MergeNotMatchedAction + : INSERT VALUES ValuesListsList + { $$ = {insert:true, values:$3}; } + | INSERT LPAR ColumnsList RPAR VALUES ValuesListsList + { $$ = {insert:true, values:$6, columns:$3}; } + | INSERT DEFAULT VALUES + { $$ = {insert:true, defaultvalues:true}; } + | INSERT LPAR ColumnsList RPAR DEFAULT VALUES + { $$ = {insert:true, defaultvalues:true, columns:$3}; } + ; + +OutputClause + : + | OUTPUT ResultColumns + { $$ = {output:{columns:$2}} } + | OUTPUT ResultColumns INTO AtDollar Literal + { $$ = {output:{columns:$2, intovar: $5, method:$4}} } + | OUTPUT ResultColumns INTO Table + { $$ = {output:{columns:$2, intotable: $4}} } + | OUTPUT ResultColumns INTO Table LPAR ColumnsList RPAR + { $$ = {output:{columns:$2, intotable: $4, intocolumns:$6}} } + ; + +/* +CreateVertex + : CREATE VERTEX + { $$ = new yy.CreateVertex(); } + | CREATE VERTEX SET SetColumnsList + { $$ = new yy.CreateVertex({set: $4}); } + | CREATE VERTEX Literal SET SetColumnsList + { $$ = new yy.CreateVertex({class:$3, set: $5}); } + | CREATE VERTEX CONTENT ExprList + { $$ = new yy.CreateVertex({content: $4}); } + | CREATE VERTEX Literal CONTENT ExprList + { $$ = new yy.CreateVertex({class:$3, content: $5}); } + | CREATE VERTEX Literal Select + { $$ = new yy.CreateVertex({class:$3, select:$4}); } + | CREATE VERTEX Select + { $$ = new yy.CreateVertex({select:$4}); } + ; +*/ +CreateVertex + : CREATE VERTEX Literal? SharpValue? StringValue? CreateVertexSet + { + $$ = new yy.CreateVertex({class:$3,sharp:$4, name:$5}); + yy.extend($$,$6); + } + ; + +SharpValue + : SHARP Literal + { $$ = $2; } + ; + +CreateVertexSet + : + {$$ = undefined; } + | SET SetColumnsList + { $$ = {sets:$2}; } + | CONTENT ExprList + { $$ = {content:$2}; } + | Select + { $$ = {select:$1}; } + ; + +CreateEdge + : CREATE EDGE StringValue? FROM Expression TO Expression CreateVertexSet + { + $$ = new yy.CreateEdge({from:$5,to:$7,name:$3}); + yy.extend($$,$8); + } +/* | CREATE EDGE StringValue? FROM Expression TO Expression + { + $$ = new yy.CreateEdge({from:$5,to:$7,name:$3}); + } +*/ ; + + +/* +CreateEdge + : CREATE EDGE Literal? + FROM Expression + TO Expression + (SET SetColumnsList | CONTENT Expression)? + + { + $$ = new yy.CreateEdge({class:$3, from:$5, to:$7}); + if(typeof $8 != 'undefined') { + $$.type = $8; + $$.expre = $9; + } + } + + ; +*/ + +CreateGraph + : CREATE GRAPH GraphList + { $$ = new yy.CreateGraph({graph:$3}); } + | CREATE GRAPH FROM Expression + { $$ = new yy.CreateGraph({from:$4}); } + ; + +GraphList + : GraphList COMMA GraphVertexEdge + { $$ = $1; $$.push($3); } + | GraphVertexEdge + { $$ = [$1]; } + ; + +GraphVertexEdge + : GraphElement Json? GraphAsClause? + { + $$ = $1; + if($2) $$.json = new yy.Json({value:$2}); + if($3) $$.as = $3; + } + | GraphElementVar GT GraphElement Json? GraphAsClause? GT GraphElementVar + { + $$ = {source:$1, target: $7}; + if($4) $$.json = new yy.Json({value:$4}); + if($5) $$.as = $5; + yy.extend($$,$3); + } + | GraphElementVar GT Json GraphAsClause? GT GraphElementVar + { + $$ = {source:$1, target: $6}; + if($4) $$.json = new yy.Json({value:$3}); + if($5) $$.as = $4; + } + | GraphElementVar GTGT GraphElementVar + { + $$ = {source:$1, target: $3}; + } + | Literal LPAR GraphList RPAR + ; + +GraphElementVar + : GraphElement { $$ = $1; } + | GraphVar { $$ = $1; } + ; + +GraphVar + : AtDollar Literal + { $$ = {vars:$2, method:$1}; } + ; + +GraphAsClause + : AS AtDollar Literal + { $$ = $3; } + ; + +GraphAtClause + : AtDollar Literal + { $$ = $2; } + ; + +GraphElement2 + : Literal? SharpLiteral? STRING? ColonLiteral? + { + var s3 = $3; + $$ = {prop:$1, sharp:$2, name:(typeof s3 == 'undefined')?undefined:s3.substr(1,s3.length-2), class:$4}; + } + ; + +GraphElement + : Literal SharpLiteral? STRING? ColonLiteral? + { + var s3 = $3; + $$ = {prop:$1, sharp:$2, name:(typeof s3 == 'undefined')?undefined:s3.substr(1,s3.length-2), class:$4}; + } + | SharpLiteral STRING? ColonLiteral? + { + var s2 = $2; + $$ = {sharp:$1, name:(typeof s2 == 'undefined')?undefined:s2.substr(1,s2.length-2), class:$3}; + } + | STRING ColonLiteral? + { + var s1 = $1; + $$ = {name:(typeof s1 == 'undefined')?undefined:s1.substr(1,s1.length-2), class:$2}; + } + | ColonLiteral + { + $$ = {class:$1}; + } + ; + + + +ColonLiteral + : COLON Literal + { $$ = $2; } + ; + +SharpLiteral + : SHARP Literal + { $$ = $2; } + | SHARP NUMBER + { $$ = +$2; } + ; + +DeleteVertex + : DELETE VERTEX Expression (WHERE Expression)? + ; + +DeleteEdge + : DELETE EDGE Expression (FROM Expression)? (TO Expression)? (WHERE Expression)? + ; + +AddRule + : Term COLONDASH TermsList + { $$ = new yy.AddRule({left:$1, right:$3}); } + | COLONDASH TermsList + { $$ = new yy.AddRule({right:$2}); } + ; + +TermsList + : TermsList COMMA Term + { $$ = $1; $$.push($3); } + | Term + { $$ = [$1]; } + ; + +Term + : Literal + { $$ = {termid: $1}; } + | Literal LPAR TermsList RPAR + { $$ = {termid:$1, args:$3}; } + ; +Query + : QUESTIONDASH FuncValue + ; + +Call + : CALL FuncValue + { $$ = new yy.ExpressionStatement({expression:$2}); } + ; + +CreateTrigger + : CREATE TRIGGER Literal BeforeAfter InsertDeleteUpdate ON Table AS? AStatement + { + $$ = new yy.CreateTrigger({trigger:$3, when:$4, action:$5, table:$7, statement:$9}); + if($9.exists) $$.exists = $9.exists; + if($9.queries) $$.queries = $9.queries; + } + | CREATE TRIGGER Literal BeforeAfter InsertDeleteUpdate ON Table Literal + { + $$ = new yy.CreateTrigger({trigger:$3, when:$4, action:$5, table:$7, funcid:$8}); + } + | CREATE TRIGGER Literal ON Table BeforeAfter InsertDeleteUpdate AS? AStatement + { + $$ = new yy.CreateTrigger({trigger:$3, when:$5, action:$6, table:$4, statement:$9}); + if($9.exists) $$.exists = $9.exists; + if($9.queries) $$.queries = $9.queries; + } + ; + +BeforeAfter + : + { $$ = 'AFTER'; } + | FOR + { $$ = 'AFTER'; } + | BEFORE + { $$ = 'BEFORE'; } + | AFTER + { $$ = 'AFTER'; } + | INSTEAD OF + { $$ = 'INSTEADOF'; } + ; + +InsertDeleteUpdate + : INSERT { $$ = 'INSERT'; } + | DELETE { $$ = 'DELETE'; } + | UPDATE { $$ = 'UPDATE'; } + ; + +DropTrigger + : DROP TRIGGER Literal + { $$ = new yy.DropTrigger({trigger:$3}); } + ; + +Reindex + : REINDEX Literal + { $$ = new yy.Reindex({indexid:$2});} + ; + +NonReserved + : A|ABSENT|ABSOLUTE|ACCORDING|ACTION|ADA|ADD|ADMIN|AFTER|ALWAYS|ASC|ASSERTION|ASSIGNMENT|ATTRIBUTE|ATTRIBUTES|BASE64|BEFORE|BERNOULLI|BLOCKED|BOM|BREADTH|C|CASCADE|CATALOG|CATALOG_NAME|CHAIN|CHARACTERISTICS|CHARACTERS|CHARACTER_SET_CATALOG|CHARACTER_SET_NAME|CHARACTER_SET_SCHEMA|CLASS_ORIGIN|COBOL|COLLATION|COLLATION_CATALOG|COLLATION_NAME|COLLATION_SCHEMA|COLUMNS|COLUMN_NAME|COMMAND_FUNCTION|COMMAND_FUNCTION_CODE|COMMITTED|CONDITION_NUMBER|CONNECTION|CONNECTION_NAME|CONSTRAINTS|CONSTRAINT_CATALOG|CONSTRAINT_NAME|CONSTRAINT_SCHEMA|CONSTRUCTOR|CONTENT|CONTINUE|CONTROL|CURSOR_NAME|DATA|DATETIME_INTERVAL_CODE|DATETIME_INTERVAL_PRECISION|DB|DEFAULTS|DEFERRABLE|DEFERRED|DEFINED|DEFINER|DEGREE|DEPTH|DERIVED|DESC|DESCRIPTOR|DIAGNOSTICS|DISPATCH|DOCUMENT|DOMAIN|DYNAMIC_FUNCTION|DYNAMIC_FUNCTION_CODE|EMPTY|ENCODING|ENFORCED|EXCLUDE|EXCLUDING|EXPRESSION|FILE|FINAL|FIRST|FLAG|FOLLOWING|FORTRAN|FOUND|FS|G|GENERAL|GENERATED|GO|GOTO|GRANTED|HEX|HIERARCHY|ID|IGNORE|IMMEDIATE|IMMEDIATELY|IMPLEMENTATION|INCLUDING|INCREMENT|INDENT|INITIALLY|INPUT|INSTANCE|INSTANTIABLE|INSTEAD|INTEGRITY|INVOKER|ISOLATION|K|KEY|KEY_MEMBER|KEY_TYPE|LAST|LENGTH|LEVEL|LIBRARY|LIMIT|LINK|LOCATION|LOCATOR|M|MAP|MAPPING|MATCHED|MAXVALUE|MESSAGE_LENGTH|MESSAGE_OCTET_LENGTH|MESSAGE_TEXT|MINVALUE|MORE|MUMPS|NAME|NAMES|NAMESPACE|NESTING|NEXT|NFC|NFD|NFKC|NFKD|NIL|NORMALIZED|NULLABLE|NULLS|NUMBER|OBJECT|OCTETS|OFF|OPTION|OPTIONS|ORDERING|ORDINALITY|OTHERS|OUTPUT|OVERRIDING|P|PAD|PARAMETER_MODE|PARAMETER_NAME|PARAMETER_ORDINAL_POSITION|PARAMETER_SPECIFIC_CATALOG|PARAMETER_SPECIFIC_NAME|PARAMETER_SPECIFIC_SCHEMA|PARTIAL|PASCAL|PASSING|PASSTHROUGH|PATH|PERMISSION|PLACING|PLI|PRECEDING|PRESERVE|PRIOR|PRIVILEGES|PUBLIC|READ|RECOVERY|RELATIVE|REPEATABLE|REQUIRING|RESPECT|RESTART|RESTORE|RESTRICT|RETURNED_CARDINALITY|RETURNED_LENGTH|RETURNED_OCTET_LENGTH|RETURNED_SQLSTATE|RETURNING|ROLE|ROUTINE|ROUTINE_CATALOG|ROUTINE_NAME|ROUTINE_SCHEMA|ROW_COUNT|SCALE|SCHEMA|SCHEMA_NAME|SCOPE_CATALOG|SCOPE_NAME|SCOPE_SCHEMA|SECTION|SECURITY|SELECTIVE|SELF|SEQUENCE|SERIALIZABLE|SERVER|SERVER_NAME|SESSION|SETS|SIMPLE|SIZE|SOURCE|SPACE|SPECIFIC_NAME|STANDALONE|STATE|STATEMENT|STRIP|STRUCTURE|STYLE|SUBCLASS_ORIGIN|T|TABLE_NAME|TEMPORARY|TIES|TOKEN|TOP_LEVEL_COUNT|TRANSACTION|TRANSACTIONS_COMMITTED|TRANSACTIONS_ROLLED_BACK|TRANSACTION_ACTIVE|TRANSFORM|TRANSFORMS|TRIGGER_CATALOG|TRIGGER_NAME|TRIGGER_SCHEMA|TYPE|UNBOUNDED|UNCOMMITTED|UNDER|UNLINK|UNNAMED|UNTYPED|URI|USAGE|USER_DEFINED_TYPE_CATALOG|USER_DEFINED_TYPE_CODE|USER_DEFINED_TYPE_NAME|USER_DEFINED_TYPE_SCHEMA|VALID|VERSION|VIEW|WHITESPACE|WORK|WRAPPER|WRITE|XMLDECLARATION|XMLSCHEMA|YES|ZONE; + +%% + +// from https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html +// JSON.stringify([].slice.call(document.querySelectorAll('tr')).filter(x => x.children.length == 5 && x.children[2].innerText == 'reserved').map(x => x.children[0].innerText)) + +var nonReserved = ["A","ABSENT","ABSOLUTE","ACCORDING","ACTION","ADA","ADD","ADMIN","AFTER","ALWAYS","ASC","ASSERTION","ASSIGNMENT","ATTRIBUTE","ATTRIBUTES","BASE64","BEFORE","BERNOULLI","BLOCKED","BOM","BREADTH","C","CASCADE","CATALOG","CATALOG_NAME","CHAIN","CHARACTERISTICS","CHARACTERS","CHARACTER_SET_CATALOG","CHARACTER_SET_NAME","CHARACTER_SET_SCHEMA","CLASS_ORIGIN","COBOL","COLLATION","COLLATION_CATALOG","COLLATION_NAME","COLLATION_SCHEMA","COLUMNS","COLUMN_NAME","COMMAND_FUNCTION","COMMAND_FUNCTION_CODE","COMMITTED","CONDITION_NUMBER","CONNECTION","CONNECTION_NAME","CONSTRAINTS","CONSTRAINT_CATALOG","CONSTRAINT_NAME","CONSTRAINT_SCHEMA","CONSTRUCTOR","CONTENT","CONTINUE","CONTROL","CURSOR_NAME","DATA","DATETIME_INTERVAL_CODE","DATETIME_INTERVAL_PRECISION","DB","DEFAULTS","DEFERRABLE","DEFERRED","DEFINED","DEFINER","DEGREE","DEPTH","DERIVED","DESC","DESCRIPTOR","DIAGNOSTICS","DISPATCH","DOCUMENT","DOMAIN","DYNAMIC_FUNCTION","DYNAMIC_FUNCTION_CODE","EMPTY","ENCODING","ENFORCED","EXCLUDE","EXCLUDING","EXPRESSION","FILE","FINAL","FIRST","FLAG","FOLLOWING","FORTRAN","FOUND","FS","G","GENERAL","GENERATED","GO","GOTO","GRANTED","HEX","HIERARCHY","ID","IGNORE","IMMEDIATE","IMMEDIATELY","IMPLEMENTATION","INCLUDING","INCREMENT","INDENT","INITIALLY","INPUT","INSTANCE","INSTANTIABLE","INSTEAD","INTEGRITY","INVOKER","ISOLATION","K","KEY","KEY_MEMBER","KEY_TYPE","LAST","LENGTH","LEVEL","LIBRARY","LIMIT","LINK","LOCATION","LOCATOR","M","MAP","MAPPING","MATCHED","MAXVALUE","MESSAGE_LENGTH","MESSAGE_OCTET_LENGTH","MESSAGE_TEXT","MINVALUE","MORE","MUMPS","NAME","NAMES","NAMESPACE","NESTING","NEXT","NFC","NFD","NFKC","NFKD","NIL","NORMALIZED","NULLABLE","NULLS","NUMBER","OBJECT","OCTETS","OFF","OPTION","OPTIONS","ORDERING","ORDINALITY","OTHERS","OUTPUT","OVERRIDING","P","PAD","PARAMETER_MODE","PARAMETER_NAME","PARAMETER_ORDINAL_POSITION","PARAMETER_SPECIFIC_CATALOG","PARAMETER_SPECIFIC_NAME","PARAMETER_SPECIFIC_SCHEMA","PARTIAL","PASCAL","PASSING","PASSTHROUGH","PATH","PERMISSION","PLACING","PLI","PRECEDING","PRESERVE","PRIOR","PRIVILEGES","PUBLIC","READ","RECOVERY","RELATIVE","REPEATABLE","REQUIRING","RESPECT","RESTART","RESTORE","RESTRICT","RETURNED_CARDINALITY","RETURNED_LENGTH","RETURNED_OCTET_LENGTH","RETURNED_SQLSTATE","RETURNING","ROLE","ROUTINE","ROUTINE_CATALOG","ROUTINE_NAME","ROUTINE_SCHEMA","ROW_COUNT","SCALE","SCHEMA","SCHEMA_NAME","SCOPE_CATALOG","SCOPE_NAME","SCOPE_SCHEMA","SECTION","SECURITY","SELECTIVE","SELF","SEQUENCE","SERIALIZABLE","SERVER","SERVER_NAME","SESSION","SETS","SIMPLE","SIZE","SOURCE","SPACE","SPECIFIC_NAME","STANDALONE","STATE","STATEMENT","STRIP","STRUCTURE","STYLE","SUBCLASS_ORIGIN","T","TABLE_NAME","TEMPORARY","TIES","TOKEN","TOP_LEVEL_COUNT","TRANSACTION","TRANSACTIONS_COMMITTED","TRANSACTIONS_ROLLED_BACK","TRANSACTION_ACTIVE","TRANSFORM","TRANSFORMS","TRIGGER_CATALOG","TRIGGER_NAME","TRIGGER_SCHEMA","TYPE","UNBOUNDED","UNCOMMITTED","UNDER","UNLINK","UNNAMED","UNTYPED","URI","USAGE","USER_DEFINED_TYPE_CATALOG","USER_DEFINED_TYPE_CODE","USER_DEFINED_TYPE_NAME","USER_DEFINED_TYPE_SCHEMA","VALID","VERSION","VIEW","WHITESPACE","WORK","WRAPPER","WRITE","XMLDECLARATION","XMLSCHEMA","YES","ZONE"] + +parser.parseError = function(str, hash) { + if (hash.expected && hash.expected.indexOf("'LITERAL'") > -1 && /[a-zA-Z_][a-zA-Z_0-9]*/.test(hash.token) && nonReserved.indexOf(hash.token) > -1) { + return + } + throw new SyntaxError(str) +} diff --git a/src/alasqlparser.js b/src/alasqlparser.js index 1299a6426..dc90a0edd 100755 --- a/src/alasqlparser.js +++ b/src/alasqlparser.js @@ -84,9 +84,9 @@ performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* actio var $0 = $$.length - 1; switch (yystate) { case 1: - - if (alasql.options.casesensitive) this.$ = $$[$0]; - else this.$ = $$[$0].toLowerCase(); + + if (alasql.options.casesensitive) this.$ = $$[$0]; + else this.$ = $$[$0].toLowerCase(); break; case 2: @@ -117,14 +117,14 @@ case 11: this.$ = $$[$0]; $$[$0].explain = true; break; case 12: - - this.$ = $$[$0]; - - // TODO combine exists and queries - if(yy.exists) this.$.exists = yy.exists; - delete yy.exists; - if(yy.queries) this.$.queries = yy.queries; - delete yy.queries; + + this.$ = $$[$0]; + + // TODO combine exists and queries + if(yy.exists) this.$.exists = yy.exists; + delete yy.exists; + if(yy.queries) this.$.queries = yy.queries; + delete yy.queries; break; case 13: case 162: case 172: case 237: case 238: case 240: case 248: case 250: case 259: case 270: case 273: case 383: case 499: case 509: case 511: case 523: case 529: case 530: case 575: @@ -140,20 +140,20 @@ case 71: this.$ = {name:$$[$0-4], select:$$[$0-1]}; break; case 72: - - yy.extend(this.$,$$[$0-9]); yy.extend(this.$,$$[$0-8]); yy.extend(this.$,$$[$0-7]); yy.extend(this.$,$$[$0-6]); - yy.extend(this.$,$$[$0-5]); yy.extend(this.$,$$[$0-4]);yy.extend(this.$,$$[$0-3]); - yy.extend(this.$,$$[$0-2]); yy.extend(this.$,$$[$0-1]); yy.extend(this.$,$$[$0]); - this.$ = $$[$0-9]; - if(yy.exists) this.$.exists = yy.exists.slice(); -/* if(yy.queries) this.$.queries = yy.queries; - delete yy.queries; + + yy.extend(this.$,$$[$0-9]); yy.extend(this.$,$$[$0-8]); yy.extend(this.$,$$[$0-7]); yy.extend(this.$,$$[$0-6]); + yy.extend(this.$,$$[$0-5]); yy.extend(this.$,$$[$0-4]);yy.extend(this.$,$$[$0-3]); + yy.extend(this.$,$$[$0-2]); yy.extend(this.$,$$[$0-1]); yy.extend(this.$,$$[$0]); + this.$ = $$[$0-9]; + if(yy.exists) this.$.exists = yy.exists.slice(); +/* if(yy.queries) this.$.queries = yy.queries; + delete yy.queries; */ break; case 73: - - this.$ = new yy.Search({selectors:$$[$0-2], from:$$[$0]}); - yy.extend(this.$,$$[$0-1]); + + this.$ = new yy.Search({selectors:$$[$0-2], from:$$[$0]}); + yy.extend(this.$,$$[$0-1]); break; case 74: @@ -190,10 +190,10 @@ case 91: this.$ = {srchid:"ORDERBY", args: $$[$0-1]}; break; case 92: - - var dir = $$[$0-1]; - if(!dir) dir = 'ASC'; - this.$ = {srchid:"ORDERBY", args: [{expression: new yy.Column({columnid:'_'}), direction:dir}]}; + + var dir = $$[$0-1]; + if(!dir) dir = 'ASC'; + this.$ = {srchid:"ORDERBY", args: [{expression: new yy.Column({columnid:'_'}), direction:dir}]}; break; case 93: @@ -359,12 +359,12 @@ case 150: this.$ = new yy.Select({ columns:$$[$0], all:true }); yy.extend(this.$, $$[$0-3]);yy.extend(this.$, $$[$0-1]); break; case 151: - - if(!$$[$0]) { - this.$ = new yy.Select({columns:[new yy.Column({columnid:'_',})], modifier:'COLUMN'}); - } else { - this.$ = new yy.Select({ columns:$$[$0] }); yy.extend(this.$, $$[$0-2]);yy.extend(this.$, $$[$0-1]); - } + + if(!$$[$0]) { + this.$ = new yy.Select({columns:[new yy.Column({columnid:'_',})], modifier:'COLUMN'}); + } else { + this.$ = new yy.Select({ columns:$$[$0] }); yy.extend(this.$, $$[$0-2]);yy.extend(this.$, $$[$0-1]); + } break; case 152: @@ -404,18 +404,18 @@ case 164: case 165: case 166: case 167: this.$ = {into: $$[$0]} break; case 168: - - var s = $$[$0]; - s = s.substr(1,s.length-2); - var x3 = s.substr(-3).toUpperCase(); - var x4 = s.substr(-4).toUpperCase(); - if(s[0] == '#') { - this.$ = {into: new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; - } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { - this.$ = {into: new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; - } else if(x4=='XLSX' || x4 == 'JSON') { - this.$ = {into: new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; - } + + var s = $$[$0]; + s = s.substr(1,s.length-2); + var x3 = s.substr(-3).toUpperCase(); + var x4 = s.substr(-4).toUpperCase(); + if(s[0] == '#') { + this.$ = {into: new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; + } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { + this.$ = {into: new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; + } else if(x4=='XLSX' || x4 == 'JSON') { + this.$ = {into: new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]})}; + } break; case 169: @@ -476,31 +476,31 @@ case 193: this.$ = {inserted:true}; break; case 200: - - var s = $$[$0]; - s = s.substr(1,s.length-2); - var x3 = s.substr(-3).toUpperCase(); - var x4 = s.substr(-4).toUpperCase(); - var r; - if(s[0] == '#') { - r = new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); - } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { - r = new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); - } else if(x4=='XLSX' || x4 == 'JSON') { - r = new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); - } else { - throw new Error('Unknown string in FROM clause'); - }; - this.$ = r; + + var s = $$[$0]; + s = s.substr(1,s.length-2); + var x3 = s.substr(-3).toUpperCase(); + var x4 = s.substr(-4).toUpperCase(); + var r; + if(s[0] == '#') { + r = new yy.FuncValue({funcid: 'HTML', args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); + } else if(x3=='XLS' || x3 == 'CSV' || x3=='TAB') { + r = new yy.FuncValue({funcid: x3, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); + } else if(x4=='XLSX' || x4 == 'JSON') { + r = new yy.FuncValue({funcid: x4, args:[new yy.StringValue({value: s}), new yy.Json({value:{headers:true}})]}); + } else { + throw new Error('Unknown string in FROM clause'); + }; + this.$ = r; break; case 201: - - if($$[$0-2] == 'INFORMATION_SCHEMA') { - this.$ = new yy.FuncValue({funcid: $$[$0-2], args:[new yy.StringValue({value:$$[$0]})]}); - } else { - this.$ = new yy.Table({databaseid: $$[$0-2], tableid:$$[$0]}); - } + + if($$[$0-2] == 'INFORMATION_SCHEMA') { + this.$ = new yy.FuncValue({funcid: $$[$0-2], args:[new yy.StringValue({value:$$[$0]})]}); + } else { + this.$ = new yy.Table({databaseid: $$[$0-2], tableid:$$[$0]}); + } break; case 202: @@ -699,11 +699,11 @@ case 306: this.$ = new yy.Json({value:$$[$0]}); break; case 309: case 310: case 311: - - if(!yy.queries) yy.queries = []; - yy.queries.push($$[$0-1]); - $$[$0-1].queriesidx = yy.queries.length; - this.$ = $$[$0-1]; + + if(!yy.queries) yy.queries = []; + yy.queries.push($$[$0-1]); + $$[$0-1].queriesidx = yy.queries.length; + this.$ = $$[$0-1]; break; case 312: @@ -749,19 +749,19 @@ case 331: this.$ = new yy.FuncValue({funcid:'CURRENT_DATE'}); break; case 332: - - if($$[$0-2].length > 1 && ($$[$0-4].toUpperCase() == 'MAX' || $$[$0-4].toUpperCase() == 'MIN')) { - this.$ = new yy.FuncValue({funcid:$$[$0-4],args:$$[$0-2]}); - } else { - this.$ = new yy.AggrValue({aggregatorid: $$[$0-4].toUpperCase(), expression: $$[$0-2].pop(), over:$$[$0]}); - } + + if($$[$0-2].length > 1 && ($$[$0-4].toUpperCase() == 'MAX' || $$[$0-4].toUpperCase() == 'MIN')) { + this.$ = new yy.FuncValue({funcid:$$[$0-4],args:$$[$0-2]}); + } else { + this.$ = new yy.AggrValue({aggregatorid: $$[$0-4].toUpperCase(), expression: $$[$0-2].pop(), over:$$[$0]}); + } break; case 333: this.$ = new yy.AggrValue({aggregatorid: $$[$0-5].toUpperCase(), expression: $$[$0-2], distinct:true, over:$$[$0]}); break; case 334: - this.$ = new yy.AggrValue({aggregatorid: $$[$0-5].toUpperCase(), expression: $$[$0-2], + this.$ = new yy.AggrValue({aggregatorid: $$[$0-5].toUpperCase(), expression: $$[$0-2], over:$$[$0]}); break; case 336: case 337: @@ -807,17 +807,17 @@ case 350: this.$ = "ARRAY"; break; case 351: - - var funcid = $$[$0-4]; - var exprlist = $$[$0-1]; - if(exprlist.length > 1 && (funcid.toUpperCase() == 'MIN' || funcid.toUpperCase() == 'MAX')) { - this.$ = new yy.FuncValue({funcid: funcid, args: exprlist}); - } else if(alasql.aggr[$$[$0-4]]) { - this.$ = new yy.AggrValue({aggregatorid: 'REDUCE', - funcid: funcid, expression: exprlist.pop(),distinct:($$[$0-2]=='DISTINCT') }); - } else { - this.$ = new yy.FuncValue({funcid: funcid, args: exprlist}); - }; + + var funcid = $$[$0-4]; + var exprlist = $$[$0-1]; + if(exprlist.length > 1 && (funcid.toUpperCase() == 'MIN' || funcid.toUpperCase() == 'MAX')) { + this.$ = new yy.FuncValue({funcid: funcid, args: exprlist}); + } else if(alasql.aggr[$$[$0-4]]) { + this.$ = new yy.AggrValue({aggregatorid: 'REDUCE', + funcid: funcid, expression: exprlist.pop(),distinct:($$[$0-2]=='DISTINCT') }); + } else { + this.$ = new yy.FuncValue({funcid: funcid, args: exprlist}); + }; break; case 352: case 355: @@ -872,10 +872,10 @@ case 370: this.$ = new yy.VarValue({variable:$$[$0]}); break; case 371: - - if(!yy.exists) yy.exists = []; - this.$ = new yy.ExistsValue({value:$$[$0-1], existsidx:yy.exists.length}); - yy.exists.push($$[$0-1]); + + if(!yy.exists) yy.exists = []; + this.$ = new yy.ExistsValue({value:$$[$0-1], existsidx:yy.exists.length}); + yy.exists.push($$[$0-1]); break; case 372: @@ -885,15 +885,15 @@ case 373: case 374: this.$ = new yy.ParamValue({param: $$[$0]}); break; case 375: - - if(typeof yy.question == 'undefined') yy.question = 0; - this.$ = new yy.ParamValue({param: yy.question++}); + + if(typeof yy.question == 'undefined') yy.question = 0; + this.$ = new yy.ParamValue({param: yy.question++}); break; case 376: - - if(typeof yy.question == 'undefined') yy.question = 0; - this.$ = new yy.ParamValue({param: yy.question++, array:true}); + + if(typeof yy.question == 'undefined') yy.question = 0; + this.$ = new yy.ParamValue({param: yy.question++, array:true}); break; case 377: @@ -1002,46 +1002,46 @@ case 419: this.$ = new yy.Op({left:$$[$0-2], op:'!===' , right:$$[$0]}); break; case 420: - - if(!yy.queries) yy.queries = []; - this.$ = new yy.Op({left:$$[$0-5], op:$$[$0-4] , allsome:$$[$0-3], right:$$[$0-1], queriesidx: yy.queries.length}); - yy.queries.push($$[$0-1]); + + if(!yy.queries) yy.queries = []; + this.$ = new yy.Op({left:$$[$0-5], op:$$[$0-4] , allsome:$$[$0-3], right:$$[$0-1], queriesidx: yy.queries.length}); + yy.queries.push($$[$0-1]); break; case 421: - - this.$ = new yy.Op({left:$$[$0-5], op:$$[$0-4] , allsome:$$[$0-3], right:$$[$0-1]}); + + this.$ = new yy.Op({left:$$[$0-5], op:$$[$0-4] , allsome:$$[$0-3], right:$$[$0-1]}); break; case 422: - - if($$[$0-2].op == 'BETWEEN1') { - - if($$[$0-2].left.op == 'AND') { - this.$ = new yy.Op({left:$$[$0-2].left.left,op:'AND',right: - new yy.Op({left:$$[$0-2].left.right, op:'BETWEEN', - right1:$$[$0-2].right, right2:$$[$0]}) - }); - } else { - this.$ = new yy.Op({left:$$[$0-2].left, op:'BETWEEN', - right1:$$[$0-2].right, right2:$$[$0]}); - } - - } else if($$[$0-2].op == 'NOT BETWEEN1') { - if($$[$0-2].left.op == 'AND') { - this.$ = new yy.Op({left:$$[$0-2].left.left,op:'AND',right: - new yy.Op({left:$$[$0-2].left.right, op:'NOT BETWEEN', - right1:$$[$0-2].right, right2:$$[$0]}) - }); - } else { - this.$ = new yy.Op({left:$$[$0-2].left, op:'NOT BETWEEN', - right1:$$[$0-2].right, right2:$$[$0]}); - } - } else { - this.$ = new yy.Op({left:$$[$0-2], op:'AND', right:$$[$0]}); - } - - + + if($$[$0-2].op == 'BETWEEN1') { + + if($$[$0-2].left.op == 'AND') { + this.$ = new yy.Op({left:$$[$0-2].left.left,op:'AND',right: + new yy.Op({left:$$[$0-2].left.right, op:'BETWEEN', + right1:$$[$0-2].right, right2:$$[$0]}) + }); + } else { + this.$ = new yy.Op({left:$$[$0-2].left, op:'BETWEEN', + right1:$$[$0-2].right, right2:$$[$0]}); + } + + } else if($$[$0-2].op == 'NOT BETWEEN1') { + if($$[$0-2].left.op == 'AND') { + this.$ = new yy.Op({left:$$[$0-2].left.left,op:'AND',right: + new yy.Op({left:$$[$0-2].left.right, op:'NOT BETWEEN', + right1:$$[$0-2].right, right2:$$[$0]}) + }); + } else { + this.$ = new yy.Op({left:$$[$0-2].left, op:'NOT BETWEEN', + right1:$$[$0-2].right, right2:$$[$0]}); + } + } else { + this.$ = new yy.Op({left:$$[$0-2], op:'AND', right:$$[$0]}); + } + + break; case 423: @@ -1066,17 +1066,17 @@ case 429: this.$ = new yy.UniOp({right: $$[$0-1]}); break; case 430: - - if(!yy.queries) yy.queries = []; - this.$ = new yy.Op({left: $$[$0-4], op:'IN', right:$$[$0-1], queriesidx: yy.queries.length}); - yy.queries.push($$[$0-1]); + + if(!yy.queries) yy.queries = []; + this.$ = new yy.Op({left: $$[$0-4], op:'IN', right:$$[$0-1], queriesidx: yy.queries.length}); + yy.queries.push($$[$0-1]); break; case 431: - - if(!yy.queries) yy.queries = []; - this.$ = new yy.Op({left: $$[$0-5], op:'NOT IN', right:$$[$0-1], queriesidx: yy.queries.length}); - yy.queries.push($$[$0-1]); + + if(!yy.queries) yy.queries = []; + this.$ = new yy.Op({left: $$[$0-5], op:'NOT IN', right:$$[$0-1], queriesidx: yy.queries.length}); + yy.queries.push($$[$0-1]); break; case 432: @@ -1098,39 +1098,39 @@ case 437: case 439: this.$ = new yy.Op({left: $$[$0-3], op:'NOT IN', right:$$[$0]}); break; case 440: - -/* var expr = $$[$0]; - if(expr.left && expr.left.op == 'AND') { - this.$ = new yy.Op({left:new yy.Op({left:$$[$0-2], op:'BETWEEN', right:expr.left}), op:'AND', right:expr.right }); - } else { -*/ - this.$ = new yy.Op({left:$$[$0-2], op:'BETWEEN1', right:$$[$0] }); -// } + +/* var expr = $$[$0]; + if(expr.left && expr.left.op == 'AND') { + this.$ = new yy.Op({left:new yy.Op({left:$$[$0-2], op:'BETWEEN', right:expr.left}), op:'AND', right:expr.right }); + } else { +*/ + this.$ = new yy.Op({left:$$[$0-2], op:'BETWEEN1', right:$$[$0] }); +// } break; case 441: - -// var expr = $$[$0]; -// if(expr.left && expr.left.op == 'AND') { -// this.$ = new yy.Op({left:new yy.Op({left:$$[$0-2], op:'NOT BETWEEN', right:expr.left}), op:'AND', right:expr.right }); -// } else { - this.$ = new yy.Op({left:$$[$0-2], op:'NOT BETWEEN1', right:$$[$0] }); -// } + +// var expr = $$[$0]; +// if(expr.left && expr.left.op == 'AND') { +// this.$ = new yy.Op({left:new yy.Op({left:$$[$0-2], op:'NOT BETWEEN', right:expr.left}), op:'AND', right:expr.right }); +// } else { + this.$ = new yy.Op({left:$$[$0-2], op:'NOT BETWEEN1', right:$$[$0] }); +// } break; case 442: this.$ = new yy.Op({op:'IS' , left:$$[$0-2], right:$$[$0]}); break; case 443: - - this.$ = new yy.Op({ - op:'IS', - left:$$[$0-2], - right: new yy.UniOp({ - op:'NOT', - right:new yy.NullValue({value:undefined}) - }) - }); + + this.$ = new yy.Op({ + op:'IS', + left:$$[$0-2], + right: new yy.UniOp({ + op:'NOT', + right:new yy.NullValue({value:undefined}) + }) + }); break; case 444: @@ -1209,21 +1209,21 @@ case 485: case 486: case 488: case 496: this.$ = $$[$0-2]; $$[$0-2].push($$[$0]) break; case 497: - - this.$ = new yy.CreateTable({table:$$[$0-4]}); - yy.extend(this.$,$$[$0-7]); - yy.extend(this.$,$$[$0-6]); - yy.extend(this.$,$$[$0-5]); - yy.extend(this.$,$$[$0-2]); - yy.extend(this.$,$$[$0]); + + this.$ = new yy.CreateTable({table:$$[$0-4]}); + yy.extend(this.$,$$[$0-7]); + yy.extend(this.$,$$[$0-6]); + yy.extend(this.$,$$[$0-5]); + yy.extend(this.$,$$[$0-2]); + yy.extend(this.$,$$[$0]); break; case 498: - - this.$ = new yy.CreateTable({table:$$[$0]}); - yy.extend(this.$,$$[$0-3]); - yy.extend(this.$,$$[$0-2]); - yy.extend(this.$,$$[$0-1]); + + this.$ = new yy.CreateTable({table:$$[$0]}); + yy.extend(this.$,$$[$0-3]); + yy.extend(this.$,$$[$0-2]); + yy.extend(this.$,$$[$0-1]); break; case 500: @@ -1260,8 +1260,8 @@ case 527: this.$ = {type: 'FOREIGN KEY', columns: $$[$0-5], fktable: $$[$0-2], fkcolumns: $$[$0-1]}; break; case 533: - - this.$ = {type: 'UNIQUE', columns: $$[$0-1], clustered:($$[$0-3]+'').toUpperCase()}; + + this.$ = {type: 'UNIQUE', columns: $$[$0-1], clustered:($$[$0-3]+'').toUpperCase()}; break; case 542: @@ -1295,8 +1295,8 @@ case 553: this.$ = undefined break; case 555: - - yy.extend($$[$0-1],$$[$0]); this.$ = $$[$0-1]; + + yy.extend($$[$0-1],$$[$0]); this.$ = $$[$0-1]; break; case 558: @@ -1384,7 +1384,7 @@ case 590: this.$ = new yy.CreateDatabase({engineid:$$[$0-7].toUpperCase(), databaseid:$$[$0-4], args:$$[$0-2], as:$$[$0] }); yy.extend(this.$,$$[$0-5]); break; case 591: - this.$ = new yy.CreateDatabase({engineid:$$[$0-4].toUpperCase(), + this.$ = new yy.CreateDatabase({engineid:$$[$0-4].toUpperCase(), as:$$[$0], args:[$$[$0-1]] }); yy.extend(this.$,$$[$0-2]); break; case 592: @@ -1451,17 +1451,17 @@ case 615: this.$ = new yy.ShowCreateTable({table: $$[$0-2], databaseid:$$[$0]}); break; case 616: - - this.$ = new yy.CreateTable({table:$$[$0-6],view:true,select:$$[$0-1],viewcolumns:$$[$0-4]}); - yy.extend(this.$,$$[$0-9]); - yy.extend(this.$,$$[$0-7]); + + this.$ = new yy.CreateTable({table:$$[$0-6],view:true,select:$$[$0-1],viewcolumns:$$[$0-4]}); + yy.extend(this.$,$$[$0-9]); + yy.extend(this.$,$$[$0-7]); break; case 617: - - this.$ = new yy.CreateTable({table:$$[$0-3],view:true,select:$$[$0-1]}); - yy.extend(this.$,$$[$0-6]); - yy.extend(this.$,$$[$0-4]); + + this.$ = new yy.CreateTable({table:$$[$0-3],view:true,select:$$[$0-1]}); + yy.extend(this.$,$$[$0-6]); + yy.extend(this.$,$$[$0-4]); break; case 621: @@ -1549,25 +1549,25 @@ case 675: this.$ = new yy.BeginTransaction(); break; case 676: - this.$ = new yy.If({expression:$$[$0-2],thenstat:$$[$0-1], elsestat:$$[$0]}); - if($$[$0-1].exists) this.$.exists = $$[$0-1].exists; - if($$[$0-1].queries) this.$.queries = $$[$0-1].queries; + this.$ = new yy.If({expression:$$[$0-2],thenstat:$$[$0-1], elsestat:$$[$0]}); + if($$[$0-1].exists) this.$.exists = $$[$0-1].exists; + if($$[$0-1].queries) this.$.queries = $$[$0-1].queries; break; case 677: - - this.$ = new yy.If({expression:$$[$0-1],thenstat:$$[$0]}); - if($$[$0].exists) this.$.exists = $$[$0].exists; - if($$[$0].queries) this.$.queries = $$[$0].queries; + + this.$ = new yy.If({expression:$$[$0-1],thenstat:$$[$0]}); + if($$[$0].exists) this.$.exists = $$[$0].exists; + if($$[$0].queries) this.$.queries = $$[$0].queries; break; case 678: this.$ = $$[$0]; break; case 679: - this.$ = new yy.While({expression:$$[$0-1],loopstat:$$[$0]}); - if($$[$0].exists) this.$.exists = $$[$0].exists; - if($$[$0].queries) this.$.queries = $$[$0].queries; + this.$ = new yy.While({expression:$$[$0-1],loopstat:$$[$0]}); + if($$[$0].exists) this.$.exists = $$[$0].exists; + if($$[$0].queries) this.$.queries = $$[$0].queries; break; case 680: @@ -1616,10 +1616,10 @@ case 701: this.$ = new yy.TruncateTable({table:$$[$0]}); break; case 702: - - this.$ = new yy.Merge(); yy.extend(this.$,$$[$0-4]); yy.extend(this.$,$$[$0-3]); - yy.extend(this.$,$$[$0-2]); - yy.extend(this.$,{matches:$$[$0-1]});yy.extend(this.$,$$[$0]); + + this.$ = new yy.Merge(); yy.extend(this.$,$$[$0-4]); yy.extend(this.$,$$[$0-3]); + yy.extend(this.$,$$[$0-2]); + yy.extend(this.$,{matches:$$[$0-1]});yy.extend(this.$,$$[$0]); break; case 703: case 704: @@ -1677,9 +1677,9 @@ case 729: this.$ = {output:{columns:$$[$0-5], intotable: $$[$0-3], intocolumns:$$[$0-1]}} break; case 730: - - this.$ = new yy.CreateVertex({class:$$[$0-3],sharp:$$[$0-2], name:$$[$0-1]}); - yy.extend(this.$,$$[$0]); + + this.$ = new yy.CreateVertex({class:$$[$0-3],sharp:$$[$0-2], name:$$[$0-1]}); + yy.extend(this.$,$$[$0]); break; case 733: @@ -1692,9 +1692,9 @@ case 735: this.$ = {select:$$[$0]}; break; case 736: - - this.$ = new yy.CreateEdge({from:$$[$0-3],to:$$[$0-1],name:$$[$0-5]}); - yy.extend(this.$,$$[$0]); + + this.$ = new yy.CreateEdge({from:$$[$0-3],to:$$[$0-1],name:$$[$0-5]}); + yy.extend(this.$,$$[$0]); break; case 737: @@ -1704,56 +1704,56 @@ case 738: this.$ = new yy.CreateGraph({from:$$[$0]}); break; case 741: - - this.$ = $$[$0-2]; - if($$[$0-1]) this.$.json = new yy.Json({value:$$[$0-1]}); - if($$[$0]) this.$.as = $$[$0]; + + this.$ = $$[$0-2]; + if($$[$0-1]) this.$.json = new yy.Json({value:$$[$0-1]}); + if($$[$0]) this.$.as = $$[$0]; break; case 742: - - this.$ = {source:$$[$0-6], target: $$[$0]}; - if($$[$0-3]) this.$.json = new yy.Json({value:$$[$0-3]}); - if($$[$0-2]) this.$.as = $$[$0-2]; - yy.extend(this.$,$$[$0-4]); + + this.$ = {source:$$[$0-6], target: $$[$0]}; + if($$[$0-3]) this.$.json = new yy.Json({value:$$[$0-3]}); + if($$[$0-2]) this.$.as = $$[$0-2]; + yy.extend(this.$,$$[$0-4]); break; case 743: - - this.$ = {source:$$[$0-5], target: $$[$0]}; - if($$[$0-2]) this.$.json = new yy.Json({value:$$[$0-3]}); - if($$[$0-1]) this.$.as = $$[$0-2]; + + this.$ = {source:$$[$0-5], target: $$[$0]}; + if($$[$0-2]) this.$.json = new yy.Json({value:$$[$0-3]}); + if($$[$0-1]) this.$.as = $$[$0-2]; break; case 744: - - this.$ = {source:$$[$0-2], target: $$[$0]}; + + this.$ = {source:$$[$0-2], target: $$[$0]}; break; case 748: this.$ = {vars:$$[$0], method:$$[$0-1]}; break; case 751: case 752: - - var s3 = $$[$0-1]; - this.$ = {prop:$$[$0-3], sharp:$$[$0-2], name:(typeof s3 == 'undefined')?undefined:s3.substr(1,s3.length-2), class:$$[$0]}; + + var s3 = $$[$0-1]; + this.$ = {prop:$$[$0-3], sharp:$$[$0-2], name:(typeof s3 == 'undefined')?undefined:s3.substr(1,s3.length-2), class:$$[$0]}; break; case 753: - - var s2 = $$[$0-1]; - this.$ = {sharp:$$[$0-2], name:(typeof s2 == 'undefined')?undefined:s2.substr(1,s2.length-2), class:$$[$0]}; + + var s2 = $$[$0-1]; + this.$ = {sharp:$$[$0-2], name:(typeof s2 == 'undefined')?undefined:s2.substr(1,s2.length-2), class:$$[$0]}; break; case 754: - - var s1 = $$[$0-1]; - this.$ = {name:(typeof s1 == 'undefined')?undefined:s1.substr(1,s1.length-2), class:$$[$0]}; + + var s1 = $$[$0-1]; + this.$ = {name:(typeof s1 == 'undefined')?undefined:s1.substr(1,s1.length-2), class:$$[$0]}; break; case 755: - - this.$ = {class:$$[$0]}; + + this.$ = {class:$$[$0]}; break; case 761: @@ -1769,22 +1769,22 @@ case 766: this.$ = {termid:$$[$0-3], args:$$[$0-1]}; break; case 769: - - this.$ = new yy.CreateTrigger({trigger:$$[$0-6], when:$$[$0-5], action:$$[$0-4], table:$$[$0-2], statement:$$[$0]}); - if($$[$0].exists) this.$.exists = $$[$0].exists; - if($$[$0].queries) this.$.queries = $$[$0].queries; + + this.$ = new yy.CreateTrigger({trigger:$$[$0-6], when:$$[$0-5], action:$$[$0-4], table:$$[$0-2], statement:$$[$0]}); + if($$[$0].exists) this.$.exists = $$[$0].exists; + if($$[$0].queries) this.$.queries = $$[$0].queries; break; case 770: - - this.$ = new yy.CreateTrigger({trigger:$$[$0-5], when:$$[$0-4], action:$$[$0-3], table:$$[$0-1], funcid:$$[$0]}); + + this.$ = new yy.CreateTrigger({trigger:$$[$0-5], when:$$[$0-4], action:$$[$0-3], table:$$[$0-1], funcid:$$[$0]}); break; case 771: - - this.$ = new yy.CreateTrigger({trigger:$$[$0-6], when:$$[$0-4], action:$$[$0-3], table:$$[$0-5], statement:$$[$0]}); - if($$[$0].exists) this.$.exists = $$[$0].exists; - if($$[$0].queries) this.$.queries = $$[$0].queries; + + this.$ = new yy.CreateTrigger({trigger:$$[$0-6], when:$$[$0-4], action:$$[$0-3], table:$$[$0-5], statement:$$[$0]}); + if($$[$0].exists) this.$.exists = $$[$0].exists; + if($$[$0].queries) this.$.queries = $$[$0].queries; break; case 772: case 773: case 775: @@ -2071,19 +2071,19 @@ _handle_error: return true; }}; - - -// from https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html -// JSON.stringify([].slice.call(document.querySelectorAll('tr')).filter(x => x.children.length == 5 && x.children[2].innerText == 'reserved').map(x => x.children[0].innerText)) - -var nonReserved = ["A","ABSENT","ABSOLUTE","ACCORDING","ACTION","ADA","ADD","ADMIN","AFTER","ALWAYS","ASC","ASSERTION","ASSIGNMENT","ATTRIBUTE","ATTRIBUTES","BASE64","BEFORE","BERNOULLI","BLOCKED","BOM","BREADTH","C","CASCADE","CATALOG","CATALOG_NAME","CHAIN","CHARACTERISTICS","CHARACTERS","CHARACTER_SET_CATALOG","CHARACTER_SET_NAME","CHARACTER_SET_SCHEMA","CLASS_ORIGIN","COBOL","COLLATION","COLLATION_CATALOG","COLLATION_NAME","COLLATION_SCHEMA","COLUMNS","COLUMN_NAME","COMMAND_FUNCTION","COMMAND_FUNCTION_CODE","COMMITTED","CONDITION_NUMBER","CONNECTION","CONNECTION_NAME","CONSTRAINTS","CONSTRAINT_CATALOG","CONSTRAINT_NAME","CONSTRAINT_SCHEMA","CONSTRUCTOR","CONTENT","CONTINUE","CONTROL","CURSOR_NAME","DATA","DATETIME_INTERVAL_CODE","DATETIME_INTERVAL_PRECISION","DB","DEFAULTS","DEFERRABLE","DEFERRED","DEFINED","DEFINER","DEGREE","DEPTH","DERIVED","DESC","DESCRIPTOR","DIAGNOSTICS","DISPATCH","DOCUMENT","DOMAIN","DYNAMIC_FUNCTION","DYNAMIC_FUNCTION_CODE","EMPTY","ENCODING","ENFORCED","EXCLUDE","EXCLUDING","EXPRESSION","FILE","FINAL","FIRST","FLAG","FOLLOWING","FORTRAN","FOUND","FS","G","GENERAL","GENERATED","GO","GOTO","GRANTED","HEX","HIERARCHY","ID","IGNORE","IMMEDIATE","IMMEDIATELY","IMPLEMENTATION","INCLUDING","INCREMENT","INDENT","INITIALLY","INPUT","INSTANCE","INSTANTIABLE","INSTEAD","INTEGRITY","INVOKER","ISOLATION","K","KEY","KEY_MEMBER","KEY_TYPE","LAST","LENGTH","LEVEL","LIBRARY","LIMIT","LINK","LOCATION","LOCATOR","M","MAP","MAPPING","MATCHED","MAXVALUE","MESSAGE_LENGTH","MESSAGE_OCTET_LENGTH","MESSAGE_TEXT","MINVALUE","MORE","MUMPS","NAME","NAMES","NAMESPACE","NESTING","NEXT","NFC","NFD","NFKC","NFKD","NIL","NORMALIZED","NULLABLE","NULLS","NUMBER","OBJECT","OCTETS","OFF","OPTION","OPTIONS","ORDERING","ORDINALITY","OTHERS","OUTPUT","OVERRIDING","P","PAD","PARAMETER_MODE","PARAMETER_NAME","PARAMETER_ORDINAL_POSITION","PARAMETER_SPECIFIC_CATALOG","PARAMETER_SPECIFIC_NAME","PARAMETER_SPECIFIC_SCHEMA","PARTIAL","PASCAL","PASSING","PASSTHROUGH","PATH","PERMISSION","PLACING","PLI","PRECEDING","PRESERVE","PRIOR","PRIVILEGES","PUBLIC","READ","RECOVERY","RELATIVE","REPEATABLE","REQUIRING","RESPECT","RESTART","RESTORE","RESTRICT","RETURNED_CARDINALITY","RETURNED_LENGTH","RETURNED_OCTET_LENGTH","RETURNED_SQLSTATE","RETURNING","ROLE","ROUTINE","ROUTINE_CATALOG","ROUTINE_NAME","ROUTINE_SCHEMA","ROW_COUNT","SCALE","SCHEMA","SCHEMA_NAME","SCOPE_CATALOG","SCOPE_NAME","SCOPE_SCHEMA","SECTION","SECURITY","SELECTIVE","SELF","SEQUENCE","SERIALIZABLE","SERVER","SERVER_NAME","SESSION","SETS","SIMPLE","SIZE","SOURCE","SPACE","SPECIFIC_NAME","STANDALONE","STATE","STATEMENT","STRIP","STRUCTURE","STYLE","SUBCLASS_ORIGIN","T","TABLE_NAME","TEMPORARY","TIES","TOKEN","TOP_LEVEL_COUNT","TRANSACTION","TRANSACTIONS_COMMITTED","TRANSACTIONS_ROLLED_BACK","TRANSACTION_ACTIVE","TRANSFORM","TRANSFORMS","TRIGGER_CATALOG","TRIGGER_NAME","TRIGGER_SCHEMA","TYPE","UNBOUNDED","UNCOMMITTED","UNDER","UNLINK","UNNAMED","UNTYPED","URI","USAGE","USER_DEFINED_TYPE_CATALOG","USER_DEFINED_TYPE_CODE","USER_DEFINED_TYPE_NAME","USER_DEFINED_TYPE_SCHEMA","VALID","VERSION","VIEW","WHITESPACE","WORK","WRAPPER","WRITE","XMLDECLARATION","XMLSCHEMA","YES","ZONE"] - -parser.parseError = function(str, hash) { - if (hash.expected && hash.expected.indexOf("'LITERAL'") > -1 && /[a-zA-Z_][a-zA-Z_0-9]*/.test(hash.token) && nonReserved.indexOf(hash.token) > -1) { - return - } - throw new SyntaxError(str) -} + + +// from https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html +// JSON.stringify([].slice.call(document.querySelectorAll('tr')).filter(x => x.children.length == 5 && x.children[2].innerText == 'reserved').map(x => x.children[0].innerText)) + +var nonReserved = ["A","ABSENT","ABSOLUTE","ACCORDING","ACTION","ADA","ADD","ADMIN","AFTER","ALWAYS","ASC","ASSERTION","ASSIGNMENT","ATTRIBUTE","ATTRIBUTES","BASE64","BEFORE","BERNOULLI","BLOCKED","BOM","BREADTH","C","CASCADE","CATALOG","CATALOG_NAME","CHAIN","CHARACTERISTICS","CHARACTERS","CHARACTER_SET_CATALOG","CHARACTER_SET_NAME","CHARACTER_SET_SCHEMA","CLASS_ORIGIN","COBOL","COLLATION","COLLATION_CATALOG","COLLATION_NAME","COLLATION_SCHEMA","COLUMNS","COLUMN_NAME","COMMAND_FUNCTION","COMMAND_FUNCTION_CODE","COMMITTED","CONDITION_NUMBER","CONNECTION","CONNECTION_NAME","CONSTRAINTS","CONSTRAINT_CATALOG","CONSTRAINT_NAME","CONSTRAINT_SCHEMA","CONSTRUCTOR","CONTENT","CONTINUE","CONTROL","CURSOR_NAME","DATA","DATETIME_INTERVAL_CODE","DATETIME_INTERVAL_PRECISION","DB","DEFAULTS","DEFERRABLE","DEFERRED","DEFINED","DEFINER","DEGREE","DEPTH","DERIVED","DESC","DESCRIPTOR","DIAGNOSTICS","DISPATCH","DOCUMENT","DOMAIN","DYNAMIC_FUNCTION","DYNAMIC_FUNCTION_CODE","EMPTY","ENCODING","ENFORCED","EXCLUDE","EXCLUDING","EXPRESSION","FILE","FINAL","FIRST","FLAG","FOLLOWING","FORTRAN","FOUND","FS","G","GENERAL","GENERATED","GO","GOTO","GRANTED","HEX","HIERARCHY","ID","IGNORE","IMMEDIATE","IMMEDIATELY","IMPLEMENTATION","INCLUDING","INCREMENT","INDENT","INITIALLY","INPUT","INSTANCE","INSTANTIABLE","INSTEAD","INTEGRITY","INVOKER","ISOLATION","K","KEY","KEY_MEMBER","KEY_TYPE","LAST","LENGTH","LEVEL","LIBRARY","LIMIT","LINK","LOCATION","LOCATOR","M","MAP","MAPPING","MATCHED","MAXVALUE","MESSAGE_LENGTH","MESSAGE_OCTET_LENGTH","MESSAGE_TEXT","MINVALUE","MORE","MUMPS","NAME","NAMES","NAMESPACE","NESTING","NEXT","NFC","NFD","NFKC","NFKD","NIL","NORMALIZED","NULLABLE","NULLS","NUMBER","OBJECT","OCTETS","OFF","OPTION","OPTIONS","ORDERING","ORDINALITY","OTHERS","OUTPUT","OVERRIDING","P","PAD","PARAMETER_MODE","PARAMETER_NAME","PARAMETER_ORDINAL_POSITION","PARAMETER_SPECIFIC_CATALOG","PARAMETER_SPECIFIC_NAME","PARAMETER_SPECIFIC_SCHEMA","PARTIAL","PASCAL","PASSING","PASSTHROUGH","PATH","PERMISSION","PLACING","PLI","PRECEDING","PRESERVE","PRIOR","PRIVILEGES","PUBLIC","READ","RECOVERY","RELATIVE","REPEATABLE","REQUIRING","RESPECT","RESTART","RESTORE","RESTRICT","RETURNED_CARDINALITY","RETURNED_LENGTH","RETURNED_OCTET_LENGTH","RETURNED_SQLSTATE","RETURNING","ROLE","ROUTINE","ROUTINE_CATALOG","ROUTINE_NAME","ROUTINE_SCHEMA","ROW_COUNT","SCALE","SCHEMA","SCHEMA_NAME","SCOPE_CATALOG","SCOPE_NAME","SCOPE_SCHEMA","SECTION","SECURITY","SELECTIVE","SELF","SEQUENCE","SERIALIZABLE","SERVER","SERVER_NAME","SESSION","SETS","SIMPLE","SIZE","SOURCE","SPACE","SPECIFIC_NAME","STANDALONE","STATE","STATEMENT","STRIP","STRUCTURE","STYLE","SUBCLASS_ORIGIN","T","TABLE_NAME","TEMPORARY","TIES","TOKEN","TOP_LEVEL_COUNT","TRANSACTION","TRANSACTIONS_COMMITTED","TRANSACTIONS_ROLLED_BACK","TRANSACTION_ACTIVE","TRANSFORM","TRANSFORMS","TRIGGER_CATALOG","TRIGGER_NAME","TRIGGER_SCHEMA","TYPE","UNBOUNDED","UNCOMMITTED","UNDER","UNLINK","UNNAMED","UNTYPED","URI","USAGE","USER_DEFINED_TYPE_CATALOG","USER_DEFINED_TYPE_CODE","USER_DEFINED_TYPE_NAME","USER_DEFINED_TYPE_SCHEMA","VALID","VERSION","VIEW","WHITESPACE","WORK","WRAPPER","WRITE","XMLDECLARATION","XMLSCHEMA","YES","ZONE"] + +parser.parseError = function(str, hash) { + if (hash.expected && hash.expected.indexOf("'LITERAL'") > -1 && /[a-zA-Z_][a-zA-Z_0-9]*/.test(hash.token) && nonReserved.indexOf(hash.token) > -1) { + return + } + throw new SyntaxError(str) +} /* generated by jison-lex 0.3.4 */ var lexer = (function(){ var lexer = ({ @@ -2882,100 +2882,98 @@ case 233:return 440 /* Is this keyword required? */ break; case 234:return 131 break; -case 235:return 131 -break; -case 236:return 115 +case 235:return 115 break; -case 237:return 137 +case 236:return 137 break; -case 238:return 179 +case 237:return 179 break; -case 239:return 322 +case 238:return 322 break; -case 240:return 180 +case 239:return 180 break; -case 241:return 133 +case 240:return 133 break; -case 242:return 138 +case 241:return 138 break; -case 243:return 331 +case 242:return 331 break; -case 244:return 328 +case 243:return 328 break; -case 245:return 330 +case 244:return 330 break; -case 246:return 327 +case 245:return 327 break; -case 247:return 325 +case 246:return 325 break; -case 248:return 323 +case 247:return 323 break; -case 249:return 324 +case 248:return 324 break; -case 250:return 142 +case 249:return 142 break; -case 251:return 141 +case 250:return 141 break; -case 252:return 139 +case 251:return 139 break; -case 253:return 326 +case 252:return 326 break; -case 254:return 329 +case 253:return 329 break; -case 255:return 140 +case 254:return 140 break; -case 256:return 124 +case 255:return 124 break; -case 257:return 329 +case 256:return 329 break; -case 258:return 77 +case 257:return 77 break; -case 259:return 78 +case 258:return 78 break; -case 260:return 145 +case 259:return 145 break; -case 261:return 429 +case 260:return 429 break; -case 262:return 431 +case 261:return 431 break; -case 263:return 305 +case 262:return 305 break; -case 264:return 510 +case 263:return 510 break; -case 265:return 512 +case 264:return 512 break; -case 266:return 122 +case 265:return 122 break; -case 267:return 116 +case 266:return 116 break; -case 268:return 74 +case 267:return 74 break; -case 269:return 338 +case 268:return 338 break; -case 270:return 152 +case 269:return 152 break; -case 271:return 768 +case 270:return 768 break; -case 272:return 143 +case 271:return 143 break; -case 273:return 181 +case 272:return 181 break; -case 274:return 136 +case 273:return 136 break; -case 275:return 123 +case 274:return 123 break; -case 276:return 317 +case 275:return 317 break; -case 277:return 4 +case 276:return 4 break; -case 278:return 10 +case 277:return 10 break; -case 279:return 'INVALID' +case 278:return 'INVALID' break; } }, -rules: [/^(?:``([^\`])+``)/i,/^(?:\[\?\])/i,/^(?:@\[)/i,/^(?:ARRAY\[)/i,/^(?:\[([^\]'])*?\])/i,/^(?:`([^\`'])*?`)/i,/^(?:N(['](\\.|[^']|\\')*?['])+)/i,/^(?:X(['](\\.|[^']|\\')*?['])+)/i,/^(?:(['](\\.|[^']|\\')*?['])+)/i,/^(?:(["](\\.|[^"]|\\")*?["])+)/i,/^(?:--(.*?)($|\r\n|\r|\n))/i,/^(?:\s+)/i,/^(?:\|\|)/i,/^(?:\|)/i,/^(?:VALUE\s+OF\s+SEARCH\b)/i,/^(?:VALUE\s+OF\s+SELECT\b)/i,/^(?:ROW\s+OF\s+SELECT\b)/i,/^(?:COLUMN\s+OF\s+SELECT\b)/i,/^(?:MATRIX\s+OF\s+SELECT\b)/i,/^(?:INDEX\s+OF\s+SELECT\b)/i,/^(?:RECORDSET\s+OF\s+SELECT\b)/i,/^(?:TEXT\s+OF\s+SELECT\b)/i,/^(?:SELECT\b)/i,/^(?:ABSOLUTE\b)/i,/^(?:ACTION\b)/i,/^(?:ADD\b)/i,/^(?:AFTER\b)/i,/^(?:AGGR\b)/i,/^(?:AGGREGATE\b)/i,/^(?:AGGREGATOR\b)/i,/^(?:ALL\b)/i,/^(?:ALTER\b)/i,/^(?:AND\b)/i,/^(?:ANTI\b)/i,/^(?:ANY\b)/i,/^(?:APPLY\b)/i,/^(?:ARRAY\b)/i,/^(?:AS\b)/i,/^(?:ASSERT\b)/i,/^(?:ASC\b)/i,/^(?:ATTACH\b)/i,/^(?:AUTO(_)?INCREMENT\b)/i,/^(?:AVG\b)/i,/^(?:BEFORE\b)/i,/^(?:BEGIN\b)/i,/^(?:BETWEEN\b)/i,/^(?:BREAK\b)/i,/^(?:NOT\s+BETWEEN\b)/i,/^(?:NOT\s+LIKE\b)/i,/^(?:BY\b)/i,/^(?:~~\*)/i,/^(?:!~~\*)/i,/^(?:~~)/i,/^(?:!~~)/i,/^(?:ILIKE\b)/i,/^(?:NOT\s+ILIKE\b)/i,/^(?:CALL\b)/i,/^(?:CASE\b)/i,/^(?:CAST\b)/i,/^(?:CHECK\b)/i,/^(?:CLASS\b)/i,/^(?:CLOSE\b)/i,/^(?:COLLATE\b)/i,/^(?:COLUMN\b)/i,/^(?:COLUMNS\b)/i,/^(?:COMMIT\b)/i,/^(?:CONSTRAINT\b)/i,/^(?:CONTENT\b)/i,/^(?:CONTINUE\b)/i,/^(?:CONVERT\b)/i,/^(?:CORRESPONDING\b)/i,/^(?:COUNT\b)/i,/^(?:CREATE\b)/i,/^(?:CROSS\b)/i,/^(?:CUBE\b)/i,/^(?:CURRENT_TIMESTAMP\b)/i,/^(?:CURRENT_DATE\b)/i,/^(?:CURDATE\b)/i,/^(?:CURSOR\b)/i,/^(?:DATABASE(S)?)/i,/^(?:DATEADD\b)/i,/^(?:DATEDIFF\b)/i,/^(?:TIMESTAMPDIFF\b)/i,/^(?:DECLARE\b)/i,/^(?:DEFAULT\b)/i,/^(?:DELETE\b)/i,/^(?:DELETED\b)/i,/^(?:DESC\b)/i,/^(?:DETACH\b)/i,/^(?:DISTINCT\b)/i,/^(?:DROP\b)/i,/^(?:ECHO\b)/i,/^(?:EDGE\b)/i,/^(?:END\b)/i,/^(?:ENUM\b)/i,/^(?:ELSE\b)/i,/^(?:ESCAPE\b)/i,/^(?:EXCEPT\b)/i,/^(?:EXEC\b)/i,/^(?:EXECUTE\b)/i,/^(?:EXISTS\b)/i,/^(?:EXPLAIN\b)/i,/^(?:FALSE\b)/i,/^(?:FETCH\b)/i,/^(?:FIRST\b)/i,/^(?:FOR\b)/i,/^(?:FOREIGN\b)/i,/^(?:FROM\b)/i,/^(?:FULL\b)/i,/^(?:FUNCTION\b)/i,/^(?:GLOB\b)/i,/^(?:GO\b)/i,/^(?:GRAPH\b)/i,/^(?:GROUP\b)/i,/^(?:GROUPING\b)/i,/^(?:HAVING\b)/i,/^(?:IF\b)/i,/^(?:IDENTITY\b)/i,/^(?:IS\b)/i,/^(?:IN\b)/i,/^(?:INDEX\b)/i,/^(?:INDEXED\b)/i,/^(?:INNER\b)/i,/^(?:INSTEAD\b)/i,/^(?:INSERT\b)/i,/^(?:INSERTED\b)/i,/^(?:INTERSECT\b)/i,/^(?:INTERVAL\b)/i,/^(?:INTO\b)/i,/^(?:JOIN\b)/i,/^(?:KEY\b)/i,/^(?:LAST\b)/i,/^(?:LET\b)/i,/^(?:LEFT\b)/i,/^(?:LIKE\b)/i,/^(?:LIMIT\b)/i,/^(?:MATCHED\b)/i,/^(?:MATRIX\b)/i,/^(?:MAX(\s+)?(?=\())/i,/^(?:MAX(\s+)?(?=(,|\))))/i,/^(?:MIN(\s+)?(?=\())/i,/^(?:MERGE\b)/i,/^(?:MINUS\b)/i,/^(?:MODIFY\b)/i,/^(?:NATURAL\b)/i,/^(?:NEXT\b)/i,/^(?:NEW\b)/i,/^(?:NOCASE\b)/i,/^(?:NO\b)/i,/^(?:NOT\b)/i,/^(?:NULL\b)/i,/^(?:NULLS\b)/i,/^(?:OFF\b)/i,/^(?:ON\b)/i,/^(?:ONLY\b)/i,/^(?:OF\b)/i,/^(?:OFFSET\b)/i,/^(?:OPEN\b)/i,/^(?:OPTION\b)/i,/^(?:OR\b)/i,/^(?:ORDER\b)/i,/^(?:OUTER\b)/i,/^(?:OVER\b)/i,/^(?:PATH\b)/i,/^(?:PARTITION\b)/i,/^(?:PERCENT\b)/i,/^(?:PIVOT\b)/i,/^(?:PLAN\b)/i,/^(?:PRIMARY\b)/i,/^(?:PRINT\b)/i,/^(?:PRIOR\b)/i,/^(?:QUERY\b)/i,/^(?:READ\b)/i,/^(?:RECORDSET\b)/i,/^(?:REDUCE\b)/i,/^(?:REFERENCES\b)/i,/^(?:REGEXP\b)/i,/^(?:REINDEX\b)/i,/^(?:RELATIVE\b)/i,/^(?:REMOVE\b)/i,/^(?:RENAME\b)/i,/^(?:REPEAT\b)/i,/^(?:REPLACE\b)/i,/^(?:REQUIRE\b)/i,/^(?:RESTORE\b)/i,/^(?:RETURN\b)/i,/^(?:RETURNS\b)/i,/^(?:RIGHT\b)/i,/^(?:ROLLBACK\b)/i,/^(?:ROLLUP\b)/i,/^(?:ROW\b)/i,/^(?:ROWS\b)/i,/^(?:SCHEMA(S)?)/i,/^(?:SEARCH\b)/i,/^(?:SEMI\b)/i,/^(?:SET\b)/i,/^(?:SETS\b)/i,/^(?:SHOW\b)/i,/^(?:SOME\b)/i,/^(?:SOURCE\b)/i,/^(?:STRATEGY\b)/i,/^(?:STORE\b)/i,/^(?:SUM\b)/i,/^(?:TOTAL\b)/i,/^(?:TABLE\b)/i,/^(?:TABLES\b)/i,/^(?:TARGET\b)/i,/^(?:TEMP\b)/i,/^(?:TEMPORARY\b)/i,/^(?:TEXTSTRING\b)/i,/^(?:THEN\b)/i,/^(?:TIMEOUT\b)/i,/^(?:TO\b)/i,/^(?:TOP\b)/i,/^(?:TRAN\b)/i,/^(?:TRANSACTION\b)/i,/^(?:TRIGGER\b)/i,/^(?:TRUE\b)/i,/^(?:TRUNCATE\b)/i,/^(?:UNION\b)/i,/^(?:UNIQUE\b)/i,/^(?:UNPIVOT\b)/i,/^(?:UPDATE\b)/i,/^(?:USE\b)/i,/^(?:USING\b)/i,/^(?:VALUE\b)/i,/^(?:VALUES\b)/i,/^(?:VERTEX\b)/i,/^(?:VIEW\b)/i,/^(?:WHEN\b)/i,/^(?:WHERE\b)/i,/^(?:WHILE\b)/i,/^(?:WITH\b)/i,/^(?:WORK\b)/i,/^(?:(\d*[.])?\d+[eE]\d+)/i,/^(?:(\d*[.])?\d+)/i,/^(?:->)/i,/^(?:#)/i,/^(?:\+)/i,/^(?:-)/i,/^(?:\*)/i,/^(?:\/)/i,/^(?:%)/i,/^(?:!===)/i,/^(?:===)/i,/^(?:!==)/i,/^(?:==)/i,/^(?:>=)/i,/^(?:&)/i,/^(?:\|)/i,/^(?:<<)/i,/^(?:>>)/i,/^(?:>)/i,/^(?:<=)/i,/^(?:<>)/i,/^(?:<)/i,/^(?:=)/i,/^(?:!=)/i,/^(?:\()/i,/^(?:\))/i,/^(?:@)/i,/^(?:\{)/i,/^(?:\})/i,/^(?:\])/i,/^(?::-)/i,/^(?:\?-)/i,/^(?:\.\.)/i,/^(?:\.)/i,/^(?:,)/i,/^(?:::)/i,/^(?::)/i,/^(?:;)/i,/^(?:\$)/i,/^(?:\?)/i,/^(?:!)/i,/^(?:\^)/i,/^(?:~)/i,/^(?:[0-9]*[a-zA-Z_]+[a-zA-Z_0-9]*)/i,/^(?:$)/i,/^(?:.)/i], -conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279],"inclusive":true}} +rules: [/^(?:``([^\`])+``)/i,/^(?:\[\?\])/i,/^(?:@\[)/i,/^(?:ARRAY\[)/i,/^(?:\[([^\]'])*?\])/i,/^(?:`([^\`'])*?`)/i,/^(?:N(['](\\.|[^']|\\')*?['])+)/i,/^(?:X(['](\\.|[^']|\\')*?['])+)/i,/^(?:(['](\\.|[^']|\\')*?['])+)/i,/^(?:(["](\\.|[^"]|\\")*?["])+)/i,/^(?:--(.*?)($|\r\n|\r|\n))/i,/^(?:\s+)/i,/^(?:\|\|)/i,/^(?:\|)/i,/^(?:VALUE\s+OF\s+SEARCH\b)/i,/^(?:VALUE\s+OF\s+SELECT\b)/i,/^(?:ROW\s+OF\s+SELECT\b)/i,/^(?:COLUMN\s+OF\s+SELECT\b)/i,/^(?:MATRIX\s+OF\s+SELECT\b)/i,/^(?:INDEX\s+OF\s+SELECT\b)/i,/^(?:RECORDSET\s+OF\s+SELECT\b)/i,/^(?:TEXT\s+OF\s+SELECT\b)/i,/^(?:SELECT\b)/i,/^(?:ABSOLUTE\b)/i,/^(?:ACTION\b)/i,/^(?:ADD\b)/i,/^(?:AFTER\b)/i,/^(?:AGGR\b)/i,/^(?:AGGREGATE\b)/i,/^(?:AGGREGATOR\b)/i,/^(?:ALL\b)/i,/^(?:ALTER\b)/i,/^(?:AND\b)/i,/^(?:ANTI\b)/i,/^(?:ANY\b)/i,/^(?:APPLY\b)/i,/^(?:ARRAY\b)/i,/^(?:AS\b)/i,/^(?:ASSERT\b)/i,/^(?:ASC\b)/i,/^(?:ATTACH\b)/i,/^(?:AUTO(_)?INCREMENT\b)/i,/^(?:AVG\b)/i,/^(?:BEFORE\b)/i,/^(?:BEGIN\b)/i,/^(?:BETWEEN\b)/i,/^(?:BREAK\b)/i,/^(?:NOT\s+BETWEEN\b)/i,/^(?:NOT\s+LIKE\b)/i,/^(?:BY\b)/i,/^(?:~~\*)/i,/^(?:!~~\*)/i,/^(?:~~)/i,/^(?:!~~)/i,/^(?:ILIKE\b)/i,/^(?:NOT\s+ILIKE\b)/i,/^(?:CALL\b)/i,/^(?:CASE\b)/i,/^(?:CAST\b)/i,/^(?:CHECK\b)/i,/^(?:CLASS\b)/i,/^(?:CLOSE\b)/i,/^(?:COLLATE\b)/i,/^(?:COLUMN\b)/i,/^(?:COLUMNS\b)/i,/^(?:COMMIT\b)/i,/^(?:CONSTRAINT\b)/i,/^(?:CONTENT\b)/i,/^(?:CONTINUE\b)/i,/^(?:CONVERT\b)/i,/^(?:CORRESPONDING\b)/i,/^(?:COUNT\b)/i,/^(?:CREATE\b)/i,/^(?:CROSS\b)/i,/^(?:CUBE\b)/i,/^(?:CURRENT_TIMESTAMP\b)/i,/^(?:CURRENT_DATE\b)/i,/^(?:CURDATE\b)/i,/^(?:CURSOR\b)/i,/^(?:DATABASE(S)?)/i,/^(?:DATEADD\b)/i,/^(?:DATEDIFF\b)/i,/^(?:TIMESTAMPDIFF\b)/i,/^(?:DECLARE\b)/i,/^(?:DEFAULT\b)/i,/^(?:DELETE\b)/i,/^(?:DELETED\b)/i,/^(?:DESC\b)/i,/^(?:DETACH\b)/i,/^(?:DISTINCT\b)/i,/^(?:DROP\b)/i,/^(?:ECHO\b)/i,/^(?:EDGE\b)/i,/^(?:END\b)/i,/^(?:ENUM\b)/i,/^(?:ELSE\b)/i,/^(?:ESCAPE\b)/i,/^(?:EXCEPT\b)/i,/^(?:EXEC\b)/i,/^(?:EXECUTE\b)/i,/^(?:EXISTS\b)/i,/^(?:EXPLAIN\b)/i,/^(?:FALSE\b)/i,/^(?:FETCH\b)/i,/^(?:FIRST\b)/i,/^(?:FOR\b)/i,/^(?:FOREIGN\b)/i,/^(?:FROM\b)/i,/^(?:FULL\b)/i,/^(?:FUNCTION\b)/i,/^(?:GLOB\b)/i,/^(?:GO\b)/i,/^(?:GRAPH\b)/i,/^(?:GROUP\b)/i,/^(?:GROUPING\b)/i,/^(?:HAVING\b)/i,/^(?:IF\b)/i,/^(?:IDENTITY\b)/i,/^(?:IS\b)/i,/^(?:IN\b)/i,/^(?:INDEX\b)/i,/^(?:INDEXED\b)/i,/^(?:INNER\b)/i,/^(?:INSTEAD\b)/i,/^(?:INSERT\b)/i,/^(?:INSERTED\b)/i,/^(?:INTERSECT\b)/i,/^(?:INTERVAL\b)/i,/^(?:INTO\b)/i,/^(?:JOIN\b)/i,/^(?:KEY\b)/i,/^(?:LAST\b)/i,/^(?:LET\b)/i,/^(?:LEFT\b)/i,/^(?:LIKE\b)/i,/^(?:LIMIT\b)/i,/^(?:MATCHED\b)/i,/^(?:MATRIX\b)/i,/^(?:MAX\s*(?=\())/i,/^(?:MAX\s*(?=(,|\))))/i,/^(?:MIN\s*(?=\())/i,/^(?:MERGE\b)/i,/^(?:MINUS\b)/i,/^(?:MODIFY\b)/i,/^(?:NATURAL\b)/i,/^(?:NEXT\b)/i,/^(?:NEW\b)/i,/^(?:NOCASE\b)/i,/^(?:NO\b)/i,/^(?:NOT\b)/i,/^(?:NULL\b)/i,/^(?:NULLS\b)/i,/^(?:OFF\b)/i,/^(?:ON\b)/i,/^(?:ONLY\b)/i,/^(?:OF\b)/i,/^(?:OFFSET\b)/i,/^(?:OPEN\b)/i,/^(?:OPTION\b)/i,/^(?:OR\b)/i,/^(?:ORDER\b)/i,/^(?:OUTER\b)/i,/^(?:OVER\b)/i,/^(?:PATH\b)/i,/^(?:PARTITION\b)/i,/^(?:PERCENT\b)/i,/^(?:PIVOT\b)/i,/^(?:PLAN\b)/i,/^(?:PRIMARY\b)/i,/^(?:PRINT\b)/i,/^(?:PRIOR\b)/i,/^(?:QUERY\b)/i,/^(?:READ\b)/i,/^(?:RECORDSET\b)/i,/^(?:REDUCE\b)/i,/^(?:REFERENCES\b)/i,/^(?:REGEXP\b)/i,/^(?:REINDEX\b)/i,/^(?:RELATIVE\b)/i,/^(?:REMOVE\b)/i,/^(?:RENAME\b)/i,/^(?:REPEAT\b)/i,/^(?:REPLACE\b)/i,/^(?:REQUIRE\b)/i,/^(?:RESTORE\b)/i,/^(?:RETURN\b)/i,/^(?:RETURNS\b)/i,/^(?:RIGHT\b)/i,/^(?:ROLLBACK\b)/i,/^(?:ROLLUP\b)/i,/^(?:ROW\b)/i,/^(?:ROWS\b)/i,/^(?:SCHEMA(S)?)/i,/^(?:SEARCH\b)/i,/^(?:SEMI\b)/i,/^(?:SET\b)/i,/^(?:SETS\b)/i,/^(?:SHOW\b)/i,/^(?:SOME\b)/i,/^(?:SOURCE\b)/i,/^(?:STRATEGY\b)/i,/^(?:STORE\b)/i,/^(?:SUM\b)/i,/^(?:TOTAL\b)/i,/^(?:TABLE\b)/i,/^(?:TABLES\b)/i,/^(?:TARGET\b)/i,/^(?:TEMP\b)/i,/^(?:TEMPORARY\b)/i,/^(?:TEXTSTRING\b)/i,/^(?:THEN\b)/i,/^(?:TIMEOUT\b)/i,/^(?:TO\b)/i,/^(?:TOP\b)/i,/^(?:TRAN\b)/i,/^(?:TRANSACTION\b)/i,/^(?:TRIGGER\b)/i,/^(?:TRUE\b)/i,/^(?:TRUNCATE\b)/i,/^(?:UNION\b)/i,/^(?:UNIQUE\b)/i,/^(?:UNPIVOT\b)/i,/^(?:UPDATE\b)/i,/^(?:USE\b)/i,/^(?:USING\b)/i,/^(?:VALUE\b)/i,/^(?:VALUES\b)/i,/^(?:VERTEX\b)/i,/^(?:VIEW\b)/i,/^(?:WHEN\b)/i,/^(?:WHERE\b)/i,/^(?:WHILE\b)/i,/^(?:WITH\b)/i,/^(?:WORK\b)/i,/^(?:(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?)/i,/^(?:->)/i,/^(?:#)/i,/^(?:\+)/i,/^(?:-)/i,/^(?:\*)/i,/^(?:\/)/i,/^(?:%)/i,/^(?:!===)/i,/^(?:===)/i,/^(?:!==)/i,/^(?:==)/i,/^(?:>=)/i,/^(?:&)/i,/^(?:\|)/i,/^(?:<<)/i,/^(?:>>)/i,/^(?:>)/i,/^(?:<=)/i,/^(?:<>)/i,/^(?:<)/i,/^(?:=)/i,/^(?:!=)/i,/^(?:\()/i,/^(?:\))/i,/^(?:@)/i,/^(?:\{)/i,/^(?:\})/i,/^(?:\])/i,/^(?::-)/i,/^(?:\?-)/i,/^(?:\.\.)/i,/^(?:\.)/i,/^(?:,)/i,/^(?:::)/i,/^(?::)/i,/^(?:;)/i,/^(?:\$)/i,/^(?:\?)/i,/^(?:!)/i,/^(?:\^)/i,/^(?:~)/i,/^(?:[0-9]*[a-zA-Z_]+[a-zA-Z_0-9]*)/i,/^(?:$)/i,/^(?:.)/i], +conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278],"inclusive":true}} }); return lexer; })();