chore: query interface (#3177)

* chore: query interface

* fix: build

* chore: typo
This commit is contained in:
ChengLei Shao 2023-12-12 21:24:52 +08:00 committed by GitHub
parent 7b74e999c9
commit 439940cd22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 11 deletions

View File

@ -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<any> {
const { name } = tableInfo;
async showTableDefinition(tableInfo: TableInfo): Promise<any> {
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' });

View File

@ -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<any> {
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',
},

View File

@ -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<any>;
abstract showTableDefinition(tableInfo: TableInfo): Promise<any>;
async dropAll(options) {
if (options.drop !== true) return;

View File

@ -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<any> {
showTableDefinition(tableInfo: TableInfo): Promise<any> {
return Promise.resolve(undefined);
}
}

View File

@ -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}'`);
}