From 439940cd22a3252a36083387a3caea96aa29fc76 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 12 Dec 2023 21:24:52 +0800 Subject: [PATCH] chore: query interface (#3177) * chore: query interface * fix: build * chore: typo --- .../database/src/query-interface/mysql-query-interface.ts | 8 ++++---- .../src/query-interface/postgres-query-interface.ts | 6 +++--- .../core/database/src/query-interface/query-interface.ts | 7 ++++++- .../src/query-interface/sqlite-query-interface.ts | 4 ++-- packages/core/database/src/utils.ts | 2 +- 5 files changed, 16 insertions(+), 11 deletions(-) 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 6fee45304..116b8a531 100644 --- a/packages/core/database/src/query-interface/mysql-query-interface.ts +++ b/packages/core/database/src/query-interface/mysql-query-interface.ts @@ -1,7 +1,7 @@ import { Transactionable } from 'sequelize'; import { Collection } from '../collection'; import sqlParser from '../sql-parser'; -import QueryInterface from './query-interface'; +import QueryInterface, { TableInfo } from './query-interface'; export default class MysqlQueryInterface extends QueryInterface { constructor(db) { @@ -77,10 +77,10 @@ export default class MysqlQueryInterface extends QueryInterface { return sql; } - async showTableDefinition(tableInfo: { name: string; schema?: string }): Promise { - const { name } = tableInfo; + async showTableDefinition(tableInfo: TableInfo): Promise { + const { tableName } = tableInfo; - const sql = `SHOW CREATE TABLE ${name}`; + const sql = `SHOW CREATE TABLE ${tableName}`; const results = await this.db.sequelize.query(sql, { type: 'SELECT' }); 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 f900fd7b1..c68944b9a 100644 --- a/packages/core/database/src/query-interface/postgres-query-interface.ts +++ b/packages/core/database/src/query-interface/postgres-query-interface.ts @@ -1,7 +1,7 @@ import lodash from 'lodash'; import { Collection } from '../collection'; import sqlParser from '../sql-parser/postgres'; -import QueryInterface from './query-interface'; +import QueryInterface, { TableInfo } from './query-interface'; export default class PostgresQueryInterface extends QueryInterface { constructor(db) { @@ -113,7 +113,7 @@ export default class PostgresQueryInterface extends QueryInterface { } } - async showTableDefinition(tableInfo: { name: string; schema?: string }) { + async showTableDefinition(tableInfo: TableInfo): Promise { const showFunc = ` CREATE OR REPLACE FUNCTION show_create_table(p_schema text, p_table_name text) RETURNS text AS @@ -134,7 +134,7 @@ $BODY$ await this.db.sequelize.query(showFunc, { type: 'RAW' }); const res = await this.db.sequelize.query( - `SELECT show_create_table('${tableInfo.schema}', '${tableInfo.name || 'public'}')`, + `SELECT show_create_table('${tableInfo.schema || 'public'}', '${tableInfo.tableName}')`, { type: 'SELECT', }, diff --git a/packages/core/database/src/query-interface/query-interface.ts b/packages/core/database/src/query-interface/query-interface.ts index 5033e3fb0..b1c7b0d5a 100644 --- a/packages/core/database/src/query-interface/query-interface.ts +++ b/packages/core/database/src/query-interface/query-interface.ts @@ -2,6 +2,11 @@ import { QueryInterface as SequelizeQueryInterface, Transactionable } from 'sequ import { Collection } from '../collection'; import Database from '../database'; +export type TableInfo = { + tableName: string; + schema?: string; +}; + export default abstract class QueryInterface { sequelizeQueryInterface: SequelizeQueryInterface; @@ -25,7 +30,7 @@ export default abstract class QueryInterface { abstract parseSQL(sql: string): any; - abstract showTableDefinition(tableInfo: { name: string; schema?: string }): Promise; + abstract showTableDefinition(tableInfo: TableInfo): 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 ee1d53adb..a1bdd0375 100644 --- a/packages/core/database/src/query-interface/sqlite-query-interface.ts +++ b/packages/core/database/src/query-interface/sqlite-query-interface.ts @@ -1,6 +1,6 @@ import { Collection } from '../collection'; import sqlParser from '../sql-parser'; -import QueryInterface from './query-interface'; +import QueryInterface, { TableInfo } from './query-interface'; export default class SqliteQueryInterface extends QueryInterface { constructor(db) { @@ -87,7 +87,7 @@ export default class SqliteQueryInterface extends QueryInterface { return sql; } - showTableDefinition(tableInfo: { name: string; schema?: string }): Promise { + showTableDefinition(tableInfo: TableInfo): Promise { return Promise.resolve(undefined); } } diff --git a/packages/core/database/src/utils.ts b/packages/core/database/src/utils.ts index bf6bf428a..6c2c464ab 100644 --- a/packages/core/database/src/utils.ts +++ b/packages/core/database/src/utils.ts @@ -39,7 +39,7 @@ function patchShowConstraintsQuery(queryGenerator, db) { `WHERE table_name='${lodash.isPlainObject(tableName) ? tableName.tableName : tableName}'`, ]; - if (!constraintName) { + if (constraintName) { lines.push(`AND constraint_name='${constraintName}'`); }