UNPKG

lite-ui-sql-parser

Version:
1,473 lines (1,445 loc) 624 kB
/* * Generated by PEG.js 0.10.0. * * http://pegjs.org/ */ (function(root, factory) { if (typeof define === "function" && define.amd) { define(["big-integer"], factory); } else if (typeof module === "object" && module.exports) { module.exports = factory(require("big-integer")); } })(this, function(BigInt) { "use strict"; function peg$subclass(child, parent) { function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); } function peg$SyntaxError(message, expected, found, location) { this.message = message; this.expected = expected; this.found = found; this.location = location; this.name = "SyntaxError"; if (typeof Error.captureStackTrace === "function") { Error.captureStackTrace(this, peg$SyntaxError); } } peg$subclass(peg$SyntaxError, Error); peg$SyntaxError.buildMessage = function(expected, found) { var DESCRIBE_EXPECTATION_FNS = { literal: function(expectation) { return "\"" + literalEscape(expectation.text) + "\""; }, "class": function(expectation) { var escapedParts = "", i; for (i = 0; i < expectation.parts.length; i++) { escapedParts += expectation.parts[i] instanceof Array ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) : classEscape(expectation.parts[i]); } return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; }, any: function(expectation) { return "any character"; }, end: function(expectation) { return "end of input"; }, other: function(expectation) { return expectation.description; } }; function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); } function literalEscape(s) { return s .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') .replace(/\0/g, '\\0') .replace(/\t/g, '\\t') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); } function classEscape(s) { return s .replace(/\\/g, '\\\\') .replace(/\]/g, '\\]') .replace(/\^/g, '\\^') .replace(/-/g, '\\-') .replace(/\0/g, '\\0') .replace(/\t/g, '\\t') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return '\\x' + hex(ch); }); } function describeExpectation(expectation) { return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation); } function describeExpected(expected) { var descriptions = new Array(expected.length), i, j; for (i = 0; i < expected.length; i++) { descriptions[i] = describeExpectation(expected[i]); } descriptions.sort(); if (descriptions.length > 0) { for (i = 1, j = 1; i < descriptions.length; i++) { if (descriptions[i - 1] !== descriptions[i]) { descriptions[j] = descriptions[i]; j++; } } descriptions.length = j; } switch (descriptions.length) { case 1: return descriptions[0]; case 2: return descriptions[0] + " or " + descriptions[1]; default: return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1]; } } function describeFound(found) { return found ? "\"" + literalEscape(found) + "\"" : "end of input"; } return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found."; }; function peg$parse(input, options) { options = options !== void 0 ? options : {}; var peg$FAILED = {}, peg$startRuleFunctions = { start: peg$parsestart }, peg$startRuleFunction = peg$parsestart, peg$c0 = function(head, tail) { const cur = [head && head.ast || head]; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); } return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: cur } }, peg$c1 = function(head, tail) { let cur = head for (let i = 0; i < tail.length; i++) { cur._next = tail[i][5] cur.union = tail[i][3] ? 'union all' : 'union' cur = cur._next } return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: head } }, peg$c2 = function(a, tp, ife, t, c, to, ir, as, qe) { if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`)); return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: a[0].toLowerCase(), keyword: 'table', temporary: tp && tp[0].toLowerCase(), if_not_exists: ife && ife[0].toLowerCase(), table: t, ignore_replace: ir && ir[0].toLowerCase(), as: as && as[0].toLowerCase(), query_expr: qe && qe.ast, create_definitions: c, table_options: to } } }, peg$c3 = function(a, tp, ife, t, lt) { if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`)); return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: a[0].toLowerCase(), keyword: 'table', temporary: tp && tp[0].toLowerCase(), if_not_exists: ife && ife[0].toLowerCase(), table: t, like: lt } } }, peg$c4 = function(t) { return { type: 'like', table: t } }, peg$c5 = function(e) { e.parentheses = true; return e; }, peg$c6 = function(head, tail) { return createList(head, tail); }, peg$c7 = "unique", peg$c8 = peg$literalExpectation("UNIQUE", true), peg$c9 = "primary", peg$c10 = peg$literalExpectation("PRIMARY", true), peg$c11 = "key", peg$c12 = peg$literalExpectation("KEY", true), peg$c13 = "auto_increment", peg$c14 = peg$literalExpectation("AUTO_INCREMENT", true), peg$c15 = "after", peg$c16 = peg$literalExpectation("AFTER", true), peg$c17 = function(c, d, u, cs, ca, n, a, df, co, cf, s, re) { columnList.add(`create::${c.table}::${c.column}`) if (n && !n.value) n.value = 'null' return { column: c, definition: d, nullable: n, default_val: df, auto_increment: a && a.toLowerCase(), unique_or_primary: u && `${u[0].toLowerCase()} ${u[2].toLowerCase()}`, comment: co, char_set: cs, collate: ca, column_format: cf, storage:s, reference_definition: re, resource: 'column' } }, peg$c18 = function(c, cn, d, u, cs, ca, n, a, df, co, cf, s, re) { columnList.add(`create::${c.table}::${c.column}`) if (n && !n.value) n.value = 'null' return { column: [c,cn], definition: d, nullable: n, default_val: df, auto_increment: a && a.toLowerCase(), unique_or_primary: u && `${u[0].toLowerCase()} ${u[2].toLowerCase()}`, comment: co, char_set: cs, collate: ca, column_format: cf, storage:s, reference_definition: re, resource: 'column' } }, peg$c19 = function(ca) { return { type: 'collate', value: ca, } }, peg$c20 = function(ca) { return { type: 'char set', value: ca, } }, peg$c21 = "column_format", peg$c22 = peg$literalExpectation("COLUMN_FORMAT", true), peg$c23 = "fixed", peg$c24 = peg$literalExpectation("FIXED", true), peg$c25 = "dynamic", peg$c26 = peg$literalExpectation("DYNAMIC", true), peg$c27 = "default", peg$c28 = peg$literalExpectation("DEFAULT", true), peg$c29 = function(k, f) { return { type: 'column_format', value: f.toLowerCase() } }, peg$c30 = "storage", peg$c31 = peg$literalExpectation("STORAGE", true), peg$c32 = "disk", peg$c33 = peg$literalExpectation("DISK", true), peg$c34 = "memory", peg$c35 = peg$literalExpectation("MEMORY", true), peg$c36 = function(k, s) { return { type: 'storage', value: s.toLowerCase() } }, peg$c37 = function(ce) { return { type: 'default', value: ce } }, peg$c38 = function(head, tail) { return createList(head, tail, 1) }, peg$c39 = function(a, b, t) { if(t) t.forEach(tt => tableList.add(`${a}::${tt.db}::${tt.table}`)); return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: a.toLowerCase(), table: t, exists: b } }; }, peg$c40 = function(a, r, i, t, op) { return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: a.toLowerCase(), keyword: r.toLowerCase(), name: i, table: t, options: op } }; }, peg$c41 = function(a, kw, t) { if(t) t.forEach(tt => tableList.add(`${a}::${tt.db}::${tt.table}`)); return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: a.toLowerCase(), keyword: kw, table: t } }; }, peg$c42 = function(d) { tableList.add(`use::${d}::null`); return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'use', db: d } }; }, peg$c43 = function(i, t, c) { return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'create', keyword: 'index', index: i, table: t, column: c } }; }, peg$c44 = function(t, e) { if (t && t.length > 0) t.forEach(table => tableList.add(`alter::${table.db}::${table.table}`)); return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'alter', table: t, expr: e } }; }, peg$c45 = function(kc, cd) { return { action: 'add', ...cd, keyword: kc, resource: 'column', type: 'alter', } }, peg$c46 = function(kc, cd) { return { action: 'change', ...cd, keyword: kc, resource: 'column', type: 'alter', } }, peg$c47 = function(kc, cd) { return { action: 'modify', ...cd, keyword: kc, resource: 'column', type: 'alter', } }, peg$c48 = function(kc, c) { return { action: 'drop', column: c, keyword: kc, resource: 'column', type: 'alter', } }, peg$c49 = function(id) { return { action: 'add', type: 'alter', ...id, } }, peg$c50 = function(kw, tn) { return { action: 'rename', type: 'alter', resource: 'table', keyword: kw && kw[0].toLowerCase(), table: tn } }, peg$c51 = "algorithm", peg$c52 = peg$literalExpectation("ALGORITHM", true), peg$c53 = "instant", peg$c54 = peg$literalExpectation("INSTANT", true), peg$c55 = "inplace", peg$c56 = peg$literalExpectation("INPLACE", true), peg$c57 = "copy", peg$c58 = peg$literalExpectation("COPY", true), peg$c59 = function(val) { return { type: 'alter', keyword: 'algorithm', resource: 'algorithm', algorithm: val } }, peg$c60 = "lock", peg$c61 = peg$literalExpectation("LOCK", true), peg$c62 = "none", peg$c63 = peg$literalExpectation("NONE", true), peg$c64 = "shared", peg$c65 = peg$literalExpectation("SHARED", true), peg$c66 = "exclusive", peg$c67 = peg$literalExpectation("EXCLUSIVE", true), peg$c68 = function(val) { return { type: 'alter', keyword: 'lock', resource: 'lock', lock: val } }, peg$c69 = function(kc, c, t, de, id) { return { index: c, definition: de, keyword: kc.toLowerCase(), index_type: t, resource: 'index', index_options: id, } }, peg$c70 = function(p, kc, c, de, id) { return { index: c, definition: de, keyword: kc && `${p.toLowerCase()} ${kc.toLowerCase()}` || p.toLowerCase(), index_options: id, resource: 'index', } }, peg$c71 = function(kc, c) { return { keyword: kc.toLowerCase(), constraint: c } }, peg$c72 = "primary key", peg$c73 = peg$literalExpectation("PRIMARY KEY", true), peg$c74 = function(kc, p, t, de, id) { return { constraint: kc && kc.constraint, definition: de, constraint_type: p.toLowerCase(), keyword: kc && kc.keyword, index_type: t, resource: 'constraint', index_options: id, } }, peg$c75 = function(kc, u, p, i, t, de, id) { return { constraint: kc && kc.constraint, definition: de, constraint_type: p && `${u.toLowerCase()} ${p.toLowerCase()}` || u.toLowerCase(), keyword: kc && kc.keyword, index_type: t, index: i, resource: 'constraint', index_options: id } }, peg$c76 = "foreign key", peg$c77 = peg$literalExpectation("FOREIGN KEY", true), peg$c78 = function(kc, p, i, de, id) { return { constraint: kc && kc.constraint, definition: de, constraint_type: p, keyword: kc && kc.keyword, index: i, resource: 'constraint', reference_definition: id } }, peg$c79 = "match full", peg$c80 = peg$literalExpectation("MATCH FULL", true), peg$c81 = "match partial", peg$c82 = peg$literalExpectation("MATCH PARTIAL", true), peg$c83 = "match simple", peg$c84 = peg$literalExpectation("MATCH SIMPLE", true), peg$c85 = function(kc, t, de, m, od, ou) { return { definition: de, table: t, keyword: kc.toLowerCase(), match:m && m.toLowerCase(), on_delete: od, on_update: ou, } }, peg$c86 = "on delete", peg$c87 = peg$literalExpectation("ON DELETE", true), peg$c88 = "on update", peg$c89 = peg$literalExpectation("ON UPDATE", true), peg$c90 = function(kw, ro) { return { type: kw.toLowerCase(), value: ro } }, peg$c91 = "restrict", peg$c92 = peg$literalExpectation("RESTRICT", true), peg$c93 = "cascade", peg$c94 = peg$literalExpectation("CASCADE", true), peg$c95 = "set null", peg$c96 = peg$literalExpectation("SET NULL", true), peg$c97 = "no action", peg$c98 = peg$literalExpectation("NO ACTION", true), peg$c99 = "set default", peg$c100 = peg$literalExpectation("SET DEFAULT", true), peg$c101 = function(kc) { return kc.toLowerCase() }, peg$c102 = function(head, tail) { return createList(head, tail) }, peg$c103 = "avg_row_length", peg$c104 = peg$literalExpectation("AVG_ROW_LENGTH", true), peg$c105 = "key_block_size", peg$c106 = peg$literalExpectation("KEY_BLOCK_SIZE", true), peg$c107 = "max_rows", peg$c108 = peg$literalExpectation("MAX_ROWS", true), peg$c109 = "min_rows", peg$c110 = peg$literalExpectation("MIN_ROWS", true), peg$c111 = "stats_sample_pages", peg$c112 = peg$literalExpectation("STATS_SAMPLE_PAGES", true), peg$c113 = function(kw, s, v) { return { keyword: kw.toLowerCase(), symbol: s, value: v.value } }, peg$c114 = "character set", peg$c115 = peg$literalExpectation("CHARACTER SET", true), peg$c116 = "charset", peg$c117 = peg$literalExpectation("CHARSET", true), peg$c118 = "collate", peg$c119 = peg$literalExpectation("COLLATE", true), peg$c120 = function(kw, t, s, v) { return { keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(), symbol: s, value: v } }, peg$c121 = "connection", peg$c122 = peg$literalExpectation("CONNECTION", true), peg$c123 = function(kw, s, c) { return { keyword: kw.toLowerCase(), symbol: s, value: `'${c.value}'` } }, peg$c124 = "compression", peg$c125 = peg$literalExpectation("COMPRESSION", true), peg$c126 = "'", peg$c127 = peg$literalExpectation("'", false), peg$c128 = "zlib", peg$c129 = peg$literalExpectation("ZLIB", true), peg$c130 = "lz4", peg$c131 = peg$literalExpectation("LZ4", true), peg$c132 = function(kw, s, v) { return { keyword: kw.toLowerCase(), symbol: s, value: v.join('').toUpperCase() } }, peg$c133 = "engine", peg$c134 = peg$literalExpectation("ENGINE", true), peg$c135 = function(kw, s, c) { return { keyword: kw.toLowerCase(), symbol: s, value: c.toUpperCase() } }, peg$c136 = function(fsid) { return { action: 'add', type: 'alter', ...fsid, } }, peg$c137 = function(t) { t.forEach(tg => tg.forEach(dt => dt.table && tableList.add(`rename::${dt.db}::${dt.table}`))) return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'rename', table: t } }; }, peg$c138 = function(kw, a) { a.keyword = kw return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'set', expr: a } } }, peg$c139 = function() { return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'unlock', keyword: 'tables' } } }, peg$c140 = "read", peg$c141 = peg$literalExpectation("READ", true), peg$c142 = "local", peg$c143 = peg$literalExpectation("LOCAL", true), peg$c144 = function(s) { return { type: 'read', suffix: s && 'local' } }, peg$c145 = "low_priority", peg$c146 = peg$literalExpectation("LOW_PRIORITY", true), peg$c147 = "write", peg$c148 = peg$literalExpectation("WRITE", true), peg$c149 = function(p) { return { type: 'write', prefix: p && 'low_priority' } }, peg$c150 = function(t, lt) { tableList.add(`lock::${t.db}::${t.table}`) return { table: t, lock_type: lt } }, peg$c151 = function(head, tail) { return createList(head, tail); }, peg$c152 = function(ltl) { return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'lock', keyword: 'tables', tables: ltl } } }, peg$c153 = function(e) { return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'call', expr: e } } }, peg$c154 = "(", peg$c155 = peg$literalExpectation("(", false), peg$c156 = ")", peg$c157 = peg$literalExpectation(")", false), peg$c158 = function(s) { return s[2]; }, peg$c159 = function(cte) { cte.recursive = true; return [cte] }, peg$c160 = function(name, columns, stmt) { return { name, stmt, columns }; }, peg$c161 = function(cte, opts, d, c, f, w, g, h, o, l) { if(f) f.forEach(info => info.table && tableList.add(`select::${info.db}::${info.table}`)); return { with: cte, type: 'select', options: opts, distinct: d, columns: c, from: f, where: w, groupby: g, having: h, orderby: o, limit: l }; }, peg$c162 = function(head, tail) { const opts = [head]; for (let i = 0, l = tail.length; i < l; ++i) { opts.push(tail[i][1]); } return opts; }, peg$c163 = function(option) { return option; }, peg$c164 = function(head, tail) { columnList.add('select::null::(.*)'); if (tail && tail.length > 0) { head[0] = { expr: { type: 'column_ref', table: null, column: '*' }, as: null }; return createList(head[0], tail); } return head[0]; }, peg$c165 = function(tbl) { columnList.add(`select::${tbl}::(.*)`); return { expr: { type: 'column_ref', table: tbl, column: '*' }, as: null }; }, peg$c166 = function(e, alias) { return { expr: e, as: alias }; }, peg$c167 = function(i) { return i; }, peg$c168 = function(l) { return l; }, peg$c169 = function(head, tail) { return [head, tail] }, peg$c170 = "btree", peg$c171 = peg$literalExpectation("BTREE", true), peg$c172 = "hash", peg$c173 = peg$literalExpectation("HASH", true), peg$c174 = function(t) { return { keyword: 'using', type: t.toLowerCase(), } }, peg$c175 = function(head, tail) { const result = [head]; for (let i = 0; i < tail.length; i++) { result.push(tail[i][1]); } return result; }, peg$c176 = function(k, e, kbs) { return { type: k.toLowerCase(), symbol: e, expr: kbs }; }, peg$c177 = "with", peg$c178 = peg$literalExpectation("WITH", true), peg$c179 = "parser", peg$c180 = peg$literalExpectation("PARSER", true), peg$c181 = function(pn) { return { type: 'with parser', expr: pn } }, peg$c182 = "visible", peg$c183 = peg$literalExpectation("VISIBLE", true), peg$c184 = "invisible", peg$c185 = peg$literalExpectation("INVISIBLE", true), peg$c186 = function(k) { return { type: k.toLowerCase(), expr: k.toLowerCase() } }, peg$c187 = function(head, tail) { tail.unshift(head); tail.forEach(tableInfo => { const { table, as } = tableInfo tableAlias[table] = table if (as) tableAlias[as] = table }) return tail; }, peg$c188 = function(t) { return t; }, peg$c189 = function(op, t, head, tail) { t.join = op; t.using = createList(head, tail); return t; }, peg$c190 = function(op, t, expr) { t.join = op; t.on = expr; return t; }, peg$c191 = function(op, stmt, alias, expr) { stmt.parentheses = true; return { expr: stmt, as: alias, join: op, on: expr }; }, peg$c192 = function() { return { type: 'dual' }; }, peg$c193 = function(t, alias) { if (t.type === 'var') { t.as = alias; return t; } else { return { db: t.db, table: t.table, as: alias }; } }, peg$c194 = function(stmt, alias) { stmt.parentheses = true; return { expr: stmt, as: alias }; }, peg$c195 = function() { return 'LEFT JOIN'; }, peg$c196 = function() { return 'RIGHT JOIN'; }, peg$c197 = function() { return 'FULL JOIN'; }, peg$c198 = function() { return 'INNER JOIN'; }, peg$c199 = function(dt, tail) { const obj = { db: null, table: dt }; if (tail !== null) { obj.db = dt; obj.table = tail[3]; } return obj; }, peg$c200 = function(v) { v.db = null; v.table = v.name; return v; }, peg$c201 = function(e) { return e; }, peg$c202 = function(e) { return e.value; }, peg$c203 = function(l) { return l }, peg$c204 = function(e, d) { const obj = { expr: e, type: 'ASC' }; if (d === 'DESC') obj.type = 'DESC'; return obj; }, peg$c205 = function(i1, tail) { const res = [i1]; if (tail) res.push(tail[2]); return { seperator: tail && tail[0] && tail[0].toLowerCase() || '', value: res }; }, peg$c206 = function(t, l, w) { if (t) t.forEach(tableInfo => { const { db, as, table } = tableInfo tableList.add(`update::${db}::${table}`) }); if(l) { l.forEach(col => columnList.add(`update::${col.table}::${col.column}`)); } return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'update', table: t, set: l, where: w } }; }, peg$c207 = function(t, f, w) { if(f) f.forEach(info => { info.table && tableList.add(`delete::${info.db}::${info.table}`); columnList.add(`delete::${info.table}::(.*)`); }); if (t === null && f.length === 1) { const tableInfo = f[0] t = [{ db: tableInfo.db, table: tableInfo.table, as: tableInfo.as, addition: true }] } return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: 'delete', table: t, from: f, where: w } }; }, peg$c208 = "=", peg$c209 = peg$literalExpectation("=", false), peg$c210 = function(tbl, c, v) { return { column: c, value: v, table: tbl && tbl[0] }; }, peg$c211 = function(head, tail) { return createList(head, tail) }, peg$c212 = function(v) { return v }, peg$c213 = function(ri, t, p, c, v) { if (t) { tableList.add(`insert::${t.db}::${t.table}`) t.as = null } if (c) { let table = t && t.table || null c.forEach(c => columnList.add(`insert::${table}::${c}`)); } return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: ri, table: [t], columns: c, values: v, partition: p, } }; }, peg$c214 = function(ri, t, p, v) { if (t) { tableList.add(`insert::${t.db}::${t.table}`) columnList.add(`insert::${t.table}::(.*)`); t.as = null } return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), ast: { type: ri, table: [t], columns: null, values: v, partition: p, } }; }, peg$c215 = function() { return 'insert'; }, peg$c216 = function() { return 'replace'; }, peg$c217 = function(l) { return l; }, peg$c218 = function(head, tail) { const el = { type: 'expr_list' }; el.value = createList(head, tail); return el; }, peg$c219 = function(e, u) { return { type: 'interval', expr: e, unit: u.toLowerCase(), } }, peg$c220 = function(expr, condition_list, otherwise) { if (otherwise) condition_list.push(otherwise); return { type: 'case', expr: expr || null, args: condition_list }; }, peg$c221 = function(condition, result) { return { type: 'when', cond: condition, result: result }; }, peg$c222 = function(result) { return { type: 'else', result: result }; }, peg$c223 = function(head, tail) { return createBinaryExprChain(head, tail); }, peg$c224 = function(op, tail) { return createUnaryExpr(op, tail[0][1]); }, peg$c225 = function(head, tail) { return createBinaryExprChain(head, tail); }, peg$c226 = "!", peg$c227 = peg$literalExpectation("!", false), peg$c228 = function(expr) { return createUnaryExpr('NOT', expr); }, peg$c229 = function(left, rh) { if (rh === null) return left; else if (rh.type === 'arithmetic') return createBinaryExprChain(left, rh.tail); else return createBinaryExpr(rh.op, left, rh.right); }, peg$c230 = function(op, stmt) { stmt.parentheses = true; return createUnaryExpr(op, stmt); }, peg$c231 = function(nk) { return nk[0] + ' ' + nk[2]; }, peg$c232 = function(l) { return { type: 'arithmetic', tail: l }; }, peg$c233 = ">=", peg$c234 = peg$literalExpectation(">=", false), peg$c235 = ">", peg$c236 = peg$literalExpectation(">", false), peg$c237 = "<=", peg$c238 = peg$literalExpectation("<=", false), peg$c239 = "<>", peg$c240 = peg$literalExpectation("<>", false), peg$c241 = "<", peg$c242 = peg$literalExpectation("<", false), peg$c243 = "!=", peg$c244 = peg$literalExpectation("!=", false), peg$c245 = function(right) { return { op: 'IS', right: right }; }, peg$c246 = function(right) { return { op: 'IS NOT', right: right }; }, peg$c247 = function(op, begin, end) { return { op: op, right: { type: 'expr_list', value: [begin, end] } }; }, peg$c248 = function(op, right) { return { op: op, right: right }; }, peg$c249 = function(op, l) { return { op: op, right: l }; }, peg$c250 = function(op, e) { return { op: op, right: e }; }, peg$c251 = "+", peg$c252 = peg$literalExpectation("+", false), peg$c253 = "-", peg$c254 = peg$literalExpectation("-", false), peg$c255 = function(head, tail) { return createBinaryExprChain(head, tail) }, peg$c256 = "*", peg$c257 = peg$literalExpectation("*", false), peg$c258 = "/", peg$c259 = peg$literalExpectation("/", false), peg$c260 = "%", peg$c261 = peg$literalExpectation("%", false), peg$c262 = function(e) { e.parentheses = true; return e; }, peg$c263 = function(list) { list.parentheses = true; return list; }, peg$c264 = function(tbl, col, a, j, ca) { const tableName = tbl && tbl[0] || null columnList.add(`select::${tableName}::${col}`); return { type: 'column_ref', table: tableName, column: col, collate: ca, arrow: a, property: j }; }, peg$c265 = function(tbl, col) { columnList.add(`select::${tbl}::${col}`); return { type: 'column_ref', table: tbl, column: col }; }, peg$c266 = function(col) { columnList.add(`select::null::${col}`); return { type: 'column_ref', table: null, column: col }; }, peg$c267 = function(name) { return reservedMap[name.toUpperCase()] === true; }, peg$c268 = function(name) { return name; }, peg$c269 = function(name) { if (reservedMap[name.toUpperCase()] === true) throw new Error("Error: "+ JSON.stringify(name)+" is a reserved word, can not as alias clause"); return false }, peg$c270 = "\"", peg$c271 = peg$literalExpectation("\"", false), peg$c272 = /^[^"]/, peg$c273 = peg$classExpectation(["\""], true, false), peg$c274 = function(chars) { return chars.join(''); }, peg$c275 = /^[^']/, peg$c276 = peg$classExpectation(["'"], true, false), peg$c277 = "`", peg$c278 = peg$literalExpectation("`", false), peg$c279 = /^[^`]/, peg$c280 = peg$classExpectation(["`"], true, false), peg$c281 = function(name) { return name; }, peg$c282 = function(name) { return name; }, peg$c283 = function(start, parts) { return start + parts.join(''); }, peg$c284 = /^[A-Za-z_]/, peg$c285 = peg$classExpectation([["A", "Z"], ["a", "z"], "_"], false, false), peg$c286 = /^[A-Za-z0-9_]/, peg$c287 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_"], false, false), peg$c288 = /^[A-Za-z0-9_:]/, peg$c289 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", ":"], false, false), peg$c290 = ":", peg$c291 = peg$literalExpectation(":", false), peg$c292 = function(l) { return { type: 'param', value: l[1] }; }, peg$c293 = function(name, e) { return { type: 'aggr_func', name: name, args: { expr: e } }; }, peg$c294 = function(bc, l) { return { partitionby: bc, orderby: l } }, peg$c295 = function(name, arg, bc) { return { type: 'aggr_func', name: name, args: arg, over: bc }; }, peg$c296 = function(name, arg) { return { type: 'aggr_func', name: name, args: arg }; }, peg$c297 = function(e) { return { expr: e }; }, peg$c298 = function(d, c) { return { distinct: d, expr: c }; }, peg$c299 = function() { return { type: 'star', value: '*' }; }, peg$c300 = function(name, l, bc) { return { type: 'function', name: name, args: l ? l: { type: 'expr_list', value: [] }, over: bc }; }, peg$c301 = function(name, l) { return { type: 'function', name: name, args: l ? l: { type: 'expr_list', value: [] } }; }, peg$c302 = function(name) { return { type: 'function', name: name, args: { type: 'expr_list', value: [] } }; }, peg$c303 = function(e, t) { return { type: 'cast', expr: e, symbol: 'as', target: t }; }, peg$c304 = function(e, precision) { return { type: 'cast', expr: e, symbol: 'as', target: { dataType: 'DECIMAL(' + precision + ')' } }; }, peg$c305 = function(e, precision, scale) { return { type: 'cast', expr: e, symbol: 'as', target: { dataType: 'DECIMAL(' + precision + ', ' + scale + ')' } }; }, peg$c306 = function(e, s, t) { /* MySQL cast to un-/signed integer */ return { type: 'cast', expr: e, symbol: 'as', target: { dataType: s + (t ? ' ' + t: '') } }; }, peg$c307 = function() { return { type: 'null', value: null }; }, peg$c308 = function() { return { type: 'not null', value: 'not null', } }, peg$c309 = function() { return { type: 'bool', value: true }; }, peg$c310 = function() { return { type: 'bool', value: false }; }, peg$c311 = function(ca) { return { type: 'string', value: ca[1].join('') }; }, peg$c312 = function(type, ca) { return { type: type.toLowerCase(), value: ca[1].join('') }; }, peg$c313 = function(type, lf, rt) { if (lf && rt) return true }, peg$c314 = "on update current_timestamp", peg$c315 = peg$literalExpectation("ON UPDATE CURRENT_TIMESTAMP", true), peg$c316 = function(type, lf, rt, up) { return { type: 'origin', value: (up ? `${type} ${up}` : type).toLowerCase() }; }, peg$c317 = /^[^"\\\0-\x1F\x7F]/, peg$c318 = peg$classExpectation(["\"", "\\", ["\0", "\x1F"], "\x7F"], true, false), peg$c319 = /^[^'\\]/, peg$c320 = peg$classExpectation(["'", "\\"], true, false), peg$c321 = "\\'", peg$c322 = peg$literalExpectation("\\'", false), peg$c323 = function() { return "'"; }, peg$c324 = "\\\"", peg$c325 = peg$literalExpectation("\\\"", false), peg$c326 = function() { return '"'; }, peg$c327 = "\\\\", peg$c328 = peg$literalExpectation("\\\\", false), peg$c329 = function() { return "\\"; }, peg$c330 = "\\/", peg$c331 = peg$literalExpectation("\\/", false), peg$c332 = function() { return "/"; }, peg$c333 = "\\b", peg$c334 = peg$literalExpectation("\\b", false), peg$c335 = function() { return "\b"; }, peg$c336 = "\\f", peg$c337 = peg$literalExpectation("\\f", false), peg$c338 = function() { return "\f"; }, peg$c339 = "\\n", peg$c340 = peg$literalExpectation("\\n", false), peg$c341 = function() { return "\n"; }, peg$c342 = "\\r", peg$c343 = peg$literalExpectation("\\r", false), peg$c344 = function() { return "\r"; }, peg$c345 = "\\t", peg$c346 = peg$literalExpectation("\\t", false), peg$c347 = function() { return "\t"; }, peg$c348 = "\\u", peg$c349 = peg$literalExpectation("\\u", false), peg$c350 = function(h1, h2, h3, h4) { return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4)); }, peg$c351 = "\\", peg$c352 = peg$literalExpectation("\\", false), peg$c353 = /^[\n\r]/, peg$c354 = peg$classExpectation(["\n", "\r"], false, false), peg$c355 = function(n) { if (n && n.type === 'bigint') return n return { type: 'number', value: n }; }, peg$c356 = function(int_, frac, exp) { const numStr = int_ + frac + exp return { type: 'bigint', value: numStr } }, peg$c357 = function(int_, frac) { const numStr = int_ + frac if (isBigInt(int_)) return { type: 'bigint', value: numStr } return parseFloat(numStr); }, peg$c358 = function(int_, exp) { const numStr = int_ + exp return { type: 'bigint', value: numStr } }, peg$c359 = function(int_) { if (isBigInt(int_)) return { type: 'bigint', value: int_ } return parseFloat(int_); }, peg$c360 = function(op, digits) { return "-" + digits; }, peg$c361 = function(op, digit) { return "-" + digit; }, peg$c362 = ".", peg$c363 = peg$literalExpectation(".", false), peg$c364 = function(digits) { return "." + digits; }, peg$c365 = function(e, digits) { return e + digits; }, peg$c366 = function(digits) { return digits.join(""); }, peg$c367 = /^[0-9]/, peg$c368 = peg$classExpectation([["0", "9"]], false, false), peg$c369 = /^[0-9a-fA-F]/, peg$c370 = peg$classExpectation([["0", "9"], ["a", "f"], ["A", "F"]], false, false), peg$c371 = /^[eE]/, peg$c372 = peg$classExpectation(["e", "E"], false, false), peg$c373 = /^[+\-]/, peg$c374 = peg$classExpectation(["+", "-"], false, false), peg$c375 = function(e, sign) { return e + (sign !== null ? sign: ''); }, peg$c376 = "null", peg$c377 = peg$literalExpectation("NULL", true), peg$c378 = "not null", peg$c379 = peg$literalExpectation("NOT NULL", true), peg$c380 = "true", peg$c381 = peg$literalExpectation("TRUE", true), peg$c382 = "to", peg$c383 = peg$literalExpectation("TO", true), peg$c384 = "false", peg$c385 = peg$literalExpectation("FALSE", true), peg$c386 = "show", peg$c387 = peg$literalExpectation("SHOW", true), peg$c388 = "drop", peg$c389 = peg$literalExpectation("DROP", true), peg$c390 = function() { return 'DROP'; }, peg$c391 = "change", peg$c392 = peg$literalExpectation("CHANGE", true), peg$c393 = function() { return 'CHANGE'; }, peg$c394 = "modify", peg$c395 = peg$literalExpectation("MODIFY", true), peg$c396 = function() { return 'MODIFY'; }, peg$c397 = "use", peg$c398 = peg$literalExpectation("USE", true), peg$c399 = "alter", peg$c400 = peg$literalExpectation("ALTER", true), peg$c401 = "select", peg$c402 = peg$literalExpectation("SELECT", true), peg$c403 = "update", peg$c404 = peg$literalExpectation("UPDATE", true), peg$c405 = "create", peg$c406 = peg$literalExpectation("CREATE", true), peg$c407 = "temporary", peg$c408 = peg$literalExpectation("TEMPORARY", true), peg$c409 = "if not exists", peg$c410 = peg$literalExpectation("IF NOT EXISTS", true), peg$c411 = "if exists", peg$c412 = peg$literalExpectation("IF EXISTS", true), peg$c413 = "delete", peg$c414 = peg$literalExpectation("DELETE", true), peg$c415 = "insert", peg$c416 = peg$literalExpectation("INSERT", true), peg$c417 = "RECURSIVE", peg$c418 = peg$literalExpectation("RECURSIVE", false), peg$c419 = "re