From 732ebb4f87e6bffa88eba775e43186d8e6659172 Mon Sep 17 00:00:00 2001 From: Michael Drury Date: Thu, 18 May 2023 22:09:44 +0100 Subject: [PATCH] Adding function to fetch table names. --- .../src/integrations/microsoftSqlServer.ts | 15 ++++++++- packages/server/src/integrations/mysql.ts | 33 +++++++++++++------ packages/server/src/integrations/oracle.ts | 11 +++++++ packages/server/src/integrations/postgres.ts | 11 +++++++ packages/types/src/sdk/datasources.ts | 1 + 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 47f36f60e9..ab0a3e14e8 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -20,7 +20,6 @@ import { } from "./utils" import Sql from "./base/sql" import { MSSQLTablesResponse, MSSQLColumn } from "./base/types" - const sqlServer = require("mssql") const DEFAULT_SCHEMA = "dbo" @@ -284,6 +283,20 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { this.schemaErrors = final.errors } + async queryTableNames() { + let tableInfo: MSSQLTablesResponse[] = await this.runSQL(this.TABLES_SQL) + const schema = this.config.schema || DEFAULT_SCHEMA + return tableInfo + .filter((record: any) => record.TABLE_SCHEMA === schema) + .map((record: any) => record.TABLE_NAME) + .filter((name: string) => this.MASTER_TABLES.indexOf(name) === -1) + } + + async getTableNames() { + await this.connect() + return this.queryTableNames() + } + async read(query: SqlQuery | string) { await this.connect() const response = await this.internalQuery(getSqlQuery(query)) diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index eb721a6e0f..ca0c962d25 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -214,20 +214,11 @@ class MySQLIntegration extends Sql implements DatasourcePlus { async buildSchema(datasourceId: string, entities: Record) { const tables: { [key: string]: Table } = {} - const database = this.config.database await this.connect() try { // get the tables first - const tablesResp: Record[] = await this.internalQuery( - { sql: "SHOW TABLES;" }, - { connect: false } - ) - const tableNames: string[] = tablesResp.map( - (obj: any) => - obj[`Tables_in_${database}`] || - obj[`Tables_in_${database.toLowerCase()}`] - ) + const tableNames = await this.queryTableNames() for (let tableName of tableNames) { const primaryKeys = [] const schema: TableSchema = {} @@ -274,6 +265,28 @@ class MySQLIntegration extends Sql implements DatasourcePlus { this.schemaErrors = final.errors } + async queryTableNames() { + const database = this.config.database + const tablesResp: Record[] = await this.internalQuery( + { sql: "SHOW TABLES;" }, + { connect: false } + ) + return tablesResp.map( + (obj: any) => + obj[`Tables_in_${database}`] || + obj[`Tables_in_${database.toLowerCase()}`] + ) + } + + async getTableNames() { + await this.connect() + try { + return this.queryTableNames() + } finally { + await this.disconnect() + } + } + async create(query: SqlQuery | string) { const results = await this.internalQuery(getSqlQuery(query)) return results.length ? results : [{ created: true }] diff --git a/packages/server/src/integrations/oracle.ts b/packages/server/src/integrations/oracle.ts index f8ec6e8bae..53e88752cd 100644 --- a/packages/server/src/integrations/oracle.ts +++ b/packages/server/src/integrations/oracle.ts @@ -323,6 +323,17 @@ class OracleIntegration extends Sql implements DatasourcePlus { this.schemaErrors = final.errors } + async getTableNames() { + const columnsResponse = await this.internalQuery({ + sql: this.COLUMNS_SQL, + }) + if (!columnsResponse.rows) { + return [] + } else { + return columnsResponse.rows.map(row => row.TABLE_NAME) + } + } + async testConnection() { const response: ConnectionInfo = { connected: false, diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index bf77ec08c6..0954f94ecb 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -311,6 +311,17 @@ class PostgresIntegration extends Sql implements DatasourcePlus { } } + async getTableNames() { + try { + await this.openConnection() + const columnsResponse: { rows: PostgresColumn[] } = + await this.client.query(this.COLUMNS_SQL) + return columnsResponse.rows.map(row => row.table_name) + } finally { + await this.closeConnection() + } + } + async create(query: SqlQuery | string) { const response = await this.internalQuery(getSqlQuery(query)) return response.rows.length ? response.rows : [{ created: true }] diff --git a/packages/types/src/sdk/datasources.ts b/packages/types/src/sdk/datasources.ts index 9df9670877..4631a091b4 100644 --- a/packages/types/src/sdk/datasources.ts +++ b/packages/types/src/sdk/datasources.ts @@ -150,4 +150,5 @@ export interface DatasourcePlus extends IntegrationBase { getBindingIdentifier(): string getStringConcat(parts: string[]): string buildSchema(datasourceId: string, entities: Record): any + getTableNames(): Promise }