@dbml/core 3.5.1 → 3.5.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,750 @@
1
+ "use strict";
2
+
3
+ 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); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports["default"] = void 0;
8
+ var _lodash = require("lodash");
9
+ var _SnowflakeParserVisitor = _interopRequireDefault(require("../../parsers/snowflake/SnowflakeParserVisitor"));
10
+ var _AST = require("../AST");
11
+ var _constants = require("../constants");
12
+ var _helpers = require("../helpers");
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
14
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
15
+ 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."); }
16
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
17
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
18
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
19
+ 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."); }
20
+ 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); }
21
+ 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; }
22
+ 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; } }
23
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
24
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
+ 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); } }
26
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
27
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
28
+ 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); }
29
+ 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); }
30
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
+ 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); }; }
32
+ 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); }
33
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
34
+ 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; } }
35
+ 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 */
36
+ var sanitizeComment = function sanitizeComment(stringContext) {
37
+ return (0, _helpers.getOriginalText)(stringContext).replace(/''/g, "'").slice(1, -1);
38
+ };
39
+ var SnowflakeASTGen = /*#__PURE__*/function (_SnowflakeParserVisit) {
40
+ _inherits(SnowflakeASTGen, _SnowflakeParserVisit);
41
+ var _super = _createSuper(SnowflakeASTGen);
42
+ function SnowflakeASTGen() {
43
+ var _this;
44
+ _classCallCheck(this, SnowflakeASTGen);
45
+ _this = _super.call(this);
46
+ _this.data = {
47
+ schemas: [],
48
+ tables: [],
49
+ refs: [],
50
+ enums: [],
51
+ tableGroups: [],
52
+ aliases: [],
53
+ project: {}
54
+ };
55
+ return _this;
56
+ }
57
+
58
+ // batch? EOF
59
+ _createClass(SnowflakeASTGen, [{
60
+ key: "visitSnowflake_file",
61
+ value: function visitSnowflake_file(ctx) {
62
+ var _ctx$batch;
63
+ (_ctx$batch = ctx.batch()) === null || _ctx$batch === void 0 ? void 0 : _ctx$batch.accept(this);
64
+ return this.data;
65
+ }
66
+
67
+ // sql_command (SEMI sql_command)* SEMI?
68
+ }, {
69
+ key: "visitBatch",
70
+ value: function visitBatch(ctx) {
71
+ var _this2 = this;
72
+ ctx.sql_command().forEach(function (c) {
73
+ c.accept(_this2);
74
+ });
75
+ }
76
+
77
+ // ddl_command | dml_command | show_command | use_command | describe_command | other_command
78
+ }, {
79
+ key: "visitSql_command",
80
+ value: function visitSql_command(ctx) {
81
+ if (ctx.ddl_command()) {
82
+ ctx.ddl_command().accept(this);
83
+ }
84
+ }
85
+
86
+ // alter_command | create_command | drop_command | undrop_command
87
+ }, {
88
+ key: "visitDdl_command",
89
+ value: function visitDdl_command(ctx) {
90
+ if (ctx.alter_command()) {
91
+ ctx.alter_command().accept(this);
92
+ } else if (ctx.create_command()) {
93
+ ctx.create_command().accept(this);
94
+ }
95
+ }
96
+
97
+ // check SnowflakeParser.g4 line 1442
98
+ }, {
99
+ key: "visitCreate_command",
100
+ value: function visitCreate_command(ctx) {
101
+ if (ctx.create_table()) {
102
+ var table = ctx.create_table().accept(this);
103
+ this.data.tables.push(table);
104
+ } else if (ctx.create_table_like()) {
105
+ var _ctx$create_table_lik = ctx.create_table_like().accept(this),
106
+ _ctx$create_table_lik2 = _slicedToArray(_ctx$create_table_lik, 4),
107
+ schemaNameLike = _ctx$create_table_lik2[0],
108
+ nameLike = _ctx$create_table_lik2[1],
109
+ schemaNameOrigin = _ctx$create_table_lik2[2],
110
+ nameOrigin = _ctx$create_table_lik2[3];
111
+ var originTable = this.data.tables.reduce(function (acc, ele) {
112
+ if (ele.name === nameOrigin && ele.schemaName === schemaNameOrigin) return ele;
113
+ return acc;
114
+ }, null);
115
+ if (originTable) {
116
+ var likeTable = new _AST.Table({
117
+ name: nameLike,
118
+ schemaName: schemaNameLike,
119
+ fields: originTable.fields
120
+ });
121
+ this.data.tables.push(likeTable);
122
+ }
123
+ }
124
+ }
125
+
126
+ // : CREATE or_replace? TRANSIENT? DATABASE if_not_exists? id_ clone_at_before? (
127
+ // DATA_RETENTION_TIME_IN_DAYS EQ num
128
+ // )? (MAX_DATA_EXTENSION_TIME_IN_DAYS EQ num)? default_ddl_collation? with_tags? comment_clause?
129
+ // ;
130
+ }, {
131
+ key: "visitCreate_database",
132
+ value: function visitCreate_database(ctx) {
133
+ var schema = (0, _helpers.getOriginalText)(ctx.object_name());
134
+ this.data.schemas.push(schema);
135
+ }
136
+
137
+ // CREATE or_replace? table_type? TABLE (
138
+ // if_not_exists? object_name
139
+ // | object_name if_not_exists?
140
+ // ) ((comment_clause? create_table_clause) | (create_table_clause comment_clause?))
141
+ }, {
142
+ key: "visitCreate_table",
143
+ value: function visitCreate_table(ctx) {
144
+ var _ctx$comment_clause,
145
+ _this3 = this,
146
+ _this$data$refs,
147
+ _this$data$refs2;
148
+ var _ctx$object_name$acce = ctx.object_name().accept(this),
149
+ _ctx$object_name$acce2 = _slicedToArray(_ctx$object_name$acce, 3),
150
+ databaseName = _ctx$object_name$acce2[0],
151
+ schemaName = _ctx$object_name$acce2[1],
152
+ tableName = _ctx$object_name$acce2[2];
153
+ var _ctx$create_table_cla = ctx.create_table_clause().accept(this),
154
+ definitions = _ctx$create_table_cla.definitions,
155
+ tableNote = _ctx$create_table_cla.tableNote;
156
+ var note = ((_ctx$comment_clause = ctx.comment_clause()) === null || _ctx$comment_clause === void 0 ? void 0 : _ctx$comment_clause.accept(this)) || tableNote;
157
+ var _definitions$reduce = definitions.reduce(function (acc, ele) {
158
+ 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) {
159
+ /** @type {Index} */
160
+ var index = ele.value;
161
+ if (index.columns.length > 1) acc[1].push(ele.value);else acc[3] = index;
162
+ }
163
+ return acc;
164
+ }, [[], [], [], null]),
165
+ _definitions$reduce2 = _slicedToArray(_definitions$reduce, 4),
166
+ fieldsData = _definitions$reduce2[0],
167
+ indexes = _definitions$reduce2[1],
168
+ tableRefs = _definitions$reduce2[2],
169
+ singlePkIndex = _definitions$reduce2[3];
170
+ var inlineRefsOfFields = fieldsData.map(function (fieldData) {
171
+ var _field$type$type_name;
172
+ var field = fieldData.field,
173
+ inlineRefs = fieldData.inlineRefs;
174
+ if (((_field$type$type_name = field.type.type_name) === null || _field$type$type_name === void 0 ? void 0 : _field$type$type_name.toLowerCase()) === 'enum') {
175
+ var _values = field.type.args.map(function (arg) {
176
+ var newValue = arg.replace(/'|"|`/g, '').trim();
177
+ return {
178
+ name: newValue
179
+ };
180
+ });
181
+ var _enum = new _AST.Enum({
182
+ name: "".concat(tableName, "_").concat(field.name, "_enum"),
183
+ schemaName: schemaName,
184
+ values: _values
185
+ });
186
+ field.type.type_name = _enum.name;
187
+ field.type.schemaName = _enum.schemaName;
188
+ _this3.data.enums.push(_enum);
189
+ }
190
+ inlineRefs.forEach(function (inlineRef) {
191
+ inlineRef.endpoints[0].tableName = tableName;
192
+ inlineRef.endpoints[0].schemaName = schemaName;
193
+ inlineRef.endpoints[0].fieldNames = [field.name];
194
+ });
195
+ return inlineRefs;
196
+ });
197
+ (_this$data$refs = this.data.refs).push.apply(_this$data$refs, _toConsumableArray((0, _lodash.flatten)(inlineRefsOfFields)));
198
+ (_this$data$refs2 = this.data.refs).push.apply(_this$data$refs2, _toConsumableArray(tableRefs.map(function (tableRef) {
199
+ tableRef.endpoints[0].tableName = tableName;
200
+ tableRef.endpoints[0].schemaName = schemaName;
201
+ return tableRef;
202
+ })));
203
+ var table = new _AST.Table({
204
+ name: tableName,
205
+ schemaName: schemaName,
206
+ fields: fieldsData.map(function (fd) {
207
+ return fd.field;
208
+ }),
209
+ indexes: indexes,
210
+ note: note
211
+ });
212
+ if (singlePkIndex) {
213
+ var field = table.fields.find(function (f) {
214
+ return f.name === singlePkIndex.columns[0].value;
215
+ });
216
+ if (field) field.pk = true;
217
+ }
218
+ return table;
219
+ }
220
+
221
+ // : ALTER TABLE if_exists? object_name RENAME TO object_name
222
+ // | ALTER TABLE if_exists? object_name SWAP WITH object_name
223
+ // | ALTER TABLE if_exists? object_name (
224
+ // clustering_action
225
+ // | table_column_action
226
+ // | constraint_action
227
+ // )
228
+ // | ALTER TABLE if_exists? object_name ext_table_column_action
229
+ // | ALTER TABLE if_exists? object_name search_optimization_action
230
+ // | ALTER TABLE if_exists? object_name SET stage_file_format? (
231
+ // STAGE_COPY_OPTIONS EQ '(' copy_options ')'
232
+ // )? (DATA_RETENTION_TIME_IN_DAYS EQ num)? (MAX_DATA_EXTENSION_TIME_IN_DAYS EQ num)? (
233
+ // CHANGE_TRACKING EQ true_false
234
+ // )? default_ddl_collation? comment_clause?
235
+ // | ALTER TABLE if_exists? object_name set_tags
236
+ // | ALTER TABLE if_exists? object_name unset_tags
237
+ // | ALTER TABLE if_exists? object_name UNSET (
238
+ // DATA_RETENTION_TIME_IN_DAYS
239
+ // | MAX_DATA_EXTENSION_TIME_IN_DAYS
240
+ // | CHANGE_TRACKING
241
+ // | DEFAULT_DDL_COLLATION_
242
+ // | COMMENT
243
+ // |
244
+ // )
245
+ // //[ , ... ]
246
+ // | ALTER TABLE if_exists? object_name ADD ROW ACCESS POLICY id_ ON column_list_in_parentheses
247
+ // | ALTER TABLE if_exists? object_name DROP ROW ACCESS POLICY id_
248
+ // | ALTER TABLE if_exists? object_name DROP ROW ACCESS POLICY id_ COMMA ADD ROW ACCESS POLICY id_ ON column_list_in_parentheses
249
+ // | ALTER TABLE if_exists? object_name DROP ALL ROW ACCESS POLICIES
250
+ }, {
251
+ key: "visitAlter_table",
252
+ value: function visitAlter_table(ctx) {
253
+ if (ctx.constraint_action()) {
254
+ var _ctx$object_name$0$ac = ctx.object_name()[0].accept(this),
255
+ _ctx$object_name$0$ac2 = _slicedToArray(_ctx$object_name$0$ac, 3),
256
+ databaseName = _ctx$object_name$0$ac2[0],
257
+ schemaName = _ctx$object_name$0$ac2[1],
258
+ tableName = _ctx$object_name$0$ac2[2];
259
+ var definition = ctx.constraint_action().accept(this);
260
+ if (definition) {
261
+ var _this$data$refs3;
262
+ var fieldsData = [];
263
+ var indexes = [];
264
+ var tableRefs = [];
265
+ 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);
266
+ (_this$data$refs3 = this.data.refs).push.apply(_this$data$refs3, _toConsumableArray(tableRefs.map(function (tableRef) {
267
+ tableRef.endpoints[0].tableName = tableName;
268
+ tableRef.endpoints[0].schemaName = schemaName;
269
+ return tableRef;
270
+ })));
271
+ var table = this.data.tables.reduce(function (acc, ele) {
272
+ if (ele.name === tableName && ele.schemaName === schemaName) return ele;
273
+ return acc;
274
+ }, null);
275
+ if (table) {
276
+ var _table$fields, _table$indexes;
277
+ (_table$fields = table.fields).push.apply(_table$fields, fieldsData);
278
+ (_table$indexes = table.indexes).push.apply(_table$indexes, indexes);
279
+ }
280
+ return table;
281
+ }
282
+ }
283
+ return null;
284
+ }
285
+
286
+ // CREATE or_replace? TRANSIENT? TABLE if_not_exists? object_name LIKE object_name cluster_by? copy_grants?
287
+ }, {
288
+ key: "visitCreate_table_like",
289
+ value: function visitCreate_table_like(ctx) {
290
+ var _ctx$object_name$0$ac3 = ctx.object_name()[0].accept(this),
291
+ _ctx$object_name$0$ac4 = _slicedToArray(_ctx$object_name$0$ac3, 3),
292
+ databaseNameLike = _ctx$object_name$0$ac4[0],
293
+ schemaNameLike = _ctx$object_name$0$ac4[1],
294
+ nameLike = _ctx$object_name$0$ac4[2];
295
+ var _ctx$object_name$1$ac = ctx.object_name()[1].accept(this),
296
+ _ctx$object_name$1$ac2 = _slicedToArray(_ctx$object_name$1$ac, 3),
297
+ databaseNameOrigin = _ctx$object_name$1$ac2[0],
298
+ schemaNameOrigin = _ctx$object_name$1$ac2[1],
299
+ nameOrigin = _ctx$object_name$1$ac2[2];
300
+ return [schemaNameLike, nameLike, schemaNameOrigin, nameOrigin];
301
+ }
302
+
303
+ // : d = id_ DOT s = id_ DOT o = id_
304
+ // | s = id_ DOT o = id_
305
+ // | o = id_
306
+ }, {
307
+ key: "visitObject_name",
308
+ value: function visitObject_name(ctx) {
309
+ var _ctx$d, _ctx$s;
310
+ 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)];
311
+ }
312
+
313
+ // : ID | ID2 | DOUBLE_QUOTE_ID | DOUBLE_QUOTE_BLANK
314
+ // | keyword | non_reserved_words | object_type_plural | data_type
315
+ // | builtin_function | unary_or_binary_builtin_function | binary_builtin_function
316
+ // | binary_or_ternary_builtin_function | ternary_builtin_function
317
+ }, {
318
+ key: "visitId_",
319
+ value: function visitId_(ctx) {
320
+ if (ctx.DOUBLE_QUOTE_ID()) return (0, _helpers.getOriginalText)(ctx).slice(1, -1);
321
+ return (0, _helpers.getOriginalText)(ctx);
322
+ }
323
+
324
+ // : (
325
+ // column_decl_item_list_paren cluster_by?
326
+ // | cluster_by? comment_clause? column_decl_item_list_paren
327
+ // ) stage_file_format? (STAGE_COPY_OPTIONS EQ LR_BRACKET copy_options RR_BRACKET)? (
328
+ // DATA_RETENTION_TIME_IN_DAYS EQ num
329
+ // )? (MAX_DATA_EXTENSION_TIME_IN_DAYS EQ num)? change_tracking? default_ddl_collation? copy_grants? comment_clause? with_row_access_policy?
330
+ // with_tags?
331
+ }, {
332
+ key: "visitCreate_table_clause",
333
+ value: function visitCreate_table_clause(ctx) {
334
+ var _ctx$comment_clause$m,
335
+ _this4 = this;
336
+ return {
337
+ definitions: ctx.column_decl_item_list_paren().accept(this),
338
+ tableNote: (_ctx$comment_clause$m = ctx.comment_clause().map(function (c) {
339
+ return c.accept(_this4);
340
+ })) === null || _ctx$comment_clause$m === void 0 ? void 0 : _ctx$comment_clause$m[-1]
341
+ };
342
+ }
343
+
344
+ // : COMMENT EQ string
345
+ }, {
346
+ key: "visitComment_clause",
347
+ value: function visitComment_clause(ctx) {
348
+ return sanitizeComment(ctx.string());
349
+ }
350
+
351
+ // '(' column_decl_item_list ')'
352
+ }, {
353
+ key: "visitColumn_decl_item_list_paren",
354
+ value: function visitColumn_decl_item_list_paren(ctx) {
355
+ return ctx.column_decl_item_list().accept(this);
356
+ }
357
+
358
+ // column_decl_item (COMMA column_decl_item)*
359
+ }, {
360
+ key: "visitColumn_decl_item_list",
361
+ value: function visitColumn_decl_item_list(ctx) {
362
+ var _this5 = this;
363
+ return ctx.column_decl_item().map(function (c) {
364
+ return c.accept(_this5);
365
+ }).filter(function (r) {
366
+ return r;
367
+ });
368
+ }
369
+
370
+ // full_col_decl | out_of_line_constraint
371
+ }, {
372
+ key: "visitColumn_decl_item",
373
+ value: function visitColumn_decl_item(ctx) {
374
+ if (ctx.full_col_decl()) {
375
+ return {
376
+ kind: _constants.TABLE_CONSTRAINT_KIND.FIELD,
377
+ value: ctx.full_col_decl().accept(this)
378
+ };
379
+ }
380
+ if (ctx.out_of_line_constraint()) {
381
+ var test = ctx.out_of_line_constraint().accept(this);
382
+ return test;
383
+ }
384
+ return null;
385
+ }
386
+
387
+ // col_decl (collate | inline_constraint | null_not_null | (default_value | NULL_))* with_masking_policy? with_tags? ( COMMENT string )?
388
+ }, {
389
+ key: "visitFull_col_decl",
390
+ value: function visitFull_col_decl(ctx) {
391
+ var _this6 = this;
392
+ var field = ctx.col_decl().accept(this);
393
+ var inlineRefs = [];
394
+ if (ctx.inline_constraint()) {
395
+ var inlineConstraints = ctx.inline_constraint().map(function (c) {
396
+ return c.accept(_this6);
397
+ });
398
+ if (!(0, _lodash.isEmpty)(inlineConstraints)) {
399
+ inlineConstraints.forEach(function (inlineConstraint) {
400
+ 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);
401
+ });
402
+ }
403
+ }
404
+ if (ctx.null_not_null()) {
405
+ var notNulls = ctx.null_not_null().map(function (c) {
406
+ return c.accept(_this6);
407
+ });
408
+ if (!(0, _lodash.isEmpty)(notNulls)) {
409
+ notNulls.forEach(function (notNull) {
410
+ if (notNull.kind === _constants.COLUMN_CONSTRAINT_KIND.NOT_NULL) field.not_null = notNull.value;
411
+ });
412
+ }
413
+ }
414
+ if (ctx.default_value()) {
415
+ var defaultOrIncrements = ctx.default_value().map(function (c) {
416
+ return c.accept(_this6);
417
+ });
418
+ defaultOrIncrements.forEach(function (defaultOrIncrement) {
419
+ if (defaultOrIncrement.kind === _constants.COLUMN_CONSTRAINT_KIND.DEFAULT) {
420
+ field.dbdefault = defaultOrIncrement.value;
421
+ } else if (defaultOrIncrement.kind === _constants.COLUMN_CONSTRAINT_KIND.INCREMENT) {
422
+ field.increment = defaultOrIncrement.value;
423
+ }
424
+ });
425
+ }
426
+ if (ctx.COMMENT()) {
427
+ field.note = sanitizeComment(ctx.string());
428
+ }
429
+ return {
430
+ field: field,
431
+ inlineRefs: inlineRefs
432
+ };
433
+ }
434
+
435
+ // column_name data_type virtual_column_decl?
436
+ }, {
437
+ key: "visitCol_decl",
438
+ value: function visitCol_decl(ctx) {
439
+ return new _AST.Field({
440
+ name: ctx.column_name().accept(this),
441
+ type: {
442
+ type_name: (0, _helpers.getOriginalText)(ctx.data_type()),
443
+ schemaName: null
444
+ }
445
+ });
446
+ }
447
+
448
+ // : (id_ '.')? id_
449
+ }, {
450
+ key: "visitColumn_name",
451
+ value: function visitColumn_name(ctx) {
452
+ var _this7 = this;
453
+ return ctx.id_().map(function (c) {
454
+ return c.accept(_this7);
455
+ }).join('.');
456
+ }
457
+
458
+ // : (CONSTRAINT id_)? (
459
+ // (UNIQUE | primary_key) common_constraint_properties*
460
+ // | foreign_key REFERENCES object_name (LR_BRACKET column_name RR_BRACKET)? constraint_properties
461
+ // )
462
+ }, {
463
+ key: "visitInline_constraint",
464
+ value: function visitInline_constraint(ctx) {
465
+ if (ctx.UNIQUE()) {
466
+ return {
467
+ kind: _constants.COLUMN_CONSTRAINT_KIND.UNIQUE,
468
+ value: true
469
+ };
470
+ }
471
+ if (ctx.primary_key()) {
472
+ return {
473
+ kind: _constants.COLUMN_CONSTRAINT_KIND.PK,
474
+ value: true
475
+ };
476
+ }
477
+ if (ctx.foreign_key() && ctx.column_name()) {
478
+ var _ctx$object_name$acce3 = ctx.object_name().accept(this),
479
+ _ctx$object_name$acce4 = _slicedToArray(_ctx$object_name$acce3, 3),
480
+ databaseName = _ctx$object_name$acce4[0],
481
+ schemaName = _ctx$object_name$acce4[1],
482
+ tableName = _ctx$object_name$acce4[2];
483
+ var destColumns = [ctx.column_name().accept(this)];
484
+ return {
485
+ kind: _constants.COLUMN_CONSTRAINT_KIND.FK,
486
+ value: {
487
+ endpoints: [{
488
+ tableName: null,
489
+ schemaName: null,
490
+ fieldNames: null,
491
+ relation: '*'
492
+ }, {
493
+ tableName: tableName,
494
+ schemaName: schemaName,
495
+ fieldNames: destColumns,
496
+ relation: '1'
497
+ }]
498
+ }
499
+ };
500
+ }
501
+ return {
502
+ kind: null,
503
+ value: null
504
+ };
505
+ }
506
+
507
+ // : (CONSTRAINT id_)? (
508
+ // (UNIQUE | primary_key) column_list_in_parentheses common_constraint_properties*
509
+ // | foreign_key column_list_in_parentheses REFERENCES object_name column_list_in_parentheses constraint_properties
510
+ // )
511
+ }, {
512
+ key: "visitOut_of_line_constraint",
513
+ value: function visitOut_of_line_constraint(ctx) {
514
+ var _this8 = this;
515
+ if (ctx.UNIQUE()) {
516
+ var _ctx$id_;
517
+ var name = (_ctx$id_ = ctx.id_()) === null || _ctx$id_ === void 0 ? void 0 : _ctx$id_.accept(this);
518
+ var colNames = (0, _lodash.flatten)(ctx.column_list_in_parentheses().map(function (c) {
519
+ return c.accept(_this8);
520
+ }));
521
+ var value = new _AST.Index({
522
+ unique: true,
523
+ columns: colNames.map(function (colName) {
524
+ return {
525
+ value: colName,
526
+ type: _constants.CONSTRAINT_TYPE.COLUMN
527
+ };
528
+ })
529
+ });
530
+ return {
531
+ name: name,
532
+ kind: _constants.TABLE_CONSTRAINT_KIND.UNIQUE,
533
+ value: value
534
+ };
535
+ }
536
+ if (ctx.primary_key()) {
537
+ var _ctx$id_2;
538
+ var _name = (_ctx$id_2 = ctx.id_()) === null || _ctx$id_2 === void 0 ? void 0 : _ctx$id_2.accept(this);
539
+ var _colNames = (0, _lodash.flatten)(ctx.column_list_in_parentheses().map(function (c) {
540
+ return c.accept(_this8);
541
+ }));
542
+ var _value = new _AST.Index({
543
+ name: _name,
544
+ pk: true,
545
+ columns: _colNames.map(function (colName) {
546
+ return {
547
+ value: colName,
548
+ type: _constants.CONSTRAINT_TYPE.COLUMN
549
+ };
550
+ })
551
+ });
552
+ return {
553
+ kind: _constants.TABLE_CONSTRAINT_KIND.PK,
554
+ value: _value
555
+ };
556
+ }
557
+ if (ctx.foreign_key() && ctx.column_list_in_parentheses().length === 2) {
558
+ var _ctx$object_name$acce5 = ctx.object_name().accept(this),
559
+ _ctx$object_name$acce6 = _slicedToArray(_ctx$object_name$acce5, 3),
560
+ databaseName = _ctx$object_name$acce6[0],
561
+ schemaName = _ctx$object_name$acce6[1],
562
+ tableName = _ctx$object_name$acce6[2];
563
+ var sourceColumns = ctx.column_list_in_parentheses()[0].accept(this);
564
+ var destColumns = ctx.column_list_in_parentheses()[1].accept(this);
565
+ return {
566
+ kind: _constants.TABLE_CONSTRAINT_KIND.FK,
567
+ value: {
568
+ endpoints: [{
569
+ tableName: null,
570
+ schemaName: null,
571
+ fieldNames: sourceColumns,
572
+ relation: '*'
573
+ }, {
574
+ tableName: tableName,
575
+ schemaName: schemaName,
576
+ fieldNames: destColumns,
577
+ relation: '1'
578
+ }]
579
+ }
580
+ };
581
+ }
582
+ return null;
583
+ }
584
+
585
+ // LR_BRACKET column_list RR_BRACKET
586
+ }, {
587
+ key: "visitColumn_list_in_parentheses",
588
+ value: function visitColumn_list_in_parentheses(ctx) {
589
+ return ctx.column_list().accept(this);
590
+ }
591
+
592
+ // column_name (COMMA column_name)*
593
+ }, {
594
+ key: "visitColumn_list",
595
+ value: function visitColumn_list(ctx) {
596
+ var _this9 = this;
597
+ return ctx.column_name().map(function (c) {
598
+ return c.accept(_this9);
599
+ });
600
+ }
601
+
602
+ // NOT? NULL_
603
+ }, {
604
+ key: "visitNull_not_null",
605
+ value: function visitNull_not_null(ctx) {
606
+ return {
607
+ kind: _constants.COLUMN_CONSTRAINT_KIND.NOT_NULL,
608
+ value: !!ctx.NOT()
609
+ };
610
+ }
611
+
612
+ // : DEFAULT expr
613
+ // | (AUTOINCREMENT | IDENTITY) (
614
+ // LR_BRACKET num COMMA num RR_BRACKET
615
+ // | start_with
616
+ // | increment_by
617
+ // | start_with increment_by
618
+ // )? order_noorder?
619
+ }, {
620
+ key: "visitDefault_value",
621
+ value: function visitDefault_value(ctx) {
622
+ // dbdefault: {value: string, type: 'string' | 'number' | 'boolean' | 'expression'},
623
+ if (ctx.DEFAULT()) {
624
+ return {
625
+ kind: _constants.COLUMN_CONSTRAINT_KIND.DEFAULT,
626
+ value: ctx.expr().accept(this)
627
+ };
628
+ }
629
+ if (ctx.AUTOINCREMENT() || ctx.IDENTITY()) {
630
+ return {
631
+ kind: _constants.COLUMN_CONSTRAINT_KIND.INCREMENT,
632
+ value: true
633
+ };
634
+ }
635
+ return null;
636
+ }
637
+
638
+ // : object_name DOT NEXTVAL
639
+ // | expr LSB expr RSB //array access
640
+ // | expr COLON expr //json access
641
+ // | expr DOT (VALUE | expr)
642
+ // | expr COLLATE string
643
+ // | case_expression
644
+ // | iff_expr
645
+ // | bracket_expression
646
+ // | op = ( PLUS | MINUS) expr
647
+ // | expr op = (STAR | DIVIDE | MODULE) expr
648
+ // | expr op = (PLUS | MINUS | PIPE_PIPE) expr
649
+ // | expr comparison_operator expr
650
+ // | op = NOT+ expr
651
+ // | expr AND expr //bool operation
652
+ // | expr OR expr //bool operation
653
+ // | arr_literal
654
+ // // | expr time_zone
655
+ // | expr over_clause
656
+ // | cast_expr
657
+ // | expr COLON_COLON data_type // Cast also
658
+ // | try_cast_expr
659
+ // | json_literal
660
+ // | trim_expression
661
+ // | function_call
662
+ // | subquery
663
+ // | expr IS null_not_null
664
+ // | expr NOT? IN LR_BRACKET (subquery | expr_list) RR_BRACKET
665
+ // | expr NOT? ( LIKE | ILIKE) expr (ESCAPE expr)?
666
+ // | expr NOT? RLIKE expr
667
+ // | expr NOT? (LIKE | ILIKE) ANY LR_BRACKET expr (COMMA expr)* RR_BRACKET (ESCAPE expr)?
668
+ // | primitive_expression //Should be latest rule as it's nearly a catch all
669
+ }, {
670
+ key: "visitExpr",
671
+ value: function visitExpr(ctx) {
672
+ if (ctx.primitive_expression()) return ctx.primitive_expression().accept(this);
673
+ return {
674
+ value: (0, _helpers.getOriginalText)(ctx),
675
+ type: 'expression'
676
+ };
677
+ }
678
+
679
+ // : DEFAULT //?
680
+ // | NULL_
681
+ // | id_ ('.' id_)* // json field access
682
+ // | full_column_name
683
+ // | literal
684
+ // | BOTH_Q
685
+ // | ARRAY_Q
686
+ // | OBJECT_Q
687
+ // //| json_literal
688
+ // //| arr_literal
689
+ }, {
690
+ key: "visitPrimitive_expression",
691
+ value: function visitPrimitive_expression(ctx) {
692
+ if (ctx.NULL_()) return {
693
+ value: null,
694
+ type: 'boolean'
695
+ };
696
+ if (ctx.literal()) return ctx.literal().accept(this);
697
+ return {
698
+ value: (0, _helpers.getOriginalText)(ctx),
699
+ type: 'expression'
700
+ };
701
+ }
702
+
703
+ // : STRING // string, date, time, timestamp
704
+ // | sign? DECIMAL
705
+ // | sign? (REAL | FLOAT)
706
+ // | true_false
707
+ // | NULL_
708
+ // | AT_Q
709
+ // ;
710
+ }, {
711
+ key: "visitLiteral",
712
+ value: function visitLiteral(ctx) {
713
+ if (ctx.STRING()) return {
714
+ value: (0, _helpers.getOriginalText)(ctx),
715
+ type: 'string'
716
+ };
717
+ if (ctx.DECIMAL() || ctx.REAL || ctx.FLOAT) return {
718
+ value: (0, _helpers.getOriginalText)(ctx),
719
+ type: 'number'
720
+ };
721
+ if (ctx.true_false() || ctx.NULL_()) return {
722
+ value: (0, _helpers.getOriginalText)(ctx),
723
+ type: 'boolean'
724
+ };
725
+ return {
726
+ value: (0, _helpers.getOriginalText)(ctx),
727
+ type: 'expression'
728
+ };
729
+ }
730
+
731
+ // : ADD out_of_line_constraint
732
+ // | RENAME CONSTRAINT id_ TO id_
733
+ // | alter_modify (CONSTRAINT id_ | primary_key | UNIQUE | foreign_key) column_list_in_parentheses enforced_not_enforced? (
734
+ // VALIDATE
735
+ // | NOVALIDATE
736
+ // ) (RELY | NORELY)
737
+ // | DROP (CONSTRAINT id_ | primary_key | UNIQUE | foreign_key) column_list_in_parentheses? cascade_restrict?
738
+ // | DROP PRIMARY KEY
739
+ }, {
740
+ key: "visitConstraint_action",
741
+ value: function visitConstraint_action(ctx) {
742
+ if (ctx.ADD()) {
743
+ return ctx.out_of_line_constraint().accept(this);
744
+ }
745
+ return null;
746
+ }
747
+ }]);
748
+ return SnowflakeASTGen;
749
+ }(_SnowflakeParserVisitor["default"]);
750
+ exports["default"] = SnowflakeASTGen;