Skip to content

Commit 4e4a04c

Browse files
committed
Further improvements in sql COMMNET
1 parent 06a5d0a commit 4e4a04c

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

dist/main.cjs

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

dist/main.mjs

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "schema-to-erd",
3-
"version": "1.2.11",
3+
"version": "1.2.12",
44
"description": "Generate ERD UML file from Schema DDL file",
55
"type": "module",
66
"main": "dist/main.cjs",

src/parse_ddl.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ function _splitDdl(sqlStr) {
8181
if (start === -1) {
8282
break;
8383
}
84-
const end = restSqlStr.indexOf(';', start) + 1;
85-
const ddlStr = restSqlStr.substring(start, end);
84+
const end = restSqlStr.indexOf(';', start);
85+
if (end === -1) {
86+
throw `Not found semicolon(;). Error - ${restSqlStr}`;
87+
}
88+
const ddlStr = restSqlStr.substring(start, end + 1);
8689
ddls.push(_removeUnparsableToken(ddlStr).replace(/,[\n\r\s]+\)/gm, '\n)'));
8790
offset += end;
8891
}
@@ -101,9 +104,14 @@ function _extractTableObj(jsonSchemaDocuments) {
101104
};
102105
}
103106

107+
const _removeSqlComments = (sqlStr) =>
108+
sqlStr
109+
.replace(/alter\s+table\s+(.*)\s+comment\s+['"](.*?)['"]/gi, '') // https://stackoverflow.com/a/6441056
110+
.replace(/COMMENT\s+['"](.*?)['"]/gi, '') // https://stackoverflow.com/a/171483
111+
.replace(/\/\*.*?\*\/|--.*?\n/gs, ''); // https://stackoverflow.com/a/21018155
112+
104113
export default (sqlStr) =>
105-
// https://stackoverflow.com/a/21018155
106-
_splitDdl(sqlStr.replace(/\/\*.*?\*\/|--.*?\n/gs, '')).reduce(
114+
_splitDdl(_removeSqlComments(sqlStr)).reduce(
107115
(acc, sql) => {
108116
try {
109117
const options = { useRef: true };
@@ -115,7 +123,7 @@ export default (sqlStr) =>
115123
const { tableName, columnNames, primaryKeys } = _extractTableObj(jsonSchemaDocuments[0]);
116124
return { ...acc, [tableName]: { columnNames, primaryKeys } };
117125
} catch (err) {
118-
console.error(`Can not parse "${sql}"`, err);
126+
console.error(`Can not parse "${sql}"`);
119127
return acc;
120128
}
121129
},

0 commit comments

Comments
 (0)