chore: query interface (#3177)
* chore: query interface * fix: build * chore: typo
This commit is contained in:
		
							parent
							
								
									7b74e999c9
								
							
						
					
					
						commit
						439940cd22
					
				@ -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' });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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',
 | 
			
		||||
      },
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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}'`);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user