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

[Hacktoberfest] Insert data into a table functionality #83

Merged
merged 7 commits into from
Nov 18, 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 @@ -47,4 +47,13 @@ export default class Table {
async delete(): Promise<void> {
await this.tablesApiClient.deleteTable(this.name, this.integration);
}

/**
* Insert data into this table.
* @param {string} select - SELECT query to insert data from.
* @throws {MindsDbError} - Something went wrong inserting data into the table.
*/
async insert(select: string): Promise<void> {
await this.tablesApiClient.insertTable(this.name, this.integration, select);
}
}
8 changes: 8 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export default abstract class TablesApiClient {
*/
abstract deleteTable(name: string, integration: 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
17 changes: 17 additions & 0 deletions src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ export default class TablesRestApiClient extends TablesApiClient {
}
}

/**
* 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.
*/
override async insertTable(name: string, integration: string, select: string): Promise<void> {
try {
const sqlQuery = `INSERT INTO ${mysql.escapeId(integration)}.${mysql.escapeId(name)} (${select})`;
const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
} catch(error) {
throw new MindsDbError(`Insert into a table failed: ${error}`);
}
}

/**
* Deletes a file from the files integration.
* @param {string} name - Name of the file to be deleted.
Expand Down