From c45fedad8f3cbc43aaaf7d7bcc0bfdc0cb231f17 Mon Sep 17 00:00:00 2001 From: Niharika Goulikar Date: Thu, 24 Oct 2024 09:07:49 +0000 Subject: [PATCH] Added update table function --- src/tables/table.ts | 11 +++++++++- src/tables/tablesApiClient.ts | 14 +++++++++++++ src/tables/tablesRestApiClient.ts | 34 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/tables/table.ts b/src/tables/table.ts index a4e9c8b..8c52435 100644 --- a/src/tables/table.ts +++ b/src/tables/table.ts @@ -37,4 +37,13 @@ export default class Table { async delete(): Promise { await this.tablesApiClient.deleteTable(this.name, this.integration); } -} + + /** + * Updates a table from its integration. + * @param {string} updateQuery - The SQL UPDATE query to run for updating the table. + * @throws {MindsDbError} - Something went wrong deleting the table. + */ + async update(updateQuery: string): Promise { + await this.tablesApiClient.updateTable(this.name, this.integration,updateQuery); + } +} \ No newline at end of file diff --git a/src/tables/tablesApiClient.ts b/src/tables/tablesApiClient.ts index d82c562..a508b19 100644 --- a/src/tables/tablesApiClient.ts +++ b/src/tables/tablesApiClient.ts @@ -40,4 +40,18 @@ export default abstract class TablesApiClient { * @throws {MindsDbError} - Something went wrong deleting the table. */ abstract deleteTable(name: string, integration: string): Promise; + + + /** + * Updates a table from its integration. + * @param {string} name - Name of the table to be updated. + * @param {string} integration - Name of the integration the table to be updated is a part of. + * @param {string} updateQuery - The SQL UPDATE query to run for updating the table. + * @throws {MindsDbError} - Something went wrong deleting the table. + */ + abstract updateTable( + name: string, + integration: string, + updateQuery: string + ): Promise; } diff --git a/src/tables/tablesRestApiClient.ts b/src/tables/tablesRestApiClient.ts index 2b83531..2bdb496 100644 --- a/src/tables/tablesRestApiClient.ts +++ b/src/tables/tablesRestApiClient.ts @@ -87,4 +87,38 @@ export default class TablesRestApiClient extends TablesApiClient { throw new MindsDbError(sqlQueryResult.error_message); } } + + /** + * Updates a table from its integration. + * @param {string} name - Name of the table to be updated. + * @param {string} integration - Name of the integration the table to be updated is a part of. + * @param {string} updateQuery - The SQL UPDATE query to run for updating the table. + * @throws {MindsDbError} - Something went wrong deleting the table. + */ + override async updateTable( + name: string, + integration: string, + updateQuery: string + ): Promise { + + let keyword="SET"; + let setPosition = updateQuery.toUpperCase().indexOf(keyword); + let result; + + if (setPosition !== -1) { + // Extract the substring starting just after "SET" + result = updateQuery.substring(setPosition + keyword.length).trim(); + } + + // Construct the full SQL query to update the table + const sqlQuery = `UPDATE ${mysql.escapeId(integration)}.${mysql.escapeId(name)} SET ${result}`; + + // 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); + } + } }