From 57d6a82fccc9560a767694a81a157014696ffe61 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Mon, 20 Nov 2023 09:58:28 +0800 Subject: [PATCH] feat: show table definition (#3061) * feat: show table definition * chore: test --- .../query-interface/mysql-query-interface.ts | 10 +++++++ .../postgres-query-interface.ts | 30 +++++++++++++++++++ .../src/query-interface/query-interface.ts | 2 ++ .../query-interface/sqlite-query-interface.ts | 4 +++ 4 files changed, 46 insertions(+) diff --git a/packages/core/database/src/query-interface/mysql-query-interface.ts b/packages/core/database/src/query-interface/mysql-query-interface.ts index 522ba12c4..6fee45304 100644 --- a/packages/core/database/src/query-interface/mysql-query-interface.ts +++ b/packages/core/database/src/query-interface/mysql-query-interface.ts @@ -76,4 +76,14 @@ export default class MysqlQueryInterface extends QueryInterface { const sql = match[0]; return sql; } + + async showTableDefinition(tableInfo: { name: string; schema?: string }): Promise { + const { name } = tableInfo; + + const sql = `SHOW CREATE TABLE ${name}`; + + const results = await this.db.sequelize.query(sql, { type: 'SELECT' }); + + return results[0]['Create Table']; + } } diff --git a/packages/core/database/src/query-interface/postgres-query-interface.ts b/packages/core/database/src/query-interface/postgres-query-interface.ts index 5754af93f..6e920e828 100644 --- a/packages/core/database/src/query-interface/postgres-query-interface.ts +++ b/packages/core/database/src/query-interface/postgres-query-interface.ts @@ -112,4 +112,34 @@ export default class PostgresQueryInterface extends QueryInterface { return {}; } } + + async showTableDefinition(tableInfo: { name: string; schema?: string }) { + const showFunc = ` +CREATE OR REPLACE FUNCTION show_create_table(p_schema text, p_table_name text) +RETURNS text AS +$BODY$ +SELECT 'CREATE TABLE ' || p_schema || '.' || p_table_name || ' (' || E'\\n' || '' || + string_agg(column_list.column_expr, ', ' || E'\\n' || '') || + '' || E'\\n' || ');' +FROM ( + SELECT ' ' || column_name || ' ' || data_type || + coalesce('(' || character_maximum_length || ')', '') || + case when is_nullable = 'YES' then '' else ' NOT NULL' end as column_expr + FROM information_schema.columns + WHERE table_schema = p_schema AND table_name = p_table_name + ORDER BY ordinal_position) column_list; +$BODY$ + LANGUAGE SQL STABLE; + `; + await this.db.sequelize.query(showFunc, { type: 'RAW' }); + + const res = await this.db.sequelize.query( + `SELECT show_create_table('${tableInfo.schema}', '${tableInfo.name || 'public'}')`, + { + type: 'SELECT', + }, + ); + + return res[0]['show_create_table']; + } } diff --git a/packages/core/database/src/query-interface/query-interface.ts b/packages/core/database/src/query-interface/query-interface.ts index 3c21554bb..5033e3fb0 100644 --- a/packages/core/database/src/query-interface/query-interface.ts +++ b/packages/core/database/src/query-interface/query-interface.ts @@ -25,6 +25,8 @@ export default abstract class QueryInterface { abstract parseSQL(sql: string): any; + abstract showTableDefinition(tableInfo: { name: string; schema?: string }): Promise; + async dropAll(options) { if (options.drop !== true) return; diff --git a/packages/core/database/src/query-interface/sqlite-query-interface.ts b/packages/core/database/src/query-interface/sqlite-query-interface.ts index 5b99bc222..ee1d53adb 100644 --- a/packages/core/database/src/query-interface/sqlite-query-interface.ts +++ b/packages/core/database/src/query-interface/sqlite-query-interface.ts @@ -86,4 +86,8 @@ export default class SqliteQueryInterface extends QueryInterface { return sql; } + + showTableDefinition(tableInfo: { name: string; schema?: string }): Promise { + return Promise.resolve(undefined); + } }