1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-02-17 04:00:48 +01:00

feat(SQLite): manual commit mode

This commit is contained in:
Fabio Di Stasio 2022-02-14 18:53:55 +01:00
parent d81e0911ab
commit 7dcd4441c4
3 changed files with 65 additions and 8 deletions

View File

@ -134,7 +134,7 @@ export default class {
{ name: 'phoneNumberFormat', group: 'phone', types: ['string'] }, { name: 'phoneNumberFormat', group: 'phone', types: ['string'] },
{ name: 'phoneFormats', group: 'phone', types: ['string'] }, { name: 'phoneFormats', group: 'phone', types: ['string'] },
{ name: 'number', group: 'random', types: ['string', 'number'], params: ['min', 'max'] }, { name: 'number', group: 'datatype', types: ['string', 'number'], params: ['min', 'max'] },
{ name: 'float', group: 'random', types: ['string', 'float'], params: ['min', 'max'] }, { name: 'float', group: 'random', types: ['string', 'float'], params: ['min', 'max'] },
{ name: 'arrayElement', group: 'random', types: ['string'] }, { name: 'arrayElement', group: 'random', types: ['string'] },
{ name: 'arrayElements', group: 'random', types: ['string'] }, { name: 'arrayElements', group: 'random', types: ['string'] },

View File

@ -1147,8 +1147,10 @@ export class PostgreSQLClient extends AntaresCore {
*/ */
async commitTab (tabUid) { async commitTab (tabUid) {
const connection = this._connectionsToCommit.get(tabUid); const connection = this._connectionsToCommit.get(tabUid);
if (connection) if (connection) {
return await connection.query('COMMIT'); await connection.query('COMMIT');
return this.destroyConnectionToCommit(tabUid);
}
} }
/** /**
@ -1158,14 +1160,16 @@ export class PostgreSQLClient extends AntaresCore {
*/ */
async rollbackTab (tabUid) { async rollbackTab (tabUid) {
const connection = this._connectionsToCommit.get(tabUid); const connection = this._connectionsToCommit.get(tabUid);
if (connection) if (connection) {
return await connection.query('ROLLBACK'); await connection.query('ROLLBACK');
return this.destroyConnectionToCommit(tabUid);
}
} }
destroyConnectionToCommit (tabUid) { destroyConnectionToCommit (tabUid) {
const connection = this._connectionsToCommit.get(tabUid); const connection = this._connectionsToCommit.get(tabUid);
if (connection) { if (connection) {
connection.destroy(); connection.end();
this._connectionsToCommit.delete(tabUid); this._connectionsToCommit.delete(tabUid);
} }
} }

View File

@ -9,6 +9,7 @@ export class SQLiteClient extends AntaresCore {
super(args); super(args);
this._schema = null; this._schema = null;
this._connectionsToCommit = new Map();
} }
_getTypeInfo (type) { _getTypeInfo (type) {
@ -21,7 +22,11 @@ export class SQLiteClient extends AntaresCore {
* @memberof SQLiteClient * @memberof SQLiteClient
*/ */
async connect () { async connect () {
this._connection = sqlite(this._params.databasePath, { this._connection = this.getConnection();
}
getConnection () {
return sqlite(this._params.databasePath, {
fileMustExist: true, fileMustExist: true,
readonly: this._params.readonly readonly: this._params.readonly
}); });
@ -446,6 +451,40 @@ export class SQLiteClient extends AntaresCore {
async killProcess () {} async killProcess () {}
/**
*
* @param {string} tabUid
* @returns {Promise<null>}
*/
async commitTab (tabUid) {
const connection = this._connectionsToCommit.get(tabUid);
if (connection) {
connection.prepare('COMMIT').run();
return this.destroyConnectionToCommit(tabUid);
}
}
/**
*
* @param {string} tabUid
* @returns {Promise<null>}
*/
async rollbackTab (tabUid) {
const connection = this._connectionsToCommit.get(tabUid);
if (connection) {
connection.prepare('ROLLBACK').run();
return this.destroyConnectionToCommit(tabUid);
}
}
destroyConnectionToCommit (tabUid) {
const connection = this._connectionsToCommit.get(tabUid);
if (connection) {
connection.close();
this._connectionsToCommit.delete(tabUid);
}
}
/** /**
* CREATE TABLE * CREATE TABLE
* *
@ -666,6 +705,7 @@ export class SQLiteClient extends AntaresCore {
details: false, details: false,
split: true, split: true,
comments: true, comments: true,
autocommit: true,
...args ...args
}; };
@ -679,7 +719,20 @@ export class SQLiteClient extends AntaresCore {
.filter(Boolean) .filter(Boolean)
.map(q => q.trim()) .map(q => q.trim())
: [sql]; : [sql];
const connection = this._connection;
let connection;
if (!args.autocommit && args.tabUid) { // autocommit OFF
if (this._connectionsToCommit.has(args.tabUid))
connection = this._connectionsToCommit.get(args.tabUid);
else {
connection = this.getConnection();
connection.prepare('BEGIN TRANSACTION').run();
this._connectionsToCommit.set(args.tabUid, connection);
}
}
else// autocommit ON
connection = this._connection;
for (const query of queries) { for (const query of queries) {
if (!query) continue; if (!query) continue;