@dbml/core 2.6.1 → 3.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.
@@ -56,13 +56,13 @@ var MySQLExporter = /*#__PURE__*/function () {
56
56
  if (field.dbdefault.type === 'expression') {
57
57
  line += " DEFAULT (".concat(field.dbdefault.value, ")");
58
58
  } else if (field.dbdefault.type === 'string') {
59
- line += " DEFAULT \"".concat(field.dbdefault.value, "\"");
59
+ line += " DEFAULT '".concat(field.dbdefault.value, "'");
60
60
  } else {
61
61
  line += " DEFAULT ".concat(field.dbdefault.value);
62
62
  }
63
63
  }
64
64
  if (field.note) {
65
- line += " COMMENT '".concat(field.note.replace(/'/g, "\\'"), "'");
65
+ line += " COMMENT '".concat(field.note.replace(/'/g, "''"), "'");
66
66
  }
67
67
  return line;
68
68
  });
@@ -246,7 +246,7 @@ var MySQLExporter = /*#__PURE__*/function () {
246
246
  if (comment.type === 'table') {
247
247
  var table = model.tables[comment.tableId];
248
248
  var schema = model.schemas[table.schemaId];
249
- line += "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "`".concat(schema.name, "`.") : '', "`").concat(table.name, "` COMMENT = '").concat(table.note.replace(/'/g, "\\'"), "'");
249
+ line += "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "`".concat(schema.name, "`.") : '', "`").concat(table.name, "` COMMENT = '").concat(table.note.replace(/'/g, "''"), "'");
250
250
  }
251
251
  line += ';\n';
252
252
  return line;
@@ -269,13 +269,13 @@ var PostgresExporter = /*#__PURE__*/function () {
269
269
  switch (comment.type) {
270
270
  case 'table':
271
271
  {
272
- line += " TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\" IS '").concat(table.note.replace(/'/g, "\""), "'");
272
+ line += " TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\" IS '").concat(table.note.replace(/'/g, "''"), "'");
273
273
  break;
274
274
  }
275
275
  case 'column':
276
276
  {
277
277
  var field = model.fields[comment.fieldId];
278
- line += " COLUMN ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\".\"").concat(field.name, "\" IS '").concat(field.note.replace(/'/g, "\""), "'");
278
+ line += " COLUMN ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\".\"").concat(field.name, "\" IS '").concat(field.note.replace(/'/g, "''"), "'");
279
279
  break;
280
280
  }
281
281
  default:
@@ -246,7 +246,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
246
246
  case 'table':
247
247
  {
248
248
  line += "@name = N'Table_Description',\n";
249
- line += "@value = '".concat(table.note.replace(/'/g, "\""), "',\n");
249
+ line += "@value = '".concat(table.note.replace(/'/g, "''"), "',\n");
250
250
  line += "@level0type = N'Schema', @level0name = '".concat((0, _utils.shouldPrintSchema)(schema, model) ? "".concat(schema.name) : 'dbo', "',\n");
251
251
  line += "@level1type = N'Table', @level1name = '".concat(table.name, "';\n");
252
252
  break;
@@ -255,7 +255,7 @@ var SqlServerExporter = /*#__PURE__*/function () {
255
255
  {
256
256
  var field = model.fields[comment.fieldId];
257
257
  line += "@name = N'Column_Description',\n";
258
- line += "@value = '".concat(field.note.replace(/'/g, "\""), "',\n");
258
+ line += "@value = '".concat(field.note.replace(/'/g, "''"), "',\n");
259
259
  line += "@level0type = N'Schema', @level0name = '".concat((0, _utils.shouldPrintSchema)(schema, model) ? "".concat(schema.name) : 'dbo', "',\n");
260
260
  line += "@level1type = N'Table', @level1name = '".concat(table.name, "',\n");
261
261
  line += "@level2type = N'Column', @level2name = '".concat(field.name, "';\n");
@@ -8,7 +8,7 @@ var _ModelExporter = _interopRequireDefault(require("./ModelExporter"));
8
8
  var _Parser = _interopRequireDefault(require("../parse/Parser"));
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
10
  function _export(str, format) {
11
- var database = _Parser["default"].parse(str, 'dbml');
11
+ var database = new _Parser["default"]().parse(str, 'dbmlv2');
12
12
  return _ModelExporter["default"]["export"](database.normalize(), format);
13
13
  }
14
14
  var _default = {
@@ -8,7 +8,7 @@ var _Parser = _interopRequireDefault(require("../parse/Parser"));
8
8
  var _ModelExporter = _interopRequireDefault(require("../export/ModelExporter"));
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
10
  function _import(str, format) {
11
- var database = _Parser["default"].parse(str, format);
11
+ var database = new _Parser["default"]().parse(str, format);
12
12
  var dbml = _ModelExporter["default"]["export"](database.normalize(), 'dbml');
13
13
  return dbml;
14
14
  }
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
+ var _parse = require("@dbml/parse");
7
8
  var _database = _interopRequireDefault(require("../model_structure/database"));
8
9
  var _mysqlParser = _interopRequireDefault(require("./mysqlParser"));
9
10
  var _postgresParser = _interopRequireDefault(require("./postgresParser"));
@@ -19,46 +20,11 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
19
20
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
20
21
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
21
22
  var Parser = /*#__PURE__*/function () {
22
- function Parser() {
23
+ function Parser(DBMLCompiler) {
23
24
  _classCallCheck(this, Parser);
25
+ this.DBMLCompiler = DBMLCompiler || new _parse.Compiler();
24
26
  }
25
- _createClass(Parser, null, [{
26
- key: "parseJSONToDatabase",
27
- value: function parseJSONToDatabase(rawDatabase) {
28
- var database = new _database["default"](rawDatabase);
29
- return database;
30
- }
31
- }, {
32
- key: "parseMySQLToJSON",
33
- value: function parseMySQLToJSON(str) {
34
- return _mysqlParser["default"].parse(str);
35
- }
36
- }, {
37
- key: "parsePostgresToJSONv2",
38
- value: function parsePostgresToJSONv2(str) {
39
- return (0, _ASTGeneration.parse)(str, 'postgres');
40
- }
41
- }, {
42
- key: "parsePostgresToJSON",
43
- value: function parsePostgresToJSON(str) {
44
- return _postgresParser["default"].parse(str);
45
- }
46
- }, {
47
- key: "parseDBMLToJSON",
48
- value: function parseDBMLToJSON(str) {
49
- return _dbmlParser["default"].parse(str);
50
- }
51
- }, {
52
- key: "parseSchemaRbToJSON",
53
- value: function parseSchemaRbToJSON(str) {
54
- return _schemarbParser["default"].parse(str);
55
- }
56
- }, {
57
- key: "parseMSSQLToJSON",
58
- value: function parseMSSQLToJSON(str) {
59
- return _mssqlParser["default"].parseWithPegError(str);
60
- }
61
- }, {
27
+ _createClass(Parser, [{
62
28
  key: "parse",
63
29
  value: function parse(str, format) {
64
30
  var rawDatabase = {};
@@ -75,6 +41,31 @@ var Parser = /*#__PURE__*/function () {
75
41
  case 'dbml':
76
42
  rawDatabase = Parser.parseDBMLToJSON(str);
77
43
  break;
44
+ case 'dbmlv2':
45
+ {
46
+ this.DBMLCompiler.setSource(str);
47
+ var errors = this.DBMLCompiler.parse.errors();
48
+ if (errors.length > 0) {
49
+ throw errors.map(function (error) {
50
+ return {
51
+ message: error.diagnostic,
52
+ location: {
53
+ start: {
54
+ line: error.nodeOrToken.startPos.line + 1,
55
+ column: error.nodeOrToken.startPos.column + 1
56
+ },
57
+ end: {
58
+ line: error.nodeOrToken.endPos.line + 1,
59
+ column: error.nodeOrToken.endPos.column + 1
60
+ }
61
+ },
62
+ code: error.code
63
+ };
64
+ });
65
+ }
66
+ rawDatabase = this.DBMLCompiler.parse.rawDb();
67
+ break;
68
+ }
78
69
  case 'schemarb':
79
70
  rawDatabase = Parser.parseSchemaRbToJSON(str);
80
71
  break;
@@ -94,6 +85,42 @@ var Parser = /*#__PURE__*/function () {
94
85
  var schema = Parser.parseJSONToDatabase(rawDatabase);
95
86
  return schema;
96
87
  }
88
+ }], [{
89
+ key: "parseJSONToDatabase",
90
+ value: function parseJSONToDatabase(rawDatabase) {
91
+ var database = new _database["default"](rawDatabase);
92
+ return database;
93
+ }
94
+ }, {
95
+ key: "parseMySQLToJSON",
96
+ value: function parseMySQLToJSON(str) {
97
+ return _mysqlParser["default"].parse(str);
98
+ }
99
+ }, {
100
+ key: "parsePostgresToJSONv2",
101
+ value: function parsePostgresToJSONv2(str) {
102
+ return (0, _ASTGeneration.parse)(str, 'postgres');
103
+ }
104
+ }, {
105
+ key: "parsePostgresToJSON",
106
+ value: function parsePostgresToJSON(str) {
107
+ return _postgresParser["default"].parse(str);
108
+ }
109
+ }, {
110
+ key: "parseDBMLToJSON",
111
+ value: function parseDBMLToJSON(str) {
112
+ return _dbmlParser["default"].parse(str);
113
+ }
114
+ }, {
115
+ key: "parseSchemaRbToJSON",
116
+ value: function parseSchemaRbToJSON(str) {
117
+ return _schemarbParser["default"].parse(str);
118
+ }
119
+ }, {
120
+ key: "parseMSSQLToJSON",
121
+ value: function parseMSSQLToJSON(str) {
122
+ return _mssqlParser["default"].parseWithPegError(str);
123
+ }
97
124
  }]);
98
125
  return Parser;
99
126
  }();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbml/core",
3
- "version": "2.6.1",
3
+ "version": "3.0.0",
4
4
  "description": "> TODO: description",
5
5
  "author": "Holistics <dev@holistics.io>",
6
6
  "license": "Apache-2.0",
@@ -32,6 +32,7 @@
32
32
  "prepublish": "npm run build"
33
33
  },
34
34
  "dependencies": {
35
+ "@dbml/parse": "^3.0.0",
35
36
  "antlr4": "^4.13.1",
36
37
  "lodash": "^4.17.15",
37
38
  "parsimmon": "^1.13.0",
@@ -58,5 +59,5 @@
58
59
  "\\.(?!json$)[^.]*$": "@glen/jest-raw-loader"
59
60
  }
60
61
  },
61
- "gitHead": "54ed18bb2232c6143992aa3d81f4a2b48c36a249"
62
+ "gitHead": "d475bdf2122e26b3b2c3bb00c67124b5e669ba10"
62
63
  }
@@ -1,5 +1,7 @@
1
+ import { Compiler } from '@dbml/parse';
1
2
  import Database, { RawDatabase } from '../model_structure/database';
2
3
  declare class Parser {
4
+ constructor(DBMLCompiler: Compiler?);
3
5
  static parseJSONToDatabase(rawDatabase: RawDatabase): Database;
4
6
  static parseMySQLToJSON(str: string): RawDatabase;
5
7
  static parsePostgresToJSON(str: string): RawDatabase;
@@ -7,6 +9,6 @@ declare class Parser {
7
9
  static parseDBMLToJSON(str: string): RawDatabase;
8
10
  static parseSchemaRbToJSON(str: string): RawDatabase;
9
11
  static parseMSSQLToJSON(str: string): RawDatabase;
10
- static parse(str: string, format: 'mysql' | 'postgres' | 'dbml' | 'schemarb' | 'mssql' | 'json'): Database;
12
+ parse(str: string, format: 'mysql' | 'postgres' | 'dbml' | 'dbmlv2' | 'schemarb' | 'mssql' | 'json'): Database;
11
13
  }
12
14
  export default Parser;