Skip to content

Commit

Permalink
Merge pull request #67 from sanketshinde3001/four
Browse files Browse the repository at this point in the history
Added a function to remove a table
  • Loading branch information
mpacheco12 authored Nov 18, 2024
2 parents c129e80 + 65fea85 commit df58b93
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/tables/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await this.tablesApiClient.removeTable(this.name, this.integration);
}
/**
* Creates a table in an integration from a given SELECT statement.
* @param {string} select - SELECT statement to use for populating the new table with data.
Expand Down
10 changes: 10 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ export default abstract class TablesApiClient {
*/
abstract deleteFile(name: string): Promise<void>;
}

/**
* 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<void>} - Resolves when the table is successfully removed.
* @throws {MindsDbError} - Something went wrong removing the table.
*/
abstract removeTable(name: string, integration: string): Promise<void>;

24 changes: 24 additions & 0 deletions src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,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<void>} - Resolves when the table is successfully removed.
* @throws {MindsDbError} - Something went wrong removing the table.
*/
override async removeTable(
name: string,
integration: string
): Promise<void> {
// 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);
}
}

0 comments on commit df58b93

Please sign in to comment.