@dbml/core 3.14.1 → 4.0.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.
Files changed (30) hide show
  1. package/lib/export/DbmlExporter.js +38 -1
  2. package/lib/export/MysqlExporter.js +36 -1
  3. package/lib/export/OracleExporter.js +36 -1
  4. package/lib/export/PostgresExporter.js +36 -1
  5. package/lib/export/SqlServerExporter.js +36 -1
  6. package/lib/index.js +1 -1
  7. package/lib/model_structure/constraint.js +86 -0
  8. package/lib/model_structure/database.js +1 -0
  9. package/lib/model_structure/dbState.js +1 -0
  10. package/lib/model_structure/field.js +23 -1
  11. package/lib/model_structure/table.js +51 -17
  12. package/lib/model_structure/tablePartial.js +3 -0
  13. package/lib/parse/ANTLR/ASTGeneration/AST.js +18 -6
  14. package/lib/parse/ANTLR/ASTGeneration/constants.js +2 -1
  15. package/lib/parse/ANTLR/ASTGeneration/mssql/MssqlASTGen.js +53 -101
  16. package/lib/parse/ANTLR/ASTGeneration/mysql/MySQLASTGen.js +73 -19
  17. package/lib/parse/ANTLR/ASTGeneration/postgres/PostgresASTGen.js +50 -16
  18. package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.g4 +1 -1
  19. package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.interp +1 -1
  20. package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.js +2 -2
  21. package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.tokens +1314 -1314
  22. package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParserVisitor.js +1 -1
  23. package/lib/parse/schemarb/parser.pegjs +41 -0
  24. package/lib/parse/schemarbParser.js +568 -226
  25. package/package.json +3 -3
  26. package/types/model_structure/constraint.d.ts +52 -0
  27. package/types/model_structure/database.d.ts +2 -0
  28. package/types/model_structure/field.d.ts +7 -1
  29. package/types/model_structure/table.d.ts +8 -1
  30. package/types/model_structure/tablePartial.d.ts +4 -1
@@ -11,6 +11,7 @@ var _field = _interopRequireDefault(require("./field"));
11
11
  var _indexes = _interopRequireDefault(require("./indexes"));
12
12
  var _config = require("./config");
13
13
  var _utils = require("./utils");
14
+ var _constraint = _interopRequireDefault(require("./constraint"));
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
15
16
  function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
16
17
  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."); }
@@ -44,6 +45,8 @@ var Table = /*#__PURE__*/function (_Element) {
44
45
  fields = _ref$fields === void 0 ? [] : _ref$fields,
45
46
  _ref$indexes = _ref.indexes,
46
47
  indexes = _ref$indexes === void 0 ? [] : _ref$indexes,
48
+ _ref$constraints = _ref.constraints,
49
+ constraints = _ref$constraints === void 0 ? [] : _ref$constraints,
47
50
  _ref$schema = _ref.schema,
48
51
  schema = _ref$schema === void 0 ? {} : _ref$schema,
49
52
  token = _ref.token,
@@ -61,6 +64,7 @@ var Table = /*#__PURE__*/function (_Element) {
61
64
  _this.headerColor = headerColor;
62
65
  _this.fields = [];
63
66
  _this.indexes = [];
67
+ _this.constraints = [];
64
68
  _this.schema = schema;
65
69
  _this.partials = partials;
66
70
  _this.dbState = _this.schema.dbState;
@@ -71,6 +75,7 @@ var Table = /*#__PURE__*/function (_Element) {
71
75
  _this.processPartials();
72
76
  _this.checkFieldCount();
73
77
  _this.processIndexes(indexes);
78
+ _this.processConstraints(constraints);
74
79
  return _this;
75
80
  }
76
81
  _inherits(Table, _Element);
@@ -137,6 +142,21 @@ var Table = /*#__PURE__*/function (_Element) {
137
142
  }
138
143
  });
139
144
  }
145
+ }, {
146
+ key: "processConstraints",
147
+ value: function processConstraints(constraints) {
148
+ var _this5 = this;
149
+ constraints.forEach(function (constraint) {
150
+ _this5.pushConstraint(new _constraint["default"](_objectSpread(_objectSpread({}, constraint), {}, {
151
+ table: _this5
152
+ })));
153
+ });
154
+ }
155
+ }, {
156
+ key: "pushConstraint",
157
+ value: function pushConstraint(constraint) {
158
+ this.constraints.push(constraint);
159
+ }
140
160
  }, {
141
161
  key: "findField",
142
162
  value: function findField(fieldName) {
@@ -153,7 +173,7 @@ var Table = /*#__PURE__*/function (_Element) {
153
173
  key: "processPartials",
154
174
  value: function processPartials() {
155
175
  var _this$partials,
156
- _this5 = this;
176
+ _this6 = this;
157
177
  if (!((_this$partials = this.partials) !== null && _this$partials !== void 0 && _this$partials.length)) return;
158
178
  /**
159
179
  * When encountering conflicting columns or settings with identical names, the following resolution order is applied:
@@ -182,14 +202,14 @@ var Table = /*#__PURE__*/function (_Element) {
182
202
 
183
203
  // insert placeholder into table.fields
184
204
  sortedPartials.toReversed().forEach(function (partial) {
185
- _this5.fields.splice(partial.order, 0, 'dummy');
205
+ _this6.fields.splice(partial.order, 0, 'dummy');
186
206
  });
187
207
  sortedPartials.forEach(function (partial) {
188
- var tablePartial = _this5.schema.database.findTablePartial(partial.name);
189
- if (!tablePartial) _this5.error("Table partial ".concat(partial.name, " not found"), partial.token);
208
+ var tablePartial = _this6.schema.database.findTablePartial(partial.name);
209
+ if (!tablePartial) _this6.error("Table partial ".concat(partial.name, " not found"), partial.token);
190
210
  partial.id = tablePartial.id;
191
211
  if (tablePartial.fields) {
192
- var _this5$fields;
212
+ var _this6$fields;
193
213
  // ignore fields that already exist in the table, or have been added by a later partial
194
214
  var rawFields = tablePartial.fields.filter(function (f) {
195
215
  return !existingFieldNames.has(f.name);
@@ -200,12 +220,12 @@ var Table = /*#__PURE__*/function (_Element) {
200
220
  // convert inline_refs from injected fields to refs
201
221
  if (rawField.inline_refs) {
202
222
  rawField.inline_refs.forEach(function (iref) {
203
- var _this5$schema;
223
+ var _this6$schema;
204
224
  var ref = {
205
225
  token: rawField.token,
206
226
  endpoints: [{
207
- tableName: _this5.name,
208
- schemaName: (_this5$schema = _this5.schema) === null || _this5$schema === void 0 ? void 0 : _this5$schema.name,
227
+ tableName: _this6.name,
228
+ schemaName: (_this6$schema = _this6.schema) === null || _this6$schema === void 0 ? void 0 : _this6$schema.name,
209
229
  fieldNames: [rawField.name],
210
230
  relation: ['-', '<'].includes(iref.relation) ? '1' : '*',
211
231
  token: rawField.token
@@ -219,36 +239,44 @@ var Table = /*#__PURE__*/function (_Element) {
219
239
  injectedPartial: tablePartial
220
240
  };
221
241
  // The global array containing references with 1 endpoint being a field injected from a partial to a table
222
- _this5.schema.database.injectedRawRefs.push(ref);
242
+ _this6.schema.database.injectedRawRefs.push(ref);
223
243
  });
224
244
  }
225
245
  return new _field["default"](_objectSpread(_objectSpread({}, rawField), {}, {
226
246
  noteToken: null,
227
- table: _this5,
247
+ table: _this6,
228
248
  injectedPartial: tablePartial,
229
249
  injectedToken: partial.token
230
250
  }));
231
251
  });
232
- (_this5$fields = _this5.fields).splice.apply(_this5$fields, [partial.order, 1].concat(_toConsumableArray(fields)));
252
+ (_this6$fields = _this6.fields).splice.apply(_this6$fields, [partial.order, 1].concat(_toConsumableArray(fields)));
233
253
  } else {
234
- _this5.fields.splice(partial.order, 1); // still need to remove the dummy element, even when there's no field in the partial
254
+ _this6.fields.splice(partial.order, 1); // still need to remove the dummy element, even when there's no field in the partial
235
255
  }
236
256
 
237
257
  // merge settings
238
258
  if (!existingSettingNames.has('note') && !(0, _lodash.isNil)(tablePartial.note)) {
239
- _this5.noteToken = null;
240
- _this5.note = tablePartial.note;
259
+ _this6.noteToken = null;
260
+ _this6.note = tablePartial.note;
241
261
  existingSettingNames.add('note');
242
262
  }
243
263
  if (!existingSettingNames.has('headerColor') && !(0, _lodash.isNil)(tablePartial.headerColor)) {
244
- _this5.headerColor = tablePartial.headerColor;
264
+ _this6.headerColor = tablePartial.headerColor;
245
265
  existingSettingNames.add('headerColor');
246
266
  }
247
267
 
248
268
  // merge indexes
249
269
  tablePartial.indexes.forEach(function (index) {
250
- _this5.indexes.push(new _indexes["default"](_objectSpread(_objectSpread({}, index), {}, {
251
- table: _this5,
270
+ _this6.indexes.push(new _indexes["default"](_objectSpread(_objectSpread({}, index), {}, {
271
+ table: _this6,
272
+ injectedPartial: tablePartial
273
+ })));
274
+ });
275
+ tablePartial.constraints.forEach(function (constraint) {
276
+ _this6.constraints.push(new _constraint["default"](_objectSpread(_objectSpread({}, constraint), {}, {
277
+ name: constraint.name && "".concat(_this6.name, ".").concat(constraint.name),
278
+ // deduplicate constraint names when instantiated to tables
279
+ table: _this6,
252
280
  injectedPartial: tablePartial
253
281
  })));
254
282
  });
@@ -280,6 +308,9 @@ var Table = /*#__PURE__*/function (_Element) {
280
308
  }),
281
309
  indexIds: this.indexes.map(function (i) {
282
310
  return i.id;
311
+ }),
312
+ constraintIds: this.constraints.map(function (c) {
313
+ return c.id;
283
314
  })
284
315
  };
285
316
  }
@@ -314,6 +345,9 @@ var Table = /*#__PURE__*/function (_Element) {
314
345
  this.indexes.forEach(function (index) {
315
346
  return index.normalize(model);
316
347
  });
348
+ this.constraints.forEach(function (constraint) {
349
+ return constraint.normalize(model);
350
+ });
317
351
  }
318
352
  }]);
319
353
  }(_element["default"]);
@@ -33,6 +33,8 @@ var TablePartial = /*#__PURE__*/function (_Element) {
33
33
  fields = _ref$fields === void 0 ? [] : _ref$fields,
34
34
  _ref$indexes = _ref.indexes,
35
35
  indexes = _ref$indexes === void 0 ? [] : _ref$indexes,
36
+ _ref$constraints = _ref.constraints,
37
+ constraints = _ref$constraints === void 0 ? [] : _ref$constraints,
36
38
  token = _ref.token,
37
39
  headerColor = _ref.headerColor,
38
40
  _ref$noteToken = _ref.noteToken,
@@ -46,6 +48,7 @@ var TablePartial = /*#__PURE__*/function (_Element) {
46
48
  _this.headerColor = headerColor;
47
49
  _this.fields = fields;
48
50
  _this.indexes = indexes;
51
+ _this.constraints = constraints;
49
52
  _this.dbState = dbState;
50
53
  _this.generateId();
51
54
  return _this;
@@ -70,7 +70,8 @@ var Field = exports.Field = /*#__PURE__*/function () {
70
70
  * dbdefault: {value: string, type: 'string' | 'number' | 'boolean' | 'expression'},
71
71
  * unique: boolean,
72
72
  * pk: boolean,
73
- * note: {value: string}
73
+ * note: {value: string},
74
+ * constraints: {expression: string, name?: string}[]
74
75
  * }} param0
75
76
  */
76
77
  function Field(_ref2) {
@@ -81,7 +82,8 @@ var Field = exports.Field = /*#__PURE__*/function () {
81
82
  dbdefault = _ref2.dbdefault,
82
83
  unique = _ref2.unique,
83
84
  pk = _ref2.pk,
84
- note = _ref2.note;
85
+ note = _ref2.note,
86
+ constraints = _ref2.constraints;
85
87
  _classCallCheck(this, Field);
86
88
  /** @type {string} */
87
89
  this.name = name;
@@ -106,6 +108,9 @@ var Field = exports.Field = /*#__PURE__*/function () {
106
108
 
107
109
  /** @type {{value: string}} */
108
110
  this.note = note;
111
+
112
+ /** @type {{expression: string, name?: string}[]} */
113
+ this.constraints = constraints;
109
114
  }
110
115
  return _createClass(Field, [{
111
116
  key: "toJSON",
@@ -118,7 +123,8 @@ var Field = exports.Field = /*#__PURE__*/function () {
118
123
  dbdefault: this.dbdefault,
119
124
  unique: this.unique,
120
125
  pk: this.pk,
121
- note: this.note
126
+ note: this.note,
127
+ constraints: this.constraints
122
128
  };
123
129
  }
124
130
  }]);
@@ -130,7 +136,8 @@ var Table = exports.Table = /*#__PURE__*/function () {
130
136
  * schemaName: string,
131
137
  * fields: Field[],
132
138
  * indexes: Index[],
133
- * note: {value: string}
139
+ * note: {value: string},
140
+ * constraints: {expression: string, name?: string}[]
134
141
  * }} param0
135
142
  */
136
143
  function Table(_ref3) {
@@ -138,7 +145,8 @@ var Table = exports.Table = /*#__PURE__*/function () {
138
145
  schemaName = _ref3.schemaName,
139
146
  fields = _ref3.fields,
140
147
  indexes = _ref3.indexes,
141
- note = _ref3.note;
148
+ note = _ref3.note,
149
+ constraints = _ref3.constraints;
142
150
  _classCallCheck(this, Table);
143
151
  /** @type {string} */
144
152
  this.name = name;
@@ -154,6 +162,9 @@ var Table = exports.Table = /*#__PURE__*/function () {
154
162
 
155
163
  /** @type {{value: string}} */
156
164
  this.note = note;
165
+
166
+ /** @type {{expression: string, name?: string}[]} */
167
+ this.constraints = constraints || [];
157
168
  }
158
169
  return _createClass(Table, [{
159
170
  key: "toJSON",
@@ -168,7 +179,8 @@ var Table = exports.Table = /*#__PURE__*/function () {
168
179
  indexes: (_this$indexes = this.indexes) === null || _this$indexes === void 0 ? void 0 : _this$indexes.map(function (i) {
169
180
  return i.toJSON();
170
181
  }),
171
- note: this.note
182
+ note: this.note,
183
+ constraints: this.constraints
172
184
  };
173
185
  }
174
186
  }]);
@@ -10,7 +10,8 @@ var TABLE_CONSTRAINT_KIND = exports.TABLE_CONSTRAINT_KIND = {
10
10
  FK: 'fk',
11
11
  UNIQUE: 'unique',
12
12
  PK: 'pk',
13
- DEFAULT: 'default'
13
+ DEFAULT: 'default',
14
+ CHECK: 'check'
14
15
  };
15
16
  var COLUMN_CONSTRAINT_KIND = exports.COLUMN_CONSTRAINT_KIND = {
16
17
  NOT_NULL: 'not_null',
@@ -11,6 +11,13 @@ var _constants = require("../constants");
11
11
  var _helpers = require("../helpers");
12
12
  var _AST = require("../AST");
13
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
14
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
15
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
16
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
17
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
18
+ 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."); }
19
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
20
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
14
21
  function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
15
22
  function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
16
23
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
@@ -23,10 +30,6 @@ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.
23
30
  function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
24
31
  function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
25
32
  function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
26
- function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
27
- 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."); }
28
- function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
29
- function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
30
33
  function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
31
34
  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."); }
32
35
  function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
@@ -34,10 +37,6 @@ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length)
34
37
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
35
38
  function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } /* eslint-disable class-methods-use-this */
36
39
  var ADD_DESCRIPTION_FUNCTION_NAME = 'sp_addextendedproperty';
37
- var CHECK_CONSTRAINT_CONDITION_TYPE = {
38
- RAW: 'raw',
39
- ENUM: 'enum'
40
- };
41
40
  var getSchemaAndTableName = function getSchemaAndTableName(names) {
42
41
  var tableName = (0, _lodash.last)(names);
43
42
  var schemaName = names.length > 1 ? (0, _lodash.nth)(names, -2) : undefined;
@@ -104,7 +103,6 @@ var splitColumnDefTableConstraints = function splitColumnDefTableConstraints(col
104
103
  };
105
104
  var parseFieldsAndInlineRefsFromFieldsData = function parseFieldsAndInlineRefsFromFieldsData(fieldsData, tableName, schemaName) {
106
105
  var _fieldsData$reduce = fieldsData.reduce(function (acc, fieldData) {
107
- var _acc$;
108
106
  var inlineRefs = fieldData.inline_refs.map(function (inlineRef) {
109
107
  inlineRef.endpoints[0].tableName = tableName;
110
108
  inlineRef.endpoints[0].schemaName = schemaName;
@@ -113,17 +111,14 @@ var parseFieldsAndInlineRefsFromFieldsData = function parseFieldsAndInlineRefsFr
113
111
  });
114
112
  acc[0].push(inlineRefs);
115
113
  acc[1].push(fieldData.field);
116
- (_acc$ = acc[2]).push.apply(_acc$, _toConsumableArray(fieldData.checkConstraints));
117
114
  return acc;
118
115
  }, [[], [], []]),
119
- _fieldsData$reduce2 = _slicedToArray(_fieldsData$reduce, 3),
116
+ _fieldsData$reduce2 = _slicedToArray(_fieldsData$reduce, 2),
120
117
  resInlineRefs = _fieldsData$reduce2[0],
121
- fields = _fieldsData$reduce2[1],
122
- checkConstraints = _fieldsData$reduce2[2];
118
+ fields = _fieldsData$reduce2[1];
123
119
  return {
124
120
  inlineRefs: resInlineRefs,
125
- fields: fields,
126
- checkConstraints: checkConstraints
121
+ fields: fields
127
122
  };
128
123
  };
129
124
  var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor) {
@@ -602,41 +597,19 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
602
597
  tableCheckConstraints = _splitColumnDefTableC.checkConstraints;
603
598
  var _parseFieldsAndInline = parseFieldsAndInlineRefsFromFieldsData(fieldsData, tableName, schemaName),
604
599
  inlineRefs = _parseFieldsAndInline.inlineRefs,
605
- fields = _parseFieldsAndInline.fields,
606
- columnCheckConstraints = _parseFieldsAndInline.checkConstraints;
600
+ fields = _parseFieldsAndInline.fields;
607
601
  (_this$data$refs = this.data.refs).push.apply(_this$data$refs, _toConsumableArray((0, _lodash.flatten)(inlineRefs)));
608
602
  (_this$data$refs2 = this.data.refs).push.apply(_this$data$refs2, _toConsumableArray(tableRefs.map(function (tableRef) {
609
603
  tableRef.endpoints[0].tableName = tableName;
610
604
  tableRef.endpoints[0].schemaName = schemaName;
611
605
  return tableRef;
612
606
  })));
613
-
614
- // these check constraints represent enums
615
- var checkConstraints = columnCheckConstraints.concat(tableCheckConstraints);
616
- checkConstraints.forEach(function (checkConstraint) {
617
- var field = fields.find(function (fieldItem) {
618
- return fieldItem.name === checkConstraint.column;
619
- });
620
- if (!field) return;
621
- var enumObject = new _AST.Enum({
622
- name: "".concat(tableName, "_").concat(field.name, "_enum"),
623
- values: checkConstraint.columnValues.map(function (value) {
624
- return {
625
- name: value
626
- };
627
- }),
628
- schemaName: schemaName
629
- });
630
- _this8.data.enums.push(enumObject);
631
- // TODO: handle multiple enums for the same field
632
- field.type.type_name = enumObject.name;
633
- field.type.schemaName = enumObject.schemaName;
634
- });
635
607
  var table = new _AST.Table({
636
608
  name: tableName,
637
609
  schemaName: schemaName,
638
610
  fields: fields,
639
- indexes: tableIndices.concat(indexes)
611
+ indexes: tableIndices.concat(indexes),
612
+ constraints: tableCheckConstraints
640
613
  });
641
614
  this.data.tables.push(table);
642
615
  }
@@ -716,8 +689,7 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
716
689
  kind: _constants.TABLE_CONSTRAINT_KIND.FIELD,
717
690
  value: {
718
691
  field: field,
719
- inline_refs: [],
720
- checkConstraints: []
692
+ inline_refs: []
721
693
  }
722
694
  };
723
695
  var columnDefinitions = ctx.column_definition_element().map(function (columnDef) {
@@ -754,12 +726,18 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
754
726
  case _constants.COLUMN_CONSTRAINT_KIND.CHECK:
755
727
  {
756
728
  var _columnDef$value = columnDef.value,
757
- columnDefType = _columnDef$value.type,
758
- value = _columnDef$value.value;
759
-
760
- // we keep the current behavior: when a field has a check constraints cannot be converted to enum, we will ignore it
761
- if (columnDefType !== CHECK_CONSTRAINT_CONDITION_TYPE.ENUM) return;
762
- definition.value.checkConstraints.push(value);
729
+ _expression = _columnDef$value.expression,
730
+ name = _columnDef$value.name;
731
+ if (!field.constraints) {
732
+ field.constraints = [];
733
+ }
734
+ var constraintObj = {
735
+ expression: _expression
736
+ };
737
+ if (name) {
738
+ constraintObj.name = name;
739
+ }
740
+ field.constraints.push(constraintObj);
763
741
  break;
764
742
  }
765
743
  default:
@@ -868,12 +846,14 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
868
846
  }
869
847
  };
870
848
  }
871
-
872
- // we do not handle check constraint since it is complicated and hard to extract enum from it
873
849
  if (ctx.check_constraint()) {
850
+ var constraintName = ctx.id_() ? ctx.id_().accept(this) : null;
851
+ var checkConstraintResult = ctx.check_constraint().accept(this);
874
852
  return {
875
853
  kind: _constants.COLUMN_CONSTRAINT_KIND.CHECK,
876
- value: ctx.check_constraint().accept(this)
854
+ value: _objectSpread(_objectSpread({}, checkConstraintResult), {}, {
855
+ name: constraintName
856
+ })
877
857
  };
878
858
  }
879
859
  return null;
@@ -896,24 +876,8 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
896
876
  }, {
897
877
  key: "visitSearch_condition",
898
878
  value: function visitSearch_condition(ctx) {
899
- // we will parse the enum from the most basic condition - map to the old behavior
900
- // others, we will get the check constraint to ensure the constraint is applied - enhance the old behavior
901
- if (!ctx.predicate() || ctx.NOT().length) {
902
- return {
903
- type: CHECK_CONSTRAINT_CONDITION_TYPE.RAW,
904
- value: (0, _helpers.getOriginalText)(ctx)
905
- };
906
- }
907
- var predicate = ctx.predicate().accept(this);
908
- if (!predicate) {
909
- return {
910
- type: CHECK_CONSTRAINT_CONDITION_TYPE.RAW,
911
- value: (0, _helpers.getOriginalText)(ctx)
912
- };
913
- }
914
879
  return {
915
- type: CHECK_CONSTRAINT_CONDITION_TYPE.ENUM,
916
- value: predicate
880
+ expression: (0, _helpers.getOriginalText)(ctx)
917
881
  };
918
882
  }
919
883
 
@@ -1113,10 +1077,12 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
1113
1077
  }
1114
1078
  if (ctx.check_constraint()) {
1115
1079
  var checkConstraint = ctx.check_constraint().accept(this);
1116
- if (checkConstraint.type !== CHECK_CONSTRAINT_CONDITION_TYPE.ENUM) return null;
1117
1080
  return {
1118
1081
  kind: _constants.TABLE_CONSTRAINT_KIND.CHECK,
1119
- value: checkConstraint.value
1082
+ value: {
1083
+ expression: checkConstraint.expression,
1084
+ name: name
1085
+ }
1120
1086
  };
1121
1087
  }
1122
1088
  return null;
@@ -1180,11 +1146,7 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
1180
1146
  }, {
1181
1147
  key: "visitAlter_table",
1182
1148
  value: function visitAlter_table(ctx) {
1183
- var _this$data$refs3,
1184
- _this$data$refs4,
1185
- _table$fields,
1186
- _table$indexes,
1187
- _this16 = this;
1149
+ var _this$data$refs3, _this$data$refs4, _table$fields, _table$indexes;
1188
1150
  // table_name() returns an array because there are multiple table_name in the clause (REFERENCES table_name ...)
1189
1151
  var names = ctx.table_name()[0].accept(this);
1190
1152
  var _getSchemaAndTableNam3 = getSchemaAndTableName(names),
@@ -1221,23 +1183,13 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
1221
1183
  field.dbdefault = columnDefault.defaultValue;
1222
1184
  });
1223
1185
  checkConstraints.forEach(function (checkConstraint) {
1224
- var field = table.fields.find(function (fieldItem) {
1225
- return fieldItem.name === checkConstraint.column;
1226
- });
1227
- if (!field) return;
1228
- var enumObject = new _AST.Enum({
1229
- name: "".concat(tableName, "_").concat(field.name, "_enum"),
1230
- values: checkConstraint.columnValues.map(function (value) {
1231
- return {
1232
- name: value
1233
- };
1234
- }),
1235
- schemaName: schemaName
1236
- });
1237
- _this16.data.enums.push(enumObject);
1238
- // TODO: handle multiple enums for the same field
1239
- field.type.type_name = enumObject.name;
1240
- field.type.schemaName = enumObject.schemaName;
1186
+ var constraintObj = {
1187
+ expression: checkConstraint.expression
1188
+ };
1189
+ if (checkConstraint.name) {
1190
+ constraintObj.name = checkConstraint.name;
1191
+ }
1192
+ table.constraints.push(constraintObj);
1241
1193
  });
1242
1194
  }
1243
1195
 
@@ -1381,12 +1333,12 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
1381
1333
  }, {
1382
1334
  key: "visitFunc_proc_name_server_database_schema",
1383
1335
  value: function visitFunc_proc_name_server_database_schema(ctx) {
1384
- var _this17 = this;
1336
+ var _this16 = this;
1385
1337
  if (ctx.func_proc_name_database_schema()) {
1386
1338
  return ctx.func_proc_name_database_schema().accept(this);
1387
1339
  }
1388
1340
  return ctx.id_().map(function (id) {
1389
- return id.accept(_this17);
1341
+ return id.accept(_this16);
1390
1342
  });
1391
1343
  }
1392
1344
 
@@ -1397,12 +1349,12 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
1397
1349
  }, {
1398
1350
  key: "visitFunc_proc_name_database_schema",
1399
1351
  value: function visitFunc_proc_name_database_schema(ctx) {
1400
- var _this18 = this;
1352
+ var _this17 = this;
1401
1353
  if (ctx.func_proc_name_schema()) {
1402
1354
  return ctx.func_proc_name_schema().accept(this);
1403
1355
  }
1404
1356
  return ctx.id_().map(function (id) {
1405
- return id.accept(_this18);
1357
+ return id.accept(_this17);
1406
1358
  });
1407
1359
  }
1408
1360
 
@@ -1412,9 +1364,9 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
1412
1364
  }, {
1413
1365
  key: "visitFunc_proc_name_schema",
1414
1366
  value: function visitFunc_proc_name_schema(ctx) {
1415
- var _this19 = this;
1367
+ var _this18 = this;
1416
1368
  return ctx.id_().map(function (id) {
1417
- return id.accept(_this19);
1369
+ return id.accept(_this18);
1418
1370
  });
1419
1371
  }
1420
1372
 
@@ -1425,14 +1377,14 @@ var MssqlASTGen = exports["default"] = /*#__PURE__*/function (_TSqlParserVisitor
1425
1377
  }, {
1426
1378
  key: "visitExecute_statement_arg",
1427
1379
  value: function visitExecute_statement_arg(ctx) {
1428
- var _this20 = this;
1380
+ var _this19 = this;
1429
1381
  if (ctx.execute_statement_arg_unnamed()) {
1430
1382
  return ctx.execute_statement_arg_unnamed().map(function (item) {
1431
- return item.accept(_this20);
1383
+ return item.accept(_this19);
1432
1384
  });
1433
1385
  }
1434
1386
  return ctx.execute_statement_arg_named().map(function (item) {
1435
- return item.accept(_this20);
1387
+ return item.accept(_this19);
1436
1388
  });
1437
1389
  }
1438
1390