From b0576acdf65d41c1c8e0b0bca6cf6522dcb372be Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Mon, 8 Feb 2021 11:46:57 +0100 Subject: [PATCH] perf(core): bulk inserts support --- src/main/ipc-handlers/tables.js | 14 +++++++------- src/main/libs/AntaresCore.js | 8 ++++---- src/main/libs/clients/MySQLClient.js | 15 ++++----------- src/renderer/components/ModalNewTableRow.vue | 2 ++ 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index 752c31f8..1d390daa 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -189,13 +189,13 @@ export default (connections) => { insertObj[key] = escapedParam; } - for (let i = 0; i < params.repeat; i++) { - await connections[params.uid] - .schema(params.schema) - .into(params.table) - .insert(insertObj) - .run(); - } + const rows = new Array(+params.repeat).fill(insertObj); + + await connections[params.uid] + .schema(params.schema) + .into(params.table) + .insert(rows) + .run(); return { status: 'success' }; } diff --git a/src/main/libs/AntaresCore.js b/src/main/libs/AntaresCore.js index 4d9b1295..4b50c30e 100644 --- a/src/main/libs/AntaresCore.js +++ b/src/main/libs/AntaresCore.js @@ -28,7 +28,7 @@ export class AntaresCore { limit: [], join: [], update: [], - insert: {}, + insert: [], delete: false }; this._query = Object.assign({}, this._queryDefaults); @@ -120,12 +120,12 @@ export class AntaresCore { } /** - * @param {Object} obj field: value + * @param {Array} arr Array of row objects * @returns * @memberof AntaresCore */ - insert (obj) { - this._query.insert = { ...this._query.insert, ...obj }; + insert (arr) { + this._query.insert = [...this._query.insert, ...arr]; return this; } diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 138a925a..38efb3a8 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -1157,18 +1157,11 @@ export class MySQLClient extends AntaresCore { // INSERT let insertRaw = ''; - if (Object.keys(this._query.insert).length) { - const fieldsList = []; - const valueList = []; - const fields = this._query.insert; + if (this._query.insert.length) { + const fieldsList = Object.keys(this._query.insert[0]); + const rowsList = this._query.insert.map(el => `(${Object.values(el).join(', ')})`); - for (const key in fields) { - if (fields[key] === null) continue; - fieldsList.push(key); - valueList.push(fields[key]); - } - - insertRaw = `(${fieldsList.join(', ')}) VALUES (${valueList.join(', ')}) `; + insertRaw = `(${fieldsList.join(', ')}) VALUES ${rowsList.join(', ')} `; } // GROUP BY diff --git a/src/renderer/components/ModalNewTableRow.vue b/src/renderer/components/ModalNewTableRow.vue index 122c7e20..f30ebe3b 100644 --- a/src/renderer/components/ModalNewTableRow.vue +++ b/src/renderer/components/ModalNewTableRow.vue @@ -157,6 +157,8 @@ export default { nInserts (val) { if (!val || val < 1) this.nInserts = 1; + if (!val || val > 1000) + this.nInserts = 1000; } }, created () {