Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added deleteFromTable function #79

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/tables/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ export default class Table {
async delete(): Promise<void> {
await this.tablesApiClient.deleteTable(this.name, this.integration);
}

/**
* Deletes specific row (or multiple rows) from the table present in the given integration.
* @param {string} select - select statement to specify which rows should be deleted.
* @throws {MindsDbError} - Something went wrong deleting the data from the table.
*/
async deleteFromTable(select?:string):Promise<void>{
await this.tablesApiClient.deleteFromTable(this.name,this.integration,select);
}
}
11 changes: 11 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ export default abstract class TablesApiClient {
abstract deleteTable(name: string, integration: string): Promise<void>;

/**

* Deletes specific row (or multiple rows) from the table present in the given integration.
* @param {string} name - Name of the table from which data is to be deleted.
* @param {string} integration - Name of the integration the table is a part of.
* @param {string} select - select statement to specify which rows should be deleted.
* @throws {MindsDbError} - Something went wrong deleting the data from the table.
*/
abstract deleteFromTable(name: string, integration: string, select?: string): Promise<void>;

/*
* Deletes a file from the files integration.
* @param {string} name - Name of the file to be deleted.
* @throws {MindsDbError} - Something went wrong deleting the file.
*/
abstract deleteFile(name: 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 @@ -89,13 +89,37 @@ export default class TablesRestApiClient extends TablesApiClient {
}

/**

* Deletes specific row (or multiple rows) from the table present in the given integration.
* @param {string} name - Name of the table from which data is to be deleted.
* @param {string} integration - Name of the integration the table is a part of.
* @param {string} select - select statement to specify which rows should be deleted.
* @throws {MindsDbError} - Something went wrong deleting the data from the table.
*/
override async deleteFromTable(name: string, integration: string,select?:string): Promise<void> {

/**
If select parameter is not passed then entire data from the table is deleted.
*/
const sqlQuery = select ?? `DELETE FROM TABLE ${mysql.escapeId(
integration
)}.${mysql.escapeId(name)}`;
const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}


/*
* Deletes a file from the files integration.
* @param {string} name - Name of the file to be deleted.
* @throws {MindsDbError} - Something went wrong deleting the file.
*/
override async deleteFile(name: string): Promise<void> {
const sqlQuery = `DROP TABLE files.${mysql.escapeId(name)}`;


const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
Expand Down