diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index 05bb4f0c..d66de0b4 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -44,4 +44,14 @@ export default (connections) => { return { status: 'error', response: err.toString() }; } }); + + ipcMain.handle('insertTableRows', async (event, params) => { + try { + await Tables.insertTableRows(connections[params.uid], params); + return { status: 'success' }; + } + catch (err) { + return { status: 'error', response: err.toString() }; + } + }); }; diff --git a/src/main/libs/AntaresConnector.js b/src/main/libs/AntaresConnector.js index 87e1b47d..e9d5b901 100644 --- a/src/main/libs/AntaresConnector.js +++ b/src/main/libs/AntaresConnector.js @@ -32,7 +32,7 @@ export class AntaresConnector { limit: [], join: [], update: [], - insert: [], + insert: {}, delete: false }; this._query = Object.assign({}, this._queryDefaults); @@ -108,6 +108,11 @@ export class AntaresConnector { return this; } + into (table) { + this._query.from = table; + return this; + } + delete (table) { this._query.delete = true; this.from(table); @@ -162,6 +167,16 @@ export class AntaresConnector { return this; } + /** + * @param {Object} obj field: value + * @returns + * @memberof AntaresConnector + */ + insert (obj) { + this._query.insert = { ...this._query.insert, ...obj }; + return this; + } + /** * @returns {string} SQL string * @memberof AntaresConnector @@ -188,8 +203,10 @@ export class AntaresConnector { // FROM let fromRaw = ''; - if (!this._query.update.length && !!this._query.from) + if (!this._query.update.length && !Object.keys(this._query.insert).length && !!this._query.from) fromRaw = 'FROM'; + else if (Object.keys(this._query.insert).length) + fromRaw = 'INTO'; switch (this._client) { case 'maria': @@ -209,6 +226,21 @@ export class AntaresConnector { const updateArray = this._query.update.reduce(this._reducer, []); const updateRaw = updateArray.length ? `SET ${updateArray.join(', ')} ` : ''; + let insertRaw = ''; + if (Object.keys(this._query.insert).length) { + const fieldsList = []; + const valueList = []; + const fields = this._query.insert; + + for (const key in fields) { + if (fields[key] === null) continue; + fieldsList.push(key); + valueList.push(typeof fields[key] === 'number' ? fields[key] : `"${fields[key]}"`); + } + + insertRaw = ` (${fieldsList.join(',')}) VALUES (${valueList.join(',')}) `; + } + const groupByArray = this._query.groupBy.reduce(this._reducer, []); const groupByRaw = groupByArray.length ? `GROUP BY ${groupByArray.join(', ')} ` : ''; @@ -229,7 +261,7 @@ export class AntaresConnector { break; } - return `${selectRaw}${updateRaw ? 'UPDATE' : ''}${this._query.delete ? 'DELETE ' : ''}${fromRaw}${updateRaw}${whereRaw}${groupByRaw}${orderByRaw}${limitRaw}`; + return `${selectRaw}${updateRaw ? 'UPDATE' : ''}${insertRaw ? 'INSERT ' : ''}${this._query.delete ? 'DELETE ' : ''}${fromRaw}${updateRaw}${whereRaw}${groupByRaw}${orderByRaw}${limitRaw}${insertRaw}`; } /** diff --git a/src/main/models/InformationSchema.js b/src/main/models/InformationSchema.js index 8bdda7f9..130e95bd 100644 --- a/src/main/models/InformationSchema.js +++ b/src/main/models/InformationSchema.js @@ -31,7 +31,10 @@ export default class { datePrecision: field.DATETIME_PRECISION, charLength: field.CHARACTER_MAXIMUM_LENGTH, isNullable: field.IS_NULLABLE, - default: field.COLUMN_DEFAULT + default: field.COLUMN_DEFAULT, + charset: field.CHARACTER_SET_NAME, + collation: field.COLLATION_NAME, + autoIncrement: field.EXTRA.includes('auto_increment') }; }); } diff --git a/src/main/models/Tables.js b/src/main/models/Tables.js index 89b2ff86..13d7bbe9 100644 --- a/src/main/models/Tables.js +++ b/src/main/models/Tables.js @@ -50,4 +50,14 @@ export default class { .where({ [params.primary]: `IN (${params.rows.join(',')})` }) .run(); } + + static async insertTableRows (connection, params) { // Prepare every field like updateTableCell method + for (let i = 0; i < params.repeat; i++) { + await connection + .schema(params.schema) + .into(params.table) + .insert(params.row) + .run(); + } + } } diff --git a/src/renderer/components/ModalNewTableRow.vue b/src/renderer/components/ModalNewTableRow.vue new file mode 100644 index 00000000..aa6443f7 --- /dev/null +++ b/src/renderer/components/ModalNewTableRow.vue @@ -0,0 +1,278 @@ + + + + + diff --git a/src/renderer/components/WorkspaceTableTab.vue b/src/renderer/components/WorkspaceTableTab.vue index ebf8a47b..da1e593e 100644 --- a/src/renderer/components/WorkspaceTableTab.vue +++ b/src/renderer/components/WorkspaceTableTab.vue @@ -40,19 +40,28 @@ @deleteSelected="deleteSelected" /> +