Skip to content

Commit c129e80

Browse files
authored
Merge pull request #83 from HahaBill/f/mindsdb-insert-table
[Hacktoberfest] Insert data into a table functionality
2 parents 16d40aa + cf3286f commit c129e80

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/tables/table.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ export default class Table {
4747
async delete(): Promise<void> {
4848
await this.tablesApiClient.deleteTable(this.name, this.integration);
4949
}
50+
51+
/**
52+
* Insert data into this table.
53+
* @param {string} select - SELECT query to insert data from.
54+
* @throws {MindsDbError} - Something went wrong inserting data into the table.
55+
*/
56+
async insert(select: string): Promise<void> {
57+
await this.tablesApiClient.insertTable(this.name, this.integration, select);
58+
}
5059
}

src/tables/tablesApiClient.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ export default abstract class TablesApiClient {
4141
*/
4242
abstract deleteTable(name: string, integration: string): Promise<void>;
4343

44+
/**
45+
* Insert data into this table.
46+
* @param {string} name - Name of the table to be deleted.
47+
* @param {string} integration - Name of the integration the table to be deleted is a part of.
48+
* @param {string} select - SELECT query to insert data from.
49+
* @throws {MindsDbError} - Something went wrong inserting data into the table.
50+
*/
51+
abstract insertTable(name: string, integration: string, select: string): Promise<void>;
4452
/**
4553
* Deletes a file from the files integration.
4654
* @param {string} name - Name of the file to be deleted.

src/tables/tablesRestApiClient.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ export default class TablesRestApiClient extends TablesApiClient {
8888
}
8989
}
9090

91+
/**
92+
* Insert data into this table.
93+
* @param {Array<Array<any>> | string} data - A 2D array of values to insert, or a SELECT query to insert data from.
94+
* @throws {MindsDbError} - Something went wrong inserting data into the table.
95+
*/
96+
override async insertTable(name: string, integration: string, select: string): Promise<void> {
97+
try {
98+
const sqlQuery = `INSERT INTO ${mysql.escapeId(integration)}.${mysql.escapeId(name)} (${select})`;
99+
const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
100+
if (sqlQueryResult.error_message) {
101+
throw new MindsDbError(sqlQueryResult.error_message);
102+
}
103+
} catch(error) {
104+
throw new MindsDbError(`Insert into a table failed: ${error}`);
105+
}
106+
}
107+
91108
/**
92109
* Deletes a file from the files integration.
93110
* @param {string} name - Name of the file to be deleted.

0 commit comments

Comments
 (0)