drawDB/src/utils/importSQL/index.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-06-14 06:00:47 +08:00
import {
DB,
tableColorStripHeight,
tableFieldHeight,
tableHeaderHeight,
} from "../../data/constants";
import { fromMariaDB } from "./mariadb";
import { fromMySQL } from "./mysql";
2024-06-14 06:00:47 +08:00
import { fromSQLite } from "./sqlite";
2024-06-14 06:00:47 +08:00
export function importSQL(ast, toDb = DB.MYSQL, diagramDb = DB.GENERIC) {
let diagram = { tables: [], relationships: [] };
switch (toDb) {
case DB.SQLITE:
2024-06-14 06:00:47 +08:00
diagram = fromSQLite(ast, diagramDb);
break;
case DB.MYSQL:
2024-06-14 06:00:47 +08:00
diagram = fromMySQL(ast, diagramDb);
break;
case DB.POSTGRES:
2024-06-14 06:00:47 +08:00
diagram = { tables: [], relationships: [] };
break;
case DB.MARIADB:
diagram = fromMariaDB(ast, diagramDb);
2024-06-14 06:00:47 +08:00
break;
case DB.MSSQL:
2024-06-14 06:00:47 +08:00
diagram = { tables: [], relationships: [] };
break;
default:
2024-06-14 06:00:47 +08:00
diagram = { tables: [], relationships: [] };
break;
}
2024-06-14 06:00:47 +08:00
let maxHeight = -1;
const tableWidth = 200;
const gapX = 54;
const gapY = 40;
diagram.tables.forEach((table, i) => {
if (i < diagram.tables.length / 2) {
table.x = i * tableWidth + (i + 1) * gapX;
table.y = gapY;
const height =
table.fields.length * tableFieldHeight +
tableHeaderHeight +
tableColorStripHeight;
maxHeight = Math.max(height, maxHeight);
} else {
const index = diagram.tables.length - i - 1;
table.x = index * tableWidth + (index + 1) * gapX;
table.y = maxHeight + 2 * gapY;
}
});
return diagram;
}