@dbml/core
Version:
> TODO: description
750 lines (720 loc) • 32.5 kB
JavaScript
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _lodash = require("lodash");
var _SnowflakeParserVisitor = _interopRequireDefault(require("../../parsers/snowflake/SnowflakeParserVisitor"));
var _AST = require("../AST");
var _constants = require("../constants");
var _helpers = require("../helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": 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 _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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(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 _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 _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /* eslint-disable class-methods-use-this */
var sanitizeComment = function sanitizeComment(stringContext) {
return (0, _helpers.getOriginalText)(stringContext).replace(/''/g, "'").slice(1, -1);
};
var SnowflakeASTGen = /*#__PURE__*/function (_SnowflakeParserVisit) {
_inherits(SnowflakeASTGen, _SnowflakeParserVisit);
var _super = _createSuper(SnowflakeASTGen);
function SnowflakeASTGen() {
var _this;
_classCallCheck(this, SnowflakeASTGen);
_this = _super.call(this);
_this.data = {
schemas: [],
tables: [],
refs: [],
enums: [],
tableGroups: [],
aliases: [],
project: {}
};
return _this;
}
// batch? EOF
_createClass(SnowflakeASTGen, [{
key: "visitSnowflake_file",
value: function visitSnowflake_file(ctx) {
var _ctx$batch;
(_ctx$batch = ctx.batch()) === null || _ctx$batch === void 0 ? void 0 : _ctx$batch.accept(this);
return this.data;
}
// sql_command (SEMI sql_command)* SEMI?
}, {
key: "visitBatch",
value: function visitBatch(ctx) {
var _this2 = this;
ctx.sql_command().forEach(function (c) {
c.accept(_this2);
});
}
// ddl_command | dml_command | show_command | use_command | describe_command | other_command
}, {
key: "visitSql_command",
value: function visitSql_command(ctx) {
if (ctx.ddl_command()) {
ctx.ddl_command().accept(this);
}
}
// alter_command | create_command | drop_command | undrop_command
}, {
key: "visitDdl_command",
value: function visitDdl_command(ctx) {
if (ctx.alter_command()) {
ctx.alter_command().accept(this);
} else if (ctx.create_command()) {
ctx.create_command().accept(this);
}
}
// check SnowflakeParser.g4 line 1442
}, {
key: "visitCreate_command",
value: function visitCreate_command(ctx) {
if (ctx.create_table()) {
var table = ctx.create_table().accept(this);
this.data.tables.push(table);
} else if (ctx.create_table_like()) {
var _ctx$create_table_lik = ctx.create_table_like().accept(this),
_ctx$create_table_lik2 = _slicedToArray(_ctx$create_table_lik, 4),
schemaNameLike = _ctx$create_table_lik2[0],
nameLike = _ctx$create_table_lik2[1],
schemaNameOrigin = _ctx$create_table_lik2[2],
nameOrigin = _ctx$create_table_lik2[3];
var originTable = this.data.tables.reduce(function (acc, ele) {
if (ele.name === nameOrigin && ele.schemaName === schemaNameOrigin) return ele;
return acc;
}, null);
if (originTable) {
var likeTable = new _AST.Table({
name: nameLike,
schemaName: schemaNameLike,
fields: originTable.fields
});
this.data.tables.push(likeTable);
}
}
}
// : CREATE or_replace? TRANSIENT? DATABASE if_not_exists? id_ clone_at_before? (
// DATA_RETENTION_TIME_IN_DAYS EQ num
// )? (MAX_DATA_EXTENSION_TIME_IN_DAYS EQ num)? default_ddl_collation? with_tags? comment_clause?
// ;
}, {
key: "visitCreate_database",
value: function visitCreate_database(ctx) {
var schema = (0, _helpers.getOriginalText)(ctx.object_name());
this.data.schemas.push(schema);
}
// CREATE or_replace? table_type? TABLE (
// if_not_exists? object_name
// | object_name if_not_exists?
// ) ((comment_clause? create_table_clause) | (create_table_clause comment_clause?))
}, {
key: "visitCreate_table",
value: function visitCreate_table(ctx) {
var _ctx$comment_clause,
_this3 = this,
_this$data$refs,
_this$data$refs2;
var _ctx$object_name$acce = ctx.object_name().accept(this),
_ctx$object_name$acce2 = _slicedToArray(_ctx$object_name$acce, 3),
databaseName = _ctx$object_name$acce2[0],
schemaName = _ctx$object_name$acce2[1],
tableName = _ctx$object_name$acce2[2];
var _ctx$create_table_cla = ctx.create_table_clause().accept(this),
definitions = _ctx$create_table_cla.definitions,
tableNote = _ctx$create_table_cla.tableNote;
var note = ((_ctx$comment_clause = ctx.comment_clause()) === null || _ctx$comment_clause === void 0 ? void 0 : _ctx$comment_clause.accept(this)) || tableNote;
var _definitions$reduce = definitions.reduce(function (acc, ele) {
if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.FIELD) acc[0].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.UNIQUE) acc[1].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.FK) acc[2].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.PK) {
/** @type {Index} */
var index = ele.value;
if (index.columns.length > 1) acc[1].push(ele.value);else acc[3] = index;
}
return acc;
}, [[], [], [], null]),
_definitions$reduce2 = _slicedToArray(_definitions$reduce, 4),
fieldsData = _definitions$reduce2[0],
indexes = _definitions$reduce2[1],
tableRefs = _definitions$reduce2[2],
singlePkIndex = _definitions$reduce2[3];
var inlineRefsOfFields = fieldsData.map(function (fieldData) {
var _field$type$type_name;
var field = fieldData.field,
inlineRefs = fieldData.inlineRefs;
if (((_field$type$type_name = field.type.type_name) === null || _field$type$type_name === void 0 ? void 0 : _field$type$type_name.toLowerCase()) === 'enum') {
var _values = field.type.args.map(function (arg) {
var newValue = arg.replace(/'|"|`/g, '').trim();
return {
name: newValue
};
});
var _enum = new _AST.Enum({
name: "".concat(tableName, "_").concat(field.name, "_enum"),
schemaName: schemaName,
values: _values
});
field.type.type_name = _enum.name;
field.type.schemaName = _enum.schemaName;
_this3.data.enums.push(_enum);
}
inlineRefs.forEach(function (inlineRef) {
inlineRef.endpoints[0].tableName = tableName;
inlineRef.endpoints[0].schemaName = schemaName;
inlineRef.endpoints[0].fieldNames = [field.name];
});
return inlineRefs;
});
(_this$data$refs = this.data.refs).push.apply(_this$data$refs, _toConsumableArray((0, _lodash.flatten)(inlineRefsOfFields)));
(_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;
})));
var table = new _AST.Table({
name: tableName,
schemaName: schemaName,
fields: fieldsData.map(function (fd) {
return fd.field;
}),
indexes: indexes,
note: note
});
if (singlePkIndex) {
var field = table.fields.find(function (f) {
return f.name === singlePkIndex.columns[0].value;
});
if (field) field.pk = true;
}
return table;
}
// : ALTER TABLE if_exists? object_name RENAME TO object_name
// | ALTER TABLE if_exists? object_name SWAP WITH object_name
// | ALTER TABLE if_exists? object_name (
// clustering_action
// | table_column_action
// | constraint_action
// )
// | ALTER TABLE if_exists? object_name ext_table_column_action
// | ALTER TABLE if_exists? object_name search_optimization_action
// | ALTER TABLE if_exists? object_name SET stage_file_format? (
// STAGE_COPY_OPTIONS EQ '(' copy_options ')'
// )? (DATA_RETENTION_TIME_IN_DAYS EQ num)? (MAX_DATA_EXTENSION_TIME_IN_DAYS EQ num)? (
// CHANGE_TRACKING EQ true_false
// )? default_ddl_collation? comment_clause?
// | ALTER TABLE if_exists? object_name set_tags
// | ALTER TABLE if_exists? object_name unset_tags
// | ALTER TABLE if_exists? object_name UNSET (
// DATA_RETENTION_TIME_IN_DAYS
// | MAX_DATA_EXTENSION_TIME_IN_DAYS
// | CHANGE_TRACKING
// | DEFAULT_DDL_COLLATION_
// | COMMENT
// |
// )
// //[ , ... ]
// | ALTER TABLE if_exists? object_name ADD ROW ACCESS POLICY id_ ON column_list_in_parentheses
// | ALTER TABLE if_exists? object_name DROP ROW ACCESS POLICY id_
// | ALTER TABLE if_exists? object_name DROP ROW ACCESS POLICY id_ COMMA ADD ROW ACCESS POLICY id_ ON column_list_in_parentheses
// | ALTER TABLE if_exists? object_name DROP ALL ROW ACCESS POLICIES
}, {
key: "visitAlter_table",
value: function visitAlter_table(ctx) {
if (ctx.constraint_action()) {
var _ctx$object_name$0$ac = ctx.object_name()[0].accept(this),
_ctx$object_name$0$ac2 = _slicedToArray(_ctx$object_name$0$ac, 3),
databaseName = _ctx$object_name$0$ac2[0],
schemaName = _ctx$object_name$0$ac2[1],
tableName = _ctx$object_name$0$ac2[2];
var definition = ctx.constraint_action().accept(this);
if (definition) {
var _this$data$refs3;
var fieldsData = [];
var indexes = [];
var tableRefs = [];
if (definition.kind === _constants.TABLE_CONSTRAINT_KIND.FIELD) fieldsData.push(definition.value);else if (definition.kind === _constants.TABLE_CONSTRAINT_KIND.UNIQUE) indexes.push(definition.value);else if (definition.kind === _constants.TABLE_CONSTRAINT_KIND.FK) tableRefs.push(definition.value);
(_this$data$refs3 = this.data.refs).push.apply(_this$data$refs3, _toConsumableArray(tableRefs.map(function (tableRef) {
tableRef.endpoints[0].tableName = tableName;
tableRef.endpoints[0].schemaName = schemaName;
return tableRef;
})));
var table = this.data.tables.reduce(function (acc, ele) {
if (ele.name === tableName && ele.schemaName === schemaName) return ele;
return acc;
}, null);
if (table) {
var _table$fields, _table$indexes;
(_table$fields = table.fields).push.apply(_table$fields, fieldsData);
(_table$indexes = table.indexes).push.apply(_table$indexes, indexes);
}
return table;
}
}
return null;
}
// CREATE or_replace? TRANSIENT? TABLE if_not_exists? object_name LIKE object_name cluster_by? copy_grants?
}, {
key: "visitCreate_table_like",
value: function visitCreate_table_like(ctx) {
var _ctx$object_name$0$ac3 = ctx.object_name()[0].accept(this),
_ctx$object_name$0$ac4 = _slicedToArray(_ctx$object_name$0$ac3, 3),
databaseNameLike = _ctx$object_name$0$ac4[0],
schemaNameLike = _ctx$object_name$0$ac4[1],
nameLike = _ctx$object_name$0$ac4[2];
var _ctx$object_name$1$ac = ctx.object_name()[1].accept(this),
_ctx$object_name$1$ac2 = _slicedToArray(_ctx$object_name$1$ac, 3),
databaseNameOrigin = _ctx$object_name$1$ac2[0],
schemaNameOrigin = _ctx$object_name$1$ac2[1],
nameOrigin = _ctx$object_name$1$ac2[2];
return [schemaNameLike, nameLike, schemaNameOrigin, nameOrigin];
}
// : d = id_ DOT s = id_ DOT o = id_
// | s = id_ DOT o = id_
// | o = id_
}, {
key: "visitObject_name",
value: function visitObject_name(ctx) {
var _ctx$d, _ctx$s;
return [(_ctx$d = ctx.d) === null || _ctx$d === void 0 ? void 0 : _ctx$d.accept(this), (_ctx$s = ctx.s) === null || _ctx$s === void 0 ? void 0 : _ctx$s.accept(this), ctx.o.accept(this)];
}
// : ID | ID2 | DOUBLE_QUOTE_ID | DOUBLE_QUOTE_BLANK
// | keyword | non_reserved_words | object_type_plural | data_type
// | builtin_function | unary_or_binary_builtin_function | binary_builtin_function
// | binary_or_ternary_builtin_function | ternary_builtin_function
}, {
key: "visitId_",
value: function visitId_(ctx) {
if (ctx.DOUBLE_QUOTE_ID()) return (0, _helpers.getOriginalText)(ctx).slice(1, -1);
return (0, _helpers.getOriginalText)(ctx);
}
// : (
// column_decl_item_list_paren cluster_by?
// | cluster_by? comment_clause? column_decl_item_list_paren
// ) stage_file_format? (STAGE_COPY_OPTIONS EQ LR_BRACKET copy_options RR_BRACKET)? (
// DATA_RETENTION_TIME_IN_DAYS EQ num
// )? (MAX_DATA_EXTENSION_TIME_IN_DAYS EQ num)? change_tracking? default_ddl_collation? copy_grants? comment_clause? with_row_access_policy?
// with_tags?
}, {
key: "visitCreate_table_clause",
value: function visitCreate_table_clause(ctx) {
var _ctx$comment_clause$m,
_this4 = this;
return {
definitions: ctx.column_decl_item_list_paren().accept(this),
tableNote: (_ctx$comment_clause$m = ctx.comment_clause().map(function (c) {
return c.accept(_this4);
})) === null || _ctx$comment_clause$m === void 0 ? void 0 : _ctx$comment_clause$m[-1]
};
}
// : COMMENT EQ string
}, {
key: "visitComment_clause",
value: function visitComment_clause(ctx) {
return sanitizeComment(ctx.string());
}
// '(' column_decl_item_list ')'
}, {
key: "visitColumn_decl_item_list_paren",
value: function visitColumn_decl_item_list_paren(ctx) {
return ctx.column_decl_item_list().accept(this);
}
// column_decl_item (COMMA column_decl_item)*
}, {
key: "visitColumn_decl_item_list",
value: function visitColumn_decl_item_list(ctx) {
var _this5 = this;
return ctx.column_decl_item().map(function (c) {
return c.accept(_this5);
}).filter(function (r) {
return r;
});
}
// full_col_decl | out_of_line_constraint
}, {
key: "visitColumn_decl_item",
value: function visitColumn_decl_item(ctx) {
if (ctx.full_col_decl()) {
return {
kind: _constants.TABLE_CONSTRAINT_KIND.FIELD,
value: ctx.full_col_decl().accept(this)
};
}
if (ctx.out_of_line_constraint()) {
var test = ctx.out_of_line_constraint().accept(this);
return test;
}
return null;
}
// col_decl (collate | inline_constraint | null_not_null | (default_value | NULL_))* with_masking_policy? with_tags? ( COMMENT string )?
}, {
key: "visitFull_col_decl",
value: function visitFull_col_decl(ctx) {
var _this6 = this;
var field = ctx.col_decl().accept(this);
var inlineRefs = [];
if (ctx.inline_constraint()) {
var inlineConstraints = ctx.inline_constraint().map(function (c) {
return c.accept(_this6);
});
if (!(0, _lodash.isEmpty)(inlineConstraints)) {
inlineConstraints.forEach(function (inlineConstraint) {
if (inlineConstraint.kind === _constants.COLUMN_CONSTRAINT_KIND.UNIQUE) field.unique = true;else if (inlineConstraint.kind === _constants.COLUMN_CONSTRAINT_KIND.PK) field.pk = true;else if (inlineConstraint.kind === _constants.COLUMN_CONSTRAINT_KIND.FK) inlineRefs.push(inlineConstraint.value);
});
}
}
if (ctx.null_not_null()) {
var notNulls = ctx.null_not_null().map(function (c) {
return c.accept(_this6);
});
if (!(0, _lodash.isEmpty)(notNulls)) {
notNulls.forEach(function (notNull) {
if (notNull.kind === _constants.COLUMN_CONSTRAINT_KIND.NOT_NULL) field.not_null = notNull.value;
});
}
}
if (ctx.default_value()) {
var defaultOrIncrements = ctx.default_value().map(function (c) {
return c.accept(_this6);
});
defaultOrIncrements.forEach(function (defaultOrIncrement) {
if (defaultOrIncrement.kind === _constants.COLUMN_CONSTRAINT_KIND.DEFAULT) {
field.dbdefault = defaultOrIncrement.value;
} else if (defaultOrIncrement.kind === _constants.COLUMN_CONSTRAINT_KIND.INCREMENT) {
field.increment = defaultOrIncrement.value;
}
});
}
if (ctx.COMMENT()) {
field.note = sanitizeComment(ctx.string());
}
return {
field: field,
inlineRefs: inlineRefs
};
}
// column_name data_type virtual_column_decl?
}, {
key: "visitCol_decl",
value: function visitCol_decl(ctx) {
return new _AST.Field({
name: ctx.column_name().accept(this),
type: {
type_name: (0, _helpers.getOriginalText)(ctx.data_type()),
schemaName: null
}
});
}
// : (id_ '.')? id_
}, {
key: "visitColumn_name",
value: function visitColumn_name(ctx) {
var _this7 = this;
return ctx.id_().map(function (c) {
return c.accept(_this7);
}).join('.');
}
// : (CONSTRAINT id_)? (
// (UNIQUE | primary_key) common_constraint_properties*
// | foreign_key REFERENCES object_name (LR_BRACKET column_name RR_BRACKET)? constraint_properties
// )
}, {
key: "visitInline_constraint",
value: function visitInline_constraint(ctx) {
if (ctx.UNIQUE()) {
return {
kind: _constants.COLUMN_CONSTRAINT_KIND.UNIQUE,
value: true
};
}
if (ctx.primary_key()) {
return {
kind: _constants.COLUMN_CONSTRAINT_KIND.PK,
value: true
};
}
if (ctx.foreign_key() && ctx.column_name()) {
var _ctx$object_name$acce3 = ctx.object_name().accept(this),
_ctx$object_name$acce4 = _slicedToArray(_ctx$object_name$acce3, 3),
databaseName = _ctx$object_name$acce4[0],
schemaName = _ctx$object_name$acce4[1],
tableName = _ctx$object_name$acce4[2];
var destColumns = [ctx.column_name().accept(this)];
return {
kind: _constants.COLUMN_CONSTRAINT_KIND.FK,
value: {
endpoints: [{
tableName: null,
schemaName: null,
fieldNames: null,
relation: '*'
}, {
tableName: tableName,
schemaName: schemaName,
fieldNames: destColumns,
relation: '1'
}]
}
};
}
return {
kind: null,
value: null
};
}
// : (CONSTRAINT id_)? (
// (UNIQUE | primary_key) column_list_in_parentheses common_constraint_properties*
// | foreign_key column_list_in_parentheses REFERENCES object_name column_list_in_parentheses constraint_properties
// )
}, {
key: "visitOut_of_line_constraint",
value: function visitOut_of_line_constraint(ctx) {
var _this8 = this;
if (ctx.UNIQUE()) {
var _ctx$id_;
var name = (_ctx$id_ = ctx.id_()) === null || _ctx$id_ === void 0 ? void 0 : _ctx$id_.accept(this);
var colNames = (0, _lodash.flatten)(ctx.column_list_in_parentheses().map(function (c) {
return c.accept(_this8);
}));
var value = new _AST.Index({
unique: true,
columns: colNames.map(function (colName) {
return {
value: colName,
type: _constants.CONSTRAINT_TYPE.COLUMN
};
})
});
return {
name: name,
kind: _constants.TABLE_CONSTRAINT_KIND.UNIQUE,
value: value
};
}
if (ctx.primary_key()) {
var _ctx$id_2;
var _name = (_ctx$id_2 = ctx.id_()) === null || _ctx$id_2 === void 0 ? void 0 : _ctx$id_2.accept(this);
var _colNames = (0, _lodash.flatten)(ctx.column_list_in_parentheses().map(function (c) {
return c.accept(_this8);
}));
var _value = new _AST.Index({
name: _name,
pk: true,
columns: _colNames.map(function (colName) {
return {
value: colName,
type: _constants.CONSTRAINT_TYPE.COLUMN
};
})
});
return {
kind: _constants.TABLE_CONSTRAINT_KIND.PK,
value: _value
};
}
if (ctx.foreign_key() && ctx.column_list_in_parentheses().length === 2) {
var _ctx$object_name$acce5 = ctx.object_name().accept(this),
_ctx$object_name$acce6 = _slicedToArray(_ctx$object_name$acce5, 3),
databaseName = _ctx$object_name$acce6[0],
schemaName = _ctx$object_name$acce6[1],
tableName = _ctx$object_name$acce6[2];
var sourceColumns = ctx.column_list_in_parentheses()[0].accept(this);
var destColumns = ctx.column_list_in_parentheses()[1].accept(this);
return {
kind: _constants.TABLE_CONSTRAINT_KIND.FK,
value: {
endpoints: [{
tableName: null,
schemaName: null,
fieldNames: sourceColumns,
relation: '*'
}, {
tableName: tableName,
schemaName: schemaName,
fieldNames: destColumns,
relation: '1'
}]
}
};
}
return null;
}
// LR_BRACKET column_list RR_BRACKET
}, {
key: "visitColumn_list_in_parentheses",
value: function visitColumn_list_in_parentheses(ctx) {
return ctx.column_list().accept(this);
}
// column_name (COMMA column_name)*
}, {
key: "visitColumn_list",
value: function visitColumn_list(ctx) {
var _this9 = this;
return ctx.column_name().map(function (c) {
return c.accept(_this9);
});
}
// NOT? NULL_
}, {
key: "visitNull_not_null",
value: function visitNull_not_null(ctx) {
return {
kind: _constants.COLUMN_CONSTRAINT_KIND.NOT_NULL,
value: !!ctx.NOT()
};
}
// : DEFAULT expr
// | (AUTOINCREMENT | IDENTITY) (
// LR_BRACKET num COMMA num RR_BRACKET
// | start_with
// | increment_by
// | start_with increment_by
// )? order_noorder?
}, {
key: "visitDefault_value",
value: function visitDefault_value(ctx) {
// dbdefault: {value: string, type: 'string' | 'number' | 'boolean' | 'expression'},
if (ctx.DEFAULT()) {
return {
kind: _constants.COLUMN_CONSTRAINT_KIND.DEFAULT,
value: ctx.expr().accept(this)
};
}
if (ctx.AUTOINCREMENT() || ctx.IDENTITY()) {
return {
kind: _constants.COLUMN_CONSTRAINT_KIND.INCREMENT,
value: true
};
}
return null;
}
// : object_name DOT NEXTVAL
// | expr LSB expr RSB //array access
// | expr COLON expr //json access
// | expr DOT (VALUE | expr)
// | expr COLLATE string
// | case_expression
// | iff_expr
// | bracket_expression
// | op = ( PLUS | MINUS) expr
// | expr op = (STAR | DIVIDE | MODULE) expr
// | expr op = (PLUS | MINUS | PIPE_PIPE) expr
// | expr comparison_operator expr
// | op = NOT+ expr
// | expr AND expr //bool operation
// | expr OR expr //bool operation
// | arr_literal
// // | expr time_zone
// | expr over_clause
// | cast_expr
// | expr COLON_COLON data_type // Cast also
// | try_cast_expr
// | json_literal
// | trim_expression
// | function_call
// | subquery
// | expr IS null_not_null
// | expr NOT? IN LR_BRACKET (subquery | expr_list) RR_BRACKET
// | expr NOT? ( LIKE | ILIKE) expr (ESCAPE expr)?
// | expr NOT? RLIKE expr
// | expr NOT? (LIKE | ILIKE) ANY LR_BRACKET expr (COMMA expr)* RR_BRACKET (ESCAPE expr)?
// | primitive_expression //Should be latest rule as it's nearly a catch all
}, {
key: "visitExpr",
value: function visitExpr(ctx) {
if (ctx.primitive_expression()) return ctx.primitive_expression().accept(this);
return {
value: (0, _helpers.getOriginalText)(ctx),
type: 'expression'
};
}
// : DEFAULT //?
// | NULL_
// | id_ ('.' id_)* // json field access
// | full_column_name
// | literal
// | BOTH_Q
// | ARRAY_Q
// | OBJECT_Q
// //| json_literal
// //| arr_literal
}, {
key: "visitPrimitive_expression",
value: function visitPrimitive_expression(ctx) {
if (ctx.NULL_()) return {
value: null,
type: 'boolean'
};
if (ctx.literal()) return ctx.literal().accept(this);
return {
value: (0, _helpers.getOriginalText)(ctx),
type: 'expression'
};
}
// : STRING // string, date, time, timestamp
// | sign? DECIMAL
// | sign? (REAL | FLOAT)
// | true_false
// | NULL_
// | AT_Q
// ;
}, {
key: "visitLiteral",
value: function visitLiteral(ctx) {
if (ctx.STRING()) return {
value: sanitizeComment(ctx),
type: 'string'
};
if (ctx.DECIMAL() || ctx.REAL || ctx.FLOAT) return {
value: (0, _helpers.getOriginalText)(ctx),
type: 'number'
};
if (ctx.true_false() || ctx.NULL_()) return {
value: (0, _helpers.getOriginalText)(ctx),
type: 'boolean'
};
return {
value: (0, _helpers.getOriginalText)(ctx),
type: 'expression'
};
}
// : ADD out_of_line_constraint
// | RENAME CONSTRAINT id_ TO id_
// | alter_modify (CONSTRAINT id_ | primary_key | UNIQUE | foreign_key) column_list_in_parentheses enforced_not_enforced? (
// VALIDATE
// | NOVALIDATE
// ) (RELY | NORELY)
// | DROP (CONSTRAINT id_ | primary_key | UNIQUE | foreign_key) column_list_in_parentheses? cascade_restrict?
// | DROP PRIMARY KEY
}, {
key: "visitConstraint_action",
value: function visitConstraint_action(ctx) {
if (ctx.ADD()) {
return ctx.out_of_line_constraint().accept(this);
}
return null;
}
}]);
return SnowflakeASTGen;
}(_SnowflakeParserVisitor["default"]);
exports["default"] = SnowflakeASTGen;
;