UNPKG

@dbml/core

Version:
1,161 lines (1,124 loc) 43.6 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _lodash = require("lodash"); var _AST = require("../AST"); var _constants = require("../constants"); var _helpers = require("../helpers"); var _error = require("../../../error"); var _OracleSqlParserVisitor = _interopRequireDefault(require("../../parsers/oraclesql/OracleSqlParserVisitor")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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 _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } // We cannot use TABLE_CONSTRAINT_KIND and COLUMN_CONSTRAINT_KIND from '../constants' as their values are indistinguishable from each other // For example: TABLE_CONSTRAINT_KIND.UNIQUE === COLUMN_CONSTRAINT_KIND.UNIQUE // Therefore, we redefine them to be different // TODO: Migrate the other parser to use our version instead var TABLE_CONSTRAINT_KIND = { FIELD: 'table_field', INDEX: 'table_index', FK: 'table_fk', UNIQUE: 'table_unique', PK: 'table_pk', DEFAULT: 'table_default', CHECK: 'table_check' }; var COLUMN_CONSTRAINT_KIND = { NOT_NULL: 'col_not_null', NULLABLE: 'col_nullable', UNIQUE: 'col_unique', PK: 'col_pk', DEFAULT: 'col_dbdefault', INCREMENT: 'col_increment', INLINE_REF: 'col_inline_ref', NOTE: 'col_note', CHECK: 'col_check' }; var findTable = function findTable(tables, schemaName, tableName) { var realSchemaName = schemaName || null; var table = tables.find(function (table) { var targetSchemaName = table.schemaName || null; return targetSchemaName === realSchemaName && table.name === tableName; }); return table; }; var findColumn = function findColumn(table, columnName) { var column = table.fields.find(function (field) { return field.name === columnName; }); return column; }; // `e` is the value returned by `visitExpression` var processDefaultExpression = function processDefaultExpression(e) { return { value: e.type !== _constants.CONSTRAINT_TYPE.COLUMN ? e.value : e.rawValue, type: e.type !== _constants.CONSTRAINT_TYPE.COLUMN ? e.type : _constants.DATA_TYPE.EXPRESSION }; }; // `e` is the value returned by `visitExpression` var processIndexExpression = function processIndexExpression(e) { var type; switch (e.type) { case _constants.CONSTRAINT_TYPE.COLUMN: type = _constants.CONSTRAINT_TYPE.COLUMN; break; case _constants.DATA_TYPE.STRING: type = _constants.CONSTRAINT_TYPE.STRING; break; default: type = _constants.CONSTRAINT_TYPE.EXPRESSION; break; } return { value: e.value.toString(), type: type }; }; var createCompilerError = function createCompilerError(ctx, message) { return _error.CompilerError.create([{ message: message, location: { start: { line: ctx.start.line, column: ctx.start.column + 1 }, end: { line: ctx.stop.line, column: ctx.stop.column + (0, _helpers.getOriginalText)(ctx).length + 1 } } }]); }; var unquoteString = function unquoteString(str) { var quoteChar = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '"'; return !str.startsWith(quoteChar) ? str : str.slice(1, -1) // Strip off starting and ending quotes .replaceAll("".concat(quoteChar).concat(quoteChar), quoteChar); // Unescape quotes, in Oracle, quotes are escaped by doubling it }; // Only methods for rules representing whole statements can mutate `data`: // * create_table // * alter_table // * create_index // * insert_statement // All other methods must be pure var OracleSqlASTGen = exports["default"] = /*#__PURE__*/function (_OracleSqlParserVisit) { function OracleSqlASTGen() { var _this; _classCallCheck(this, OracleSqlASTGen); _this = _callSuper(this, OracleSqlASTGen); _this.data = { schemas: [], tables: [], refs: [], enums: [], tableGroups: [], aliases: [], project: {}, records: [] }; return _this; } // sql_script // : sql_plus_command_no_semicolon? ( // (((sql_plus_command SEMICOLON? '/'?) | (unit_statement SEMICOLON '/'?))* (sql_plus_command | unit_statement)) SEMICOLON? '/'? // )? EOF // ; _inherits(OracleSqlASTGen, _OracleSqlParserVisit); return _createClass(OracleSqlASTGen, [{ key: "visitSql_script", value: function visitSql_script(ctx) { var _ctx$unit_statement, _this2 = this; (_ctx$unit_statement = ctx.unit_statement()) === null || _ctx$unit_statement === void 0 || _ctx$unit_statement.forEach(function (stmt) { return stmt.accept(_this2); }); return this.data; } // unit_statement // : create_table // | comment_on_table // | comment_on_column // | alter_table // | create_index // | data_manipulation_language_statements // | <other rules> // ; }, { key: "visitUnit_statement", value: function visitUnit_statement(ctx) { if (ctx.create_table()) { ctx.create_table().accept(this); } else if (ctx.comment_on_table()) { ctx.comment_on_table().accept(this); } else if (ctx.comment_on_column()) { ctx.comment_on_column().accept(this); } else if (ctx.alter_table()) { ctx.alter_table().accept(this); } else if (ctx.create_index()) { ctx.create_index().accept(this); } else if (ctx.data_manipulation_language_statements()) { ctx.data_manipulation_language_statements().accept(this); } } // data_manipulation_language_statements // : insert_statement // | <other rules> // ; }, { key: "visitData_manipulation_language_statements", value: function visitData_manipulation_language_statements(ctx) { if (ctx.insert_statement()) { ctx.insert_statement().accept(this); } } // create_table // : CREATE TABLE (schema_name '.')? table_name // (relational_table | <other rules>) // | <other rules> // ; }, { key: "visitCreate_table", value: function visitCreate_table(ctx) { var tableName = ctx.table_name().accept(this); var schemaName = ctx.schema_name() ? ctx.schema_name().accept(this) : undefined; if (ctx.relational_table()) { var _this$data$refs, _this$data$refs2; var tableElements = ctx.relational_table().accept(this); var _tableElements$reduce = tableElements.reduce(function (acc, ele) { var _acc$; if (ele.type === TABLE_CONSTRAINT_KIND.FIELD) acc[0].push(ele.value);else if ([TABLE_CONSTRAINT_KIND.INDEX, TABLE_CONSTRAINT_KIND.UNIQUE, TABLE_CONSTRAINT_KIND.PK].includes(ele.type)) acc[1].push(ele.value);else if (ele.type === TABLE_CONSTRAINT_KIND.FK) acc[2].push(ele.value);else if (ele.type === TABLE_CONSTRAINT_KIND.CHECK) (_acc$ = acc[3]).push.apply(_acc$, _toConsumableArray(ele.value.checks)); return acc; }, [[], [], [], []]), _tableElements$reduce2 = _slicedToArray(_tableElements$reduce, 4), fieldsData = _tableElements$reduce2[0], indexes = _tableElements$reduce2[1], tableRefs = _tableElements$reduce2[2], tableChecks = _tableElements$reduce2[3]; (_this$data$refs = this.data.refs).push.apply(_this$data$refs, _toConsumableArray((0, _lodash.flatten)(fieldsData.map(function (fieldData) { return fieldData.inline_refs.map(function (inlineRef) { inlineRef.endpoints[0].tableName = tableName; inlineRef.endpoints[0].schemaName = schemaName; inlineRef.endpoints[0].fieldNames = [fieldData.field.name]; return inlineRef; }); })))); (_this$data$refs2 = this.data.refs).push.apply(_this$data$refs2, _toConsumableArray(tableRefs.map(function (tableRef) { tableRef.endpoints[0].tableName = tableName; tableRef.endpoints[0].schemaName = schemaName; return tableRef; }))); this.data.tables.push(new _AST.Table({ name: tableName, schemaName: schemaName, fields: fieldsData.map(function (fd) { return fd.field; }), indexes: indexes, checks: tableChecks })); } } // relational_table // : ('(' relational_property (',' relational_property)* ')')? relational_table_properties? // ; }, { key: "visitRelational_table", value: function visitRelational_table(ctx) { var _this3 = this; if (ctx.relational_property()) { return ctx.relational_property().map(function (p) { return p.accept(_this3); }); } return []; } // relational_property // : column_definition // | out_of_line_constraint // | out_of_line_ref_constraint // | <other rules> // ; }, { key: "visitRelational_property", value: function visitRelational_property(ctx) { if (ctx.column_definition()) { return ctx.column_definition().accept(this); } if (ctx.out_of_line_constraint()) { return ctx.out_of_line_constraint().accept(this); } if (ctx.out_of_line_ref_constraint()) { return ctx.out_of_line_ref_constraint().accept(this); } return null; } // column_definition // : column_name ((datatype | type_name) (COLLATE column_collation_name)?)? SORT? ( // VISIBLE // | INVISIBLE // )? (DEFAULT (ON NULL_)? expression | identity_clause)? (ENCRYPT encryption_spec)? ( // inline_constraint+ // | inline_ref_constraint // )? annotations_clause? // ; }, { key: "visitColumn_definition", value: function visitColumn_definition(ctx) { var _ctx$datatype, _ctx$type_name, _this4 = this; var name = (0, _lodash.last)(ctx.column_name().accept(this)); var type = ((_ctx$datatype = ctx.datatype()) === null || _ctx$datatype === void 0 ? void 0 : _ctx$datatype.accept(this)) || ((_ctx$type_name = ctx.type_name()) === null || _ctx$type_name === void 0 ? void 0 : _ctx$type_name.accept(this)); if (!type) throw createCompilerError(ctx, 'Importing a column definition without a type is not supported'); var constraints = (ctx.inline_constraint() || []).map(function (c) { return c.accept(_this4); }).filter(Boolean); if (ctx.DEFAULT()) { var value = ctx.expression().accept(this); constraints.push({ type: COLUMN_CONSTRAINT_KIND.DEFAULT, value: { dbdefault: processDefaultExpression(value) } }); } if (ctx.identity_clause()) { constraints.push({ type: COLUMN_CONSTRAINT_KIND.INCREMENT, value: { increment: true } }); } var settings = { checks: [], inline_refs: [] }; constraints.forEach(function (constraint) { var _settings$checks, _settings$inline_refs; switch (constraint.type) { case COLUMN_CONSTRAINT_KIND.DEFAULT: settings.dbdefault = constraint.value.dbdefault; break; case COLUMN_CONSTRAINT_KIND.NOT_NULL: settings.not_null = true; break; case COLUMN_CONSTRAINT_KIND.NULLABLE: settings.not_null = false; break; case COLUMN_CONSTRAINT_KIND.PK: settings.pk = true; break; case COLUMN_CONSTRAINT_KIND.UNIQUE: settings.unique = true; break; case COLUMN_CONSTRAINT_KIND.CHECK: (_settings$checks = settings.checks).push.apply(_settings$checks, _toConsumableArray(constraint.value.checks)); break; case COLUMN_CONSTRAINT_KIND.INCREMENT: settings.increment = true; break; case COLUMN_CONSTRAINT_KIND.FK: (_settings$inline_refs = settings.inline_refs).push.apply(_settings$inline_refs, _toConsumableArray(constraint.value.inline_refs)); break; default: } }); var field = new _AST.Field(_objectSpread({ name: name, type: type }, settings)); return { type: TABLE_CONSTRAINT_KIND.FIELD, value: { field: field, inline_refs: settings.inline_refs || [] } }; } // inline_constraint // : constraint_name? ( // NOT? NULL_ // | (UNIQUE | PRIMARY) KEY? // | references_clause // | check_constraint // ) constraint_state? // ; }, { key: "visitInline_constraint", value: function visitInline_constraint(ctx) { var _ctx$constraint_name; if (ctx.NULL_() && ctx.NOT()) { return { type: COLUMN_CONSTRAINT_KIND.NOT_NULL, value: { not_null: true } }; } if (ctx.NULL_()) { return { type: COLUMN_CONSTRAINT_KIND.NULLABLE, value: { not_null: false } }; } if (ctx.UNIQUE()) { return { type: COLUMN_CONSTRAINT_KIND.UNIQUE, value: { unique: true } }; } if (ctx.PRIMARY()) { return { type: COLUMN_CONSTRAINT_KIND.PK, value: { pk: true } }; } var name = (_ctx$constraint_name = ctx.constraint_name()) === null || _ctx$constraint_name === void 0 ? void 0 : _ctx$constraint_name.accept(this); if (ctx.references_clause()) { var ref = ctx.references_clause().accept(this); ref.name = name; return { type: COLUMN_CONSTRAINT_KIND.FK, value: { inline_refs: [ref] } }; } if (ctx.check_constraint()) { var checkCtx = ctx.check_constraint(); checkCtx.isColumn = true; var check = checkCtx.accept(this); check.value.checks[0].name = name; return check; } return null; } // out_of_line_constraint // : ( // ((CONSTRAINT | CONSTRAINTS) constraint_name)? ( // UNIQUE '(' column_name (',' column_name)* ')' // | PRIMARY KEY '(' column_name (',' column_name)* ')' // | foreign_key_clause // | CHECK '(' condition ')' // ) // ) // constraint_state? // parallel_clause? // ; }, { key: "visitOut_of_line_constraint", value: function visitOut_of_line_constraint(ctx) { var _this5 = this; if (ctx.UNIQUE()) { var _ctx$constraint_name2; var columns = ctx.column_name().map(function (c) { return { value: (0, _lodash.last)(c.accept(_this5)), type: _constants.CONSTRAINT_TYPE.COLUMN }; }); return { type: TABLE_CONSTRAINT_KIND.UNIQUE, value: new _AST.Index({ columns: columns, unique: true, name: (_ctx$constraint_name2 = ctx.constraint_name()) === null || _ctx$constraint_name2 === void 0 ? void 0 : _ctx$constraint_name2.accept(this) }) }; } if (ctx.PRIMARY()) { var _ctx$constraint_name3; var _columns = ctx.column_name().map(function (c) { return { value: (0, _lodash.last)(c.accept(_this5)), type: _constants.CONSTRAINT_TYPE.COLUMN }; }); return { type: TABLE_CONSTRAINT_KIND.PK, value: new _AST.Index({ columns: _columns, pk: true, name: (_ctx$constraint_name3 = ctx.constraint_name()) === null || _ctx$constraint_name3 === void 0 ? void 0 : _ctx$constraint_name3.accept(this) }) }; } if (ctx.foreign_key_clause()) { var _ctx$constraint_name4; var ref = ctx.foreign_key_clause().accept(this); ref.value.name = (_ctx$constraint_name4 = ctx.constraint_name()) === null || _ctx$constraint_name4 === void 0 ? void 0 : _ctx$constraint_name4.accept(this); return ref; } if (ctx.condition()) { var _ctx$constraint_name5; var expression = (0, _helpers.getOriginalText)(ctx.condition()); return { type: TABLE_CONSTRAINT_KIND.CHECK, value: { checks: [{ name: (_ctx$constraint_name5 = ctx.constraint_name()) === null || _ctx$constraint_name5 === void 0 ? void 0 : _ctx$constraint_name5.accept(this), expression: expression }] } }; } return null; } // out_of_line_ref_constraint // : (CONSTRAINT constraint_name)? FOREIGN KEY '(' (','? ref_col_or_attr = regular_id)+ ')' references_clause constraint_state? // | <other rules> // ; }, { key: "visitOut_of_line_ref_constraint", value: function visitOut_of_line_ref_constraint(ctx) { var _ctx$constraint_name6, _this6 = this; var refName = (_ctx$constraint_name6 = ctx.constraint_name()) === null || _ctx$constraint_name6 === void 0 ? void 0 : _ctx$constraint_name6.accept(this); var firstFieldNames = ctx.regular_id().map(function (c) { return c.accept(_this6); }); var ref = ctx.references_clause().accept(this); ref.endpoints[0].fieldNames = firstFieldNames; if (refName) { ref.name = refName; } return { type: TABLE_CONSTRAINT_KIND.FK, value: ref }; } // foreign_key_clause // : FOREIGN KEY paren_column_list references_clause on_delete_clause? // ; }, { key: "visitForeign_key_clause", value: function visitForeign_key_clause(ctx) { var firstFieldNames = ctx.paren_column_list().accept(this).map(function (c) { return (0, _lodash.last)(c); }); var ref = ctx.references_clause().accept(this); ref.endpoints[0].fieldNames = firstFieldNames; if (ctx.on_delete_clause()) { ref.onDelete = ctx.on_delete_clause().accept(this); } return { type: TABLE_CONSTRAINT_KIND.FK, value: ref }; } // references_clause // : REFERENCES tableview_name paren_column_list? (ON DELETE (CASCADE | SET NULL_))? // ; }, { key: "visitReferences_clause", value: function visitReferences_clause(ctx) { if (!ctx.paren_column_list()) throw createCompilerError(ctx.tableview_name(), 'Importing a foreign key with implicit referenced columns is not supported'); var names = ctx.tableview_name().accept(this); var refTableName = (0, _lodash.last)(names); var refSchemaName = names.length > 1 ? names[names.length - 2] : undefined; var secondFieldNames = ctx.paren_column_list().accept(this).map(function (c) { return (0, _lodash.last)(c); }); return new _AST.Ref({ endpoints: [new _AST.Endpoint({ tableName: null, schemaName: null, fieldNames: [], relation: '*' }), new _AST.Endpoint({ tableName: refTableName, schemaName: refSchemaName, fieldNames: secondFieldNames, relation: '1' })] }); } // on_delete_clause // : ON DELETE (CASCADE | SET NULL_) // ; }, { key: "visitOn_delete_clause", value: function visitOn_delete_clause(ctx) { if (ctx.CASCADE()) return 'cascade'; if (ctx.SET() && ctx.NULL_()) return 'set null'; return null; } // check_constraint // : CHECK '(' condition ')' // ; }, { key: "visitCheck_constraint", value: function visitCheck_constraint(ctx) { var expression = (0, _helpers.getOriginalText)(ctx.condition()); return { type: ctx.isColumn ? COLUMN_CONSTRAINT_KIND.CHECK : TABLE_CONSTRAINT_KIND.CHECK, value: { checks: [{ expression: expression }] } }; } }, { key: "visitColumn_list", value: function visitColumn_list(ctx) { var _this7 = this; return ctx.column_name().map(function (c) { return c.accept(_this7); }); } // tableview_name // : identifier ('.' id_expression)? ( // AT_SIGN link_name // | /*TODO{!(input.LA(2) == BY)}?*/ partition_extension_clause // )? // | xmltable outer_join_sign? // ; }, { key: "visitTableview_name", value: function visitTableview_name(ctx) { var schema = ctx.id_expression() ? [ctx.identifier().accept(this)] : []; var table = ctx.id_expression() ? ctx.id_expression().accept(this) : ctx.identifier().accept(this); return [].concat(schema, [table]); } }, { key: "visitSchema_name", value: function visitSchema_name(ctx) { return ctx.identifier().accept(this); } }, { key: "visitTable_name", value: function visitTable_name(ctx) { return ctx.identifier().accept(this); } }, { key: "visitColumn_name", value: function visitColumn_name(ctx) { var _this8 = this; return [ctx.identifier().accept(this)].concat(ctx.id_expression().map(function (i) { return i.accept(_this8); })); } }, { key: "visitIdentifier", value: function visitIdentifier(ctx) { var text = (0, _helpers.getOriginalText)(ctx); return unquoteString(text); } }, { key: "visitType_name", value: function visitType_name(ctx) { var _this9 = this; var names = ctx.id_expression().map(function (i) { return i.accept(_this9); }); var typeName = (0, _lodash.last)(names); var schemaName = names.length > 1 ? names[names.length - 2] : undefined; return { type_name: typeName, schemaName: schemaName }; } }, { key: "visitDatatype", value: function visitDatatype(ctx) { var typeName = (0, _helpers.getOriginalText)(ctx); return { type_name: unquoteString(typeName) }; } // comment_on_table // : COMMENT ON TABLE tableview_name IS quoted_string // ; }, { key: "visitComment_on_table", value: function visitComment_on_table(ctx) { var names = ctx.tableview_name().accept(this); var tableName = (0, _lodash.last)(names); var schemaName = names.length > 1 ? names[names.length - 2] : undefined; var table = findTable(this.data.tables, schemaName, tableName); if (!table) { throw createCompilerError(ctx.tableview_name(), "Table \"".concat(tableName, "\" not found")); } var note = ctx.quoted_string().accept(this); table.note = { value: note }; } // comment_on_column // : COMMENT ON COLUMN column_name IS quoted_string // ; }, { key: "visitComment_on_column", value: function visitComment_on_column(ctx) { var names = ctx.column_name().accept(this); var columnName = (0, _lodash.last)(names); names.pop(); var tableName = (0, _lodash.last)(names); names.pop(); var schemaName = (0, _lodash.last)(names); var table = findTable(this.data.tables, schemaName, tableName); if (!table) { throw createCompilerError(ctx.column_name(), "Table \"".concat(tableName, "\" not found")); } var field = findColumn(table, columnName); if (!field) { throw createCompilerError(ctx.column_name(), "Column \"".concat(columnName, "\" not found in table \"").concat(tableName, "\"")); } var note = ctx.quoted_string().accept(this); field.note = { value: note }; } // alter_table // : ALTER TABLE tableview_name memoptimize_read_write_clause* (constraint_clauses | column_clauses | <other rules>) ((enable_disable_clause | enable_or_disable (TABLE LOCK | ALL TRIGGERS))+)? // ; }, { key: "visitAlter_table", value: function visitAlter_table(ctx) { var _this0 = this; var names = ctx.tableview_name().accept(this); var tableName = (0, _lodash.last)(names); var schemaName = names.length > 1 ? names[names.length - 2] : undefined; var table = findTable(this.data.tables, schemaName, tableName); if (!table) { throw createCompilerError(ctx.tableview_name(), "Table \"".concat(tableName, "\" not found")); } function handleConstraint(column, constraint) { var _table$checks, _column$checks; switch (constraint.type) { case TABLE_CONSTRAINT_KIND.FK: case COLUMN_CONSTRAINT_KIND.FK: constraint.value.endpoints[0].tableName = tableName; constraint.value.endpoints[0].schemaName = schemaName; this.data.refs.push(constraint.value); break; case TABLE_CONSTRAINT_KIND.CHECK: if (!table.checks) table.checks = []; (_table$checks = table.checks).push.apply(_table$checks, _toConsumableArray(constraint.value.checks)); break; case COLUMN_CONSTRAINT_KIND.CHECK: if (!column.checks) table.checks = []; (_column$checks = column.checks).push.apply(_column$checks, _toConsumableArray(constraint.value.checks)); break; case COLUMN_CONSTRAINT_KIND.PK: column.pk = true; column.unique = undefined; break; case COLUMN_CONSTRAINT_KIND.UNIQUE: column.unique = true; break; case TABLE_CONSTRAINT_KIND.UNIQUE: case TABLE_CONSTRAINT_KIND.INDEX: case TABLE_CONSTRAINT_KIND.PK: if (!table.indexes) table.indexes = []; table.indexes.push(constraint.value); break; case COLUMN_CONSTRAINT_KIND.DEFAULT: column.dbdefault = constraint.value.dbdefault; break; case COLUMN_CONSTRAINT_KIND.NOT_NULL: column.not_null = true; break; case COLUMN_CONSTRAINT_KIND.NULLABLE: column.not_null = false; break; case COLUMN_CONSTRAINT_KIND.INCREMENT: column.increment = true; break; default: } } if (ctx.constraint_clauses() || ctx.column_clauses()) { var res = ctx.constraint_clauses() ? [{ column: null, constraints: ctx.constraint_clauses().accept(this) }] : ctx.column_clauses().accept(this); res.forEach(function (r) { var column = r.column !== null ? findColumn(table, r.column) : null; if (r.column !== null && !column) { throw createCompilerError(ctx.tableview_name(), "Column \"".concat(r.column, "\" not found on Table \"").concat(tableName, "\"")); } r.constraints.forEach(function (c) { return handleConstraint.bind(_this0)(column, c); }); }); } } // column_clauses // : add_modify_drop_column_clauses // | <other rules> // ; }, { key: "visitColumn_clauses", value: function visitColumn_clauses(ctx) { if (ctx.add_modify_drop_column_clauses()) { return ctx.add_modify_drop_column_clauses().accept(this); } return []; } // add_modify_drop_column_clauses // : (constraint_clauses | add_column_clause | modify_column_clauses | drop_column_clause)+ // ; }, { key: "visitAdd_modify_drop_column_clauses", value: function visitAdd_modify_drop_column_clauses(ctx) { var _this1 = this; var constraints = []; if (ctx.constraint_clauses()) { constraints.push({ column: null, constraints: ctx.constraint_clauses().flatMap(function (c) { return c.accept(_this1); }) }); } if (ctx.modify_column_clauses()) { constraints.push.apply(constraints, _toConsumableArray(ctx.modify_column_clauses().flatMap(function (c) { return c.accept(_this1); }))); } return constraints; } // modify_column_clauses // : MODIFY ( // '(' modify_col_properties (',' modify_col_properties)* ')' // | '(' modify_col_visibility (',' modify_col_visibility)* ')' // | modify_col_properties // | modify_col_visibility // | modify_col_substitutable // ) // ; }, { key: "visitModify_column_clauses", value: function visitModify_column_clauses(ctx) { var _this10 = this; return ctx.modify_col_properties().map(function (c) { return c.accept(_this10); }); } // modify_col_properties // : column_name datatype? (DEFAULT (ON NULL_)? expression)? (ENCRYPT encryption_spec | DECRYPT)? inline_constraint* lob_storage_clause? annotations_clause? // ; }, { key: "visitModify_col_properties", value: function visitModify_col_properties(ctx) { var _this11 = this; var constraints = []; var columnName = (0, _lodash.last)(ctx.column_name().accept(this)); if (ctx.DEFAULT()) { var expression = ctx.expression().accept(this); constraints.push({ type: COLUMN_CONSTRAINT_KIND.DEFAULT, value: { dbdefault: processDefaultExpression(expression) } }); } else if (ctx.inline_constraint()) { constraints.push.apply(constraints, _toConsumableArray(ctx.inline_constraint().map(function (c) { return c.accept(_this11); }))); } return { column: columnName, constraints: constraints }; } // constraint_clauses // : ADD '(' (out_of_line_constraint (',' out_of_line_constraint)* | out_of_line_ref_constraint) ')' // | ADD (out_of_line_constraint | out_of_line_ref_constraint) // | <other rules> // ; }, { key: "visitConstraint_clauses", value: function visitConstraint_clauses(ctx) { var _this12 = this; if (ctx.out_of_line_constraint()) { return ctx.out_of_line_constraint().map(function (c) { return c.accept(_this12); }).filter(Boolean); } if (ctx.out_of_line_ref_constraint()) { return [ctx.out_of_line_ref_constraint().accept(this)]; } return []; } // create_index // : CREATE (UNIQUE | BITMAP)? INDEX index_name (IF NOT EXISTS)? ON ( // cluster_index_clause // | table_index_clause // | bitmap_join_index_clause // ) (USABLE | UNUSABLE)? ((DEFERRED | IMMEDIATE) INVALIDATION)? // ; }, { key: "visitCreate_index", value: function visitCreate_index(ctx) { if (!ctx.table_index_clause()) return; var unique = !!ctx.UNIQUE(); var bitmap = !!ctx.BITMAP(); var indexName = ctx.index_name().accept(this); var _ctx$table_index_clau = ctx.table_index_clause().accept(this), names = _ctx$table_index_clau.names, columns = _ctx$table_index_clau.columns; var tableName = (0, _lodash.last)(names); var schemaName = names.length > 1 ? names[names.length - 2] : undefined; var table = findTable(this.data.tables, schemaName, tableName); if (!table) { throw createCompilerError(ctx.table_index_clause(), "Table ".concat(tableName, " not found")); } if (!table.indexes) table.indexes = []; table.indexes.push(new _AST.Index({ name: indexName, columns: columns, unique: unique, type: bitmap ? 'bitmap' : undefined })); } // table_index_clause // : tableview_name table_alias? '(' index_expr (ASC | DESC)? (',' index_expr (ASC | DESC)?)* ')' index_properties? // ; }, { key: "visitTable_index_clause", value: function visitTable_index_clause(ctx) { var _this13 = this; var names = ctx.tableview_name().accept(this); var columns = ctx.index_expr().map(function (i) { return i.accept(_this13); }); return { names: names, columns: columns }; } // constraint_name // : id_expression // ; }, { key: "visitConstraint_name", value: function visitConstraint_name(ctx) { return unquoteString((0, _helpers.getOriginalText)(ctx)); } }, { key: "visitIndex_name", value: function visitIndex_name(ctx) { return ctx.identifier().accept(this); } // index_expr // : column_name // | expression // ; }, { key: "visitIndex_expr", value: function visitIndex_expr(ctx) { if (ctx.expression()) { var expression = processIndexExpression(ctx.expression().accept(this)); return { value: expression.value, type: expression.type }; } var columnName = (0, _lodash.last)(ctx.column_name().accept(this)); return { value: columnName, type: _constants.CONSTRAINT_TYPE.COLUMN }; } // insert_statement // : INSERT (single_table_insert | <other rules>) // ; }, { key: "visitInsert_statement", value: function visitInsert_statement(ctx) { if (ctx.single_table_insert()) { ctx.single_table_insert().accept(this); } } // single_table_insert // : insert_into_clause (values_clause | <other rules>) // ; }, { key: "visitSingle_table_insert", value: function visitSingle_table_insert(ctx) { var intoClause = ctx.insert_into_clause().accept(this); var valuesClause = ctx.values_clause().accept(this); if (intoClause && valuesClause) { var tableName = intoClause.tableName, schemaName = intoClause.schemaName, columns = intoClause.columns; var values = valuesClause.values; var record = new _AST.TableRecord({ schemaName: schemaName, tableName: tableName, columns: columns, values: values }); this.data.records.push(record); } } // insert_into_clause // : INTO general_table_ref paren_column_list? // ; }, { key: "visitInsert_into_clause", value: function visitInsert_into_clause(ctx) { var names = ctx.general_table_ref().accept(this); var tableName = (0, _lodash.last)(names); var schemaName = names.length > 1 ? names[names.length - 2] : undefined; var columns = ctx.paren_column_list() ? ctx.paren_column_list().accept(this).map(function (c) { return (0, _lodash.last)(c); }) : []; return { tableName: tableName, schemaName: schemaName, columns: columns }; } }, { key: "visitGeneral_table_ref", value: function visitGeneral_table_ref(ctx) { return ctx.dml_table_expression_clause().accept(this); } }, { key: "visitDml_table_expression_clause", value: function visitDml_table_expression_clause(ctx) { if (ctx.tableview_name()) { return ctx.tableview_name().accept(this); } return []; } }, { key: "visitParen_column_list", value: function visitParen_column_list(ctx) { return ctx.column_list().accept(this); } // values_clause // : VALUES '(' expressions_ ')' // ; }, { key: "visitValues_clause", value: function visitValues_clause(ctx) { var expressions = ctx.expressions_().accept(this); return { values: [expressions.map(function (e) { return { value: e.type !== _constants.CONSTRAINT_TYPE.COLUMN ? e.value : e.rawValue, type: e.type !== _constants.CONSTRAINT_TYPE.COLUMN ? e.type : _constants.DATA_TYPE.EXPRESSION }; })] }; } }, { key: "visitExpressions_", value: function visitExpressions_(ctx) { var _this14 = this; return ctx.expression().map(function (e) { return e.accept(_this14); }); } }, { key: "visitId_expression", value: function visitId_expression(ctx) { var text = (0, _helpers.getOriginalText)(ctx); return unquoteString(text); } // expression // : CHAR_STRING // | TRUE // | FALSE // | NULL_ // | APPROXIMATE_NUM_LIT // | UNSIGNED_INTEGER // | column_name // | cursor_expression // | logical_expression // ; }, { key: "visitExpression", value: function visitExpression(ctx) { var value = (0, _helpers.getOriginalText)(ctx); if (ctx.CHAR_STRING()) { return { type: _constants.DATA_TYPE.STRING, value: unquoteString(value, "'") // string literals use single quotes }; } if (ctx.TRUE()) { return { type: _constants.DATA_TYPE.BOOLEAN, value: true }; } if (ctx.FALSE()) { return { type: _constants.DATA_TYPE.BOOLEAN, value: false }; } if (ctx.NULL_()) { return { type: _constants.DATA_TYPE.BOOLEAN, value: null }; } if (ctx.APPROXIMATE_NUM_LIT() || ctx.UNSIGNED_INTEGER()) { return { type: _constants.DATA_TYPE.NUMBER, value: parseFloat(value) }; } if (ctx.column_name()) { return { type: _constants.CONSTRAINT_TYPE.COLUMN, rawValue: unquoteString(value), value: unquoteString((0, _helpers.getOriginalText)(ctx.column_name())) }; } return { type: _constants.CONSTRAINT_TYPE.EXPRESSION, value: value }; } }, { key: "visitQuoted_string", value: function visitQuoted_string(ctx) { return unquoteString((0, _helpers.getOriginalText)(ctx), "'"); // string literals use single quotes } }, { key: "visitRegular_id", value: function visitRegular_id(ctx) { return unquoteString((0, _helpers.getOriginalText)(ctx), '"'); } }]); }(_OracleSqlParserVisitor["default"]);