Skip to content

Commit

Permalink
feat: option to insert table rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Aug 12, 2020
1 parent 128a6cd commit 2f1dfdc
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/main/ipc-handlers/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}
});
};
38 changes: 35 additions & 3 deletions src/main/libs/AntaresConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AntaresConnector {
limit: [],
join: [],
update: [],
insert: [],
insert: {},
delete: false
};
this._query = Object.assign({}, this._queryDefaults);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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':
Expand All @@ -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(', ')} ` : '';

Expand All @@ -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}`;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/models/InformationSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
};
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/models/Tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Loading

0 comments on commit 2f1dfdc

Please sign in to comment.