diff --git a/src/tables/table.ts b/src/tables/table.ts index a4e9c8b..9772978 100644 --- a/src/tables/table.ts +++ b/src/tables/table.ts @@ -29,7 +29,13 @@ export default class Table { this.name = name; this.integration = integration; } - + /** + * Removes this table from its integration. + * @throws {MindsDbError} - Something went wrong removing this table. + */ + async removeTable(): Promise { + await this.tablesApiClient.removeTable(this.name, this.integration); + } /** * Deletes this table from its integration. * @throws {MindsDbError} - Something went wrong deleting this table. diff --git a/src/tables/tablesApiClient.ts b/src/tables/tablesApiClient.ts index d82c562..48d596f 100644 --- a/src/tables/tablesApiClient.ts +++ b/src/tables/tablesApiClient.ts @@ -41,3 +41,13 @@ export default abstract class TablesApiClient { */ abstract deleteTable(name: string, integration: string): Promise; } + +/** + * Removes a table from its integration. + * @param {string} name - Name of the table to be removed. + * @param {string} integration - Name of the integration the table belongs to. + * @returns {Promise} - Resolves when the table is successfully removed. + * @throws {MindsDbError} - Something went wrong removing the table. + */ +abstract removeTable(name: string, integration: string): Promise; + diff --git a/src/tables/tablesRestApiClient.ts b/src/tables/tablesRestApiClient.ts index 2b83531..66b099c 100644 --- a/src/tables/tablesRestApiClient.ts +++ b/src/tables/tablesRestApiClient.ts @@ -88,3 +88,27 @@ export default class TablesRestApiClient extends TablesApiClient { } } } + +/** + * Removes a table from its integration. + * @param {string} name - Name of the table to be removed. + * @param {string} integration - Name of the integration the table belongs to. + * @returns {Promise} - Resolves when the table is successfully removed. + * @throws {MindsDbError} - Something went wrong removing the table. + */ +override async removeTable( + name: string, + integration: string +): Promise { + // Construct the SQL query to drop the table + const sqlQuery = `DROP TABLE ${mysql.escapeId(integration)}.${mysql.escapeId(name)}`; + + // Execute the SQL query using the sqlClient + const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery); + + // Check for errors + if (sqlQueryResult.error_message) { + throw new MindsDbError(sqlQueryResult.error_message); + } +} +