lite-ui-sql-parser
Version:
simple node sql parser
1,439 lines (1,411 loc) • 549 kB
JavaScript
/*
* 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 = "auto_increment",
peg$c8 = peg$literalExpectation("AUTO_INCREMENT", true),
peg$c9 = "unique",
peg$c10 = peg$literalExpectation("UNIQUE", true),
peg$c11 = "primary",
peg$c12 = peg$literalExpectation("PRIMARY", true),
peg$c13 = "key",
peg$c14 = peg$literalExpectation("KEY", true),
peg$c15 = function(c, d, n, df, a, u, co, ca, 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,
collate: ca,
column_format: cf,
storage:s,
reference_definition: re,
resource: 'column'
}
},
peg$c16 = function(ca) {
return {
type: 'collate',
value: ca,
}
},
peg$c17 = "column_format",
peg$c18 = peg$literalExpectation("COLUMN_FORMAT", true),
peg$c19 = "fixed",
peg$c20 = peg$literalExpectation("FIXED", true),
peg$c21 = "dynamic",
peg$c22 = peg$literalExpectation("DYNAMIC", true),
peg$c23 = "default",
peg$c24 = peg$literalExpectation("DEFAULT", true),
peg$c25 = function(k, f) {
return {
type: 'column_format',
value: f.toLowerCase()
}
},
peg$c26 = "storage",
peg$c27 = peg$literalExpectation("STORAGE", true),
peg$c28 = "disk",
peg$c29 = peg$literalExpectation("DISK", true),
peg$c30 = "memory",
peg$c31 = peg$literalExpectation("MEMORY", true),
peg$c32 = function(k, s) {
return {
type: 'storage',
value: s.toLowerCase()
}
},
peg$c33 = function(ce) {
return {
type: 'default',
value: ce
}
},
peg$c34 = function(a, 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
}
};
},
peg$c35 = 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$c36 = function(d) {
tableList.add(`use::${d}::null`);
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'use',
db: d
}
};
},
peg$c37 = 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$c38 = function(kc, cd) {
return {
action: 'add',
...cd,
keyword: kc,
resource: 'column',
type: 'alter',
}
},
peg$c39 = function(kc, c) {
return {
action: 'drop',
column: c,
keyword: kc,
resource: 'column',
type: 'alter',
}
},
peg$c40 = function(id) {
return {
action: 'add',
type: 'alter',
...id,
}
},
peg$c41 = function(kw, tn) {
return {
action: 'rename',
type: 'alter',
resource: 'table',
keyword: kw && kw[0].toLowerCase(),
table: tn
}
},
peg$c42 = "algorithm",
peg$c43 = peg$literalExpectation("ALGORITHM", true),
peg$c44 = "instant",
peg$c45 = peg$literalExpectation("INSTANT", true),
peg$c46 = "inplace",
peg$c47 = peg$literalExpectation("INPLACE", true),
peg$c48 = "copy",
peg$c49 = peg$literalExpectation("COPY", true),
peg$c50 = function(val) {
return {
type: 'alter',
keyword: 'algorithm',
resource: 'algorithm',
algorithm: val
}
},
peg$c51 = "lock",
peg$c52 = peg$literalExpectation("LOCK", true),
peg$c53 = "none",
peg$c54 = peg$literalExpectation("NONE", true),
peg$c55 = "shared",
peg$c56 = peg$literalExpectation("SHARED", true),
peg$c57 = "exclusive",
peg$c58 = peg$literalExpectation("EXCLUSIVE", true),
peg$c59 = function(val) {
return {
type: 'alter',
keyword: 'lock',
resource: 'lock',
lock: val
}
},
peg$c60 = function(kc, c, t, de, id) {
return {
index: c,
definition: de,
keyword: kc.toLowerCase(),
index_type: t,
resource: 'index',
index_options: id,
}
},
peg$c61 = 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$c62 = function(kc, c) {
return {
keyword: kc.toLowerCase(),
constraint: c
}
},
peg$c63 = "primary key",
peg$c64 = peg$literalExpectation("PRIMARY KEY", true),
peg$c65 = 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$c66 = 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$c67 = "foreign key",
peg$c68 = peg$literalExpectation("FOREIGN KEY", true),
peg$c69 = 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$c70 = "match full",
peg$c71 = peg$literalExpectation("MATCH FULL", true),
peg$c72 = "match partial",
peg$c73 = peg$literalExpectation("MATCH PARTIAL", true),
peg$c74 = "match simple",
peg$c75 = peg$literalExpectation("MATCH SIMPLE", true),
peg$c76 = 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$c77 = "on delete",
peg$c78 = peg$literalExpectation("ON DELETE", true),
peg$c79 = "on update",
peg$c80 = peg$literalExpectation("ON UPDATE", true),
peg$c81 = function(kw, ro) {
return {
type: kw.toLowerCase(),
value: ro
}
},
peg$c82 = "restrict",
peg$c83 = peg$literalExpectation("RESTRICT", true),
peg$c84 = "cascade",
peg$c85 = peg$literalExpectation("CASCADE", true),
peg$c86 = "set null",
peg$c87 = peg$literalExpectation("SET NULL", true),
peg$c88 = "no action",
peg$c89 = peg$literalExpectation("NO ACTION", true),
peg$c90 = "set default",
peg$c91 = peg$literalExpectation("SET DEFAULT", true),
peg$c92 = function(kc) {
return kc.toLowerCase()
},
peg$c93 = function(head, tail) {
return createList(head, tail)
},
peg$c94 = "avg_row_length",
peg$c95 = peg$literalExpectation("AVG_ROW_LENGTH", true),
peg$c96 = "key_block_size",
peg$c97 = peg$literalExpectation("KEY_BLOCK_SIZE", true),
peg$c98 = "max_rows",
peg$c99 = peg$literalExpectation("MAX_ROWS", true),
peg$c100 = "min_rows",
peg$c101 = peg$literalExpectation("MIN_ROWS", true),
peg$c102 = "stats_sample_pages",
peg$c103 = peg$literalExpectation("STATS_SAMPLE_PAGES", true),
peg$c104 = function(kw, s, v) {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: v.value
}
},
peg$c105 = "character set",
peg$c106 = peg$literalExpectation("CHARACTER SET", true),
peg$c107 = "charset",
peg$c108 = peg$literalExpectation("CHARSET", true),
peg$c109 = "collate",
peg$c110 = peg$literalExpectation("COLLATE", true),
peg$c111 = function(kw, t, s, v) {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
value: v
}
},
peg$c112 = "connection",
peg$c113 = peg$literalExpectation("CONNECTION", true),
peg$c114 = function(kw, s, c) {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: `'${c.value}'`
}
},
peg$c115 = "compression",
peg$c116 = peg$literalExpectation("COMPRESSION", true),
peg$c117 = "'",
peg$c118 = peg$literalExpectation("'", false),
peg$c119 = "zlib",
peg$c120 = peg$literalExpectation("ZLIB", true),
peg$c121 = "lz4",
peg$c122 = peg$literalExpectation("LZ4", true),
peg$c123 = function(kw, s, v) {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: v.join('').toUpperCase()
}
},
peg$c124 = "engine",
peg$c125 = peg$literalExpectation("ENGINE", true),
peg$c126 = function(kw, s, c) {
return {
keyword: kw.toLowerCase(),
symbol: s,
value: c.toUpperCase()
}
},
peg$c127 = function(fsid) {
return {
action: 'add',
type: 'alter',
...fsid,
}
},
peg$c128 = 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$c129 = function(kw, a) {
a.keyword = kw
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'set',
expr: a
}
}
},
peg$c130 = function() {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'unlock',
keyword: 'tables'
}
}
},
peg$c131 = "read",
peg$c132 = peg$literalExpectation("READ", true),
peg$c133 = "local",
peg$c134 = peg$literalExpectation("LOCAL", true),
peg$c135 = function(s) {
return {
type: 'read',
suffix: s && 'local'
}
},
peg$c136 = "low_priority",
peg$c137 = peg$literalExpectation("LOW_PRIORITY", true),
peg$c138 = "write",
peg$c139 = peg$literalExpectation("WRITE", true),
peg$c140 = function(p) {
return {
type: 'write',
prefix: p && 'low_priority'
}
},
peg$c141 = function(t, lt) {
tableList.add(`lock::${t.db}::${t.table}`)
return {
table: t,
lock_type: lt
}
},
peg$c142 = function(head, tail) {
return createList(head, tail);
},
peg$c143 = function(ltl) {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'lock',
keyword: 'tables',
tables: ltl
}
}
},
peg$c144 = function(e) {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'call',
expr: e
}
}
},
peg$c145 = "(",
peg$c146 = peg$literalExpectation("(", false),
peg$c147 = ")",
peg$c148 = peg$literalExpectation(")", false),
peg$c149 = function(s) {
return s[2];
},
peg$c150 = function(cte) {
cte.recursive = true;
return [cte]
},
peg$c151 = function(name, columns, stmt) {
return { name, stmt, columns };
},
peg$c152 = 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$c153 = function(head, tail) {
const opts = [head];
for (let i = 0, l = tail.length; i < l; ++i) {
opts.push(tail[i][1]);
}
return opts;
},
peg$c154 = function(option) { return option; },
peg$c155 = 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$c156 = function(tbl) {
columnList.add(`select::${tbl}::(.*)`);
return {
expr: {
type: 'column_ref',
table: tbl,
column: '*'
},
as: null
};
},
peg$c157 = function(e, alias) {
return { expr: e, as: alias };
},
peg$c158 = function(i) { return i; },
peg$c159 = function(l) { return l; },
peg$c160 = function(head, tail) {
return [head, tail]
},
peg$c161 = "btree",
peg$c162 = peg$literalExpectation("BTREE", true),
peg$c163 = "hash",
peg$c164 = peg$literalExpectation("HASH", true),
peg$c165 = function(t) {
return {
keyword: 'using',
type: t.toLowerCase(),
}
},
peg$c166 = function(head, tail) {
const result = [head];
for (let i = 0; i < tail.length; i++) {
result.push(tail[i][1]);
}
return result;
},
peg$c167 = function(k, e, kbs) {
return {
type: k.toLowerCase(),
symbol: e,
expr: kbs
};
},
peg$c168 = "with",
peg$c169 = peg$literalExpectation("WITH", true),
peg$c170 = "parser",
peg$c171 = peg$literalExpectation("PARSER", true),
peg$c172 = function(pn) {
return {
type: 'with parser',
expr: pn
}
},
peg$c173 = "visible",
peg$c174 = peg$literalExpectation("VISIBLE", true),
peg$c175 = "invisible",
peg$c176 = peg$literalExpectation("INVISIBLE", true),
peg$c177 = function(k) {
return {
type: k.toLowerCase(),
expr: k.toLowerCase()
}
},
peg$c178 = function(head, tail) {
tail.unshift(head);
tail.forEach(tableInfo => {
const { table, as } = tableInfo
tableAlias[table] = table
if (as) tableAlias[as] = table
})
return tail;
},
peg$c179 = function(t) { return t; },
peg$c180 = function(op, t, head, tail) {
t.join = op;
t.using = createList(head, tail);
return t;
},
peg$c181 = function(op, t, expr) {
t.join = op;
t.on = expr;
return t;
},
peg$c182 = function(op, stmt, alias, expr) {
stmt.parentheses = true;
return {
expr: stmt,
as: alias,
join: op,
on: expr
};
},
peg$c183 = function() {
return {
type: 'dual'
};
},
peg$c184 = function(t, alias) {
if (t.type === 'var') {
t.as = alias;
return t;
} else {
return {
db: t.db,
table: t.table,
as: alias
};
}
},
peg$c185 = function(stmt, alias) {
stmt.parentheses = true;
return {
expr: stmt,
as: alias
};
},
peg$c186 = function() { return 'LEFT JOIN'; },
peg$c187 = function() { return 'RIGHT JOIN'; },
peg$c188 = function() { return 'FULL JOIN'; },
peg$c189 = function() { return 'INNER JOIN'; },
peg$c190 = function(dt, tail) {
const obj = { db: null, table: dt };
if (tail !== null) {
obj.db = dt;
obj.table = tail[3];
}
return obj;
},
peg$c191 = function(v) {
v.db = null;
v.table = v.name;
return v;
},
peg$c192 = function(e) { return e; },
peg$c193 = function(e) { return e.value; },
peg$c194 = function(e, d) {
const obj = { expr: e, type: 'ASC' };
if (d === 'DESC') obj.type = 'DESC';
return obj;
},
peg$c195 = function(i1, tail) {
const res = [i1];
if (tail) res.push(tail[2]);
return {
seperator: tail && tail[0] && tail[0].toLowerCase() || '',
value: res
};
},
peg$c196 = 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$c197 = 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$c198 = "=",
peg$c199 = peg$literalExpectation("=", false),
peg$c200 = function(tbl, c, v) {
return { column: c, value: v, table: tbl && tbl[0] };
},
peg$c201 = function(head, tail) {
return createList(head, tail)
},
peg$c202 = function(v) {
return v
},
peg$c203 = function(ri, kw, ta, 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}`));
}
const tableKey = ta ? ` ${ta.toLowerCase()}` : ''
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: ri,
prefix: `${kw.toLowerCase()}${tableKey}`,
table: [t],
columns: c,
values: v,
partition: p,
}
};
},
peg$c204 = function(ri, kw, ta, t, p, v) {
if (t) {
tableList.add(`insert::${t.db}::${t.table}`)
columnList.add(`insert::${t.table}::(.*)`);
t.as = null
}
const tableKey = ta ? ` ${ta.toLowerCase()}` : ''
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: ri,
prefix: `${kw.toLowerCase()}${tableKey}`,
table: [t],
columns: null,
values: v,
partition: p,
}
};
},
peg$c205 = function() { return 'insert'; },
peg$c206 = function() { return 'replace'; },
peg$c207 = function(l) {
return l;
},
peg$c208 = function(head, tail) {
const el = { type: 'expr_list' };
el.value = createList(head, tail);
return el;
},
peg$c209 = function(e, u) {
return {
type: 'interval',
expr: e,
unit: u.toLowerCase(),
}
},
peg$c210 = function(expr, condition_list, otherwise) {
if (otherwise) condition_list.push(otherwise);
return {
type: 'case',
expr: expr || null,
args: condition_list
};
},
peg$c211 = function(condition, result) {
return {
type: 'when',
cond: condition,
result: result
};
},
peg$c212 = function(result) {
return { type: 'else', result: result };
},
peg$c213 = function(head, tail) {
return createBinaryExprChain(head, tail);
},
peg$c214 = function(op, tail) {
return createUnaryExpr(op, tail[0][1]);
},
peg$c215 = function(head, tail) {
return createBinaryExprChain(head, tail);
},
peg$c216 = "!",
peg$c217 = peg$literalExpectation("!", false),
peg$c218 = function(expr) {
return createUnaryExpr('NOT', expr);
},
peg$c219 = 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$c220 = function(op, stmt) {
stmt.parentheses = true;
return createUnaryExpr(op, stmt);
},
peg$c221 = function(nk) { return nk[0] + ' ' + nk[2]; },
peg$c222 = function(l) {
return { type: 'arithmetic', tail: l };
},
peg$c223 = ">=",
peg$c224 = peg$literalExpectation(">=", false),
peg$c225 = ">",
peg$c226 = peg$literalExpectation(">", false),
peg$c227 = "<=",
peg$c228 = peg$literalExpectation("<=", false),
peg$c229 = "<>",
peg$c230 = peg$literalExpectation("<>", false),
peg$c231 = "<",
peg$c232 = peg$literalExpectation("<", false),
peg$c233 = "!=",
peg$c234 = peg$literalExpectation("!=", false),
peg$c235 = function(right) {
return { op: 'IS', right: right };
},
peg$c236 = function(right) {
return { op: 'IS NOT', right: right };
},
peg$c237 = function(op, begin, end) {
return {
op: op,
right: {
type: 'expr_list',
value: [begin, end]
}
};
},
peg$c238 = function(op, right) {
return { op: op, right: right };
},
peg$c239 = function(op, l) {
return { op: op, right: l };
},
peg$c240 = function(op, e) {
return { op: op, right: e };
},
peg$c241 = "+",
peg$c242 = peg$literalExpectation("+", false),
peg$c243 = "-",
peg$c244 = peg$literalExpectation("-", false),
peg$c245 = function(head, tail) {
return createBinaryExprChain(head, tail)
},
peg$c246 = "*",
peg$c247 = peg$literalExpectation("*", false),
peg$c248 = "/",
peg$c249 = peg$literalExpectation("/", false),
peg$c250 = "%",
peg$c251 = peg$literalExpectation("%", false),
peg$c252 = function(e) {
e.parentheses = true;
return e;
},
peg$c253 = function(list) {
list.parentheses = true;
return list;
},
peg$c254 = function(tbl, col) {
columnList.add(`select::${tbl}::${col}`);
return {
type: 'column_ref',
table: tbl,
column: col
};
},
peg$c255 = function(col) {
columnList.add(`select::null::${col}`);
return {
type: 'column_ref',
table: null,
column: col
};
},
peg$c256 = function(name) { return reservedMap[name.toUpperCase()] === true; },
peg$c257 = function(name) {
return name;
},
peg$c258 = 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$c259 = "\"",
peg$c260 = peg$literalExpectation("\"", false),
peg$c261 = /^[^"]/,
peg$c262 = peg$classExpectation(["\""], true, false),
peg$c263 = function(chars) { return chars.join(''); },
peg$c264 = /^[^']/,
peg$c265 = peg$classExpectation(["'"], true, false),
peg$c266 = "`",
peg$c267 = peg$literalExpectation("`", false),
peg$c268 = /^[^`]/,
peg$c269 = peg$classExpectation(["`"], true, false),
peg$c270 = function(name) { return name; },
peg$c271 = function(start, parts) { return start + parts.join(''); },
peg$c272 = /^[A-Za-z_]/,
peg$c273 = peg$classExpectation([["A", "Z"], ["a", "z"], "_"], false, false),
peg$c274 = /^[A-Za-z0-9_]/,
peg$c275 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_"], false, false),
peg$c276 = /^[A-Za-z0-9_:]/,
peg$c277 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", ":"], false, false),
peg$c278 = ":",
peg$c279 = peg$literalExpectation(":", false),
peg$c280 = function(l) {
return { type: 'param', value: l[1] };
},
peg$c281 = function(name, e) {
return {
type: 'aggr_func',
name: name,
args: {
expr: e
}
};
},
peg$c282 = function(name, arg) {
return {
type: 'aggr_func',
name: name,
args: arg
};
},
peg$c283 = function(e) { return { expr: e }; },
peg$c284 = function(d, c) { return { distinct: d, expr: c }; },
peg$c285 = function() { return { type: 'star', value: '*' }; },
peg$c286 = function(name, l) {
return {
type: 'function',
name: name,
args: l ? l: { type: 'expr_list', value: [] }
};
},
peg$c287 = function(name) {
return {
type: 'function',
name: name,
args: { type: 'expr_list', value: [] }
};
},
peg$c288 = function(e, t) {
return {
type: 'cast',
expr: e,
symbol: 'as',
target: t
};
},
peg$c289 = function(e, precision) {
return {
type: 'cast',
expr: e,
symbol: 'as',
target: {
dataType: 'DECIMAL(' + precision + ')'
}
};
},
peg$c290 = function(e, precision, scale) {
return {
type: 'cast',
expr: e,
symbol: 'as',
target: {
dataType: 'DECIMAL(' + precision + ', ' + scale + ')'
}
};
},
peg$c291 = function(e, s, t) { /* MySQL cast to un-/signed integer */
return {
type: 'cast',
expr: e,
symbol: 'as',
target: {
dataType: s + (t ? ' ' + t: '')
}
};
},
peg$c292 = function() {
return { type: 'null', value: null };
},
peg$c293 = function() {
return {
type: 'not null',
value: 'not null',
}
},
peg$c294 = function() {
return { type: 'bool', value: true };
},
peg$c295 = function() {
return { type: 'bool', value: false };
},
peg$c296 = function(ca) {
return {
type: 'string',
value: ca[1].join('')
};
},
peg$c297 = function(type, ca) {
return {
type: type.toLowerCase(),
value: ca[1].join('')
};
},
peg$c298 = function(type, lf, rt) { if (lf && rt) return true },
peg$c299 = "on update current_timestamp",
peg$c300 = peg$literalExpectation("ON UPDATE CURRENT_TIMESTAMP", true),
peg$c301 = function(type, lf, rt, up) {
return {
type: 'origin',
value: (up ? `${type} ${up}` : type).toLowerCase()
};
},
peg$c302 = /^[^"\\\0-\x1F\x7F]/,
peg$c303 = peg$classExpectation(["\"", "\\", ["\0", "\x1F"], "\x7F"], true, false),
peg$c304 = /^[^'\\]/,
peg$c305 = peg$classExpectation(["'", "\\"], true, false),
peg$c306 = "\\'",
peg$c307 = peg$literalExpectation("\\'", false),
peg$c308 = function() { return "'"; },
peg$c309 = "\\\"",
peg$c310 = peg$literalExpectation("\\\"", false),
peg$c311 = function() { return '"'; },
peg$c312 = "\\\\",
peg$c313 = peg$literalExpectation("\\\\", false),
peg$c314 = function() { return "\\"; },
peg$c315 = "\\/",
peg$c316 = peg$literalExpectation("\\/", false),
peg$c317 = function() { return "/"; },
peg$c318 = "\\b",
peg$c319 = peg$literalExpectation("\\b", false),
peg$c320 = function() { return "\b"; },
peg$c321 = "\\f",
peg$c322 = peg$literalExpectation("\\f", false),
peg$c323 = function() { return "\f"; },
peg$c324 = "\\n",
peg$c325 = peg$literalExpectation("\\n", false),
peg$c326 = function() { return "\n"; },
peg$c327 = "\\r",
peg$c328 = peg$literalExpectation("\\r", false),
peg$c329 = function() { return "\r"; },
peg$c330 = "\\t",
peg$c331 = peg$literalExpectation("\\t", false),
peg$c332 = function() { return "\t"; },
peg$c333 = "\\u",
peg$c334 = peg$literalExpectation("\\u", false),
peg$c335 = function(h1, h2, h3, h4) {
return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4));
},
peg$c336 = "\\",
peg$c337 = peg$literalExpectation("\\", false),
peg$c338 = /^[\n\r]/,
peg$c339 = peg$classExpectation(["\n", "\r"], false, false),
peg$c340 = function(n) {
if (n && n.type === 'bigint') return n
return { type: 'number', value: n };
},
peg$c341 = function(int_, frac, exp) {
const numStr = int_ + frac + exp
return {
type: 'bigint',
value: numStr
}
},
peg$c342 = function(int_, frac) {
const numStr = int_ + frac
if (isBigInt(int_)) return {
type: 'bigint',
value: numStr
}
return parseFloat(numStr);
},
peg$c343 = function(int_, exp) {
const numStr = int_ + exp
return {
type: 'bigint',
value: numStr
}
},
peg$c344 = function(int_) {
if (isBigInt(int_)) return {
type: 'bigint',
value: int_
}
return parseFloat(int_);
},
peg$c345 = function(op, digits) { return "-" + digits; },
peg$c346 = function(op, digit) { return "-" + digit; },
peg$c347 = ".",
peg$c348 = peg$literalExpectation(".", false),
peg$c349 = function(digits) { return "." + digits; },
peg$c350 = function(e, digits) { return e + digits; },
peg$c351 = function(digits) { return digits.join(""); },
peg$c352 = /^[0-9]/,
peg$c353 = peg$classExpectation([["0", "9"]], false, false),
peg$c354 = /^[0-9a-fA-F]/,
peg$c355 = peg$classExpectation([["0", "9"], ["a", "f"], ["A", "F"]], false, false),
peg$c356 = /^[eE]/,
peg$c357 = peg$classExpectation(["e", "E"], false, false),
peg$c358 = /^[+\-]/,
peg$c359 = peg$classExpectation(["+", "-"], false, false),
peg$c360 = function(e, sign) { return e + (sign !== null ? sign: ''); },
peg$c361 = "null",
peg$c362 = peg$literalExpectation("NULL", true),
peg$c363 = "not null",
peg$c364 = peg$literalExpectation("NOT NULL", true),
peg$c365 = "true",
peg$c366 = peg$literalExpectation("TRUE", true),
peg$c367 = "to",
peg$c368 = peg$literalExpectation("TO", true),
peg$c369 = "false",
peg$c370 = peg$literalExpectation("FALSE", true),
peg$c371 = "show",
peg$c372 = peg$literalExpectation("SHOW", true),
peg$c373 = "drop",
peg$c374 = peg$literalExpectation("DROP", true),
peg$c375 = function() { return 'DROP'; },
peg$c376 = "use",
peg$c377 = peg$literalExpectation("USE", true),
peg$c378 = "alter",
peg$c379 = peg$literalExpectation("ALTER", true),
peg$c380 = "select",
peg$c381 = peg$literalExpectation("SELECT", true),
peg$c382 = "update",
peg$c383 = peg$literalExpectation("UPDATE", true),
peg$c384 = "create",
peg$c385 = peg$literalExpectation("CREATE", true),
peg$c386 = "temporary",
peg$c387 = peg$literalExpectation("TEMPORARY", true),
peg$c388 = "if not exists",
peg$c389 = peg$literalExpectation("IF NOT EXISTS", true),
peg$c390 = "delete",
peg$c391 = peg$literalExpectation("DELETE", true),
peg$c392 = "insert",
peg$c393 = peg$literalExpectation("INSERT", true),
peg$c394 = "RECURSIVE",
peg$c395 = peg$literalExpectation("RECURSIVE", false),
peg$c396 = "replace",
peg$c397 = peg$literalExpectation("REPLACE", true),
peg$c398 = "rename",
peg$c399 = peg$literalExpectation("RENAME", true),
peg$c400 = "ignore",
peg$c401 = peg$literalExpectation("IGNORE", true),
peg$c402 = "explain",
peg$c403 = peg$literalExpectation("EXPLAIN", true),
peg$c404 = "partition",
peg$c405 = peg$literalExpectation("PARTITION", true),
peg$c406 = function() { return 'PARTITION' },
peg$c407 = "into",
peg$c408 = peg$literalExpectation("INTO", true),
peg$c409 = function() { return 'INTO'; },
peg$c410 = "overwrite",
peg$c411 = peg$literalExpectation("OVERWRITE", true),
peg$c412 = function() { return 'OVERWRITE'; },
peg$c413 = "from",
peg$c414 = peg$literalExpectation("FROM", true),
peg$c415 = "set",
peg$c416 = peg$literalExpectation("SET", true),
peg$c417 = "unlock",
peg$c418 = peg$literalExpectation("UNLOCK", true),
peg$c419 = "as",
peg$c420 = peg$literalExpectation("AS", true),
peg$c421 = "table",
peg$c422 = peg$literalExpectation("TABLE", true),
peg$c423 = function() { return 'TABLE'; },
peg$c424 = "tables",
peg$c425 = peg$literalExpectation("TABLES", true),
peg$c426 = function() { return 'TABLES'; },
peg$c427 = function() { return 'COLLATE'; },
peg$c428 = "on",
peg$c429 = peg$literalExpectation("ON", true),
peg$c430 = "left",
peg$c431 = peg$literalExpectation("LEFT", true),
peg$c432 = "right",
peg$c433 = peg$literalExpectation("RIGHT", true),
peg$c434 = "full",
peg$c435 = peg$literalExpectation("FULL", true),
peg$c436 = "inner",
peg$c437 = peg$literalExpectation("INNER", true),
peg$c438 = "join",
peg$c439 = peg$literalExpectation("JOIN", true),
peg$c440 = "outer",
peg$c441 = peg$literalExpectation("OUTER", true),
peg$c442 = "union",
peg$c443 = peg$literalExpectation("UNION", true),
peg$c444 = "values",
peg$c445 = peg$literalExpectation("VALUES", true),
peg$c446 = "using",
peg$c447 = peg$literalExpectation("USING", true),
peg$c448 = "where",
peg$c449 = peg$literalExpectation("WHERE", true),
peg$c450 = "group",
peg$c451 = peg$literalExpectation("GROUP", true),
peg$c452 = "by",
peg$c453 = peg$literalExpectation("BY", true),
peg$c454 = "order",
peg$c455 = peg$literalExpectation("ORDER", true),
peg$c456 = "having",
peg$c457 = peg$literalExpectation("HAVING", true),
peg$c458 = "limit",
peg$c459 = peg$literalExpectation("LIMIT", true),
peg$c460 = "offset",
peg$c461 = peg$literalExpectation("OFFSET", true),
peg$c462 = function() { return 'OFFSET'; },
peg$c463 = "asc",
peg$c464 = peg$literalExpectation("ASC", true),
peg$c465 = function() { return 'ASC'; },
peg$c466 = "desc",
peg$c467 = peg$literalExpectation("DESC", true),
peg$c468 = function() { return 'DESC'; },
peg$c469 = "all",
peg$c470 = peg$literalExpectation("ALL", true),
peg$c471 = function() { return 'ALL'; },
peg$c472 = "distinct",
peg$c473 = peg$literalExpectation("DISTINCT", true),
peg$c474 = function() { return 'DISTINCT';},
peg$c475 = "between",
peg$c476 = peg$literalExpectation("BETWEEN", true),
peg$c477 = function() { return 'BETWEEN'; },
peg$c478 = "in",
peg$c479 = peg$literalExpectation("IN", true),
peg$c480 = function() { return 'IN'; },
peg$c481 = "is",
peg$c482 = peg$literal