2020-05-14 15:21:57 +02:00
|
|
|
|
|
|
|
import { ipcMain } from 'electron';
|
|
|
|
import knex from 'knex';
|
|
|
|
import InformationSchema from '../models/InformationSchema';
|
2020-06-06 16:27:42 +02:00
|
|
|
import { RawQuery } from '../models/RawQuery';
|
2020-05-14 15:21:57 +02:00
|
|
|
|
|
|
|
const connections = {};
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
ipcMain.handle('testConnection', async (event, conn) => {
|
2020-06-05 21:00:15 +02:00
|
|
|
const connection = knex({
|
|
|
|
client: conn.client,
|
|
|
|
connection: {
|
|
|
|
host: conn.host,
|
|
|
|
port: +conn.port,
|
|
|
|
user: conn.user,
|
|
|
|
password: conn.password
|
|
|
|
},
|
|
|
|
pool: {
|
|
|
|
min: 1,
|
|
|
|
max: 3
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-14 15:21:57 +02:00
|
|
|
try {
|
2020-06-05 21:00:15 +02:00
|
|
|
await InformationSchema.testConnection(connection);
|
2020-05-14 15:21:57 +02:00
|
|
|
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle('checkConnection', async (event, uid) => {
|
|
|
|
return uid in connections;
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle('connect', async (event, conn) => {
|
2020-05-17 19:34:56 +02:00
|
|
|
const connection = knex({
|
2020-05-14 15:21:57 +02:00
|
|
|
client: conn.client,
|
|
|
|
connection: {
|
|
|
|
host: conn.host,
|
|
|
|
port: +conn.port,
|
|
|
|
user: conn.user,
|
|
|
|
password: conn.password
|
|
|
|
},
|
|
|
|
pool: {
|
|
|
|
min: 1,
|
|
|
|
max: 3
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-17 19:34:56 +02:00
|
|
|
try {
|
2020-05-20 18:00:14 +02:00
|
|
|
const structure = await InformationSchema.getStructure(connection);
|
|
|
|
connections[conn.uid] = connection;
|
|
|
|
return { status: 'success', response: structure };
|
2020-05-17 19:34:56 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
2020-05-14 15:21:57 +02:00
|
|
|
});
|
2020-05-18 18:06:32 +02:00
|
|
|
|
|
|
|
ipcMain.handle('disconnect', (event, uid) => {
|
|
|
|
connections[uid].destroy();
|
|
|
|
delete connections[uid];
|
|
|
|
});
|
2020-05-19 18:06:05 +02:00
|
|
|
|
|
|
|
ipcMain.handle('refresh', async (event, uid) => {
|
|
|
|
try {
|
2020-05-20 18:00:14 +02:00
|
|
|
const structure = await InformationSchema.getStructure(connections[uid]);
|
|
|
|
return { status: 'success', response: structure };
|
2020-05-19 18:06:05 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
2020-06-06 16:27:42 +02:00
|
|
|
|
|
|
|
ipcMain.on('runQuery', async (event, { connection, query, database }) => {
|
|
|
|
const knexIstance = connections[connection.uid];
|
|
|
|
const Query = new RawQuery({ knexIstance, database });
|
|
|
|
try {
|
|
|
|
Query.runQuery(query);
|
|
|
|
|
|
|
|
Query.on('row', row => {
|
|
|
|
event.sender.send('row', row);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
});
|
2020-05-14 15:21:57 +02:00
|
|
|
};
|