Skip to content

Commit

Permalink
Merge pull request #95 from Niharika0104/Fixes-58
Browse files Browse the repository at this point in the history
Added update table function
  • Loading branch information
mpacheco12 authored Nov 26, 2024
2 parents 41d4e60 + 6fca4b1 commit 4d06d49
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/tables/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export default class Table {
}

/**
* 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<void> {
await this.tablesApiClient.updateTable(this.name, this.integration,updateQuery);
}
}

* Insert data into this table.
* @param {string} select - SELECT query to insert data from.
* @throws {MindsDbError} - Something went wrong inserting data into the table.
Expand All @@ -72,3 +82,4 @@ export default class Table {
await this.tablesApiClient.insertTable(this.name, this.integration, select);
}
}

16 changes: 16 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ export default abstract class TablesApiClient {
*/
abstract deleteTable(name: string, integration: string): Promise<void>;



/**
* 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<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.
Expand All @@ -65,6 +80,7 @@ export default abstract class TablesApiClient {
* @throws {MindsDbError} - Something went wrong deleting the file.
*/
abstract deleteFile(name: string): Promise<void>;

}

/**
Expand Down
34 changes: 34 additions & 0 deletions src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ export default class TablesRestApiClient extends TablesApiClient {
}
}


/**
* 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<void> {

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);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}


/**
* 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.
Expand Down

0 comments on commit 4d06d49

Please sign in to comment.