feat(MySQL): manual commit mode

This commit is contained in:
Fabio Di Stasio 2022-02-11 23:19:43 +01:00
parent c5eb73ed3f
commit 4ed2f9a939
8 changed files with 266 additions and 62 deletions

View File

@ -135,7 +135,7 @@ export default connections => {
} }
}); });
ipcMain.handle('raw-query', async (event, { uid, query, schema, tabUid }) => { ipcMain.handle('raw-query', async (event, { uid, query, schema, tabUid, autocommit }) => {
if (!query) return; if (!query) return;
try { try {
@ -144,6 +144,7 @@ export default connections => {
details: true, details: true,
schema, schema,
tabUid, tabUid,
autocommit,
comments: false comments: false
}); });
@ -165,4 +166,40 @@ export default connections => {
return { status: 'error', response: err.toString() }; return { status: 'error', response: err.toString() };
} }
}); });
ipcMain.handle('commit-tab', async (event, { uid, tabUid }) => {
if (!tabUid) return;
try {
await connections[uid].commitTab(tabUid);
return { status: 'success' };
}
catch (err) {
return { status: 'error', response: err.toString() };
}
});
ipcMain.handle('rollback-tab', async (event, { uid, tabUid }) => {
if (!tabUid) return;
try {
await connections[uid].rollbackTab(tabUid);
return { status: 'success' };
}
catch (err) {
return { status: 'error', response: err.toString() };
}
});
ipcMain.handle('destroy-connection-to-commit', async (event, { uid, tabUid }) => {
if (!tabUid) return;
try {
await connections[uid].destroyConnectionToCommit(tabUid);
return { status: 'success' };
}
catch (err) {
return { status: 'error', response: err.toString() };
}
});
}; };

View File

@ -10,6 +10,7 @@ export class MySQLClient extends AntaresCore {
this._schema = null; this._schema = null;
this._runningConnections = new Map(); this._runningConnections = new Map();
this._connectionsToCommit = new Map();
this.types = { this.types = {
0: 'DECIMAL', 0: 'DECIMAL',
@ -101,9 +102,10 @@ export class MySQLClient extends AntaresCore {
} }
/** /**
* @memberof MySQLClient *
* @returns dbConfig
*/ */
async connect () { async getDbConfig () {
delete this._params.application_name; delete this._params.application_name;
const dbConfig = { const dbConfig = {
@ -134,48 +136,17 @@ export class MySQLClient extends AntaresCore {
} }
} }
if (!this._poolSize) { return dbConfig;
this._connection = await mysql.createConnection(dbConfig); }
// ANSI_QUOTES check /**
const res = await this.getVariable('sql_mode', 'global'); * @memberof MySQLClient
const sqlMode = res?.value.split(','); */
const hasAnsiQuotes = sqlMode.includes('ANSI_QUOTES'); async connect () {
if (!this._poolSize)
if (this._params.readonly) this._connection = await this.getConnection();
await this.raw('SET SESSION TRANSACTION READ ONLY'); else
this._connection = await this.getConnectionPool();
if (hasAnsiQuotes)
await this.raw(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`);
}
else {
this._connection = mysql.createPool({
...dbConfig,
connectionLimit: this._poolSize,
typeCast: (field, next) => {
if (field.type === 'DATETIME')
return field.string();
else
return next();
}
});
// ANSI_QUOTES check
const res = await this.getVariable('sql_mode', 'global');
const sqlMode = res?.value.split(',');
const hasAnsiQuotes = sqlMode.includes('ANSI_QUOTES');
if (hasAnsiQuotes)
await this._connection.query(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`);
this._connection.on('connection', connection => {
if (this._params.readonly)
connection.query('SET SESSION TRANSACTION READ ONLY');
if (hasAnsiQuotes)
connection.query(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`);
});
}
} }
/** /**
@ -186,6 +157,56 @@ export class MySQLClient extends AntaresCore {
if (this._ssh) this._ssh.close(); if (this._ssh) this._ssh.close();
} }
async getConnection () {
const dbConfig = await this.getDbConfig();
const connection = await mysql.createConnection(dbConfig);
// ANSI_QUOTES check
const [res] = await connection.query('SHOW GLOBAL VARIABLES LIKE \'%sql_mode%\'');
const sqlMode = res[0]?.Variable_name?.split(',');
const hasAnsiQuotes = sqlMode.includes('ANSI_QUOTES');
if (this._params.readonly)
await connection.query('SET SESSION TRANSACTION READ ONLY');
if (hasAnsiQuotes)
await connection.query(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`);
return connection;
}
async getConnectionPool () {
const dbConfig = await this.getDbConfig();
const connection = mysql.createPool({
...dbConfig,
connectionLimit: this._poolSize,
typeCast: (field, next) => {
if (field.type === 'DATETIME')
return field.string();
else
return next();
}
});
// ANSI_QUOTES check
const [res] = await connection.query('SHOW GLOBAL VARIABLES LIKE \'%sql_mode%\'');
const sqlMode = res[0]?.Variable_name?.split(',');
const hasAnsiQuotes = sqlMode.includes('ANSI_QUOTES');
if (hasAnsiQuotes)
await connection.query(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`);
connection.on('connection', connection => {
if (this._params.readonly)
connection.query('SET SESSION TRANSACTION READ ONLY');
if (hasAnsiQuotes)
connection.query(`SET SESSION sql_mode = "${sqlMode.filter(m => m !== 'ANSI_QUOTES').join(',')}"`);
});
return connection;
}
/** /**
* Executes an USE query * Executes an USE query
* *
@ -1276,6 +1297,36 @@ export class MySQLClient extends AntaresCore {
return await this.killProcess(id); return await this.killProcess(id);
} }
/**
*
* @param {string} tabUid
* @returns {Promise<null>}
*/
async commitTab (tabUid) {
const connection = this._connectionsToCommit.get(tabUid);
if (connection)
return await connection.query('COMMIT');
}
/**
*
* @param {string} tabUid
* @returns {Promise<null>}
*/
async rollbackTab (tabUid) {
const connection = this._connectionsToCommit.get(tabUid);
if (connection)
return await connection.query('ROLLBACK');
}
destroyConnectionToCommit (tabUid) {
const connection = this._connectionsToCommit.get(tabUid);
if (connection) {
connection.destroy();
this._connectionsToCommit.delete(tabUid);
}
}
/** /**
* CREATE TABLE * CREATE TABLE
* *
@ -1580,6 +1631,7 @@ export class MySQLClient extends AntaresCore {
details: false, details: false,
split: true, split: true,
comments: true, comments: true,
autocommit: true,
...args ...args
}; };
@ -1594,8 +1646,21 @@ export class MySQLClient extends AntaresCore {
.filter(Boolean) .filter(Boolean)
.map(q => q.trim()) .map(q => q.trim())
: [sql]; : [sql];
let connection;
const isPool = typeof this._connection.getConnection === 'function'; const isPool = typeof this._connection.getConnection === 'function';
const connection = isPool ? await this._connection.getConnection() : this._connection;
if (!args.autocommit && args.tabUid) { // autocommit OFF
if (this._connectionsToCommit.has(args.tabUid))
connection = this._connectionsToCommit.get(args.tabUid);
else {
connection = await this.getConnection();
await connection.query('SET SESSION autocommit=0');
this._connectionsToCommit.set(args.tabUid, connection);
}
}
else// autocommit ON
connection = isPool ? await this._connection.getConnection() : this._connection;
if (args.tabUid && isPool) if (args.tabUid && isPool)
this._runningConnections.set(args.tabUid, connection.connection.connectionId); this._runningConnections.set(args.tabUid, connection.connection.connectionId);
@ -1660,7 +1725,7 @@ export class MySQLClient extends AntaresCore {
}); });
} }
catch (err) { catch (err) {
if (isPool) { if (isPool && args.autocommit) {
connection.release(); connection.release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }
@ -1672,7 +1737,7 @@ export class MySQLClient extends AntaresCore {
keysArr = keysArr ? [...keysArr, ...response] : response; keysArr = keysArr ? [...keysArr, ...response] : response;
} }
catch (err) { catch (err) {
if (isPool) { if (isPool && args.autocommit) {
connection.release(); connection.release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }
@ -1690,7 +1755,7 @@ export class MySQLClient extends AntaresCore {
keys: keysArr keys: keysArr
}); });
}).catch((err) => { }).catch((err) => {
if (isPool) { if (isPool && args.autocommit) {
connection.release(); connection.release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }
@ -1701,7 +1766,7 @@ export class MySQLClient extends AntaresCore {
resultsArr.push({ rows, report, fields, keys, duration }); resultsArr.push({ rows, report, fields, keys, duration });
} }
if (isPool) { if (isPool && args.autocommit) {
connection.release(); connection.release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }

View File

@ -6,7 +6,7 @@
<div class="modal-title h6"> <div class="modal-title h6">
<div class="d-flex"> <div class="d-flex">
<i class="mdi mdi-24px mdi-playlist-plus mr-1" /> <i class="mdi mdi-24px mdi-playlist-plus mr-1" />
<span class="cut-text">{{ $t('message.tableFiller') }}</span> <span class="cut-text">{{ $tc('message.insertRow', 2) }}</span>
</div> </div>
</div> </div>
<a class="btn btn-clear c-hand" @click.stop="closeModal" /> <a class="btn btn-clear c-hand" @click.stop="closeModal" />

View File

@ -11,9 +11,9 @@
<div class="footer-right-elements"> <div class="footer-right-elements">
<ul class="footer-elements"> <ul class="footer-elements">
<li class="footer-element footer-link" @click="openOutside('https://www.treedom.net/en/user/fabio-di-stasio/event/antares-for-the-planet')"> <li class="footer-element footer-link" @click="openOutside('https://www.paypal.com/paypalme/fabiodistasio')">
<i class="mdi mdi-18px mdi-tree mr-1" /> <i class="mdi mdi-18px mdi-coffee mr-1" />
<small>{{ $t('message.plantATree') }}</small> <small>{{ $t('word.donate') }}</small>
</li> </li>
<li class="footer-element footer-link" @click="openOutside('https://github.com/Fabio286/antares/issues')"> <li class="footer-element footer-link" @click="openOutside('https://github.com/Fabio286/antares/issues')">
<i class="mdi mdi-18px mdi-bug" /> <i class="mdi mdi-18px mdi-bug" />

View File

@ -46,6 +46,24 @@
<span>{{ $t('word.run') }}</span> <span>{{ $t('word.run') }}</span>
</button> </button>
</div> </div>
<button
v-if="!autocommit"
class="btn btn-dark btn-sm"
:class="{'loading':isQuering}"
@click="commitTab()"
>
<i class="mdi mdi-24px mdi-cube-send pr-1" />
<span>{{ $t('word.commit') }}</span>
</button>
<button
v-if="!autocommit"
class="btn btn-dark btn-sm"
:class="{'loading':isQuering}"
@click="rollbackTab()"
>
<i class="mdi mdi-24px mdi-undo-variant pr-1" />
<span>{{ $t('word.rollback') }}</span>
</button>
<button <button
class="btn btn-link btn-sm mr-0" class="btn btn-link btn-sm mr-0"
:disabled="!query || isQuering" :disabled="!query || isQuering"
@ -78,7 +96,7 @@
</button> </button>
<div class="dropdown table-dropdown pr-2"> <div class="dropdown table-dropdown pr-2">
<button <button
:disabled="!results.length || isQuering" :disabled="!hasResults || isQuering"
class="btn btn-dark btn-sm dropdown-toggle mr-0 pr-0" class="btn btn-dark btn-sm dropdown-toggle mr-0 pr-0"
tabindex="0" tabindex="0"
> >
@ -95,6 +113,17 @@
</li> </li>
</ul> </ul>
</div> </div>
<div class="input-group pr-2" :title="$t('message.commitMode')">
<i class="input-group-addon addon-sm mdi mdi-24px mdi-source-commit p-0" />
<select v-model="autocommit" class="form-select select-sm text-bold">
<option :value="true">
{{ $t('message.autoCommit') }}
</option>
<option :value="false">
{{ $t('message.manualCommit') }}
</option>
</select>
</div>
</div> </div>
<div class="workspace-query-info"> <div class="workspace-query-info">
<div <div
@ -104,11 +133,19 @@
> >
<i class="mdi mdi-timer-sand mdi-rotate-180 pr-1" /> <b>{{ durationsCount / 1000 }}s</b> <i class="mdi mdi-timer-sand mdi-rotate-180 pr-1" /> <b>{{ durationsCount / 1000 }}s</b>
</div> </div>
<div v-if="resultsCount"> <div
{{ $t('word.results') }}: <b>{{ resultsCount.toLocaleString() }}</b> v-if="resultsCount"
class="d-flex"
:title="$t('word.results')"
>
<i class="mdi mdi-equal pr-1" /> <b>{{ resultsCount.toLocaleString() }}</b>
</div> </div>
<div v-if="affectedCount !== null"> <div
{{ $t('message.affectedRows') }}: <b>{{ affectedCount }}</b> v-if="hasAffected"
class="d-flex"
:title="$t('message.affectedRows')"
>
<i class="mdi mdi-target pr-1" /> <b>{{ affectedCount }}</b>
</div> </div>
<div class="input-group" :title="$t('word.schema')"> <div class="input-group" :title="$t('word.schema')">
<i class="input-group-addon addon-sm mdi mdi-24px mdi-database" /> <i class="input-group-addon addon-sm mdi mdi-24px mdi-database" />
@ -182,6 +219,7 @@ export default {
isQuering: false, isQuering: false,
isCancelling: false, isCancelling: false,
showCancel: false, showCancel: false,
autocommit: true,
results: [], results: [],
selectedSchema: null, selectedSchema: null,
resultsCount: 0, resultsCount: 0,
@ -214,6 +252,12 @@ export default {
}, },
history () { history () {
return this.getHistoryByWorkspace(this.connection.uid) || []; return this.getHistoryByWorkspace(this.connection.uid) || [];
},
hasResults () {
return this.results.length && this.results[0].rows;
},
hasAffected () {
return this.affectedCount || (!this.resultsCount && this.affectedCount !== null);
} }
}, },
watch: { watch: {
@ -251,6 +295,11 @@ export default {
}, },
beforeDestroy () { beforeDestroy () {
window.removeEventListener('keydown', this.onKey); window.removeEventListener('keydown', this.onKey);
const params = {
uid: this.connection.uid,
tabUid: this.tab.uid
};
Schema.destroyConnectionToCommit(params);
}, },
methods: { methods: {
...mapActions({ ...mapActions({
@ -270,6 +319,7 @@ export default {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.selectedSchema, schema: this.selectedSchema,
tabUid: this.tab.uid, tabUid: this.tab.uid,
autocommit: this.autocommit,
query query
}; };
@ -391,6 +441,38 @@ export default {
}, },
downloadTable (format) { downloadTable (format) {
this.$refs.queryTable.downloadTable(format, `${this.tab.type}-${this.tab.index}`); this.$refs.queryTable.downloadTable(format, `${this.tab.type}-${this.tab.index}`);
},
async commitTab () {
this.isQuering = true;
try {
const params = {
uid: this.connection.uid,
tabUid: this.tab.uid
};
await Schema.commitTab(params);
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isQuering = false;
},
async rollbackTab () {
this.isQuering = true;
try {
const params = {
uid: this.connection.uid,
tabUid: this.tab.uid
};
await Schema.rollbackTab(params);
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isQuering = false;
} }
} }
}; };
@ -419,10 +501,12 @@ export default {
.workspace-query-runner-footer { .workspace-query-runner-footer {
display: flex; display: flex;
flex-wrap: wrap;
row-gap: 0.4rem;
justify-content: space-between; justify-content: space-between;
padding: 0.3rem 0.6rem 0.4rem; padding: 0.3rem 0.6rem 0.4rem;
align-items: center; align-items: center;
height: 42px; min-height: 42px;
.workspace-query-buttons, .workspace-query-buttons,
.workspace-query-info { .workspace-query-info {

View File

@ -85,7 +85,7 @@
@click="showFakerModal" @click="showFakerModal"
> >
<i class="mdi mdi-24px mdi-playlist-plus mr-1" /> <i class="mdi mdi-24px mdi-playlist-plus mr-1" />
<span>{{ $t('message.tableFiller') }}</span> <span>{{ $tc('message.insertRow', 2) }}</span>
</button> </button>
<div class="dropdown table-dropdown pr-2"> <div class="dropdown table-dropdown pr-2">

View File

@ -124,7 +124,9 @@ module.exports = {
filter: 'Filter', filter: 'Filter',
disabled: 'Disabled', disabled: 'Disabled',
enable: 'Enable', enable: 'Enable',
disable: 'Disable' disable: 'Disable',
commit: 'Commit',
rollback: 'Rollback'
}, },
message: { message: {
appWelcome: 'Welcome to Antares SQL Client!', appWelcome: 'Welcome to Antares SQL Client!',
@ -252,7 +254,11 @@ module.exports = {
closeTab: 'Close tab', closeTab: 'Close tab',
goToDownloadPage: 'Go to download page', goToDownloadPage: 'Go to download page',
readOnlyMode: 'Read-only mode', readOnlyMode: 'Read-only mode',
killQuery: 'Kill query' killQuery: 'Kill query',
insertRow: 'Insert row | Insert rows',
commitMode: 'Commit mode',
autoCommit: 'Auto commit',
manualCommit: 'Manual commit'
}, },
faker: { faker: {
address: 'Address', address: 'Address',

View File

@ -50,6 +50,18 @@ export default class {
return ipcRenderer.invoke('kill-tab-query', params); return ipcRenderer.invoke('kill-tab-query', params);
} }
static commitTab (params) {
return ipcRenderer.invoke('commit-tab', params);
}
static rollbackTab (params) {
return ipcRenderer.invoke('rollback-tab', params);
}
static destroyConnectionToCommit (params) {
return ipcRenderer.invoke('destroy-connection-to-commit', params);
}
static useSchema (params) { static useSchema (params) {
return ipcRenderer.invoke('use-schema', params); return ipcRenderer.invoke('use-schema', params);
} }