mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-08-02 22:58:43 +08:00
feat: optimize fetch database schema (#65)
This commit is contained in:
@ -8,7 +8,7 @@ export interface Connector {
|
||||
execute: (databaseName: string, statement: string) => Promise<ExecutionResult>;
|
||||
getDatabases: () => Promise<string[]>;
|
||||
getTables: (databaseName: string) => Promise<string[]>;
|
||||
getTableStructure: (databaseName: string, tableName: string) => Promise<string>;
|
||||
getTableStructure: (databaseName: string, tableName: string, structureFetched: (tableName: string, structure: string) => void) => Promise<void>;
|
||||
}
|
||||
|
||||
export const newConnector = (connection: Connection): Connector => {
|
||||
|
@ -75,7 +75,7 @@ const getTables = async (connection: Connection, databaseName: string): Promise<
|
||||
return tableList;
|
||||
};
|
||||
|
||||
const getTableStructure = async (connection: Connection, databaseName: string, tableName: string): Promise<string> => {
|
||||
const getTableStructure = async (connection: Connection, databaseName: string, tableName: string, structureFetched: (tableName: string,structure: string) => void): Promise<void> => {
|
||||
const pool = await getMSSQLConnection(connection);
|
||||
const request = pool.request();
|
||||
const { recordset } = await request.query(
|
||||
@ -89,10 +89,9 @@ const getTableStructure = async (connection: Connection, databaseName: string, t
|
||||
`${row["COLUMN_NAME"]} ${row["DATA_TYPE"].toUpperCase()} ${String(row["IS_NULLABLE"]).toUpperCase() === "NO" ? "NOT NULL" : ""}`
|
||||
);
|
||||
}
|
||||
|
||||
return `CREATE TABLE [${tableName}] (
|
||||
structureFetched(tableName, `CREATE TABLE [${tableName}] (
|
||||
${columnList.join(",\n")}
|
||||
);`;
|
||||
);`);
|
||||
};
|
||||
|
||||
const newConnector = (connection: Connection): Connector => {
|
||||
@ -101,7 +100,7 @@ const newConnector = (connection: Connection): Connector => {
|
||||
execute: (databaseName: string, statement: string) => execute(connection, databaseName, statement),
|
||||
getDatabases: () => getDatabases(connection),
|
||||
getTables: (databaseName: string) => getTables(connection, databaseName),
|
||||
getTableStructure: (databaseName: string, tableName: string) => getTableStructure(connection, databaseName, tableName),
|
||||
getTableStructure: (databaseName: string, tableName: string, structureFetched: (tableName: string, structure: string) => void) => getTableStructure(connection, databaseName, tableName, structureFetched),
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -80,14 +80,14 @@ const getTables = async (connection: Connection, databaseName: string): Promise<
|
||||
return tableList;
|
||||
};
|
||||
|
||||
const getTableStructure = async (connection: Connection, databaseName: string, tableName: string): Promise<string> => {
|
||||
const getTableStructure = async (connection: Connection, databaseName: string, tableName: string, structureFetched: (tableName: string,structure: string) => void): Promise<void> => {
|
||||
const conn = await getMySQLConnection(connection);
|
||||
const [rows] = await conn.query<RowDataPacket[]>(`SHOW CREATE TABLE \`${databaseName}\`.\`${tableName}\`;`);
|
||||
conn.destroy();
|
||||
if (rows.length !== 1) {
|
||||
throw new Error("Unexpected number of rows.");
|
||||
}
|
||||
return rows[0]["Create Table"] || "";
|
||||
structureFetched(tableName, rows[0]["Create Table"] || "");
|
||||
};
|
||||
|
||||
const newConnector = (connection: Connection): Connector => {
|
||||
@ -96,7 +96,7 @@ const newConnector = (connection: Connection): Connector => {
|
||||
execute: (databaseName: string, statement: string) => execute(connection, databaseName, statement),
|
||||
getDatabases: () => getDatabases(connection),
|
||||
getTables: (databaseName: string) => getTables(connection, databaseName),
|
||||
getTableStructure: (databaseName: string, tableName: string) => getTableStructure(connection, databaseName, tableName),
|
||||
getTableStructure: (databaseName: string, tableName: string, structureFetched: (tableName: string, structure: string) => void) => getTableStructure(connection, databaseName, tableName, structureFetched),
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -83,7 +83,7 @@ const getTables = async (connection: Connection, databaseName: string): Promise<
|
||||
return tableList;
|
||||
};
|
||||
|
||||
const getTableStructure = async (connection: Connection, databaseName: string, tableName: string): Promise<string> => {
|
||||
const getTableStructure = async (connection: Connection, databaseName: string, tableName: string, structureFetched: (tableName: string,structure: string) => void): Promise<void> => {
|
||||
connection.database = databaseName;
|
||||
const client = newPostgresClient(connection);
|
||||
await client.connect();
|
||||
@ -99,9 +99,9 @@ const getTableStructure = async (connection: Connection, databaseName: string, t
|
||||
`${row["column_name"]} ${row["data_type"].toUpperCase()} ${String(row["is_nullable"]).toUpperCase() === "NO" ? "NOT NULL" : ""}`
|
||||
);
|
||||
}
|
||||
return `CREATE TABLE \`${tableName}\` (
|
||||
structureFetched(tableName, `CREATE TABLE \`${tableName}\` (
|
||||
${columnList.join(",\n")}
|
||||
);`;
|
||||
);`);
|
||||
};
|
||||
|
||||
const newConnector = (connection: Connection): Connector => {
|
||||
@ -110,7 +110,7 @@ const newConnector = (connection: Connection): Connector => {
|
||||
execute: (databaseName: string, statement: string) => execute(connection, databaseName, statement),
|
||||
getDatabases: () => getDatabases(connection),
|
||||
getTables: (databaseName: string) => getTables(connection, databaseName),
|
||||
getTableStructure: (databaseName: string, tableName: string) => getTableStructure(connection, databaseName, tableName),
|
||||
getTableStructure: (databaseName: string, tableName: string, structureFetched: (tableName: string, structure: string) => void) => getTableStructure(connection, databaseName, tableName, structureFetched),
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -16,15 +16,18 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const connector = newConnector(connection);
|
||||
const tableStructures: Table[] = [];
|
||||
const rawTableNameList = await connector.getTables(db);
|
||||
for (const tableName of rawTableNameList) {
|
||||
const structure = await connector.getTableStructure(db, tableName);
|
||||
const structureFetched = (tableName:string, structure:string) => {
|
||||
tableStructures.push({
|
||||
name: tableName,
|
||||
structure,
|
||||
});
|
||||
}
|
||||
res.status(200).json({
|
||||
data: tableStructures,
|
||||
Promise.all(rawTableNameList.map(async (tableName) =>
|
||||
connector.getTableStructure(db, tableName, structureFetched)
|
||||
)).then(() => {
|
||||
res.status(200).json({
|
||||
data: tableStructures,
|
||||
});
|
||||
});
|
||||
} catch (error: any) {
|
||||
res.status(400).json({
|
||||
|
Reference in New Issue
Block a user