@dbml/core
Version:
> TODO: description
1,551 lines (1,547 loc) • 268 kB
JavaScript
/*
* Generated by PEG.js 0.10.0.
*
* http://pegjs.org/
*/
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var _ = require("lodash"),
pluralize = require("pluralize");
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 literal(expectation) {
return "\"" + literalEscape(expectation.text) + "\"";
},
"class": function _class(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 any(expectation) {
return "any character";
},
end: function end(expectation) {
return "end of input";
},
other: function other(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 = {
Rule: peg$parseRule
},
peg$startRuleFunction = peg$parseRule,
peg$c0 = function peg$c0() {
return {
tables: tables,
refs: refs,
enums: enums
};
},
peg$c1 = function peg$c1(t) {
tables.push(t);
},
peg$c2 = "(",
peg$c3 = peg$literalExpectation("(", false),
peg$c4 = ")",
peg$c5 = peg$literalExpectation(")", false),
peg$c6 = function peg$c6(table_name, body, options) {
var fields = body.fields;
var indexes = body.indexes;
var bodyRefs = body.refs;
fields.forEach(function (field) {
// process enum: rename enum and push to array `enums`
if (field.type.type_name.toLowerCase() === 'enum') {
var enumValuesArr = field.type.args.split(/[\s\r\n\n]*,[\s\r\n\n]*/);
var values = [];
enumValuesArr.forEach(function (ele) {
var newValue = ele.replace(/'|"|`/g, "").trim();
var enumValue = {
name: newValue
};
values.push(enumValue);
});
var _enum = {
name: "".concat(table_name.schemaName ? "".concat(table_name.schemaName, "_") : '').concat(table_name.name, "_").concat(field.name, "_enum"),
values: values
};
enums.push(_enum);
field.type.type_name = _enum.name;
}
});
bodyRefs.forEach(function (ref) {
ref.endpoints[0].tableName = table_name.name;
ref.endpoints[0].schemaName = table_name.schemaName;
ref.endpoints[0].relation = '*';
refs.push(ref);
});
var res = _objectSpread(_objectSpread({}, table_name), {}, {
fields: fields
});
if (options && options.comment) res.note = {
value: options.comment
};
if (indexes) res.indexes = indexes;
// return statement
return res;
},
peg$c7 = function peg$c7(lines) {
// classify lines into pk, fk, unique, index and fields
var pks = _.flatten(lines.filter(function (l) {
return l.type === "pk";
}).map(function (l) {
return l.pk;
}));
var fks = _.flatten(lines.filter(function (l) {
return l.type === "fk";
}).map(function (l) {
return l.fk;
}));
var indexes = _.flatten(lines.filter(function (l) {
return l.type === "index";
}).map(function (l) {
return l.index;
}));
var fields = lines.filter(function (l) {
return l.type === "field";
}).map(function (l) {
return l.field;
});
var refs = [];
// process primary key. If it's composite key, push it into indexes
if (pks.length > 1) {
var index = {
columns: pks.map(function (field) {
return {
value: field,
type: 'column'
};
}),
pk: true
};
indexes.push(index);
} else {
pks.forEach(function (key) {
return fields.find(function (f) {
return f.name === key;
}).pk = true;
});
}
// Set inline_ref for fields
fks.map(function (key) {
var field = fields.find(function (f) {
return f.name === key.endpoints[0].fieldNames[0];
});
if (!field.inline_ref) {
field.inline_ref = [];
}
field.inline_ref.push({
endpoint: key.endpoints[1],
onUpdate: key.onUpdate,
onDelete: key.onDelete
});
});
return {
fields: fields,
indexes: indexes,
refs: fks
};
},
peg$c8 = peg$otherExpectation("fields"),
peg$c9 = function peg$c9(pk) {
return {
type: "pk",
pk: pk
};
},
peg$c10 = function peg$c10(fk) {
return {
type: "fk",
fk: fk
};
},
peg$c11 = function peg$c11(index) {
return {
type: "index",
index: index
};
},
peg$c12 = function peg$c12(field) {
return {
type: "field",
field: field
};
},
peg$c13 = function peg$c13(names) {
return names;
},
peg$c14 = "constraint",
peg$c15 = peg$literalExpectation("CONSTRAINT", true),
peg$c16 = function peg$c16(constraint, fields, table2, fields2, fkActions) {
var name = constraint ? constraint[2] : null;
var fkObj = {
name: name,
endpoints: [{
tableName: null,
fieldNames: fields,
relation: "*"
}, {
tableName: table2.name,
schemaName: table2.schemaName,
fieldNames: fields2,
relation: "1"
}]
};
fkActions.forEach(function (fkAction) {
if (fkAction.type === 'delete') {
fkObj.onDelete = fkAction.action;
return;
}
fkObj.onUpdate = fkAction.action;
});
return fkObj;
},
peg$c17 = "on",
peg$c18 = peg$literalExpectation("ON", true),
peg$c19 = "update",
peg$c20 = peg$literalExpectation("UPDATE", true),
peg$c21 = "delete",
peg$c22 = peg$literalExpectation("DELETE", true),
peg$c23 = function peg$c23(type, action) {
return {
type: type.toLowerCase(),
action: action.toLowerCase()
};
},
peg$c24 = "unique",
peg$c25 = peg$literalExpectation("UNIQUE", true),
peg$c26 = "key",
peg$c27 = peg$literalExpectation("KEY", true),
peg$c28 = function peg$c28(name) {
return name;
},
peg$c29 = function peg$c29(unique, name, type1, columns, type2) {
var index = {
columns: columns
};
if (name) {
index.name = name;
}
if (unique) {
index.unique = true;
}
if (type2 && type2[1] && type2[1].type === 'index_type' && type2[1].value) index.type = type2[1].value;else if (type1) index.type = type1;
return index;
},
peg$c30 = "index",
peg$c31 = peg$literalExpectation("INDEX", true),
peg$c32 = function peg$c32(unique) {
return unique;
},
peg$c33 = function peg$c33() {
return "unique";
},
peg$c34 = function peg$c34(first, rest) {
return [first].concat(_toConsumableArray(rest.map(function (r) {
return r[2];
}))).join(",");
},
peg$c35 = "ASC",
peg$c36 = peg$literalExpectation("ASC", false),
peg$c37 = "DESC",
peg$c38 = peg$literalExpectation("DESC", false),
peg$c39 = function peg$c39(column) {
return column.type_name;
},
peg$c40 = function peg$c40(name, type, fieldSettings) {
var field = {
name: name,
type: type
};
if (fieldSettings) {
Object.assign(field, fieldSettings);
}
return field;
},
peg$c41 = function peg$c41(fieldSettingsArray) {
var fieldSettings = {};
fieldSettingsArray.forEach(function (field) {
if (field === "null") fieldSettings["not_null"] = false;else if (field.type === "default" && field.value) fieldSettings.dbdefault = field.value;else if (field.type === "comment" && field.value) fieldSettings.note = {
value: field.value
};else if (field !== "not_supported") {
fieldSettings[field] = true;
}
});
return fieldSettings;
},
peg$c42 = peg$otherExpectation("field setting"),
peg$c43 = "not",
peg$c44 = peg$literalExpectation("NOT", true),
peg$c45 = "null",
peg$c46 = peg$literalExpectation("NULL", true),
peg$c47 = function peg$c47(a) {
return "not_null";
},
peg$c48 = function peg$c48(a) {
return "null";
},
peg$c49 = function peg$c49(a) {
return "pk";
},
peg$c50 = function peg$c50(a) {
return "unique";
},
peg$c51 = "auto_increment",
peg$c52 = peg$literalExpectation("AUTO_INCREMENT", true),
peg$c53 = function peg$c53(a) {
return "increment";
},
peg$c54 = "unsigned",
peg$c55 = peg$literalExpectation("UNSIGNED", true),
peg$c56 = function peg$c56(a) {
return "unsigned";
},
peg$c57 = "collate",
peg$c58 = peg$literalExpectation("COLLATE", true),
peg$c59 = "column_format",
peg$c60 = peg$literalExpectation("COLUMN_FORMAT", true),
peg$c61 = "storage",
peg$c62 = peg$literalExpectation("STORAGE", true),
peg$c63 = "check",
peg$c64 = peg$literalExpectation("CHECK", true),
peg$c65 = "generated_always",
peg$c66 = peg$literalExpectation("GENERATED_ALWAYS", true),
peg$c67 = "as",
peg$c68 = peg$literalExpectation("AS", true),
peg$c69 = "virtual",
peg$c70 = peg$literalExpectation("VIRTUAL", true),
peg$c71 = "stored",
peg$c72 = peg$literalExpectation("STORED", true),
peg$c73 = "character",
peg$c74 = peg$literalExpectation("CHARACTER", true),
peg$c75 = "set",
peg$c76 = peg$literalExpectation("SET", true),
peg$c77 = "charset",
peg$c78 = peg$literalExpectation("CHARSET", true),
peg$c79 = function peg$c79() {
return "not_supported";
},
peg$c80 = function peg$c80(v) {
return {
type: "default",
value: v
};
},
peg$c81 = function peg$c81(v) {
return {
type: "comment",
value: v
};
},
peg$c82 = "default",
peg$c83 = peg$literalExpectation("DEFAULT", true),
peg$c84 = function peg$c84(val) {
return val;
},
peg$c85 = function peg$c85(val) {
return {
value: val,
type: 'string'
};
},
peg$c86 = function peg$c86(val) {
return {
value: val,
type: 'number'
};
},
peg$c87 = "true",
peg$c88 = peg$literalExpectation("TRUE", true),
peg$c89 = "false",
peg$c90 = peg$literalExpectation("FALSE", true),
peg$c91 = function peg$c91(val) {
return {
value: val,
type: 'boolean'
};
},
peg$c92 = function peg$c92(val) {
var str = val;
if (val && val.length > 2 && val[0] === '(' && val[val.length - 1] === ')') {
str = val.slice(1, -1);
}
return {
value: str,
type: 'expression'
};
},
peg$c93 = function peg$c93(first, rest) {
var options = first;
rest.forEach(function (r) {
return Object.assign(options, r[2]);
});
return options;
},
peg$c94 = peg$otherExpectation("table option"),
peg$c95 = "=",
peg$c96 = peg$literalExpectation("=", false),
peg$c97 = function peg$c97(auto_increment) {
return {
auto_increment: auto_increment
};
},
peg$c98 = "avg_row_length",
peg$c99 = peg$literalExpectation("AVG_ROW_LENGTH", true),
peg$c100 = function peg$c100(avg_row_length) {
return {
avg_row_length: avg_row_length
};
},
peg$c101 = function peg$c101(charset_name) {
return {
charset_name: charset_name
};
},
peg$c102 = function peg$c102(collation_name) {
return {
collation_name: collation_name
};
},
peg$c103 = "compression",
peg$c104 = peg$literalExpectation("COMPRESSION", true),
peg$c105 = "zlib",
peg$c106 = peg$literalExpectation("ZLIB", true),
peg$c107 = "lz4",
peg$c108 = peg$literalExpectation("LZ4", true),
peg$c109 = "none",
peg$c110 = peg$literalExpectation("NONE", true),
peg$c111 = function peg$c111(compression) {
return {
compression: compression.toUpperCase()
};
},
peg$c112 = "connection",
peg$c113 = peg$literalExpectation("CONNECTION", true),
peg$c114 = "connect_string",
peg$c115 = peg$literalExpectation("connect_string", false),
peg$c116 = function peg$c116(connect_string) {
return {
connect_string: connect_string
};
},
peg$c117 = "encryption",
peg$c118 = peg$literalExpectation("ENCRYPTION", true),
peg$c119 = "y",
peg$c120 = peg$literalExpectation("Y", true),
peg$c121 = "n",
peg$c122 = peg$literalExpectation("N", true),
peg$c123 = function peg$c123(encryption) {
return {
encryption: encryption.toUpperCase()
};
},
peg$c124 = "engine",
peg$c125 = peg$literalExpectation("ENGINE", true),
peg$c126 = function peg$c126(engine) {
return {
engine: engine
};
},
peg$c127 = "insert_method",
peg$c128 = peg$literalExpectation("INSERT_METHOD", true),
peg$c129 = "no",
peg$c130 = peg$literalExpectation("NO", true),
peg$c131 = "first",
peg$c132 = peg$literalExpectation("FIRST", true),
peg$c133 = "last",
peg$c134 = peg$literalExpectation("LAST", true),
peg$c135 = function peg$c135(insert_method) {
return {
insert_method: insert_method.toUpperCase()
};
},
peg$c136 = "max_rows",
peg$c137 = peg$literalExpectation("MAX_ROWS", true),
peg$c138 = function peg$c138(max_rows) {
return {
max_rows: max_rows
};
},
peg$c139 = "min_rows",
peg$c140 = peg$literalExpectation("MIN_ROWS", true),
peg$c141 = function peg$c141(min_rows) {
return {
min_rows: min_rows
};
},
peg$c142 = "tablespace",
peg$c143 = peg$literalExpectation("TABLESPACE", true),
peg$c144 = "STORAGE",
peg$c145 = peg$literalExpectation("STORAGE", false),
peg$c146 = "disk",
peg$c147 = peg$literalExpectation("DISK", true),
peg$c148 = "memory",
peg$c149 = peg$literalExpectation("MEMORY", true),
peg$c150 = function peg$c150(tablespace) {
return {
tablespace: tablespace
};
},
peg$c151 = function peg$c151(comment) {
return {
comment: comment
};
},
peg$c152 = "enforce",
peg$c153 = peg$literalExpectation("ENFORCE", true),
peg$c154 = function peg$c154(table, options) {
var fks = _.flatten(options.filter(function (o) {
return o.type === "add_fk";
}).map(function (o) {
return o.fks;
}));
fks.forEach(function (fk) {
fk.endpoints[0].tableName = table.name;
fk.endpoints[0].schemaName = table.schemaName;
});
refs.push.apply(refs, _toConsumableArray(fks));
var pks = _.flatten(options.filter(function (o) {
return o.type === "add_pk";
}).map(function (o) {
return o.pks;
}));
var tableAlter = findTable(table.schemaName, table.name);
var index = {
columns: pks.map(function (field) {
return {
value: field,
type: 'column'
};
}),
pk: true
};
if (pks.length > 1) {
if (tableAlter.indexes) {
tableAlter.indexes.push(index);
} else {
tableAlter.indexes = [index];
}
} else if (pks.length === 1) {
var pkField = tableAlter.fields.find(function (field) {
return field.name === pks[0];
});
pkField.pk = true;
}
},
peg$c155 = "add",
peg$c156 = peg$literalExpectation("ADD", true),
peg$c157 = function peg$c157(fks) {
return {
type: "add_fk",
fks: fks
};
},
peg$c158 = function peg$c158(pks) {
return {
type: "add_pk",
pks: pks
};
},
peg$c159 = "column",
peg$c160 = peg$literalExpectation("COLUMN", true),
peg$c161 = function peg$c161(col_name, col_type) {
return {
type: "add_column",
field: {
col_name: col_name,
col_type: col_type
}
};
},
peg$c162 = function peg$c162(index_name, column) {
return {
type: "add_index",
index: {
column: column
}
};
},
peg$c163 = "change",
peg$c164 = peg$literalExpectation("CHANGE", true),
peg$c165 = /^[^;]/,
peg$c166 = peg$classExpectation([";"], true, false),
peg$c167 = "drop",
peg$c168 = peg$literalExpectation("DROP", true),
peg$c169 = function peg$c169(constraint, indexName, tableName, indexType, columns, option) {
var index = {
columns: columns
};
var typeInOption = option && option.type === "index_type" ? option.value : null;
if (indexName) index.name = indexName;
if (constraint.toLowerCase() === "unique") index.unique = true;
var type = typeInOption || indexType;
if (type) index.type = type;
var table = findTable(tableName.schemaName, tableName.name);
if (table.indexes) {
table.indexes.push(index);
} else {
table.indexes = [index];
}
},
peg$c170 = function peg$c170(e) {
return e;
},
peg$c171 = function peg$c171(first, rest) {
return [first].concat(_toConsumableArray(rest.map(function (r) {
return r[3];
})));
},
peg$c172 = function peg$c172(c, c1) {
return {
value: "".concat(c, "(").concat(removeReduntdantSpNewline(_.flattenDeep(c1).join("")), ")"),
type: "expression"
};
},
peg$c173 = function peg$c173(c) {
return {
value: "".concat(c),
type: "string"
};
},
peg$c174 = function peg$c174(e) {
return {
value: "".concat(e),
type: "expression"
};
},
peg$c175 = "COLLATE",
peg$c176 = peg$literalExpectation("COLLATE", false),
peg$c177 = "NULLS",
peg$c178 = peg$literalExpectation("NULLS", false),
peg$c179 = "FIRST",
peg$c180 = peg$literalExpectation("FIRST", false),
peg$c181 = "LAST",
peg$c182 = peg$literalExpectation("LAST", false),
peg$c183 = function peg$c183(columns) {
return columns;
},
peg$c184 = "key_block_size",
peg$c185 = peg$literalExpectation("KEY_BLOCK_SIZE", true),
peg$c186 = "with",
peg$c187 = peg$literalExpectation("WITH", true),
peg$c188 = "parser",
peg$c189 = peg$literalExpectation("PARSER", true),
peg$c190 = "comment",
peg$c191 = peg$literalExpectation("COMMENT", true),
peg$c192 = "string",
peg$c193 = peg$literalExpectation("string", false),
peg$c194 = "visible",
peg$c195 = peg$literalExpectation("VISIBLE", true),
peg$c196 = "invisible",
peg$c197 = peg$literalExpectation("INVISIBLE", true),
peg$c198 = function peg$c198(type) {
return {
type: "index_type",
value: type
};
},
peg$c199 = "alter",
peg$c200 = peg$literalExpectation("ALTER", true),
peg$c201 = peg$anyExpectation(),
peg$c202 = "insert",
peg$c203 = peg$literalExpectation("INSERT", true),
peg$c204 = ";",
peg$c205 = peg$literalExpectation(";", false),
peg$c206 = "create",
peg$c207 = peg$literalExpectation("CREATE", true),
peg$c208 = "schema",
peg$c209 = peg$literalExpectation("SCHEMA", true),
peg$c210 = "database",
peg$c211 = peg$literalExpectation("DATABASE", true),
peg$c212 = "use",
peg$c213 = peg$literalExpectation("USE", true),
peg$c214 = "begin",
peg$c215 = peg$literalExpectation("BEGIN", true),
peg$c216 = "commit",
peg$c217 = peg$literalExpectation("COMMIT", true),
peg$c218 = "rollback",
peg$c219 = peg$literalExpectation("ROLLBACK", true),
peg$c220 = function peg$c220(first, rest) {
return [first].concat(_toConsumableArray(rest.map(function (n) {
return n[2];
})));
},
peg$c221 = "'",
peg$c222 = peg$literalExpectation("'", false),
peg$c223 = /^[^']/,
peg$c224 = peg$classExpectation(["'"], true, false),
peg$c225 = function peg$c225(comment, c) {
return c.join('');
},
peg$c226 = "\"",
peg$c227 = peg$literalExpectation("\"", false),
peg$c228 = /^[^"]/,
peg$c229 = peg$classExpectation(["\""], true, false),
peg$c230 = peg$otherExpectation("CREATE TABLE"),
peg$c231 = "temporary",
peg$c232 = peg$literalExpectation("TEMPORARY", true),
peg$c233 = "table",
peg$c234 = peg$literalExpectation("TABLE", true),
peg$c235 = peg$otherExpectation("IF NOT EXISTS"),
peg$c236 = "if",
peg$c237 = peg$literalExpectation("IF", true),
peg$c238 = "exists",
peg$c239 = peg$literalExpectation("EXISTS", true),
peg$c240 = peg$otherExpectation("ALTER TABLE"),
peg$c241 = peg$otherExpectation("CREATE INDEX"),
peg$c242 = function peg$c242(type) {
return type;
},
peg$c243 = function peg$c243() {
return 'INDEX';
},
peg$c244 = "primary",
peg$c245 = peg$literalExpectation("PRIMARY", true),
peg$c246 = "foreign",
peg$c247 = peg$literalExpectation("FOREIGN", true),
peg$c248 = "references",
peg$c249 = peg$literalExpectation("REFERENCES", true),
peg$c250 = "restrict",
peg$c251 = peg$literalExpectation("RESTRICT", true),
peg$c252 = "cascade",
peg$c253 = peg$literalExpectation("CASCADE", true),
peg$c254 = "action",
peg$c255 = peg$literalExpectation("ACTION", true),
peg$c256 = peg$otherExpectation("index type"),
peg$c257 = "using",
peg$c258 = peg$literalExpectation("USING", true),
peg$c259 = "btree",
peg$c260 = peg$literalExpectation("BTREE", true),
peg$c261 = "hash",
peg$c262 = peg$literalExpectation("HASH", true),
peg$c263 = function peg$c263(type) {
return type.toUpperCase();
},
peg$c264 = peg$otherExpectation("valid name"),
peg$c265 = function peg$c265(c) {
return c.join("");
},
peg$c266 = /^[^`]/,
peg$c267 = peg$classExpectation(["`"], true, false),
peg$c268 = ".",
peg$c269 = peg$literalExpectation(".", false),
peg$c270 = function peg$c270(names) {
var dbName = null;
var schemaName = null;
if (names && names.length > 0) {
if (names.length === 1) schemaName = names[0][0];else {
dbName = names[0][0];
schemaName = names[1][0];
}
}
return {
dbName: dbName,
schemaName: schemaName
};
},
peg$c271 = peg$otherExpectation("valid table name"),
peg$c272 = function peg$c272(pathName, name) {
return _objectSpread(_objectSpread({}, pathName), {}, {
name: name
});
},
peg$c273 = peg$otherExpectation("type"),
peg$c274 = function peg$c274(c) {
return c;
},
peg$c275 = function peg$c275(c) {
return {
type_name: c
};
},
peg$c276 = function peg$c276(type_name, args) {
args = args ? args[2] : null;
if (type_name.toLowerCase() !== 'enum') {
type_name = args ? type_name + '(' + args + ')' : type_name;
}
return {
type_name: type_name,
args: args
};
},
peg$c277 = peg$otherExpectation("expression"),
peg$c278 = function peg$c278(factors) {
return removeReduntdantSpNewline(_.flattenDeep(factors).join(""));
},
peg$c279 = ",",
peg$c280 = peg$literalExpectation(",", false),
peg$c281 = ");",
peg$c282 = peg$literalExpectation(");", false),
peg$c283 = function peg$c283(factors) {
return _.flattenDeep(factors).join("");
},
peg$c284 = /^[',.a-z0-9_+-`%]/i,
peg$c285 = peg$classExpectation(["'", ",", ".", ["a", "z"], ["0", "9"], "_", ["+", "`"], "%"], false, true),
peg$c286 = /^['.a-z0-9_+\-]/i,
peg$c287 = peg$classExpectation(["'", ".", ["a", "z"], ["0", "9"], "_", "+", "-"], false, true),
peg$c288 = peg$otherExpectation("letter, number or underscore"),
peg$c289 = /^[a-z0-9_]/i,
peg$c290 = peg$classExpectation([["a", "z"], ["0", "9"], "_"], false, true),
peg$c291 = " ",
peg$c292 = peg$literalExpectation(" ", false),
peg$c293 = "`",
peg$c294 = peg$literalExpectation("`", false),
peg$c295 = "\t",
peg$c296 = peg$literalExpectation("\t", false),
peg$c297 = peg$otherExpectation("endline"),
peg$c298 = peg$otherExpectation("newline"),
peg$c299 = "\r\n",
peg$c300 = peg$literalExpectation("\r\n", false),
peg$c301 = "\n",
peg$c302 = peg$literalExpectation("\n", false),
peg$c303 = peg$otherExpectation("space"),
peg$c304 = peg$otherExpectation("comment"),
peg$c305 = "--",
peg$c306 = peg$literalExpectation("--", false),
peg$c307 = /^[^\n]/,
peg$c308 = peg$classExpectation(["\n"], true, false),
peg$c309 = "/*",
peg$c310 = peg$literalExpectation("/*", false),
peg$c311 = "*/",
peg$c312 = peg$literalExpectation("*/", false),
peg$c313 = peg$otherExpectation("string"),
peg$c314 = function peg$c314(chars) {
return chars.join('');
},
peg$c315 = "\\",
peg$c316 = peg$literalExpectation("\\", false),
peg$c317 = function peg$c317() {
return '"';
},
peg$c318 = function peg$c318() {
return text();
},
peg$c319 = function peg$c319() {
return "'";
},
peg$c320 = /^[0-9]/,
peg$c321 = peg$classExpectation([["0", "9"]], false, false),
peg$c322 = function peg$c322(left, right) {
return parseFloat(left.join("") + "." + right.join(""));
},
peg$c323 = function peg$c323(digits) {
return parseInt(digits.join(""), 10);
},
peg$currPos = 0,
peg$savedPos = 0,
peg$posDetailsCache = [{
line: 1,
column: 1
}],
peg$maxFailPos = 0,
peg$maxFailExpected = [],
peg$silentFails = 0,
peg$result;
if ("startRule" in options) {
if (!(options.startRule in peg$startRuleFunctions)) {
throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
}
peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
}
function text() {
return input.substring(peg$savedPos, peg$currPos);
}
function location() {
return peg$computeLocation(peg$savedPos, peg$currPos);
}
function expected(description, location) {
location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos);
throw peg$buildStructuredError([peg$otherExpectation(description)], input.substring(peg$savedPos, peg$currPos), location);
}
function error(message, location) {
location = location !== void 0 ? location : peg$computeLocation(peg$savedPos, peg$currPos);
throw peg$buildSimpleError(message, location);
}
function peg$literalExpectation(text, ignoreCase) {
return {
type: "literal",
text: text,
ignoreCase: ignoreCase
};
}
function peg$classExpectation(parts, inverted, ignoreCase) {
return {
type: "class",
parts: parts,
inverted: inverted,
ignoreCase: ignoreCase
};
}
function peg$anyExpectation() {
return {
type: "any"
};
}
function peg$endExpectation() {
return {
type: "end"
};
}
function peg$otherExpectation(description) {
return {
type: "other",
description: description
};
}
function peg$computePosDetails(pos) {
var details = peg$posDetailsCache[pos],
p;
if (details) {
return details;
} else {
p = pos - 1;
while (!peg$posDetailsCache[p]) {
p--;
}
details = peg$posDetailsCache[p];
details = {
line: details.line,
column: details.column
};
while (p < pos) {
if (input.charCodeAt(p) === 10) {
details.line++;
details.column = 1;
} else {
details.column++;
}
p++;
}
peg$posDetailsCache[pos] = details;
return details;
}
}
function peg$computeLocation(startPos, endPos) {
var startPosDetails = peg$computePosDetails(startPos),
endPosDetails = peg$computePosDetails(endPos);
return {
start: {
offset: startPos,
line: startPosDetails.line,
column: startPosDetails.column
},
end: {
offset: endPos,
line: endPosDetails.line,
column: endPosDetails.column
}
};
}
function peg$fail(expected) {
if (peg$currPos < peg$maxFailPos) {
return;
}
if (peg$currPos > peg$maxFailPos) {
peg$maxFailPos = peg$currPos;
peg$maxFailExpected = [];
}
peg$maxFailExpected.push(expected);
}
function peg$buildSimpleError(message, location) {
return new peg$SyntaxError(message, null, null, location);
}
function peg$buildStructuredError(expected, found, location) {
return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected, found), expected, found, location);
}
function peg$parseRule() {
var s0, s1, s2;
s0 = peg$currPos;
s1 = [];
s2 = peg$parseExpr();
while (s2 !== peg$FAILED) {
s1.push(s2);
s2 = peg$parseExpr();
}
if (s1 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c0();
}
s0 = s1;
return s0;
}
function peg$parseExpr() {
var s0, s1;
s0 = peg$currPos;
s1 = peg$parseTableSyntax();
if (s1 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c1(s1);
}
s0 = s1;
if (s0 === peg$FAILED) {
s0 = peg$parseAlterSyntax();
if (s0 === peg$FAILED) {
s0 = peg$parseIndexSyntax();
if (s0 === peg$FAILED) {
s0 = peg$parseIgnoreSyntax();
if (s0 === peg$FAILED) {
s0 = peg$parse__();
}
}
}
}
return s0;
}
function peg$parseTableSyntax() {
var s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15;
s0 = peg$currPos;
s1 = peg$parsecreate_table();
if (s1 !== peg$FAILED) {
s2 = peg$currPos;
s3 = peg$parse__();
if (s3 !== peg$FAILED) {
s4 = peg$parseif_not_exist();
if (s4 !== peg$FAILED) {
s3 = [s3, s4];
s2 = s3;
} else {
peg$currPos = s2;
s2 = peg$FAILED;
}
} else {
peg$currPos = s2;
s2 = peg$FAILED;
}
if (s2 === peg$FAILED) {
s2 = null;
}
if (s2 !== peg$FAILED) {
s3 = peg$parse__();
if (s3 !== peg$FAILED) {
s4 = peg$parsetable_name();
if (s4 !== peg$FAILED) {
s5 = peg$parse_();
if (s5 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 40) {
s6 = peg$c2;
peg$currPos++;
} else {
s6 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c3);
}
}
if (s6 !== peg$FAILED) {
s7 = peg$parse_();
if (s7 !== peg$FAILED) {
s8 = peg$parseTableBody();
if (s8 !== peg$FAILED) {
s9 = peg$parse_();
if (s9 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
s10 = peg$c4;
peg$currPos++;
} else {
s10 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
if (s10 !== peg$FAILED) {
s11 = peg$parse_();
if (s11 !== peg$FAILED) {
s12 = peg$parseTableOptions();
if (s12 === peg$FAILED) {
s12 = null;
}
if (s12 !== peg$FAILED) {
s13 = peg$parse_();
if (s13 !== peg$FAILED) {
s14 = peg$parsesemicolon();
if (s14 !== peg$FAILED) {
s15 = peg$parseendline();
if (s15 === peg$FAILED) {
s15 = null;
}
if (s15 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c6(s4, s8, s12);
s0 = s1;
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parseTableBody() {
var s0, s1, s2, s3;
s0 = peg$currPos;
s1 = peg$parse_();
if (s1 !== peg$FAILED) {
s2 = [];
s3 = peg$parseLine();
while (s3 !== peg$FAILED) {
s2.push(s3);
s3 = peg$parseLine();
}
if (s2 !== peg$FAILED) {
s3 = peg$parse_();
if (s3 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c7(s2);
s0 = s1;
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
return s0;
}
function peg$parseLine() {
var s0, s1, s2, s3, s4, s5, s6;
peg$silentFails++;
s0 = peg$currPos;
s1 = peg$parsePKSyntax();
if (s1 !== peg$FAILED) {
s2 = peg$parse_();
if (s2 !== peg$FAILED) {
s3 = peg$parsecomma();
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
if (input.charCodeAt(peg$currPos) === 41) {
s4 = peg$c4;
peg$currPos++;
} else {
s4 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
s4 = peg$currPos;
s5 = peg$parseendline();
if (s5 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
s6 = peg$c4;
peg$currPos++;
} else {
s6 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
if (s6 !== peg$FAILED) {
s5 = [s5, s6];
s4 = s5;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
}
}
if (s3 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c9(s1);
s0 = s1;
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = peg$parseFKSyntax();
if (s1 !== peg$FAILED) {
s2 = peg$parse_();
if (s2 !== peg$FAILED) {
s3 = peg$parsecomma();
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
if (input.charCodeAt(peg$currPos) === 41) {
s4 = peg$c4;
peg$currPos++;
} else {
s4 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
s4 = peg$currPos;
s5 = peg$parseendline();
if (s5 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
s6 = peg$c4;
peg$currPos++;
} else {
s6 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
if (s6 !== peg$FAILED) {
s5 = [s5, s6];
s4 = s5;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
}
}
if (s3 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c10(s1);
s0 = s1;
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = peg$parseIndexInLineSyntax();
if (s1 !== peg$FAILED) {
s2 = peg$parse_();
if (s2 !== peg$FAILED) {
s3 = peg$parsecomma();
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
if (input.charCodeAt(peg$currPos) === 41) {
s4 = peg$c4;
peg$currPos++;
} else {
s4 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
s4 = peg$currPos;
s5 = peg$parseendline();
if (s5 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
s6 = peg$c4;
peg$currPos++;
} else {
s6 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
if (s6 !== peg$FAILED) {
s5 = [s5, s6];
s4 = s5;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
}
}
if (s3 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c11(s1);
s0 = s1;
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
if (s0 === peg$FAILED) {
s0 = peg$currPos;
s1 = peg$parseField();
if (s1 !== peg$FAILED) {
s2 = peg$parse_();
if (s2 !== peg$FAILED) {
s3 = peg$parsecomma();
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
if (input.charCodeAt(peg$currPos) === 41) {
s4 = peg$c4;
peg$currPos++;
} else {
s4 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
if (s3 === peg$FAILED) {
s3 = peg$currPos;
peg$silentFails++;
s4 = peg$currPos;
s5 = peg$parseendline();
if (s5 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
s6 = peg$c4;
peg$currPos++;
} else {
s6 = peg$FAILED;
if (peg$silentFails === 0) {
peg$fail(peg$c5);
}
}
if (s6 !== peg$FAILED) {
s5 = [s5, s6];
s4 = s5;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
peg$silentFails--;
if (s4 !== peg$FAILED) {
peg$currPos = s3;
s3 = void 0;
} else {
s3 = peg$FAILED;
}
}
}
if (s3 !== peg$FAILED) {
peg$savedPos = s0;
s1 = peg$c12(s1);
s0 = s1;
} else {