2022-04-15 14:56:13 +02:00
|
|
|
import * as antares from 'common/interfaces/antares';
|
|
|
|
import { InsertRowsParams } from 'common/interfaces/tableApis';
|
2020-06-15 18:23:51 +02:00
|
|
|
import { ipcMain } from 'electron';
|
2022-04-15 14:56:13 +02:00
|
|
|
import { faker } from '@faker-js/faker';
|
2021-02-13 18:45:16 +01:00
|
|
|
import moment from 'moment';
|
2020-09-24 13:09:01 +02:00
|
|
|
import { sqlEscaper } from 'common/libs/sqlEscaper';
|
2021-03-17 18:06:17 +01:00
|
|
|
import { TEXT, LONG_TEXT, ARRAY, TEXT_SEARCH, NUMBER, FLOAT, BLOB, BIT, DATE, DATETIME } from 'common/fieldTypes';
|
2022-02-16 09:14:46 +01:00
|
|
|
import * as customizations from 'common/customizations';
|
2022-05-10 13:22:26 +02:00
|
|
|
import * as fs from 'fs';
|
2020-06-15 18:23:51 +02:00
|
|
|
|
2022-04-15 14:56:13 +02:00
|
|
|
export default (connections: {[key: string]: antares.Client}) => {
|
2020-10-16 17:26:47 +02:00
|
|
|
ipcMain.handle('get-table-columns', async (event, params) => {
|
2020-06-15 18:23:51 +02:00
|
|
|
try {
|
2020-10-16 17:26:47 +02:00
|
|
|
const result = await connections[params.uid].getTableColumns(params);
|
2020-06-15 18:23:51 +02:00
|
|
|
return { status: 'success', response: result };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-17 23:54:00 +02:00
|
|
|
ipcMain.handle('get-table-data', async (event, { uid, schema, table, limit, page, sortParams, where }) => {
|
2020-06-15 18:23:51 +02:00
|
|
|
try {
|
2021-05-27 22:13:59 +02:00
|
|
|
const offset = (page - 1) * limit;
|
2020-12-07 17:51:48 +01:00
|
|
|
const query = connections[uid]
|
2020-09-24 13:09:01 +02:00
|
|
|
.select('*')
|
|
|
|
.schema(schema)
|
|
|
|
.from(table)
|
2021-05-27 22:13:59 +02:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset);
|
2020-12-07 17:51:48 +01:00
|
|
|
|
|
|
|
if (sortParams && sortParams.field && sortParams.dir)
|
|
|
|
query.orderBy({ [sortParams.field]: sortParams.dir.toUpperCase() });
|
|
|
|
|
2021-10-17 23:54:00 +02:00
|
|
|
if (where)
|
|
|
|
query.where(where);
|
|
|
|
|
2021-07-13 19:23:02 +02:00
|
|
|
const result = await query.run({ details: true, schema });
|
2020-09-24 13:09:01 +02:00
|
|
|
|
2020-06-15 18:23:51 +02:00
|
|
|
return { status: 'success', response: result };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
2020-08-14 18:07:29 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-04 15:52:26 +02:00
|
|
|
ipcMain.handle('get-table-count', async (event, params) => {
|
|
|
|
try {
|
|
|
|
const result = await connections[params.uid].getTableApproximateCount(params);
|
|
|
|
return { status: 'success', response: result };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-11 16:16:58 +02:00
|
|
|
ipcMain.handle('get-table-options', async (event, params) => {
|
|
|
|
try {
|
|
|
|
const result = await connections[params.uid].getTableOptions(params);
|
|
|
|
return { status: 'success', response: result };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-20 17:24:02 +01:00
|
|
|
ipcMain.handle('get-table-indexes', async (event, params) => {
|
|
|
|
try {
|
|
|
|
const result = await connections[params.uid].getTableIndexes(params);
|
|
|
|
|
|
|
|
return { status: 'success', response: result };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-16 17:26:47 +02:00
|
|
|
ipcMain.handle('get-key-usage', async (event, params) => {
|
2020-08-14 18:07:29 +02:00
|
|
|
try {
|
2020-10-16 17:26:47 +02:00
|
|
|
const result = await connections[params.uid].getKeyUsage(params);
|
2020-09-24 13:09:01 +02:00
|
|
|
|
2020-08-14 18:07:29 +02:00
|
|
|
return { status: 'success', response: result };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
2020-06-15 18:23:51 +02:00
|
|
|
}
|
|
|
|
});
|
2020-06-27 15:14:08 +02:00
|
|
|
|
2020-08-18 18:03:59 +02:00
|
|
|
ipcMain.handle('update-table-cell', async (event, params) => {
|
2022-02-16 09:14:46 +01:00
|
|
|
delete params.row._antares_id;
|
|
|
|
const { stringsWrapper: sw } = customizations[connections[params.uid]._client];
|
2021-06-02 11:58:34 +02:00
|
|
|
|
2021-03-18 11:09:50 +01:00
|
|
|
try { // TODO: move to client classes
|
2020-09-24 13:09:01 +02:00
|
|
|
let escapedParam;
|
|
|
|
let reload = false;
|
2022-02-16 09:14:46 +01:00
|
|
|
const id = typeof params.id === 'number' ? params.id : `${sw}${params.id}${sw}`;
|
2020-09-24 13:09:01 +02:00
|
|
|
|
2021-02-10 18:24:28 +01:00
|
|
|
if ([...NUMBER, ...FLOAT].includes(params.type))
|
2020-09-24 13:09:01 +02:00
|
|
|
escapedParam = params.content;
|
2021-03-18 12:30:06 +01:00
|
|
|
else if ([...TEXT, ...LONG_TEXT].includes(params.type)) {
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
escapedParam = `"${sqlEscaper(params.content)}"`;
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
escapedParam = `'${params.content.replaceAll('\'', '\'\'')}'`;
|
|
|
|
break;
|
2022-02-16 09:14:46 +01:00
|
|
|
case 'sqlite':
|
|
|
|
escapedParam = `'${params.content.replaceAll('\'', '\'\'')}'`;
|
|
|
|
break;
|
2021-03-18 12:30:06 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-17 18:06:17 +01:00
|
|
|
else if (ARRAY.includes(params.type))
|
|
|
|
escapedParam = `'${params.content}'`;
|
|
|
|
else if (TEXT_SEARCH.includes(params.type))
|
|
|
|
escapedParam = `'${params.content.replaceAll('\'', '\'\'')}'`;
|
2020-09-24 13:09:01 +02:00
|
|
|
else if (BLOB.includes(params.type)) {
|
|
|
|
if (params.content) {
|
2021-03-18 11:09:50 +01:00
|
|
|
let fileBlob;
|
|
|
|
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
fileBlob = fs.readFileSync(params.content);
|
|
|
|
escapedParam = `0x${fileBlob.toString('hex')}`;
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
fileBlob = fs.readFileSync(params.content);
|
|
|
|
escapedParam = `decode('${fileBlob.toString('hex')}', 'hex')`;
|
|
|
|
break;
|
2022-02-16 09:14:46 +01:00
|
|
|
case 'sqlite':
|
|
|
|
fileBlob = fs.readFileSync(params.content);
|
|
|
|
escapedParam = `X'${fileBlob.toString('hex')}'`;
|
|
|
|
break;
|
2021-03-18 11:09:50 +01:00
|
|
|
}
|
2020-09-24 13:09:01 +02:00
|
|
|
reload = true;
|
|
|
|
}
|
2021-03-18 11:09:50 +01:00
|
|
|
else {
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
2021-03-18 12:30:06 +01:00
|
|
|
escapedParam = '\'\'';
|
2021-03-18 11:09:50 +01:00
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
escapedParam = 'decode(\'\', \'hex\')';
|
|
|
|
break;
|
2022-02-16 09:14:46 +01:00
|
|
|
case 'sqlite':
|
|
|
|
escapedParam = 'X\'\'';
|
|
|
|
break;
|
2021-03-18 11:09:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-24 13:09:01 +02:00
|
|
|
}
|
2022-02-19 12:40:54 +01:00
|
|
|
else if (BIT.includes(params.type)) {
|
2021-02-04 09:20:52 +01:00
|
|
|
escapedParam = `b'${sqlEscaper(params.content)}'`;
|
|
|
|
reload = true;
|
|
|
|
}
|
2021-02-28 21:45:38 +01:00
|
|
|
else if (params.content === null)
|
|
|
|
escapedParam = 'NULL';
|
2020-09-24 13:09:01 +02:00
|
|
|
else
|
2021-03-18 12:30:06 +01:00
|
|
|
escapedParam = `'${sqlEscaper(params.content)}'`;
|
2020-09-24 13:09:01 +02:00
|
|
|
|
2021-03-21 13:00:27 +01:00
|
|
|
if (params.primary) { // TODO: handle multiple primary
|
2021-01-30 14:58:12 +01:00
|
|
|
await connections[params.uid]
|
|
|
|
.update({ [params.field]: `= ${escapedParam}` })
|
|
|
|
.schema(params.schema)
|
|
|
|
.from(params.table)
|
|
|
|
.where({ [params.primary]: `= ${id}` })
|
2021-03-21 13:00:27 +01:00
|
|
|
.limit(1)
|
2021-01-30 14:58:12 +01:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
else {
|
2021-03-21 13:00:27 +01:00
|
|
|
const { orgRow } = params;
|
2021-01-30 14:58:12 +01:00
|
|
|
reload = true;
|
|
|
|
|
2021-03-21 13:00:27 +01:00
|
|
|
for (const key in orgRow) {
|
|
|
|
if (typeof orgRow[key] === 'string')
|
|
|
|
orgRow[key] = `'${orgRow[key]}'`;
|
2021-01-30 14:58:12 +01:00
|
|
|
|
2021-03-21 13:00:27 +01:00
|
|
|
orgRow[key] = `= ${orgRow[key]}`;
|
2021-01-30 14:58:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await connections[params.uid]
|
|
|
|
.schema(params.schema)
|
|
|
|
.update({ [params.field]: `= ${escapedParam}` })
|
|
|
|
.from(params.table)
|
2021-03-21 13:00:27 +01:00
|
|
|
.where(orgRow)
|
2021-01-30 14:58:12 +01:00
|
|
|
.limit(1)
|
|
|
|
.run();
|
|
|
|
}
|
2020-09-24 13:09:01 +02:00
|
|
|
|
|
|
|
return { status: 'success', response: { reload } };
|
2020-06-27 15:14:08 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
2020-07-23 19:10:14 +02:00
|
|
|
|
2021-01-30 14:58:12 +01:00
|
|
|
ipcMain.handle('delete-table-rows', async (event, params) => {
|
2021-01-28 18:33:29 +01:00
|
|
|
if (params.primary) {
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const idString = params.rows.map((row: {[key: string]: any}) => {
|
2021-04-08 21:49:38 +02:00
|
|
|
const fieldName = Object.keys(row)[0].includes('.') ? `${params.table}.${params.primary}` : params.primary;
|
|
|
|
|
|
|
|
return typeof row[fieldName] === 'string'
|
2022-02-16 09:14:46 +01:00
|
|
|
? `'${row[fieldName]}'`
|
2021-04-08 21:49:38 +02:00
|
|
|
: row[fieldName];
|
|
|
|
}).join(',');
|
2021-01-28 18:33:29 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await connections[params.uid]
|
|
|
|
.schema(params.schema)
|
|
|
|
.delete(params.table)
|
|
|
|
.where({ [params.primary]: `IN (${idString})` })
|
|
|
|
.run();
|
|
|
|
|
|
|
|
return { status: 'success', response: result };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
2020-07-23 19:10:14 +02:00
|
|
|
}
|
2021-01-28 18:33:29 +01:00
|
|
|
else {
|
|
|
|
try {
|
|
|
|
for (const row of params.rows) {
|
|
|
|
for (const key in row) {
|
|
|
|
if (typeof row[key] === 'string')
|
|
|
|
row[key] = `'${row[key]}'`;
|
|
|
|
|
|
|
|
row[key] = `= ${row[key]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
await connections[params.uid]
|
|
|
|
.schema(params.schema)
|
|
|
|
.delete(params.table)
|
|
|
|
.where(row)
|
|
|
|
.limit(1)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
return { status: 'success', response: [] };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
2020-07-23 19:10:14 +02:00
|
|
|
}
|
|
|
|
});
|
2020-08-12 18:12:30 +02:00
|
|
|
|
2020-08-18 18:03:59 +02:00
|
|
|
ipcMain.handle('insert-table-rows', async (event, params) => {
|
2021-03-18 11:09:50 +01:00
|
|
|
try { // TODO: move to client classes
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const insertObj: {[key: string]: any} = {};
|
2020-09-24 13:09:01 +02:00
|
|
|
for (const key in params.row) {
|
|
|
|
const type = params.fields[key];
|
|
|
|
let escapedParam;
|
|
|
|
|
|
|
|
if (params.row[key] === null)
|
|
|
|
escapedParam = 'NULL';
|
2021-02-10 18:24:28 +01:00
|
|
|
else if ([...NUMBER, ...FLOAT].includes(type))
|
2021-03-16 18:42:03 +01:00
|
|
|
escapedParam = +params.row[key];
|
2021-03-18 12:30:06 +01:00
|
|
|
else if ([...TEXT, ...LONG_TEXT].includes(type)) {
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
escapedParam = `"${sqlEscaper(params.row[key].value)}"`;
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
escapedParam = `'${params.row[key].value.replaceAll('\'', '\'\'')}'`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 13:09:01 +02:00
|
|
|
else if (BLOB.includes(type)) {
|
2021-03-18 11:09:50 +01:00
|
|
|
if (params.row[key].value) {
|
|
|
|
let fileBlob;
|
|
|
|
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
fileBlob = fs.readFileSync(params.row[key].value);
|
|
|
|
escapedParam = `0x${fileBlob.toString('hex')}`;
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
fileBlob = fs.readFileSync(params.row[key].value);
|
|
|
|
escapedParam = `decode('${fileBlob.toString('hex')}', 'hex')`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
escapedParam = '""';
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
escapedParam = 'decode(\'\', \'hex\')';
|
|
|
|
break;
|
|
|
|
}
|
2020-09-24 13:09:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
insertObj[key] = escapedParam;
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:46:57 +01:00
|
|
|
const rows = new Array(+params.repeat).fill(insertObj);
|
|
|
|
|
|
|
|
await connections[params.uid]
|
|
|
|
.schema(params.schema)
|
|
|
|
.into(params.table)
|
|
|
|
.insert(rows)
|
|
|
|
.run();
|
2020-09-24 13:09:01 +02:00
|
|
|
|
2020-08-12 18:12:30 +02:00
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
2020-08-17 15:10:19 +02:00
|
|
|
|
2022-04-15 14:56:13 +02:00
|
|
|
ipcMain.handle('insert-table-fake-rows', async (event, params: InsertRowsParams) => {
|
2021-03-18 11:09:50 +01:00
|
|
|
try { // TODO: move to client classes
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const rows: {[key: string]: any}[] = [];
|
2021-02-13 18:45:16 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < +params.repeat; i++) {
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const insertObj: {[key: string]: any} = {};
|
2021-02-13 18:45:16 +01:00
|
|
|
|
|
|
|
for (const key in params.row) {
|
|
|
|
const type = params.fields[key];
|
|
|
|
let escapedParam;
|
|
|
|
|
|
|
|
if (!('group' in params.row[key]) || params.row[key].group === 'manual') { // Manual value
|
2021-02-16 19:13:20 +01:00
|
|
|
if (params.row[key].value === null || params.row[key].value === undefined)
|
2021-02-13 18:45:16 +01:00
|
|
|
escapedParam = 'NULL';
|
|
|
|
else if ([...NUMBER, ...FLOAT].includes(type))
|
|
|
|
escapedParam = params.row[key].value;
|
2021-03-18 12:30:06 +01:00
|
|
|
else if ([...TEXT, ...LONG_TEXT].includes(type)) {
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
escapedParam = `"${sqlEscaper(params.row[key].value)}"`;
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
escapedParam = `'${params.row[key].value.replaceAll('\'', '\'\'')}'`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-13 18:45:16 +01:00
|
|
|
else if (BLOB.includes(type)) {
|
|
|
|
if (params.row[key].value) {
|
2021-03-18 11:09:50 +01:00
|
|
|
let fileBlob;
|
|
|
|
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
fileBlob = fs.readFileSync(params.row[key].value);
|
|
|
|
escapedParam = `0x${fileBlob.toString('hex')}`;
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
fileBlob = fs.readFileSync(params.row[key].value);
|
|
|
|
escapedParam = `decode('${fileBlob.toString('hex')}', 'hex')`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (connections[params.uid]._client) {
|
|
|
|
case 'mysql':
|
|
|
|
case 'maria':
|
|
|
|
escapedParam = '""';
|
|
|
|
break;
|
|
|
|
case 'pg':
|
|
|
|
escapedParam = 'decode(\'\', \'hex\')';
|
|
|
|
break;
|
|
|
|
}
|
2021-02-13 18:45:16 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-18 15:26:17 +01:00
|
|
|
else if (BIT.includes(type))
|
|
|
|
escapedParam = `b'${sqlEscaper(params.row[key].value)}'`;
|
2021-02-13 18:45:16 +01:00
|
|
|
else
|
2021-03-16 18:42:03 +01:00
|
|
|
escapedParam = `'${sqlEscaper(params.row[key].value)}'`;
|
2021-02-13 18:45:16 +01:00
|
|
|
|
|
|
|
insertObj[key] = escapedParam;
|
|
|
|
}
|
|
|
|
else { // Faker value
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const parsedParams: {[key: string]: any} = {};
|
2021-02-17 18:49:02 +01:00
|
|
|
let fakeValue;
|
2021-02-13 18:45:16 +01:00
|
|
|
|
2021-02-17 18:49:02 +01:00
|
|
|
if (params.locale)
|
|
|
|
faker.locale = params.locale;
|
|
|
|
|
|
|
|
if (Object.keys(params.row[key].params).length) {
|
|
|
|
Object.keys(params.row[key].params).forEach(param => {
|
|
|
|
if (!isNaN(params.row[key].params[param]))
|
|
|
|
parsedParams[param] = +params.row[key].params[param];
|
|
|
|
});
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
fakeValue = (faker as any)[params.row[key].group][params.row[key].method](parsedParams);
|
2021-02-17 18:49:02 +01:00
|
|
|
}
|
|
|
|
else
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
fakeValue = (faker as any)[params.row[key].group][params.row[key].method]();
|
2021-02-16 19:13:20 +01:00
|
|
|
|
|
|
|
if (typeof fakeValue === 'string') {
|
2021-02-14 18:25:57 +01:00
|
|
|
if (params.row[key].length)
|
|
|
|
fakeValue = fakeValue.substr(0, params.row[key].length);
|
2021-03-16 18:42:03 +01:00
|
|
|
fakeValue = `'${sqlEscaper(fakeValue)}'`;
|
2021-02-14 18:25:57 +01:00
|
|
|
}
|
2021-02-13 18:45:16 +01:00
|
|
|
else if ([...DATE, ...DATETIME].includes(type))
|
2021-03-16 18:42:03 +01:00
|
|
|
fakeValue = `'${moment(fakeValue).format('YYYY-MM-DD HH:mm:ss.SSSSSS')}'`;
|
2021-02-13 18:45:16 +01:00
|
|
|
|
|
|
|
insertObj[key] = fakeValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rows.push(insertObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
await connections[params.uid]
|
|
|
|
.schema(params.schema)
|
|
|
|
.into(params.table)
|
|
|
|
.insert(rows)
|
|
|
|
.run();
|
|
|
|
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-23 16:21:36 +02:00
|
|
|
ipcMain.handle('get-foreign-list', async (event, { uid, schema, table, column, description }) => {
|
2020-08-17 15:10:19 +02:00
|
|
|
try {
|
2020-10-16 17:26:47 +02:00
|
|
|
const query = connections[uid]
|
2021-03-21 11:51:22 +01:00
|
|
|
.select(`${column} AS foreign_column`)
|
2020-10-16 17:26:47 +02:00
|
|
|
.schema(schema)
|
|
|
|
.from(table)
|
2021-03-21 11:51:22 +01:00
|
|
|
.orderBy('foreign_column ASC');
|
2020-09-24 13:09:01 +02:00
|
|
|
|
2020-10-16 17:26:47 +02:00
|
|
|
if (description)
|
2021-03-21 11:51:22 +01:00
|
|
|
query.select(`LEFT(${description}, 20) AS foreign_description`);
|
2020-09-24 13:09:01 +02:00
|
|
|
|
|
|
|
const results = await query.run();
|
|
|
|
|
2020-08-17 15:10:19 +02:00
|
|
|
return { status: 'success', response: results };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
2020-11-13 12:39:40 +01:00
|
|
|
|
2020-12-03 13:00:54 +01:00
|
|
|
ipcMain.handle('create-table', async (event, params) => {
|
|
|
|
try {
|
|
|
|
await connections[params.uid].createTable(params);
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-13 12:39:40 +01:00
|
|
|
ipcMain.handle('alter-table', async (event, params) => {
|
|
|
|
try {
|
|
|
|
await connections[params.uid].alterTable(params);
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
2020-12-03 16:15:10 +01:00
|
|
|
|
2021-07-03 12:27:50 +02:00
|
|
|
ipcMain.handle('duplicate-table', async (event, params) => {
|
|
|
|
try {
|
|
|
|
await connections[params.uid].duplicateTable(params);
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-03 16:15:10 +01:00
|
|
|
ipcMain.handle('truncate-table', async (event, params) => {
|
|
|
|
try {
|
|
|
|
await connections[params.uid].truncateTable(params);
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle('drop-table', async (event, params) => {
|
|
|
|
try {
|
|
|
|
await connections[params.uid].dropTable(params);
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
2020-06-15 18:23:51 +02:00
|
|
|
};
|