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 all 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 @@ -54,6 +54,15 @@ export default class Table {
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);
}

/**
* Insert data into this table.
* @param {string} select - SELECT query to insert data from.
Expand Down
10 changes: 10 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@ 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>;

/*
* Insert data into this table.
* @param {string} name - Name of the table to be deleted.
* @param {string} integration - Name of the integration the table to be deleted is a part of.
* @param {string} select - SELECT query to insert data from.
* @throws {MindsDbError} - Something went wrong inserting data into the table.
*/
abstract insertTable(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.
Expand Down
22 changes: 22 additions & 0 deletions src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ 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);
}
}


/*
* Insert data into this table.
* @param {Array<Array<any>> | string} data - A 2D array of values to insert, or a SELECT query to insert data from.
* @throws {MindsDbError} - Something went wrong inserting data into the table.
Expand All @@ -113,6 +134,7 @@ export default class TablesRestApiClient extends TablesApiClient {
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
Loading