@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.
- package/lib/export/DbmlExporter.js +38 -1
- package/lib/export/MysqlExporter.js +36 -1
- package/lib/export/OracleExporter.js +36 -1
- package/lib/export/PostgresExporter.js +36 -1
- package/lib/export/SqlServerExporter.js +36 -1
- package/lib/index.js +1 -1
- package/lib/model_structure/constraint.js +86 -0
- package/lib/model_structure/database.js +1 -0
- package/lib/model_structure/dbState.js +1 -0
- package/lib/model_structure/field.js +23 -1
- package/lib/model_structure/table.js +51 -17
- package/lib/model_structure/tablePartial.js +3 -0
- package/lib/parse/ANTLR/ASTGeneration/AST.js +18 -6
- package/lib/parse/ANTLR/ASTGeneration/constants.js +2 -1
- package/lib/parse/ANTLR/ASTGeneration/mssql/MssqlASTGen.js +53 -101
- package/lib/parse/ANTLR/ASTGeneration/mysql/MySQLASTGen.js +73 -19
- package/lib/parse/ANTLR/ASTGeneration/postgres/PostgresASTGen.js +50 -16
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.g4 +1 -1
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.interp +1 -1
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.js +2 -2
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParser.tokens +1314 -1314
- package/lib/parse/ANTLR/parsers/postgresql/PostgreSQLParserVisitor.js +1 -1
- package/lib/parse/schemarb/parser.pegjs +41 -0
- package/lib/parse/schemarbParser.js +568 -226
- package/package.json +3 -3
- package/types/model_structure/constraint.d.ts +52 -0
- package/types/model_structure/database.d.ts +2 -0
- package/types/model_structure/field.d.ts +7 -1
- package/types/model_structure/table.d.ts +8 -1
- package/types/model_structure/tablePartial.d.ts +4 -1
|
@@ -9,6 +9,7 @@ var _lodash = require("lodash");
|
|
|
9
9
|
var _MySqlParserVisitor = _interopRequireDefault(require("../../parsers/mysql/MySqlParserVisitor"));
|
|
10
10
|
var _AST = require("../AST");
|
|
11
11
|
var _constants = require("../constants");
|
|
12
|
+
var _helpers = require("../helpers");
|
|
12
13
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
13
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; }
|
|
14
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; }
|
|
@@ -40,7 +41,8 @@ var TABLE_OPTIONS_KIND = {
|
|
|
40
41
|
};
|
|
41
42
|
var ALTER_KIND = {
|
|
42
43
|
ADD_PK: 'add_pk',
|
|
43
|
-
ADD_FK: 'add_fk'
|
|
44
|
+
ADD_FK: 'add_fk',
|
|
45
|
+
ADD_CHECK: 'add_check'
|
|
44
46
|
};
|
|
45
47
|
var INDEX_OPTION_KIND = {
|
|
46
48
|
TYPE: 'type'
|
|
@@ -143,18 +145,19 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
143
145
|
definitions = createTableResult.definitions,
|
|
144
146
|
options = createTableResult.options;
|
|
145
147
|
var _definitions$reduce = definitions.reduce(function (acc, ele) {
|
|
146
|
-
if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.FIELD) acc[0].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.INDEX) acc[1].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) {
|
|
148
|
+
if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.FIELD) acc[0].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.INDEX) acc[1].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.CHECK) acc[4].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.PK) {
|
|
147
149
|
/** @type {Index} */
|
|
148
150
|
var index = ele.value;
|
|
149
151
|
if (index.columns.length > 1) acc[1].push(ele.value);else acc[3] = index;
|
|
150
152
|
}
|
|
151
153
|
return acc;
|
|
152
|
-
}, [[], [], [], null]),
|
|
153
|
-
_definitions$reduce2 = _slicedToArray(_definitions$reduce,
|
|
154
|
+
}, [[], [], [], null, []]),
|
|
155
|
+
_definitions$reduce2 = _slicedToArray(_definitions$reduce, 5),
|
|
154
156
|
fieldsData = _definitions$reduce2[0],
|
|
155
157
|
indexes = _definitions$reduce2[1],
|
|
156
158
|
tableRefs = _definitions$reduce2[2],
|
|
157
|
-
singlePkIndex = _definitions$reduce2[3]
|
|
159
|
+
singlePkIndex = _definitions$reduce2[3],
|
|
160
|
+
tableConstraints = _definitions$reduce2[4];
|
|
158
161
|
var inlineRefsOfFields = fieldsData.map(function (fieldData) {
|
|
159
162
|
var _field$type$type_name;
|
|
160
163
|
var field = fieldData.field,
|
|
@@ -198,7 +201,8 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
198
201
|
fields: fieldsData.map(function (fd) {
|
|
199
202
|
return fd.field;
|
|
200
203
|
}),
|
|
201
|
-
indexes: indexes
|
|
204
|
+
indexes: indexes,
|
|
205
|
+
constraints: tableConstraints
|
|
202
206
|
}, tableOptions));
|
|
203
207
|
if (singlePkIndex) {
|
|
204
208
|
var field = table.fields.find(function (f) {
|
|
@@ -408,7 +412,8 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
408
412
|
var _this9 = this;
|
|
409
413
|
var type = ctx.dataType().accept(this);
|
|
410
414
|
var constraints = {
|
|
411
|
-
inlineRefs: []
|
|
415
|
+
inlineRefs: [],
|
|
416
|
+
constraints: []
|
|
412
417
|
};
|
|
413
418
|
ctx.columnConstraint().forEach(function (c) {
|
|
414
419
|
var constraint = c.accept(_this9);
|
|
@@ -417,6 +422,10 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
417
422
|
constraints.inlineRefs.push(constraint.value);
|
|
418
423
|
return;
|
|
419
424
|
}
|
|
425
|
+
if (constraint.kind === _constants.COLUMN_CONSTRAINT_KIND.CHECK) {
|
|
426
|
+
constraints.constraints.push(constraint.value);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
420
429
|
constraints[constraint.kind] = constraint.value;
|
|
421
430
|
});
|
|
422
431
|
return {
|
|
@@ -664,6 +673,22 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
664
673
|
};
|
|
665
674
|
}
|
|
666
675
|
|
|
676
|
+
// checkColumnConstraint: (CONSTRAINT name = uid?)? CHECK '(' expression ')'
|
|
677
|
+
}, {
|
|
678
|
+
key: "visitCheckColumnConstraint",
|
|
679
|
+
value: function visitCheckColumnConstraint(ctx) {
|
|
680
|
+
var _ctx$name;
|
|
681
|
+
var name = (_ctx$name = ctx.name) === null || _ctx$name === void 0 ? void 0 : _ctx$name.accept(this);
|
|
682
|
+
var expression = (0, _helpers.getOriginalText)(ctx.expression());
|
|
683
|
+
return {
|
|
684
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.CHECK,
|
|
685
|
+
value: {
|
|
686
|
+
expression: expression,
|
|
687
|
+
name: name
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
|
|
667
692
|
// NULL_LITERAL | CAST '(' expression AS convertedDataType ')' | unaryOperator? constant
|
|
668
693
|
// | currentTimestamp (ON UPDATE currentTimestamp)? | '(' expression ')' | '(' fullId ')'
|
|
669
694
|
}, {
|
|
@@ -863,12 +888,12 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
863
888
|
}, {
|
|
864
889
|
key: "visitPrimaryKeyTableConstraint",
|
|
865
890
|
value: function visitPrimaryKeyTableConstraint(ctx) {
|
|
866
|
-
var _ctx$
|
|
891
|
+
var _ctx$name2,
|
|
867
892
|
_ctx$index,
|
|
868
893
|
_ctx$indexType,
|
|
869
894
|
_ctx$indexOption,
|
|
870
895
|
_this1 = this;
|
|
871
|
-
var name = ((_ctx$
|
|
896
|
+
var name = ((_ctx$name2 = ctx.name) === null || _ctx$name2 === void 0 ? void 0 : _ctx$name2.accept(this)) || ((_ctx$index = ctx.index) === null || _ctx$index === void 0 ? void 0 : _ctx$index.accept(this));
|
|
872
897
|
var type = (_ctx$indexType = ctx.indexType()) === null || _ctx$indexType === void 0 ? void 0 : _ctx$indexType.accept(this);
|
|
873
898
|
if ((_ctx$indexOption = ctx.indexOption()) !== null && _ctx$indexOption !== void 0 && _ctx$indexOption.length) {
|
|
874
899
|
var indexOptions = ctx.indexOption().map(function (io) {
|
|
@@ -893,12 +918,12 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
893
918
|
}, {
|
|
894
919
|
key: "visitUniqueKeyTableConstraint",
|
|
895
920
|
value: function visitUniqueKeyTableConstraint(ctx) {
|
|
896
|
-
var _ctx$
|
|
921
|
+
var _ctx$name3,
|
|
897
922
|
_ctx$index2,
|
|
898
923
|
_ctx$indexType2,
|
|
899
924
|
_ctx$indexOption2,
|
|
900
925
|
_this10 = this;
|
|
901
|
-
var name = ((_ctx$
|
|
926
|
+
var name = ((_ctx$name3 = ctx.name) === null || _ctx$name3 === void 0 ? void 0 : _ctx$name3.accept(this)) || ((_ctx$index2 = ctx.index) === null || _ctx$index2 === void 0 ? void 0 : _ctx$index2.accept(this));
|
|
902
927
|
var type = (_ctx$indexType2 = ctx.indexType()) === null || _ctx$indexType2 === void 0 ? void 0 : _ctx$indexType2.accept(this);
|
|
903
928
|
if ((_ctx$indexOption2 = ctx.indexOption()) !== null && _ctx$indexOption2 !== void 0 && _ctx$indexOption2.length) {
|
|
904
929
|
var indexOptions = ctx.indexOption().map(function (io) {
|
|
@@ -923,9 +948,9 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
923
948
|
}, {
|
|
924
949
|
key: "visitForeignKeyTableConstraint",
|
|
925
950
|
value: function visitForeignKeyTableConstraint(ctx) {
|
|
926
|
-
var _ctx$
|
|
951
|
+
var _ctx$name4, _ctx$index3;
|
|
927
952
|
var ref = ctx.referenceDefinition().accept(this);
|
|
928
|
-
ref.name = ((_ctx$
|
|
953
|
+
ref.name = ((_ctx$name4 = ctx.name) === null || _ctx$name4 === void 0 ? void 0 : _ctx$name4.accept(this)) || ((_ctx$index3 = ctx.index) === null || _ctx$index3 === void 0 ? void 0 : _ctx$index3.accept(this));
|
|
929
954
|
ref.endpoints[0].fieldNames = ctx.indexColumnNames().accept(this).map(function (icn) {
|
|
930
955
|
return icn.value;
|
|
931
956
|
});
|
|
@@ -934,10 +959,21 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
934
959
|
value: ref
|
|
935
960
|
};
|
|
936
961
|
}
|
|
962
|
+
|
|
963
|
+
// checkTableConstraint: (CONSTRAINT name = uid?)? CHECK '(' expression ')'
|
|
937
964
|
}, {
|
|
938
965
|
key: "visitCheckTableConstraint",
|
|
939
|
-
value: function visitCheckTableConstraint() {
|
|
940
|
-
|
|
966
|
+
value: function visitCheckTableConstraint(ctx) {
|
|
967
|
+
var _ctx$name5;
|
|
968
|
+
var name = (_ctx$name5 = ctx.name) === null || _ctx$name5 === void 0 ? void 0 : _ctx$name5.accept(this);
|
|
969
|
+
var expression = (0, _helpers.getOriginalText)(ctx.expression());
|
|
970
|
+
return {
|
|
971
|
+
kind: _constants.TABLE_CONSTRAINT_KIND.CHECK,
|
|
972
|
+
value: {
|
|
973
|
+
expression: expression,
|
|
974
|
+
name: name
|
|
975
|
+
}
|
|
976
|
+
};
|
|
941
977
|
}
|
|
942
978
|
|
|
943
979
|
// USING (BTREE | HASH)
|
|
@@ -1054,6 +1090,8 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
1054
1090
|
ref.endpoints[0].schemaName = schemaName;
|
|
1055
1091
|
ref.endpoints[0].tableName = tableName;
|
|
1056
1092
|
_this13.data.refs.push(ref);
|
|
1093
|
+
} else if (alter.kind === ALTER_KIND.ADD_CHECK) {
|
|
1094
|
+
table.constraints.push(alter.value);
|
|
1057
1095
|
}
|
|
1058
1096
|
return null;
|
|
1059
1097
|
});
|
|
@@ -1067,12 +1105,12 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
1067
1105
|
}, {
|
|
1068
1106
|
key: "visitAlterByAddPrimaryKey",
|
|
1069
1107
|
value: function visitAlterByAddPrimaryKey(ctx) {
|
|
1070
|
-
var _ctx$
|
|
1108
|
+
var _ctx$name6,
|
|
1071
1109
|
_ctx$index4,
|
|
1072
1110
|
_ctx$indexType4,
|
|
1073
1111
|
_ctx$indexOption5,
|
|
1074
1112
|
_this14 = this;
|
|
1075
|
-
var name = ((_ctx$
|
|
1113
|
+
var name = ((_ctx$name6 = ctx.name) === null || _ctx$name6 === void 0 ? void 0 : _ctx$name6.accept(this)) || ((_ctx$index4 = ctx.index) === null || _ctx$index4 === void 0 ? void 0 : _ctx$index4.accept(this));
|
|
1076
1114
|
var type = (_ctx$indexType4 = ctx.indexType()) === null || _ctx$indexType4 === void 0 ? void 0 : _ctx$indexType4.accept(this);
|
|
1077
1115
|
if ((_ctx$indexOption5 = ctx.indexOption()) !== null && _ctx$indexOption5 !== void 0 && _ctx$indexOption5.length) {
|
|
1078
1116
|
var indexOptions = ctx.indexOption().map(function (io) {
|
|
@@ -1099,9 +1137,9 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
1099
1137
|
}, {
|
|
1100
1138
|
key: "visitAlterByAddForeignKey",
|
|
1101
1139
|
value: function visitAlterByAddForeignKey(ctx) {
|
|
1102
|
-
var _ctx$
|
|
1140
|
+
var _ctx$name7, _ctx$index5;
|
|
1103
1141
|
var ref = ctx.referenceDefinition().accept(this);
|
|
1104
|
-
ref.name = ((_ctx$
|
|
1142
|
+
ref.name = ((_ctx$name7 = ctx.name) === null || _ctx$name7 === void 0 ? void 0 : _ctx$name7.accept(this)) || ((_ctx$index5 = ctx.index) === null || _ctx$index5 === void 0 ? void 0 : _ctx$index5.accept(this));
|
|
1105
1143
|
ref.endpoints[0].fieldNames = ctx.indexColumnNames().accept(this).map(function (icn) {
|
|
1106
1144
|
return icn.value;
|
|
1107
1145
|
});
|
|
@@ -1111,6 +1149,22 @@ var MySQLASTGen = exports["default"] = /*#__PURE__*/function (_MySQLParserVisito
|
|
|
1111
1149
|
};
|
|
1112
1150
|
}
|
|
1113
1151
|
|
|
1152
|
+
// ADD (CONSTRAINT name = uid?)? CHECK '(' expression ')'
|
|
1153
|
+
}, {
|
|
1154
|
+
key: "visitAlterByAddCheckTableConstraint",
|
|
1155
|
+
value: function visitAlterByAddCheckTableConstraint(ctx) {
|
|
1156
|
+
var _ctx$name8;
|
|
1157
|
+
var name = (_ctx$name8 = ctx.name) === null || _ctx$name8 === void 0 ? void 0 : _ctx$name8.accept(this);
|
|
1158
|
+
var expression = (0, _helpers.getOriginalText)(ctx.expression());
|
|
1159
|
+
return {
|
|
1160
|
+
kind: ALTER_KIND.ADD_CHECK,
|
|
1161
|
+
value: {
|
|
1162
|
+
expression: expression,
|
|
1163
|
+
name: name
|
|
1164
|
+
}
|
|
1165
|
+
};
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1114
1168
|
// CREATE intimeAction = (ONLINE | OFFLINE)? indexCategory = (UNIQUE | FULLTEXT | SPATIAL)? INDEX uid indexType? ON tableName indexColumnNames
|
|
1115
1169
|
// indexOption* (ALGORITHM EQUAL_SYMBOL? algType = (DEFAULT | INPLACE | COPY) | LOCK EQUAL_SYMBOL? lockType = (DEFAULT | NONE | SHARED | EXCLUSIVE))*
|
|
1116
1170
|
}, {
|
|
@@ -9,6 +9,7 @@ var _lodash = require("lodash");
|
|
|
9
9
|
var _PostgreSQLParserVisitor = _interopRequireDefault(require("../../parsers/postgresql/PostgreSQLParserVisitor"));
|
|
10
10
|
var _AST = require("../AST");
|
|
11
11
|
var _constants = require("../constants");
|
|
12
|
+
var _helpers = require("../helpers");
|
|
12
13
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
13
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; }
|
|
14
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; }
|
|
@@ -157,13 +158,14 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
157
158
|
return e;
|
|
158
159
|
});
|
|
159
160
|
var _tableElements$reduce = tableElements.reduce(function (acc, ele) {
|
|
160
|
-
if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.FIELD) acc[0].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.INDEX) 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.UNIQUE) acc[1].push(ele.value);
|
|
161
|
+
if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.FIELD) acc[0].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.INDEX) 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.UNIQUE) acc[1].push(ele.value);else if (ele.kind === _constants.TABLE_CONSTRAINT_KIND.CHECK) acc[3].push(ele.value);
|
|
161
162
|
return acc;
|
|
162
|
-
}, [[], [], []]),
|
|
163
|
-
_tableElements$reduce2 = _slicedToArray(_tableElements$reduce,
|
|
163
|
+
}, [[], [], [], []]),
|
|
164
|
+
_tableElements$reduce2 = _slicedToArray(_tableElements$reduce, 4),
|
|
164
165
|
fieldsData = _tableElements$reduce2[0],
|
|
165
166
|
indexes = _tableElements$reduce2[1],
|
|
166
|
-
tableRefs = _tableElements$reduce2[2]
|
|
167
|
+
tableRefs = _tableElements$reduce2[2],
|
|
168
|
+
tableConstraints = _tableElements$reduce2[3];
|
|
167
169
|
(_this$data$refs = this.data.refs).push.apply(_this$data$refs, _toConsumableArray((0, _lodash.flatten)(fieldsData.map(function (fieldData) {
|
|
168
170
|
return fieldData.inline_refs.map(function (inlineRef) {
|
|
169
171
|
inlineRef.endpoints[0].tableName = tableName;
|
|
@@ -183,7 +185,8 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
183
185
|
fields: fieldsData.map(function (fd) {
|
|
184
186
|
return fd.field;
|
|
185
187
|
}),
|
|
186
|
-
indexes: indexes
|
|
188
|
+
indexes: indexes,
|
|
189
|
+
constraints: tableConstraints
|
|
187
190
|
});
|
|
188
191
|
}
|
|
189
192
|
|
|
@@ -288,6 +291,15 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
288
291
|
})
|
|
289
292
|
};
|
|
290
293
|
}
|
|
294
|
+
if (ctx.CHECK()) {
|
|
295
|
+
var expression = (0, _helpers.getOriginalText)(ctx.a_expr());
|
|
296
|
+
return {
|
|
297
|
+
kind: _constants.TABLE_CONSTRAINT_KIND.CHECK,
|
|
298
|
+
value: {
|
|
299
|
+
expression: expression
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
}
|
|
291
303
|
}
|
|
292
304
|
|
|
293
305
|
// OPEN_PAREN columnlist CLOSE_PAREN |
|
|
@@ -324,18 +336,18 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
324
336
|
value: function visitColumnDef(ctx) {
|
|
325
337
|
var name = ctx.colid().accept(this);
|
|
326
338
|
var type = ctx.typename().accept(this);
|
|
327
|
-
var
|
|
339
|
+
var constraints = ctx.colquallist().accept(this);
|
|
328
340
|
var serialIncrementType = new Set(['serial', 'smallserial', 'bigserial']);
|
|
329
341
|
var columnTypeName = type.type_name.toLowerCase();
|
|
330
|
-
if (serialIncrementType.has(columnTypeName))
|
|
342
|
+
if (serialIncrementType.has(columnTypeName)) constraints.increment = true;
|
|
331
343
|
return {
|
|
332
344
|
kind: _constants.TABLE_CONSTRAINT_KIND.FIELD,
|
|
333
345
|
value: {
|
|
334
346
|
field: new _AST.Field(_objectSpread({
|
|
335
347
|
name: name,
|
|
336
348
|
type: type
|
|
337
|
-
},
|
|
338
|
-
inline_refs:
|
|
349
|
+
}, constraints)),
|
|
350
|
+
inline_refs: constraints.inline_refs
|
|
339
351
|
}
|
|
340
352
|
};
|
|
341
353
|
}
|
|
@@ -346,7 +358,8 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
346
358
|
value: function visitColquallist(ctx) {
|
|
347
359
|
var _this5 = this;
|
|
348
360
|
var r = {
|
|
349
|
-
inline_refs: []
|
|
361
|
+
inline_refs: [],
|
|
362
|
+
constraints: []
|
|
350
363
|
};
|
|
351
364
|
ctx.colconstraint().forEach(function (c) {
|
|
352
365
|
var constraint = c.accept(_this5);
|
|
@@ -355,6 +368,10 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
355
368
|
r.inline_refs.push(constraint.value);
|
|
356
369
|
return;
|
|
357
370
|
}
|
|
371
|
+
if (constraint.kind === _constants.COLUMN_CONSTRAINT_KIND.CHECK) {
|
|
372
|
+
r.constraints.push(constraint.value);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
358
375
|
r[constraint.kind] = constraint.value;
|
|
359
376
|
});
|
|
360
377
|
return r;
|
|
@@ -419,6 +436,15 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
419
436
|
value: true
|
|
420
437
|
};
|
|
421
438
|
}
|
|
439
|
+
if (ctx.CHECK()) {
|
|
440
|
+
var expression = (0, _helpers.getOriginalText)(ctx.a_expr());
|
|
441
|
+
return {
|
|
442
|
+
kind: _constants.COLUMN_CONSTRAINT_KIND.CHECK,
|
|
443
|
+
value: {
|
|
444
|
+
expression: expression
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
}
|
|
422
448
|
if (ctx.REFERENCES()) {
|
|
423
449
|
var _ctx$opt_column_list$2;
|
|
424
450
|
var names = ctx.qualified_name().accept(this);
|
|
@@ -942,6 +968,14 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
942
968
|
cmd.value.endpoints[0].schemaName = schemaName;
|
|
943
969
|
_this8.data.refs.push(cmd.value);
|
|
944
970
|
break;
|
|
971
|
+
case _constants.TABLE_CONSTRAINT_KIND.CHECK:
|
|
972
|
+
{
|
|
973
|
+
var table = findTable(_this8.data.tables, schemaName, tableName);
|
|
974
|
+
if (!table) break;
|
|
975
|
+
if (!table.constraints) table.constraints = [];
|
|
976
|
+
table.constraints.push(cmd.value);
|
|
977
|
+
break;
|
|
978
|
+
}
|
|
945
979
|
case _constants.TABLE_CONSTRAINT_KIND.UNIQUE:
|
|
946
980
|
case _constants.TABLE_CONSTRAINT_KIND.PK:
|
|
947
981
|
case _constants.TABLE_CONSTRAINT_KIND.INDEX:
|
|
@@ -950,11 +984,11 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
950
984
|
if (!(cmd.value.pk || cmd.value.unique)) break;
|
|
951
985
|
// eslint-disable-next-line prefer-destructuring
|
|
952
986
|
kind = cmd.kind;
|
|
953
|
-
var
|
|
954
|
-
if (!
|
|
987
|
+
var _table2 = findTable(_this8.data.tables, schemaName, tableName);
|
|
988
|
+
if (!_table2) break;
|
|
955
989
|
if (cmd.value.columns.length === 1 && (cmd.value.unique || cmd.value.pk)) {
|
|
956
990
|
var key = cmd.value.columns[0].value;
|
|
957
|
-
var fieldToUpdate =
|
|
991
|
+
var fieldToUpdate = _table2.fields.find(function (f) {
|
|
958
992
|
return f.name === key;
|
|
959
993
|
});
|
|
960
994
|
if (fieldToUpdate) {
|
|
@@ -972,7 +1006,7 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
972
1006
|
type: cmd.value.type,
|
|
973
1007
|
unique: cmd.value.unique
|
|
974
1008
|
});
|
|
975
|
-
|
|
1009
|
+
_table2.indexes.push(index);
|
|
976
1010
|
break;
|
|
977
1011
|
}
|
|
978
1012
|
default:
|
|
@@ -1051,8 +1085,8 @@ var PostgresASTGen = exports["default"] = /*#__PURE__*/function (_PostgreSQLPars
|
|
|
1051
1085
|
var fieldName = (0, _lodash.last)(_names);
|
|
1052
1086
|
var _tableName = _names.length > 1 ? _names[_names.length - 2] : undefined;
|
|
1053
1087
|
var _schemaName = _names.length > 2 ? _names[_names.length - 3] : undefined;
|
|
1054
|
-
var
|
|
1055
|
-
var field =
|
|
1088
|
+
var _table3 = findTable(this.data.tables, _schemaName, _tableName);
|
|
1089
|
+
var field = _table3.fields.find(function (field) {
|
|
1056
1090
|
return field.name === fieldName;
|
|
1057
1091
|
});
|
|
1058
1092
|
if (!field) return;
|