From 33ef776c5bbce14f58336911be82ba2e5cc36e3e Mon Sep 17 00:00:00 2001 From: Bill Nguyen Ton Hoang Date: Mon, 21 Oct 2024 11:38:07 +0100 Subject: [PATCH 1/4] [feature] Inserting data into tables --- src/tables/table.ts | 9 +++++++++ src/tables/tablesApiClient.ts | 9 +++++++++ src/tables/tablesRestApiClient.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/tables/table.ts b/src/tables/table.ts index a4e9c8b..eea4654 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); } + + /** + * Insert data into this table. + * @param {Array> | 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. + */ + async insert(data: Array> | string): Promise { + await this.tablesApiClient.insertTable(this.name, this.integration, data); + } } diff --git a/src/tables/tablesApiClient.ts b/src/tables/tablesApiClient.ts index d82c562..38d91e4 100644 --- a/src/tables/tablesApiClient.ts +++ b/src/tables/tablesApiClient.ts @@ -40,4 +40,13 @@ export default abstract class TablesApiClient { * @throws {MindsDbError} - Something went wrong deleting the table. */ abstract deleteTable(name: string, integration: string): Promise; + + /** + * 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 {Array> | 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. + */ + abstract insertTable(name: string, integration: string, data: Array> | string): Promise; } diff --git a/src/tables/tablesRestApiClient.ts b/src/tables/tablesRestApiClient.ts index 2b83531..8f07477 100644 --- a/src/tables/tablesRestApiClient.ts +++ b/src/tables/tablesRestApiClient.ts @@ -87,4 +87,31 @@ export default class TablesRestApiClient extends TablesApiClient { throw new MindsDbError(sqlQueryResult.error_message); } } + + /** + * Insert data into this table. + * @param {Array> | 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. + */ + async insert(name: string, integration: string, data: Array> | string): Promise { + let sqlQuery = ''; + + if (Array.isArray(data)) { + const valuesClause = data.map( + (row) => `(${row.map((value) => mysql.escape(value)).join(', ')})` + ).join(',\n'); + + sqlQuery = `INSERT INTO ${mysql.escapeId(integration)}.${mysql.escapeId(name)} VALUES ${valuesClause}`; + } else if (typeof data === 'string') { + sqlQuery = `INSERT INTO ${mysql.escapeId(integration)}.${mysql.escapeId(name)} ${data}`; + } else { + throw new MindsDbError('Invalid data type. Expected an array of values or a SELECT query.'); + } + + const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery); + + if (sqlQueryResult.error_message) { + throw new MindsDbError(sqlQueryResult.error_message); + } + } } From 0e748807345457803f86bc758e8468bf9f3aa726 Mon Sep 17 00:00:00 2001 From: Bill Nguyen Ton Hoang Date: Mon, 21 Oct 2024 11:42:59 +0100 Subject: [PATCH 2/4] [fix] changing the function name in TablesRestApiClient --- src/tables/tablesRestApiClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tables/tablesRestApiClient.ts b/src/tables/tablesRestApiClient.ts index 8f07477..bd03c53 100644 --- a/src/tables/tablesRestApiClient.ts +++ b/src/tables/tablesRestApiClient.ts @@ -93,7 +93,7 @@ export default class TablesRestApiClient extends TablesApiClient { * @param {Array> | 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. */ - async insert(name: string, integration: string, data: Array> | string): Promise { + async insertTable(name: string, integration: string, data: Array> | string): Promise { let sqlQuery = ''; if (Array.isArray(data)) { From 05ea940314b671ab0f343ec6130919049732bd9c Mon Sep 17 00:00:00 2001 From: Bill Nguyen Ton Hoang Date: Wed, 23 Oct 2024 02:01:32 +0100 Subject: [PATCH 3/4] [feature] Inserting new items into a table via SQL completed! --- src/tables/table.ts | 6 +++--- src/tables/tablesApiClient.ts | 4 ++-- src/tables/tablesRestApiClient.ts | 30 +++++++++++------------------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/tables/table.ts b/src/tables/table.ts index eea4654..f32e019 100644 --- a/src/tables/table.ts +++ b/src/tables/table.ts @@ -40,10 +40,10 @@ export default class Table { /** * Insert data into this table. - * @param {Array> | string} data - A 2D array of values to insert, or a SELECT query to insert data from. + * @param {string} select - SELECT query to insert data from. * @throws {MindsDbError} - Something went wrong inserting data into the table. */ - async insert(data: Array> | string): Promise { - await this.tablesApiClient.insertTable(this.name, this.integration, data); + async insert(select: string): Promise { + await this.tablesApiClient.insertTable(this.name, this.integration, select); } } diff --git a/src/tables/tablesApiClient.ts b/src/tables/tablesApiClient.ts index 38d91e4..bca025a 100644 --- a/src/tables/tablesApiClient.ts +++ b/src/tables/tablesApiClient.ts @@ -45,8 +45,8 @@ export default abstract class TablesApiClient { * 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 {Array> | string} data - A 2D array of values to insert, or a SELECT query to insert data from. + * @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, data: Array> | string): Promise; + abstract insertTable(name: string, integration: string, select: string): Promise; } diff --git a/src/tables/tablesRestApiClient.ts b/src/tables/tablesRestApiClient.ts index bd03c53..7c94939 100644 --- a/src/tables/tablesRestApiClient.ts +++ b/src/tables/tablesRestApiClient.ts @@ -34,6 +34,8 @@ export default class TablesRestApiClient extends TablesApiClient { const createClause = `CREATE TABLE ${mysql.escapeId( integration )}.${mysql.escapeId(name)}`; + console.log(`Integration name: ${integration}`); + console.log(`Create clause: ${createClause}`); const selectClause = `(${select})`; const sqlQuery = [createClause, selectClause].join('\n'); @@ -93,25 +95,15 @@ export default class TablesRestApiClient extends TablesApiClient { * @param {Array> | 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. */ - async insertTable(name: string, integration: string, data: Array> | string): Promise { - let sqlQuery = ''; - - if (Array.isArray(data)) { - const valuesClause = data.map( - (row) => `(${row.map((value) => mysql.escape(value)).join(', ')})` - ).join(',\n'); - - sqlQuery = `INSERT INTO ${mysql.escapeId(integration)}.${mysql.escapeId(name)} VALUES ${valuesClause}`; - } else if (typeof data === 'string') { - sqlQuery = `INSERT INTO ${mysql.escapeId(integration)}.${mysql.escapeId(name)} ${data}`; - } else { - throw new MindsDbError('Invalid data type. Expected an array of values or a SELECT query.'); - } - - const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery); - - if (sqlQueryResult.error_message) { - throw new MindsDbError(sqlQueryResult.error_message); + async insertTable(name: string, integration: string, select: string): Promise { + 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}`); } } } From 3d6e2882bbd9e5ce5bbd8e8d1b3ac3d5321b9805 Mon Sep 17 00:00:00 2001 From: "Ton Hoang Nguyen (Bill)" <32552798+HahaBill@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:02:54 +0000 Subject: [PATCH 4/4] Update tablesRestApiClient.ts --- src/tables/tablesRestApiClient.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/tables/tablesRestApiClient.ts b/src/tables/tablesRestApiClient.ts index dda9bd7..5a2101e 100644 --- a/src/tables/tablesRestApiClient.ts +++ b/src/tables/tablesRestApiClient.ts @@ -34,8 +34,6 @@ export default class TablesRestApiClient extends TablesApiClient { const createClause = `CREATE TABLE ${mysql.escapeId( integration )}.${mysql.escapeId(name)}`; - console.log(`Integration name: ${integration}`); - console.log(`Create clause: ${createClause}`); const selectClause = `(${select})`; const sqlQuery = [createClause, selectClause].join('\n');