mirror of
https://github.com/Fabio286/antares.git
synced 2025-03-06 20:37:38 +01:00
feat(PostgreSQL): ability to cancel queries
This commit is contained in:
parent
3d56ec7b49
commit
0c002918eb
@ -1,7 +1,6 @@
|
||||
{
|
||||
"extends": [
|
||||
"stylelint-config-standard",
|
||||
"stylelint-config-standard-scss"
|
||||
"stylelint-config-standard"
|
||||
],
|
||||
"fix": true,
|
||||
"formatter": "verbose",
|
||||
|
@ -10,6 +10,7 @@ module.exports = {
|
||||
database: true,
|
||||
sslConnection: true,
|
||||
sshConnection: true,
|
||||
cancelQueries: true,
|
||||
// Tools
|
||||
processesList: true,
|
||||
// Structure
|
||||
|
@ -21,6 +21,7 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
super(args);
|
||||
|
||||
this._schema = null;
|
||||
this._runningConnections = new Map();
|
||||
|
||||
this.types = {};
|
||||
for (const key in types.builtins)
|
||||
@ -1088,10 +1089,26 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} id
|
||||
* @returns {Promise<null>}
|
||||
*/
|
||||
async killProcess (id) {
|
||||
return await this.raw(`SELECT pg_terminate_backend(${id})`);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} tabUid
|
||||
* @returns {Promise<null>}
|
||||
*/
|
||||
async killTabQuery (tabUid) {
|
||||
const id = this._runningConnections.get(tabUid);
|
||||
if (id)
|
||||
return await this.raw(`SELECT pg_cancel_backend(${id})`);
|
||||
}
|
||||
|
||||
/**
|
||||
* CREATE TABLE
|
||||
*
|
||||
@ -1396,6 +1413,8 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
* @memberof PostgreSQLClient
|
||||
*/
|
||||
async raw (sql, args) {
|
||||
if (process.env.NODE_ENV === 'development') this._logger(sql);// TODO: replace BLOB content with a placeholder
|
||||
|
||||
args = {
|
||||
nest: false,
|
||||
details: false,
|
||||
@ -1404,9 +1423,6 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
...args
|
||||
};
|
||||
|
||||
if (args.schema && args.schema !== 'public')
|
||||
await this.use(args.schema);
|
||||
|
||||
if (!args.comments)
|
||||
sql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '');// Remove comments
|
||||
|
||||
@ -1418,7 +1434,14 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
.map(q => q.trim())
|
||||
: [sql];
|
||||
|
||||
if (process.env.NODE_ENV === 'development') this._logger(sql);// TODO: replace BLOB content with a placeholder
|
||||
const isPool = this._connection instanceof Pool;
|
||||
const connection = isPool ? await this._connection.connect() : this._connection;
|
||||
|
||||
if (args.tabUid && isPool)
|
||||
this._runningConnections.set(args.tabUid, connection.processID);
|
||||
|
||||
if (args.schema && args.schema !== 'public')
|
||||
await this.use(args.schema);
|
||||
|
||||
for (const query of queries) {
|
||||
if (!query) continue;
|
||||
@ -1428,15 +1451,12 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
let keysArr = [];
|
||||
|
||||
const { rows, report, fields, keys, duration } = await new Promise((resolve, reject) => {
|
||||
this._connection.query({
|
||||
rowMode: args.nest ? 'array' : null,
|
||||
text: query
|
||||
}, async (err, res) => {
|
||||
timeStop = new Date();
|
||||
(async () => {
|
||||
try {
|
||||
const res = await connection.query({ rowMode: args.nest ? 'array' : null, text: query });
|
||||
|
||||
timeStop = new Date();
|
||||
|
||||
if (err)
|
||||
reject(err);
|
||||
else {
|
||||
let ast;
|
||||
|
||||
try {
|
||||
@ -1525,6 +1545,10 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
if (isPool) {
|
||||
connection.release();
|
||||
this._runningConnections.delete(args.tabUid);
|
||||
}
|
||||
reject(err);
|
||||
}
|
||||
|
||||
@ -1533,6 +1557,10 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
keysArr = keysArr ? [...keysArr, ...response] : response;
|
||||
}
|
||||
catch (err) {
|
||||
if (isPool) {
|
||||
connection.release();
|
||||
this._runningConnections.delete(args.tabUid);
|
||||
}
|
||||
reject(err);
|
||||
}
|
||||
}
|
||||
@ -1547,12 +1575,24 @@ export class PostgreSQLClient extends AntaresCore {
|
||||
keys: keysArr
|
||||
});
|
||||
}
|
||||
});
|
||||
catch (err) {
|
||||
if (isPool) {
|
||||
connection.release();
|
||||
this._runningConnections.delete(args.tabUid);
|
||||
}
|
||||
reject(err);
|
||||
}
|
||||
})();
|
||||
});
|
||||
|
||||
resultsArr.push({ rows, report, fields, keys, duration });
|
||||
}
|
||||
|
||||
if (isPool) {
|
||||
connection.release();
|
||||
this._runningConnections.delete(args.tabUid);
|
||||
}
|
||||
|
||||
return resultsArr.length === 1 ? resultsArr[0] : resultsArr;
|
||||
}
|
||||
}
|
||||
|
@ -200,7 +200,7 @@ export default {
|
||||
}
|
||||
|
||||
&::before {
|
||||
content: '';
|
||||
content: "";
|
||||
height: 0;
|
||||
width: 3px;
|
||||
transition: height 0.2s;
|
||||
|
@ -452,9 +452,9 @@ export default {
|
||||
top: 0;
|
||||
z-index: 2;
|
||||
|
||||
.schema-size{
|
||||
visibility: hidden;
|
||||
width: 22.5px;
|
||||
.schema-size {
|
||||
visibility: hidden;
|
||||
width: 22.5px;
|
||||
}
|
||||
}
|
||||
|
||||
@ -502,8 +502,8 @@ export default {
|
||||
&:hover {
|
||||
border-radius: $border-radius;
|
||||
|
||||
.schema-size{
|
||||
visibility: visible;
|
||||
.schema-size {
|
||||
visibility: visible;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@
|
||||
background-image: url("../images/svg/pg.svg");
|
||||
}
|
||||
|
||||
&.dbi-sqlite {
|
||||
&.dbi-sqlite {
|
||||
background-image: url("../images/svg/sqlite.svg");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user