@dbml/core 3.11.0-alpha-0 → 3.12.0-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.
- package/lib/model_structure/database.js +36 -11
- package/lib/model_structure/dbState.js +1 -0
- package/lib/parse/ANTLR/ASTGeneration/constants.js +4 -2
- package/lib/parse/ANTLR/ASTGeneration/mssql/MssqlASTGen.js +969 -28
- package/lib/parse/ANTLR/ASTGeneration/mysql/MySQLASTGen.js +8 -14
- package/lib/parse/ANTLR/ASTGeneration/postgres/PostgresASTGen.js +8 -19
- package/lib/parse/ANTLR/README.md +6 -1
- package/lib/parse/ANTLR/parsers/mssql/TSqlLexer.g4 +6 -0
- package/lib/parse/ANTLR/parsers/mssql/TSqlParser.g4 +6 -0
- package/package.json +3 -3
- package/types/model_structure/database.d.ts +30 -12
- package/types/parse/Parser.d.ts +1 -1
|
@@ -10,13 +10,12 @@ var _utils = require("../../../../model_structure/utils");
|
|
|
10
10
|
var _TSqlParserVisitor2 = _interopRequireDefault(require("../../parsers/mssql/TSqlParserVisitor"));
|
|
11
11
|
var _constants = require("../constants");
|
|
12
12
|
var _helpers = require("../helpers");
|
|
13
|
+
var _AST = require("../AST");
|
|
13
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
14
15
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
15
16
|
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 _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); }
|
|
17
17
|
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
18
18
|
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
19
|
-
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; }
|
|
20
19
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
21
20
|
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); } }
|
|
22
21
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
@@ -28,7 +27,91 @@ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflec
|
|
|
28
27
|
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); }
|
|
29
28
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
30
29
|
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; } }
|
|
31
|
-
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
30
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
31
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
32
|
+
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."); }
|
|
33
|
+
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); }
|
|
34
|
+
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; }
|
|
35
|
+
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; } }
|
|
36
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /* eslint-disable class-methods-use-this */
|
|
37
|
+
var ADD_DESCRIPTION_FUNCTION_NAME = 'sp_addextendedproperty';
|
|
38
|
+
var getSchemaAndTableName = function getSchemaAndTableName(names) {
|
|
39
|
+
var tableName = (0, _lodash.last)(names);
|
|
40
|
+
var schemaName = names.length > 1 ? (0, _lodash.nth)(names, -2) : undefined;
|
|
41
|
+
return {
|
|
42
|
+
schemaName: schemaName,
|
|
43
|
+
tableName: tableName
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
var getStringFromRawString = function getStringFromRawString(rawString) {
|
|
47
|
+
if (rawString.startsWith("N'")) {
|
|
48
|
+
return rawString;
|
|
49
|
+
}
|
|
50
|
+
return rawString.slice(1, rawString.length - 1);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {any[]} columnDefTableConstraints
|
|
55
|
+
* @returns {{
|
|
56
|
+
* fieldsData: any[],
|
|
57
|
+
* indexes: any[],
|
|
58
|
+
* tableRefs: any[],
|
|
59
|
+
* }}
|
|
60
|
+
*/
|
|
61
|
+
var splitColumnDefTableConstraints = function splitColumnDefTableConstraints(columnDefTableConstraints) {
|
|
62
|
+
var _columnDefTableConstr = columnDefTableConstraints.reduce(function (acc, columnDefTableConstraint) {
|
|
63
|
+
switch (columnDefTableConstraint.kind) {
|
|
64
|
+
case _constants.TABLE_CONSTRAINT_KIND.FIELD:
|
|
65
|
+
acc[0].push(columnDefTableConstraint.value);
|
|
66
|
+
break;
|
|
67
|
+
case _constants.TABLE_CONSTRAINT_KIND.INDEX:
|
|
68
|
+
case _constants.TABLE_CONSTRAINT_KIND.PK:
|
|
69
|
+
case _constants.TABLE_CONSTRAINT_KIND.UNIQUE:
|
|
70
|
+
acc[1].push(columnDefTableConstraint.value);
|
|
71
|
+
break;
|
|
72
|
+
case _constants.TABLE_CONSTRAINT_KIND.FK:
|
|
73
|
+
acc[2].push(columnDefTableConstraint.value);
|
|
74
|
+
break;
|
|
75
|
+
case _constants.TABLE_CONSTRAINT_KIND.DEFAULT:
|
|
76
|
+
acc[3].push(columnDefTableConstraint.value);
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
return acc;
|
|
82
|
+
}, [[], [], [], []]),
|
|
83
|
+
_columnDefTableConstr2 = _slicedToArray(_columnDefTableConstr, 4),
|
|
84
|
+
fieldsData = _columnDefTableConstr2[0],
|
|
85
|
+
indexes = _columnDefTableConstr2[1],
|
|
86
|
+
tableRefs = _columnDefTableConstr2[2],
|
|
87
|
+
columnDefaults = _columnDefTableConstr2[3];
|
|
88
|
+
return {
|
|
89
|
+
fieldsData: fieldsData,
|
|
90
|
+
indexes: indexes,
|
|
91
|
+
tableRefs: tableRefs,
|
|
92
|
+
columnDefaults: columnDefaults
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
var parseFieldsAndInlineRefsFromFieldsData = function parseFieldsAndInlineRefsFromFieldsData(fieldsData, tableName, schemaName) {
|
|
96
|
+
var _fieldsData$reduce = fieldsData.reduce(function (acc, fieldData) {
|
|
97
|
+
var inlineRefs = fieldData.inline_refs.map(function (inlineRef) {
|
|
98
|
+
inlineRef.endpoints[0].tableName = tableName;
|
|
99
|
+
inlineRef.endpoints[0].schemaName = schemaName;
|
|
100
|
+
inlineRef.endpoints[0].fieldNames = [fieldData.field.name];
|
|
101
|
+
return inlineRef;
|
|
102
|
+
});
|
|
103
|
+
acc[0].push(inlineRefs);
|
|
104
|
+
acc[1].push(fieldData.field);
|
|
105
|
+
return acc;
|
|
106
|
+
}, [[], []]),
|
|
107
|
+
_fieldsData$reduce2 = _slicedToArray(_fieldsData$reduce, 2),
|
|
108
|
+
resInlineRefs = _fieldsData$reduce2[0],
|
|
109
|
+
fields = _fieldsData$reduce2[1];
|
|
110
|
+
return {
|
|
111
|
+
inlineRefs: resInlineRefs,
|
|
112
|
+
fields: fields
|
|
113
|
+
};
|
|
114
|
+
};
|
|
32
115
|
var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
33
116
|
_inherits(MssqlASTGen, _TSqlParserVisitor);
|
|
34
117
|
var _super = _createSuper(MssqlASTGen);
|
|
@@ -44,7 +127,7 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
44
127
|
tableGroups: [],
|
|
45
128
|
aliases: [],
|
|
46
129
|
project: {},
|
|
47
|
-
records:
|
|
130
|
+
records: []
|
|
48
131
|
};
|
|
49
132
|
return _this;
|
|
50
133
|
}
|
|
@@ -91,8 +174,16 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
91
174
|
}, {
|
|
92
175
|
key: "visitSql_clauses",
|
|
93
176
|
value: function visitSql_clauses(ctx) {
|
|
177
|
+
if (ctx.ddl_clause()) {
|
|
178
|
+
ctx.ddl_clause().accept(this);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
94
181
|
if (ctx.dml_clause()) {
|
|
95
182
|
ctx.dml_clause().accept(this);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (ctx.another_statement()) {
|
|
186
|
+
ctx.another_statement().accept(this);
|
|
96
187
|
}
|
|
97
188
|
}
|
|
98
189
|
|
|
@@ -120,7 +211,6 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
120
211
|
}, {
|
|
121
212
|
key: "visitInsert_statement",
|
|
122
213
|
value: function visitInsert_statement(ctx) {
|
|
123
|
-
var _this$data$records$fu;
|
|
124
214
|
// [ 'users' ]
|
|
125
215
|
// [ 'test', 'users' ]
|
|
126
216
|
// [ 'db', 'test', 'users' ]
|
|
@@ -132,22 +222,13 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
132
222
|
var schemaName = names.length > 1 ? (0, _lodash.nth)(names, -2) : undefined;
|
|
133
223
|
var columns = ctx.insert_column_name_list() ? ctx.insert_column_name_list().accept(this) : [];
|
|
134
224
|
var values = ctx.insert_statement_value().accept(this);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (!this.data.records[fullTableName]) {
|
|
143
|
-
this.data.records[fullTableName] = {
|
|
144
|
-
schemaName: schemaName,
|
|
145
|
-
tableName: tableName,
|
|
146
|
-
columns: columns,
|
|
147
|
-
values: []
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
(_this$data$records$fu = this.data.records[fullTableName].values).push.apply(_this$data$records$fu, _toConsumableArray(values));
|
|
225
|
+
var record = {
|
|
226
|
+
schemaName: schemaName,
|
|
227
|
+
tableName: tableName,
|
|
228
|
+
columns: columns,
|
|
229
|
+
values: values
|
|
230
|
+
};
|
|
231
|
+
this.data.records.push(record);
|
|
151
232
|
}
|
|
152
233
|
|
|
153
234
|
// ddl_object
|
|
@@ -276,6 +357,9 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
276
357
|
if (ctx.unary_operator_expression()) {
|
|
277
358
|
return ctx.unary_operator_expression().accept(this);
|
|
278
359
|
}
|
|
360
|
+
if (ctx.bracket_expression()) {
|
|
361
|
+
return ctx.bracket_expression().accept(this);
|
|
362
|
+
}
|
|
279
363
|
|
|
280
364
|
// Default case for any other expression type
|
|
281
365
|
return {
|
|
@@ -284,6 +368,16 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
284
368
|
};
|
|
285
369
|
}
|
|
286
370
|
|
|
371
|
+
// bracket_expression
|
|
372
|
+
// : '(' expression ')'
|
|
373
|
+
// | '(' subquery ')'
|
|
374
|
+
// ;
|
|
375
|
+
}, {
|
|
376
|
+
key: "visitBracket_expression",
|
|
377
|
+
value: function visitBracket_expression(ctx) {
|
|
378
|
+
return ctx.expression() ? ctx.expression().accept(this) : null;
|
|
379
|
+
}
|
|
380
|
+
|
|
287
381
|
// primitive_constant
|
|
288
382
|
// : STRING // string, datetime or uniqueidentifier
|
|
289
383
|
// | BINARY
|
|
@@ -295,17 +389,18 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
295
389
|
key: "visitPrimitive_constant",
|
|
296
390
|
value: function visitPrimitive_constant(ctx) {
|
|
297
391
|
if (ctx.STRING() || ctx.BINARY()) {
|
|
392
|
+
var value = getStringFromRawString(ctx.getText());
|
|
298
393
|
return {
|
|
299
|
-
value:
|
|
300
|
-
type: _constants.DATA_TYPE.STRING
|
|
394
|
+
value: value,
|
|
395
|
+
type: value.startsWith("N'") ? _constants.DATA_TYPE.EXPRESSION : _constants.DATA_TYPE.STRING
|
|
301
396
|
};
|
|
302
397
|
}
|
|
303
398
|
if (ctx.DOLLAR()) {
|
|
304
399
|
var dollar = (0, _lodash.first)(ctx.children).getText();
|
|
305
400
|
var sign = ctx.children.length > 2 ? (0, _lodash.nth)(ctx.children, -2) : '';
|
|
306
|
-
var
|
|
401
|
+
var _value = (0, _lodash.last)(ctx.children).getText();
|
|
307
402
|
return {
|
|
308
|
-
value: "".concat(dollar).concat(sign).concat(
|
|
403
|
+
value: "".concat(dollar).concat(sign).concat(_value),
|
|
309
404
|
type: _constants.DATA_TYPE.STRING
|
|
310
405
|
};
|
|
311
406
|
}
|
|
@@ -316,7 +411,7 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
316
411
|
};
|
|
317
412
|
}
|
|
318
413
|
return {
|
|
319
|
-
value:
|
|
414
|
+
value: (0, _helpers.getOriginalText)(ctx),
|
|
320
415
|
type: _constants.DATA_TYPE.EXPRESSION
|
|
321
416
|
};
|
|
322
417
|
}
|
|
@@ -341,6 +436,14 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
341
436
|
type: _constants.DATA_TYPE.EXPRESSION
|
|
342
437
|
};
|
|
343
438
|
}
|
|
439
|
+
}, {
|
|
440
|
+
key: "visitSCALAR_FUNCTION",
|
|
441
|
+
value: function visitSCALAR_FUNCTION(ctx) {
|
|
442
|
+
return {
|
|
443
|
+
value: (0, _helpers.getOriginalText)(ctx),
|
|
444
|
+
type: _constants.DATA_TYPE.EXPRESSION
|
|
445
|
+
};
|
|
446
|
+
}
|
|
344
447
|
|
|
345
448
|
// unary_operator_expression
|
|
346
449
|
// : '~' expression
|
|
@@ -368,7 +471,32 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
368
471
|
}, {
|
|
369
472
|
key: "visitData_type",
|
|
370
473
|
value: function visitData_type(ctx) {
|
|
371
|
-
|
|
474
|
+
var id = ctx.id_().accept(this);
|
|
475
|
+
if (ctx.MAX()) {
|
|
476
|
+
return "".concat(id, "(MAX)");
|
|
477
|
+
}
|
|
478
|
+
if (ctx.IDENTITY()) {
|
|
479
|
+
if (ctx.DECIMAL().length) {
|
|
480
|
+
var seedAndInc = ctx.DECIMAL().map(function (decimal) {
|
|
481
|
+
return decimal.getText();
|
|
482
|
+
});
|
|
483
|
+
return "".concat(id, " IDENTITY(").concat(seedAndInc, ")");
|
|
484
|
+
}
|
|
485
|
+
return "".concat(id, " IDENTITY");
|
|
486
|
+
}
|
|
487
|
+
if (ctx.DOUBLE()) {
|
|
488
|
+
if (ctx.PRECISION()) {
|
|
489
|
+
return "".concat(id, "(").concat(ctx.PRECISION().getText(), ")");
|
|
490
|
+
}
|
|
491
|
+
return id;
|
|
492
|
+
}
|
|
493
|
+
if (ctx.DECIMAL().length) {
|
|
494
|
+
var scaleAndPrec = ctx.DECIMAL().map(function (decimal) {
|
|
495
|
+
return decimal.getText();
|
|
496
|
+
});
|
|
497
|
+
return "".concat(id, "(").concat(scaleAndPrec.join(','), ")");
|
|
498
|
+
}
|
|
499
|
+
return id;
|
|
372
500
|
}
|
|
373
501
|
}, {
|
|
374
502
|
key: "visitPrimitive_expression",
|
|
@@ -376,7 +504,7 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
376
504
|
if (ctx.NULL_()) {
|
|
377
505
|
return {
|
|
378
506
|
value: ctx.getText(),
|
|
379
|
-
type: _constants.DATA_TYPE.
|
|
507
|
+
type: _constants.DATA_TYPE.BOOLEAN
|
|
380
508
|
};
|
|
381
509
|
}
|
|
382
510
|
if (ctx.primitive_constant()) {
|
|
@@ -387,6 +515,819 @@ var MssqlASTGen = /*#__PURE__*/function (_TSqlParserVisitor) {
|
|
|
387
515
|
type: _constants.DATA_TYPE.EXPRESSION
|
|
388
516
|
};
|
|
389
517
|
}
|
|
518
|
+
|
|
519
|
+
// ddl_clause
|
|
520
|
+
// | alter_table
|
|
521
|
+
// | create_index
|
|
522
|
+
// | create_table
|
|
523
|
+
// ;
|
|
524
|
+
// more details at: packages/dbml-core/src/parse/ANTLR/parsers/mssql/TSqlParser.g4 line 73
|
|
525
|
+
}, {
|
|
526
|
+
key: "visitDdl_clause",
|
|
527
|
+
value: function visitDdl_clause(ctx) {
|
|
528
|
+
if (ctx.create_table()) {
|
|
529
|
+
ctx.create_table().accept(this);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (ctx.alter_table()) {
|
|
533
|
+
ctx.alter_table().accept(this);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (ctx.create_index()) {
|
|
537
|
+
ctx.create_index().accept(this);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// create_table
|
|
542
|
+
// : CREATE TABLE table_name '(' column_def_table_constraints (','? table_indices)* ','? ')' (
|
|
543
|
+
// LOCK simple_id
|
|
544
|
+
// )? table_options* (ON id_ | DEFAULT | on_partition_or_filegroup)? (TEXTIMAGE_ON id_ | DEFAULT)? ';'?
|
|
545
|
+
// ;
|
|
546
|
+
}, {
|
|
547
|
+
key: "visitCreate_table",
|
|
548
|
+
value: function visitCreate_table(ctx) {
|
|
549
|
+
var _this8 = this,
|
|
550
|
+
_this$data$refs,
|
|
551
|
+
_this$data$refs2;
|
|
552
|
+
var _getSchemaAndTableNam = getSchemaAndTableName(ctx.table_name().accept(this)),
|
|
553
|
+
schemaName = _getSchemaAndTableNam.schemaName,
|
|
554
|
+
tableName = _getSchemaAndTableNam.tableName;
|
|
555
|
+
var columnDefTableConstraints = ctx.column_def_table_constraints().accept(this);
|
|
556
|
+
var tableIndices = ctx.table_indices().map(function (tableIndex) {
|
|
557
|
+
return tableIndex.accept(_this8);
|
|
558
|
+
});
|
|
559
|
+
var _splitColumnDefTableC = splitColumnDefTableConstraints(columnDefTableConstraints),
|
|
560
|
+
fieldsData = _splitColumnDefTableC.fieldsData,
|
|
561
|
+
indexes = _splitColumnDefTableC.indexes,
|
|
562
|
+
tableRefs = _splitColumnDefTableC.tableRefs;
|
|
563
|
+
var _parseFieldsAndInline = parseFieldsAndInlineRefsFromFieldsData(fieldsData, tableName, schemaName),
|
|
564
|
+
inlineRefs = _parseFieldsAndInline.inlineRefs,
|
|
565
|
+
fields = _parseFieldsAndInline.fields;
|
|
566
|
+
(_this$data$refs = this.data.refs).push.apply(_this$data$refs, _toConsumableArray((0, _lodash.flatten)(inlineRefs)));
|
|
567
|
+
(_this$data$refs2 = this.data.refs).push.apply(_this$data$refs2, _toConsumableArray(tableRefs.map(function (tableRef) {
|
|
568
|
+
tableRef.endpoints[0].tableName = tableName;
|
|
569
|
+
tableRef.endpoints[0].schemaName = schemaName;
|
|
570
|
+
return tableRef;
|
|
571
|
+
})));
|
|
572
|
+
var table = new _AST.Table({
|
|
573
|
+
name: tableName,
|
|
574
|
+
schemaName: schemaName,
|
|
575
|
+
fields: fields,
|
|
576
|
+
indexes: tableIndices.concat(indexes)
|
|
577
|
+
});
|
|
578
|
+
this.data.tables.push(table);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// table_name
|
|
582
|
+
// : (database = id_ '.' schema = id_? '.' | schema = id_ '.')? (
|
|
583
|
+
// table = id_
|
|
584
|
+
// | blocking_hierarchy = BLOCKING_HIERARCHY
|
|
585
|
+
// )
|
|
586
|
+
// ;
|
|
587
|
+
}, {
|
|
588
|
+
key: "visitTable_name",
|
|
589
|
+
value: function visitTable_name(ctx) {
|
|
590
|
+
var _this9 = this;
|
|
591
|
+
return ctx.id_().map(function (id) {
|
|
592
|
+
return id.accept(_this9);
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// column_def_table_constraints
|
|
597
|
+
// : column_def_table_constraint (','? column_def_table_constraint)*
|
|
598
|
+
// ;
|
|
599
|
+
}, {
|
|
600
|
+
key: "visitColumn_def_table_constraints",
|
|
601
|
+
value: function visitColumn_def_table_constraints(ctx) {
|
|
602
|
+
var _this10 = this;
|
|
603
|
+
return ctx.column_def_table_constraint().map(function (columnDef) {
|
|
604
|
+
return columnDef.accept(_this10);
|
|
605
|
+
}).filter(function (columnDef) {
|
|
606
|
+
return columnDef;
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// column_def_table_constraint
|
|
611
|
+
// : column_definition
|
|
612
|
+
// | materialized_column_definition
|
|
613
|
+
// | table_constraint
|
|
614
|
+
// ;
|
|
615
|
+
}, {
|
|
616
|
+
key: "visitColumn_def_table_constraint",
|
|
617
|
+
value: function visitColumn_def_table_constraint(ctx) {
|
|
618
|
+
if (ctx.column_definition()) {
|
|
619
|
+
return ctx.column_definition().accept(this);
|
|
620
|
+
}
|
|
621
|
+
if (ctx.table_constraint()) {
|
|
622
|
+
return ctx.table_constraint().accept(this);
|
|
623
|
+
}
|
|
624
|
+
return null;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// column_definition
|
|
628
|
+
// : id_ (data_type | AS expression PERSISTED?) column_definition_element* column_index?
|
|
629
|
+
// ;
|
|
630
|
+
}, {
|
|
631
|
+
key: "visitColumn_definition",
|
|
632
|
+
value: function visitColumn_definition(ctx) {
|
|
633
|
+
var _this11 = this;
|
|
634
|
+
var columnName = ctx.id_().accept(this);
|
|
635
|
+
var type = '';
|
|
636
|
+
if (ctx.data_type()) {
|
|
637
|
+
type = ctx.data_type().accept(this);
|
|
638
|
+
} else if (ctx.expression()) {
|
|
639
|
+
// { value: "(first_name + ' ' + last_name)", type: 'expression' }
|
|
640
|
+
var expression = ctx.expression().accept(this);
|
|
641
|
+
var as = ctx.AS().getText();
|
|
642
|
+
var persisted = ctx.PERSISTED() ? " ".concat(ctx.PERSISTED().getText()) : '';
|
|
643
|
+
type = "".concat(as, " ").concat(expression.value).concat(persisted);
|
|
644
|
+
}
|
|
645
|
+
var field = new _AST.Field({
|
|
646
|
+
name: columnName,
|
|
647
|
+
type: {
|
|
648
|
+
type_name: type,
|
|
649
|
+
schemaName: null
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
var definition = {
|
|
653
|
+
kind: _constants.TABLE_CONSTRAINT_KIND.FIELD,
|
|
654
|
+
value: {
|
|
655
|
+
field: field,
|
|
656
|
+
inline_refs: []
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
var columnDefinitions = ctx.column_definition_element().map(function (columnDef) {
|
|
660
|
+
return columnDef.accept(_this11);
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
// e.g.
|
|
664
|
+
// [
|
|
665
|
+
// { kind: 'not_null', value: true },
|
|
666
|
+
// { kind: 'dbdefault', value: { value: 'GETDATE()', type: 'expression' } }
|
|
667
|
+
// ]
|
|
668
|
+
columnDefinitions.filter(function (columnDef) {
|
|
669
|
+
return columnDef;
|
|
670
|
+
}).forEach(function (columnDef) {
|
|
671
|
+
switch (columnDef.kind) {
|
|
672
|
+
case _constants.COLUMN_CONSTRAINT_KIND.DEFAULT:
|
|
673
|
+
field.dbdefault = columnDef.value;
|
|
674
|
+
break;
|
|
675
|
+
case _constants.COLUMN_CONSTRAINT_KIND.INCREMENT:
|
|
676
|
+
field.increment = columnDef.value;
|
|
677
|
+
break;
|
|
678
|
+
case _constants.COLUMN_CONSTRAINT_KIND.NOT_NULL:
|
|
679
|
+
field.not_null = columnDef.value;
|
|
680
|
+
break;
|
|
681
|
+
case _constants.COLUMN_CONSTRAINT_KIND.PK:
|
|
682
|
+
field.pk = columnDef.value;
|
|
683
|
+
break;
|
|
684
|
+
case _constants.COLUMN_CONSTRAINT_KIND.UNIQUE:
|
|
685
|
+
field.unique = columnDef.value;
|
|
686
|
+
break;
|
|
687
|
+
case _constants.COLUMN_CONSTRAINT_KIND.INLINE_REF:
|
|
688
|
+
definition.value.inline_refs.push(columnDef.value);
|
|
689
|
+
break;
|
|
690
|
+
case _constants.COLUMN_CONSTRAINT_KIND.CHECK:
|
|
691
|
+
field.type.type_name = "".concat(field.type.type_name, " ").concat(columnDef.value);
|
|
692
|
+
break;
|
|
693
|
+
default:
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
// skip column index since it is just the name of the index that a column belongs to
|
|
699
|
+
|
|
700
|
+
return definition;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// column_definition_element
|
|
704
|
+
// : FILESTREAM
|
|
705
|
+
// | COLLATE collation_name = id_
|
|
706
|
+
// | SPARSE
|
|
707
|
+
// | MASKED WITH '(' FUNCTION '=' mask_function = STRING ')'
|
|
708
|
+
// | (CONSTRAINT constraint = id_)? DEFAULT constant_expr = expression
|
|
709
|
+
// | IDENTITY ('(' seed = DECIMAL ',' increment = DECIMAL ')')?
|
|
710
|
+
// | NOT FOR REPLICATION
|
|
711
|
+
// | GENERATED ALWAYS AS (ROW | TRANSACTION_ID | SEQUENCE_NUMBER) (START | END) HIDDEN_KEYWORD?
|
|
712
|
+
// // NULL / NOT NULL is a constraint
|
|
713
|
+
// | ROWGUIDCOL
|
|
714
|
+
// | ENCRYPTED WITH '(' COLUMN_ENCRYPTION_KEY '=' key_name = STRING ',' ENCRYPTION_TYPE '=' (
|
|
715
|
+
// DETERMINISTIC
|
|
716
|
+
// | RANDOMIZED
|
|
717
|
+
// ) ',' ALGORITHM '=' algo = STRING ')'
|
|
718
|
+
// | column_constraint
|
|
719
|
+
// ;
|
|
720
|
+
}, {
|
|
721
|
+
key: "visitColumn_definition_element",
|
|
722
|
+
value: function visitColumn_definition_element(ctx) {
|
|
723
|
+
if (ctx.DEFAULT()) {
|
|
724
|
+
return {
|
|
725
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.DEFAULT,
|
|
726
|
+
value: ctx.expression().accept(this)
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
if (ctx.IDENTITY()) {
|
|
730
|
+
return {
|
|
731
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.INCREMENT,
|
|
732
|
+
value: true
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
if (ctx.column_constraint()) {
|
|
736
|
+
return ctx.column_constraint().accept(this);
|
|
737
|
+
}
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// column_constraint
|
|
742
|
+
// : (CONSTRAINT constraint = id_)? (
|
|
743
|
+
// null_notnull
|
|
744
|
+
// | ( (PRIMARY KEY | UNIQUE) clustered? primary_key_options)
|
|
745
|
+
// | ( (FOREIGN KEY)? foreign_key_options)
|
|
746
|
+
// | check_constraint
|
|
747
|
+
// )
|
|
748
|
+
// ;
|
|
749
|
+
}, {
|
|
750
|
+
key: "visitColumn_constraint",
|
|
751
|
+
value: function visitColumn_constraint(ctx) {
|
|
752
|
+
if (ctx.null_notnull()) {
|
|
753
|
+
var notNull = false;
|
|
754
|
+
var nullNotnull = ctx.null_notnull().accept(this);
|
|
755
|
+
if (nullNotnull.includes('NOT')) notNull = true;
|
|
756
|
+
return {
|
|
757
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.NOT_NULL,
|
|
758
|
+
value: notNull
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
if (ctx.UNIQUE()) {
|
|
762
|
+
return {
|
|
763
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.UNIQUE,
|
|
764
|
+
value: true
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
if (ctx.PRIMARY()) {
|
|
768
|
+
return {
|
|
769
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.PK,
|
|
770
|
+
value: true
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
if (ctx.foreign_key_options()) {
|
|
774
|
+
var _ctx$foreign_key_opti = ctx.foreign_key_options().accept(this),
|
|
775
|
+
refTableName = _ctx$foreign_key_opti.refTableName,
|
|
776
|
+
refSchemaName = _ctx$foreign_key_opti.refSchemaName,
|
|
777
|
+
fieldNames = _ctx$foreign_key_opti.fieldNames,
|
|
778
|
+
onDelete = _ctx$foreign_key_opti.onDelete,
|
|
779
|
+
onUpdate = _ctx$foreign_key_opti.onUpdate;
|
|
780
|
+
return {
|
|
781
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.INLINE_REF,
|
|
782
|
+
value: {
|
|
783
|
+
endpoints: [{
|
|
784
|
+
tableName: null,
|
|
785
|
+
schemaName: null,
|
|
786
|
+
fieldNames: null,
|
|
787
|
+
relation: '*'
|
|
788
|
+
}, {
|
|
789
|
+
tableName: refTableName,
|
|
790
|
+
schemaName: refSchemaName,
|
|
791
|
+
fieldNames: fieldNames,
|
|
792
|
+
relation: '1'
|
|
793
|
+
}],
|
|
794
|
+
onDelete: onDelete,
|
|
795
|
+
onUpdate: onUpdate
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// we do not handle check constraint since it is complicated and hard to extract enum from it
|
|
801
|
+
if (ctx.check_constraint()) {
|
|
802
|
+
return {
|
|
803
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.CHECK,
|
|
804
|
+
value: ctx.check_constraint().accept(this)
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
return null;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// check_constraint
|
|
811
|
+
// : CHECK (NOT FOR REPLICATION)? '(' search_condition ')'
|
|
812
|
+
// ;
|
|
813
|
+
}, {
|
|
814
|
+
key: "visitCheck_constraint",
|
|
815
|
+
value: function visitCheck_constraint(ctx) {
|
|
816
|
+
return (0, _helpers.getOriginalText)(ctx);
|
|
817
|
+
}
|
|
818
|
+
}, {
|
|
819
|
+
key: "visitNull_notnull",
|
|
820
|
+
value: function visitNull_notnull(ctx) {
|
|
821
|
+
return (0, _helpers.getOriginalText)(ctx);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// foreign_key_options
|
|
825
|
+
// : REFERENCES table_name '(' pk = column_name_list ')' (on_delete | on_update)* (
|
|
826
|
+
// NOT FOR REPLICATION
|
|
827
|
+
// )?
|
|
828
|
+
// ;
|
|
829
|
+
}, {
|
|
830
|
+
key: "visitForeign_key_options",
|
|
831
|
+
value: function visitForeign_key_options(ctx) {
|
|
832
|
+
var _this12 = this;
|
|
833
|
+
var names = ctx.table_name().accept(this);
|
|
834
|
+
var _getSchemaAndTableNam2 = getSchemaAndTableName(names),
|
|
835
|
+
refSchemaName = _getSchemaAndTableNam2.schemaName,
|
|
836
|
+
refTableName = _getSchemaAndTableNam2.tableName;
|
|
837
|
+
var fieldNames = ctx.column_name_list().accept(this);
|
|
838
|
+
var onDelete = ctx.on_delete() ? ctx.on_delete().map(function (action) {
|
|
839
|
+
return action.accept(_this12);
|
|
840
|
+
})[0] : null;
|
|
841
|
+
var onUpdate = ctx.on_update() ? ctx.on_update().map(function (action) {
|
|
842
|
+
return action.accept(_this12);
|
|
843
|
+
})[0] : null;
|
|
844
|
+
return {
|
|
845
|
+
refTableName: refTableName,
|
|
846
|
+
refSchemaName: refSchemaName,
|
|
847
|
+
onDelete: onDelete,
|
|
848
|
+
onUpdate: onUpdate,
|
|
849
|
+
fieldNames: fieldNames
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// column_name_list
|
|
854
|
+
// : col += id_ (',' col += id_)*
|
|
855
|
+
// ;
|
|
856
|
+
}, {
|
|
857
|
+
key: "visitColumn_name_list",
|
|
858
|
+
value: function visitColumn_name_list(ctx) {
|
|
859
|
+
var _this13 = this;
|
|
860
|
+
return ctx.id_().map(function (id) {
|
|
861
|
+
return id.accept(_this13);
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// on_delete
|
|
866
|
+
// : ON DELETE (NO ACTION | CASCADE | SET NULL_ | SET DEFAULT)
|
|
867
|
+
// ;
|
|
868
|
+
}, {
|
|
869
|
+
key: "visitOn_delete",
|
|
870
|
+
value: function visitOn_delete(ctx) {
|
|
871
|
+
if (ctx.NO()) {
|
|
872
|
+
return 'NO ACTION';
|
|
873
|
+
}
|
|
874
|
+
if (ctx.CASCADE()) {
|
|
875
|
+
return 'CASCADE';
|
|
876
|
+
}
|
|
877
|
+
if (ctx.NULL_()) {
|
|
878
|
+
return 'SET NULL';
|
|
879
|
+
}
|
|
880
|
+
return 'SET DEFAULT';
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// on_update
|
|
884
|
+
// : ON UPDATE (NO ACTION | CASCADE | SET NULL_ | SET DEFAULT)
|
|
885
|
+
// ;
|
|
886
|
+
}, {
|
|
887
|
+
key: "visitOn_update",
|
|
888
|
+
value: function visitOn_update(ctx) {
|
|
889
|
+
if (ctx.NO()) {
|
|
890
|
+
return 'NO ACTION';
|
|
891
|
+
}
|
|
892
|
+
if (ctx.CASCADE()) {
|
|
893
|
+
return 'CASCADE';
|
|
894
|
+
}
|
|
895
|
+
if (ctx.NULL_()) {
|
|
896
|
+
return 'SET NULL';
|
|
897
|
+
}
|
|
898
|
+
return 'SET DEFAULT';
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// table_constraint
|
|
902
|
+
// : (CONSTRAINT constraint = id_)? (
|
|
903
|
+
// ((PRIMARY KEY | UNIQUE) clustered? '(' column_name_list_with_order ')' primary_key_options)
|
|
904
|
+
// | ( FOREIGN KEY '(' fk = column_name_list ')' foreign_key_options)
|
|
905
|
+
// | ( CONNECTION '(' connection_node ( ',' connection_node)* ')')
|
|
906
|
+
// | ( DEFAULT constant_expr = expression FOR column = id_ (WITH VALUES)?)
|
|
907
|
+
// | check_constraint
|
|
908
|
+
// )
|
|
909
|
+
// ;
|
|
910
|
+
}, {
|
|
911
|
+
key: "visitTable_constraint",
|
|
912
|
+
value: function visitTable_constraint(ctx) {
|
|
913
|
+
var _this14 = this;
|
|
914
|
+
var name = ctx.id_() ? ctx.id_().map(function (id) {
|
|
915
|
+
return id.accept(_this14);
|
|
916
|
+
})[0] : '';
|
|
917
|
+
if (ctx.PRIMARY() || ctx.UNIQUE()) {
|
|
918
|
+
var columns = ctx.column_name_list_with_order().accept(this);
|
|
919
|
+
var index = new _AST.Index({
|
|
920
|
+
name: name,
|
|
921
|
+
columns: columns
|
|
922
|
+
});
|
|
923
|
+
if (ctx.PRIMARY()) {
|
|
924
|
+
index.pk = true;
|
|
925
|
+
}
|
|
926
|
+
if (ctx.UNIQUE()) {
|
|
927
|
+
index.unique = true;
|
|
928
|
+
}
|
|
929
|
+
return {
|
|
930
|
+
kind: ctx.PRIMARY() ? _constants.TABLE_CONSTRAINT_KIND.PK : _constants.TABLE_CONSTRAINT_KIND.UNIQUE,
|
|
931
|
+
value: index
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
if (ctx.FOREIGN()) {
|
|
935
|
+
var _columns = ctx.column_name_list().accept(this);
|
|
936
|
+
var _ctx$foreign_key_opti2 = ctx.foreign_key_options().accept(this),
|
|
937
|
+
refTableName = _ctx$foreign_key_opti2.refTableName,
|
|
938
|
+
refSchemaName = _ctx$foreign_key_opti2.refSchemaName,
|
|
939
|
+
fieldNames = _ctx$foreign_key_opti2.fieldNames,
|
|
940
|
+
onDelete = _ctx$foreign_key_opti2.onDelete,
|
|
941
|
+
onUpdate = _ctx$foreign_key_opti2.onUpdate;
|
|
942
|
+
return {
|
|
943
|
+
kind: _constants.TABLE_CONSTRAINT_KIND.FK,
|
|
944
|
+
value: {
|
|
945
|
+
name: name,
|
|
946
|
+
endpoints: [{
|
|
947
|
+
tableName: null,
|
|
948
|
+
schemaName: null,
|
|
949
|
+
fieldNames: _columns,
|
|
950
|
+
relation: '*'
|
|
951
|
+
}, {
|
|
952
|
+
tableName: refTableName,
|
|
953
|
+
schemaName: refSchemaName,
|
|
954
|
+
fieldNames: fieldNames,
|
|
955
|
+
relation: '1'
|
|
956
|
+
}],
|
|
957
|
+
onDelete: onDelete,
|
|
958
|
+
onUpdate: onUpdate
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
if (ctx.DEFAULT()) {
|
|
963
|
+
var column = ctx.column.accept(this);
|
|
964
|
+
var expression = ctx.expression().accept(this);
|
|
965
|
+
return {
|
|
966
|
+
kind: _constants.TABLE_CONSTRAINT_KIND.DEFAULT,
|
|
967
|
+
value: {
|
|
968
|
+
column: column,
|
|
969
|
+
defaultValue: expression
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
// column_name_list_with_order
|
|
977
|
+
// : id_ (ASC | DESC)? (',' id_ (ASC | DESC)?)*
|
|
978
|
+
// ;
|
|
979
|
+
}, {
|
|
980
|
+
key: "visitColumn_name_list_with_order",
|
|
981
|
+
value: function visitColumn_name_list_with_order(ctx) {
|
|
982
|
+
var _this15 = this;
|
|
983
|
+
return ctx.id_().map(function (id) {
|
|
984
|
+
return {
|
|
985
|
+
value: id.accept(_this15),
|
|
986
|
+
type: 'column'
|
|
987
|
+
};
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// table_indices
|
|
992
|
+
// : INDEX id_ UNIQUE? clustered? '(' column_name_list_with_order ')'
|
|
993
|
+
// | INDEX id_ CLUSTERED COLUMNSTORE
|
|
994
|
+
// | INDEX id_ NONCLUSTERED? COLUMNSTORE '(' column_name_list ')' create_table_index_options? (
|
|
995
|
+
// ON id_
|
|
996
|
+
// )?
|
|
997
|
+
// ;
|
|
998
|
+
}, {
|
|
999
|
+
key: "visitTable_indices",
|
|
1000
|
+
value: function visitTable_indices(ctx) {
|
|
1001
|
+
var _this16 = this;
|
|
1002
|
+
var index = new _AST.Index({
|
|
1003
|
+
name: ctx.id_().map(function (id) {
|
|
1004
|
+
return id.accept(_this16);
|
|
1005
|
+
})[0],
|
|
1006
|
+
unique: !!ctx.UNIQUE(),
|
|
1007
|
+
columns: ctx.column_name_list_with_order().accept(this)
|
|
1008
|
+
});
|
|
1009
|
+
return index;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
// alter_table
|
|
1013
|
+
// : ALTER TABLE table_name (
|
|
1014
|
+
// SET '(' LOCK_ESCALATION '=' (AUTO | TABLE | DISABLE) ')'
|
|
1015
|
+
// | ADD column_def_table_constraints
|
|
1016
|
+
// | ALTER COLUMN (column_definition | column_modifier)
|
|
1017
|
+
// | DROP COLUMN id_ (',' id_)*
|
|
1018
|
+
// | DROP CONSTRAINT constraint = id_
|
|
1019
|
+
// | WITH (CHECK | NOCHECK) ADD (CONSTRAINT constraint = id_)? (
|
|
1020
|
+
// FOREIGN KEY '(' fk = column_name_list ')' REFERENCES table_name (
|
|
1021
|
+
// '(' pk = column_name_list ')'
|
|
1022
|
+
// )? (on_delete | on_update)*
|
|
1023
|
+
// | CHECK '(' search_condition ')'
|
|
1024
|
+
// )
|
|
1025
|
+
// | (NOCHECK | CHECK) CONSTRAINT constraint = id_
|
|
1026
|
+
// | (ENABLE | DISABLE) TRIGGER id_?
|
|
1027
|
+
// | REBUILD table_options
|
|
1028
|
+
// | SWITCH switch_partition
|
|
1029
|
+
// ) ';'?
|
|
1030
|
+
// ;
|
|
1031
|
+
}, {
|
|
1032
|
+
key: "visitAlter_table",
|
|
1033
|
+
value: function visitAlter_table(ctx) {
|
|
1034
|
+
var _this$data$refs3, _this$data$refs4, _table$fields, _table$indexes;
|
|
1035
|
+
// table_name() returns an array because there are multiple table_name in the clause (REFERENCES table_name ...)
|
|
1036
|
+
var names = ctx.table_name()[0].accept(this);
|
|
1037
|
+
var _getSchemaAndTableNam3 = getSchemaAndTableName(names),
|
|
1038
|
+
schemaName = _getSchemaAndTableNam3.schemaName,
|
|
1039
|
+
tableName = _getSchemaAndTableNam3.tableName;
|
|
1040
|
+
var table = this.data.tables.find(function (t) {
|
|
1041
|
+
return t.name === tableName && t.schemaName === schemaName;
|
|
1042
|
+
});
|
|
1043
|
+
if (!table) return; // ALTER TABLE should appear after CREATE TABLE, so skip if table is not created yet
|
|
1044
|
+
|
|
1045
|
+
var columnDefTableConstraints = ctx.column_def_table_constraints() ? ctx.column_def_table_constraints().accept(this) : [];
|
|
1046
|
+
var _splitColumnDefTableC2 = splitColumnDefTableConstraints(columnDefTableConstraints),
|
|
1047
|
+
fieldsData = _splitColumnDefTableC2.fieldsData,
|
|
1048
|
+
indexes = _splitColumnDefTableC2.indexes,
|
|
1049
|
+
tableRefs = _splitColumnDefTableC2.tableRefs,
|
|
1050
|
+
columnDefaults = _splitColumnDefTableC2.columnDefaults;
|
|
1051
|
+
var _parseFieldsAndInline2 = parseFieldsAndInlineRefsFromFieldsData(fieldsData, tableName, schemaName),
|
|
1052
|
+
inlineRefs = _parseFieldsAndInline2.inlineRefs,
|
|
1053
|
+
fields = _parseFieldsAndInline2.fields;
|
|
1054
|
+
(_this$data$refs3 = this.data.refs).push.apply(_this$data$refs3, _toConsumableArray((0, _lodash.flatten)(inlineRefs)));
|
|
1055
|
+
(_this$data$refs4 = this.data.refs).push.apply(_this$data$refs4, _toConsumableArray(tableRefs.map(function (tableRef) {
|
|
1056
|
+
tableRef.endpoints[0].tableName = tableName;
|
|
1057
|
+
tableRef.endpoints[0].schemaName = schemaName;
|
|
1058
|
+
return tableRef;
|
|
1059
|
+
})));
|
|
1060
|
+
(_table$fields = table.fields).push.apply(_table$fields, _toConsumableArray(fields));
|
|
1061
|
+
(_table$indexes = table.indexes).push.apply(_table$indexes, _toConsumableArray(indexes));
|
|
1062
|
+
columnDefaults.forEach(function (columnDefault) {
|
|
1063
|
+
var field = table.fields.find(function (f) {
|
|
1064
|
+
return f.name === columnDefault.column;
|
|
1065
|
+
});
|
|
1066
|
+
if (!field) return;
|
|
1067
|
+
field.dbdefault = columnDefault.defaultValue;
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// create_index
|
|
1072
|
+
// : CREATE UNIQUE? clustered? INDEX id_ ON table_name '(' column_name_list_with_order ')' (
|
|
1073
|
+
// INCLUDE '(' column_name_list ')'
|
|
1074
|
+
// )? (WHERE where = search_condition)? (create_index_options)? (ON id_)? ';'?
|
|
1075
|
+
// ;
|
|
1076
|
+
}, {
|
|
1077
|
+
key: "visitCreate_index",
|
|
1078
|
+
value: function visitCreate_index(ctx) {
|
|
1079
|
+
var _getSchemaAndTableNam4 = getSchemaAndTableName(ctx.table_name().accept(this)),
|
|
1080
|
+
schemaName = _getSchemaAndTableNam4.schemaName,
|
|
1081
|
+
tableName = _getSchemaAndTableNam4.tableName;
|
|
1082
|
+
var table = this.data.tables.find(function (t) {
|
|
1083
|
+
return t.name === tableName && t.schemaName === schemaName;
|
|
1084
|
+
});
|
|
1085
|
+
if (!table) return; // ALTER TABLE should appear after CREATE TABLE, so skip if table is not created yet
|
|
1086
|
+
|
|
1087
|
+
var index = new _AST.Index({
|
|
1088
|
+
name: ctx.id_()[0].accept(this),
|
|
1089
|
+
unique: !!ctx.UNIQUE(),
|
|
1090
|
+
columns: ctx.column_name_list_with_order().accept(this)
|
|
1091
|
+
});
|
|
1092
|
+
table.indexes.push(index);
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
// another_statement
|
|
1096
|
+
// : alter_queue
|
|
1097
|
+
// | checkpoint_statement
|
|
1098
|
+
// | conversation_statement
|
|
1099
|
+
// | create_contract
|
|
1100
|
+
// | create_queue
|
|
1101
|
+
// | cursor_statement
|
|
1102
|
+
// | declare_statement
|
|
1103
|
+
// | execute_statement
|
|
1104
|
+
// | kill_statement
|
|
1105
|
+
// | message_statement
|
|
1106
|
+
// | reconfigure_statement
|
|
1107
|
+
// | security_statement
|
|
1108
|
+
// | set_statement
|
|
1109
|
+
// | setuser_statement
|
|
1110
|
+
// | shutdown_statement
|
|
1111
|
+
// | transaction_statement
|
|
1112
|
+
// | use_statement
|
|
1113
|
+
// ;
|
|
1114
|
+
}, {
|
|
1115
|
+
key: "visitAnother_statement",
|
|
1116
|
+
value: function visitAnother_statement(ctx) {
|
|
1117
|
+
if (ctx.execute_statement()) {
|
|
1118
|
+
ctx.execute_statement().accept(this);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
// execute_statement
|
|
1123
|
+
// : EXECUTE execute_body ';'?
|
|
1124
|
+
// ;
|
|
1125
|
+
}, {
|
|
1126
|
+
key: "visitExecute_statement",
|
|
1127
|
+
value: function visitExecute_statement(ctx) {
|
|
1128
|
+
ctx.execute_body().accept(this);
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
// execute_body
|
|
1132
|
+
// : (return_status = LOCAL_ID '=')? (func_proc_name_server_database_schema | execute_var_string) execute_statement_arg?
|
|
1133
|
+
// | '(' execute_var_string (',' execute_var_string)* ')' (AS (LOGIN | USER) '=' STRING)? (
|
|
1134
|
+
// AT_KEYWORD linkedServer = id_
|
|
1135
|
+
// )?
|
|
1136
|
+
// | AS ( (LOGIN | USER) '=' STRING | CALLER)
|
|
1137
|
+
// ;
|
|
1138
|
+
}, {
|
|
1139
|
+
key: "visitExecute_body",
|
|
1140
|
+
value: function visitExecute_body(ctx) {
|
|
1141
|
+
var funcNames = ctx.func_proc_name_server_database_schema() ? ctx.func_proc_name_server_database_schema().accept(this) : [];
|
|
1142
|
+
var funcName = (0, _lodash.last)(funcNames);
|
|
1143
|
+
if (funcName !== ADD_DESCRIPTION_FUNCTION_NAME) {
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
if (ctx.execute_statement_arg()) {
|
|
1147
|
+
// [
|
|
1148
|
+
// { name: '@name', value: "N'Column_Description'", type: 'expression' },
|
|
1149
|
+
// { name: '@value', value: '$-1', type: 'string' },
|
|
1150
|
+
// { name: '@level0type', value: "N'Schema'", type: 'expression' },
|
|
1151
|
+
// { name: '@level0name', value: 'dbo', type: 'string' },
|
|
1152
|
+
// { name: '@level1type', value: "N'Table'", type: 'expression' },
|
|
1153
|
+
// { name: '@level1name', value: 'orders', type: 'string' },
|
|
1154
|
+
// { name: '@level2type', value: "N'Column'", type: 'expression' },
|
|
1155
|
+
// { name: '@level2name', value: 'status', type: 'string' }
|
|
1156
|
+
// ]
|
|
1157
|
+
var args = ctx.execute_statement_arg().accept(this);
|
|
1158
|
+
|
|
1159
|
+
// {
|
|
1160
|
+
// name: "N'Table_Description'",
|
|
1161
|
+
// value: 'This is a note in table "orders"',
|
|
1162
|
+
// level0type: "N'Schema'",
|
|
1163
|
+
// level0name: 'dbo',
|
|
1164
|
+
// level1type: "N'Table'",
|
|
1165
|
+
// level1name: 'orders'
|
|
1166
|
+
// }
|
|
1167
|
+
var argsObj = args.reduce(function (acc, arg) {
|
|
1168
|
+
var name = arg.name.slice(1);
|
|
1169
|
+
acc[name] = arg.value;
|
|
1170
|
+
return acc;
|
|
1171
|
+
}, {});
|
|
1172
|
+
if (!argsObj['name'].includes('Description')) {
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
// https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addextendedproperty-transact-sql?view=sql-server-ver16#----level0type
|
|
1177
|
+
var level0Type = argsObj['level0type'].toLowerCase();
|
|
1178
|
+
if (!level0Type.includes('schema')) return;
|
|
1179
|
+
var schemaName = argsObj['level0name'] !== 'dbo' ? argsObj['level0name'] : undefined;
|
|
1180
|
+
var level1Type = argsObj['level1type'].toLowerCase();
|
|
1181
|
+
var tableName = level1Type.includes('table') ? argsObj['level1name'] : null;
|
|
1182
|
+
var table = this.data.tables.find(function (t) {
|
|
1183
|
+
return t.name === tableName && t.schemaName === schemaName;
|
|
1184
|
+
});
|
|
1185
|
+
if (!table) return;
|
|
1186
|
+
if (!argsObj['level2type']) {
|
|
1187
|
+
table.note = {
|
|
1188
|
+
value: argsObj.value
|
|
1189
|
+
};
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
var level2Type = argsObj['level2type'].toLowerCase();
|
|
1193
|
+
var columnName = level2Type.includes('column') ? argsObj['level2name'] : null;
|
|
1194
|
+
var field = table.fields.find(function (f) {
|
|
1195
|
+
return f.name === columnName;
|
|
1196
|
+
});
|
|
1197
|
+
if (!field) return;
|
|
1198
|
+
field.note = {
|
|
1199
|
+
value: argsObj.value
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
// func_proc_name_server_database_schema
|
|
1205
|
+
// : server = id_? '.' database = id_? '.' schema = id_? '.' procedure = id_
|
|
1206
|
+
// | func_proc_name_database_schema
|
|
1207
|
+
// ;
|
|
1208
|
+
}, {
|
|
1209
|
+
key: "visitFunc_proc_name_server_database_schema",
|
|
1210
|
+
value: function visitFunc_proc_name_server_database_schema(ctx) {
|
|
1211
|
+
var _this17 = this;
|
|
1212
|
+
if (ctx.func_proc_name_database_schema()) {
|
|
1213
|
+
return ctx.func_proc_name_database_schema().accept(this);
|
|
1214
|
+
} else {
|
|
1215
|
+
return ctx.id_().map(function (id) {
|
|
1216
|
+
return id.accept(_this17);
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// func_proc_name_database_schema
|
|
1222
|
+
// : database = id_? '.' schema = id_? '.' procedure = id_
|
|
1223
|
+
// | func_proc_name_schema
|
|
1224
|
+
// ;
|
|
1225
|
+
}, {
|
|
1226
|
+
key: "visitFunc_proc_name_database_schema",
|
|
1227
|
+
value: function visitFunc_proc_name_database_schema(ctx) {
|
|
1228
|
+
var _this18 = this;
|
|
1229
|
+
if (ctx.func_proc_name_schema()) {
|
|
1230
|
+
return ctx.func_proc_name_schema().accept(this);
|
|
1231
|
+
} else {
|
|
1232
|
+
return ctx.id_().map(function (id) {
|
|
1233
|
+
return id.accept(_this18);
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
// func_proc_name_schema
|
|
1239
|
+
// : ((schema = id_) '.')? procedure = id_
|
|
1240
|
+
// ;
|
|
1241
|
+
}, {
|
|
1242
|
+
key: "visitFunc_proc_name_schema",
|
|
1243
|
+
value: function visitFunc_proc_name_schema(ctx) {
|
|
1244
|
+
var _this19 = this;
|
|
1245
|
+
return ctx.id_().map(function (id) {
|
|
1246
|
+
return id.accept(_this19);
|
|
1247
|
+
});
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
// execute_statement_arg
|
|
1251
|
+
// : execute_statement_arg_unnamed (',' execute_statement_arg)* //Unnamed params can continue unnamed
|
|
1252
|
+
// | execute_statement_arg_named (',' execute_statement_arg_named)* //Named can only be continued by unnamed
|
|
1253
|
+
// ;
|
|
1254
|
+
}, {
|
|
1255
|
+
key: "visitExecute_statement_arg",
|
|
1256
|
+
value: function visitExecute_statement_arg(ctx) {
|
|
1257
|
+
var _this20 = this;
|
|
1258
|
+
if (ctx.execute_statement_arg_unnamed()) {
|
|
1259
|
+
return ctx.execute_statement_arg_unnamed().map(function (item) {
|
|
1260
|
+
return item.accept(_this20);
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
return ctx.execute_statement_arg_named().map(function (item) {
|
|
1264
|
+
return item.accept(_this20);
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
// execute_statement_arg_named
|
|
1269
|
+
// : name = LOCAL_ID '=' value = execute_parameter
|
|
1270
|
+
// ;
|
|
1271
|
+
}, {
|
|
1272
|
+
key: "visitExecute_statement_arg_named",
|
|
1273
|
+
value: function visitExecute_statement_arg_named(ctx) {
|
|
1274
|
+
var _ctx$execute_paramete = ctx.execute_parameter().accept(this),
|
|
1275
|
+
value = _ctx$execute_paramete.value,
|
|
1276
|
+
type = _ctx$execute_paramete.type;
|
|
1277
|
+
return {
|
|
1278
|
+
name: ctx.LOCAL_ID().getText(),
|
|
1279
|
+
value: value,
|
|
1280
|
+
type: type
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
// execute_parameter
|
|
1285
|
+
// : (constant | LOCAL_ID (OUTPUT | OUT)? | id_ | DEFAULT | NULL_)
|
|
1286
|
+
// ;
|
|
1287
|
+
}, {
|
|
1288
|
+
key: "visitExecute_parameter",
|
|
1289
|
+
value: function visitExecute_parameter(ctx) {
|
|
1290
|
+
if (ctx.constant()) {
|
|
1291
|
+
return ctx.constant().accept(this);
|
|
1292
|
+
}
|
|
1293
|
+
return ctx.getText();
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
// constant
|
|
1297
|
+
// : STRING // string, datetime or uniqueidentifier
|
|
1298
|
+
// | BINARY
|
|
1299
|
+
// | '-'? (DECIMAL | REAL | FLOAT) // float or decimal
|
|
1300
|
+
// | '-'? dollar = '$' ('-' | '+')? (DECIMAL | FLOAT) // money
|
|
1301
|
+
// | parameter
|
|
1302
|
+
// ;
|
|
1303
|
+
}, {
|
|
1304
|
+
key: "visitConstant",
|
|
1305
|
+
value: function visitConstant(ctx) {
|
|
1306
|
+
if (ctx.STRING() || ctx.BINARY()) {
|
|
1307
|
+
var value = getStringFromRawString(ctx.getText());
|
|
1308
|
+
return {
|
|
1309
|
+
value: value,
|
|
1310
|
+
type: value.startsWith("N'") ? _constants.DATA_TYPE.EXPRESSION : _constants.DATA_TYPE.STRING
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
if (ctx.DOLLAR()) {
|
|
1314
|
+
var _value2 = ctx.getText();
|
|
1315
|
+
return {
|
|
1316
|
+
value: _value2,
|
|
1317
|
+
type: _constants.DATA_TYPE.STRING
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
if (ctx.REAL() || ctx.DECIMAL() || ctx.FLOAT()) {
|
|
1321
|
+
return {
|
|
1322
|
+
value: ctx.getText(),
|
|
1323
|
+
type: _constants.DATA_TYPE.NUMBER
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1326
|
+
return {
|
|
1327
|
+
value: (0, _helpers.getOriginalText)(ctx),
|
|
1328
|
+
type: _constants.DATA_TYPE.EXPRESSION
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
390
1331
|
}]);
|
|
391
1332
|
return MssqlASTGen;
|
|
392
1333
|
}(_TSqlParserVisitor2["default"]);
|