Skip to content

Commit

Permalink
feat(PostgreSQL): ability to cancel queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Dec 26, 2021
1 parent 3d56ec7 commit 0c00291
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
3 changes: 1 addition & 2 deletions .stylelintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": [
"stylelint-config-standard",
"stylelint-config-standard-scss"
"stylelint-config-standard"
],
"fix": true,
"formatter": "verbose",
Expand Down
1 change: 1 addition & 0 deletions src/common/customizations/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
database: true,
sslConnection: true,
sshConnection: true,
cancelQueries: true,
// Tools
processesList: true,
// Structure
Expand Down
66 changes: 53 additions & 13 deletions src/main/libs/clients/PostgreSQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -1525,6 +1545,10 @@ export class PostgreSQLClient extends AntaresCore {
});
}
catch (err) {
if (isPool) {
connection.release();
this._runningConnections.delete(args.tabUid);
}
reject(err);
}

Expand All @@ -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);
}
}
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/renderer/components/TheSettingBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default {
}
&::before {
content: '';
content: "";
height: 0;
width: 3px;
transition: height 0.2s;
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/components/WorkspaceExploreBarSchema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -502,8 +502,8 @@ export default {
&:hover {
border-radius: $border-radius;
.schema-size{
visibility: visible;
.schema-size {
visibility: visible;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/scss/_db-icons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
background-image: url("../images/svg/pg.svg");
}

&.dbi-sqlite {
&.dbi-sqlite {
background-image: url("../images/svg/sqlite.svg");
}

Expand Down

0 comments on commit 0c00291

Please sign in to comment.