Skip to content

Commit

Permalink
Further improvements in sql COMMNET
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkiu committed Sep 24, 2022
1 parent 06a5d0a commit 4e4a04c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
6 changes: 3 additions & 3 deletions dist/main.cjs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/main.mjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schema-to-erd",
"version": "1.2.11",
"version": "1.2.12",
"description": "Generate ERD UML file from Schema DDL file",
"type": "module",
"main": "dist/main.cjs",
Expand Down
18 changes: 13 additions & 5 deletions src/parse_ddl.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ function _splitDdl(sqlStr) {
if (start === -1) {
break;
}
const end = restSqlStr.indexOf(';', start) + 1;
const ddlStr = restSqlStr.substring(start, end);
const end = restSqlStr.indexOf(';', start);
if (end === -1) {
throw `Not found semicolon(;). Error - ${restSqlStr}`;
}
const ddlStr = restSqlStr.substring(start, end + 1);
ddls.push(_removeUnparsableToken(ddlStr).replace(/,[\n\r\s]+\)/gm, '\n)'));
offset += end;
}
Expand All @@ -101,9 +104,14 @@ function _extractTableObj(jsonSchemaDocuments) {
};
}

const _removeSqlComments = (sqlStr) =>
sqlStr
.replace(/alter\s+table\s+(.*)\s+comment\s+['"](.*?)['"]/gi, '') // https://stackoverflow.com/a/6441056
.replace(/COMMENT\s+['"](.*?)['"]/gi, '') // https://stackoverflow.com/a/171483
.replace(/\/\*.*?\*\/|--.*?\n/gs, ''); // https://stackoverflow.com/a/21018155

export default (sqlStr) =>
// https://stackoverflow.com/a/21018155
_splitDdl(sqlStr.replace(/\/\*.*?\*\/|--.*?\n/gs, '')).reduce(
_splitDdl(_removeSqlComments(sqlStr)).reduce(
(acc, sql) => {
try {
const options = { useRef: true };
Expand All @@ -115,7 +123,7 @@ export default (sqlStr) =>
const { tableName, columnNames, primaryKeys } = _extractTableObj(jsonSchemaDocuments[0]);
return { ...acc, [tableName]: { columnNames, primaryKeys } };
} catch (err) {
console.error(`Can not parse "${sql}"`, err);
console.error(`Can not parse "${sql}"`);
return acc;
}
},
Expand Down

0 comments on commit 4e4a04c

Please sign in to comment.