MCPcopy Create free account
hub / github.com/sqlchat/sqlchat / getTableSchema

Function getTableSchema

src/lib/connectors/mysql/index.ts:87–112  ·  view source on GitHub ↗
(connection: Connection, databaseName: string)

Source from the content-addressed store, hash-verified

85};
86
87const getTableSchema = async (connection: Connection, databaseName: string): Promise<Schema[]> => {
88 const conn = await getMySQLConnection(connection);
89 // get All tableList from database
90 const [rows] = await conn.query<RowDataPacket[]>(
91 // SYSTEM VERSIONED is a special table for MariaDB https://mariadb.com/kb/en/system-versioned-tables/
92 `SELECT TABLE_NAME as table_name FROM information_schema.tables WHERE TABLE_SCHEMA=? AND (TABLE_TYPE='BASE TABLE' || TABLE_TYPE='SYSTEM VERSIONED');`,
93 [databaseName]
94 );
95 const tableList = [];
96 for (const row of rows) {
97 if (row["table_name"]) {
98 tableList.push(row["table_name"]);
99 }
100 }
101 const SchemaList: Schema[] = [{ name: "", tables: [] as Table[] }];
102
103 for (const tableName of tableList) {
104 const [rows] = await conn.query<RowDataPacket[]>(`SHOW CREATE TABLE \`${databaseName}\`.\`${tableName}\`;`);
105 if (rows.length !== 1) {
106 throw new Error("Unexpected number of rows.");
107 }
108
109 SchemaList[0].tables.push({ name: tableName, structure: rows[0]["Create Table"] || "" });
110 }
111 return SchemaList;
112};
113
114const newConnector = (connection: Connection): Connector => {
115 return {

Callers 1

newConnectorFunction · 0.70

Calls 1

getMySQLConnectionFunction · 0.85

Tested by

no test coverage detected