From a4122b4eaaa5b30d97ba5a93df8c9d21c30bc40b Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Thu, 3 Dec 2020 16:15:10 +0100 Subject: [PATCH] feat: drop and truncate tables --- src/main/ipc-handlers/tables.js | 20 ++++ src/main/libs/clients/MySQLClient.js | 22 +++++ .../components/WorkspaceExploreBar.vue | 11 ++- .../WorkspaceExploreBarDatabaseContext.vue | 18 +++- .../WorkspaceExploreBarTableContext.vue | 95 ++++++++++++------- .../components/WorkspacePropsTableContext.vue | 2 +- src/renderer/components/WorkspaceTableTab.vue | 7 +- src/renderer/i18n/en-US.js | 4 +- src/renderer/ipc-api/Tables.js | 8 ++ 9 files changed, 146 insertions(+), 41 deletions(-) diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index 0da6cae8..d848c97a 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -184,4 +184,24 @@ export default (connections) => { return { status: 'error', response: err.toString() }; } }); + + ipcMain.handle('truncate-table', async (event, params) => { + try { + await connections[params.uid].truncateTable(params); + return { status: 'success' }; + } + catch (err) { + return { status: 'error', response: err.toString() }; + } + }); + + ipcMain.handle('drop-table', async (event, params) => { + try { + await connections[params.uid].dropTable(params); + return { status: 'success' }; + } + catch (err) { + return { status: 'error', response: err.toString() }; + } + }); }; diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index c186e6ed..b8e2cae2 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -448,6 +448,28 @@ export class MySQLClient extends AntaresCore { return await this.raw(sql); } + /** + * TRUNCATE TABLE + * + * @returns {Array.} parameters + * @memberof MySQLClient + */ + async truncateTable (params) { + const sql = `TRUNCATE TABLE \`${params.table}\``; + return await this.raw(sql); + } + + /** + * DROP TABLE + * + * @returns {Array.} parameters + * @memberof MySQLClient + */ + async dropTable (params) { + const sql = `DROP TABLE \`${params.table}\``; + return await this.raw(sql); + } + /** * @returns {String} SQL string * @memberof MySQLClient diff --git a/src/renderer/components/WorkspaceExploreBar.vue b/src/renderer/components/WorkspaceExploreBar.vue index f1ff98c5..e6254143 100644 --- a/src/renderer/components/WorkspaceExploreBar.vue +++ b/src/renderer/components/WorkspaceExploreBar.vue @@ -62,6 +62,13 @@ @show-create-table-modal="showCreateTableModal" @reload="refresh" /> + @@ -72,6 +79,7 @@ import Tables from '@/ipc-api/Tables'; import WorkspaceConnectPanel from '@/components/WorkspaceConnectPanel'; import WorkspaceExploreBarDatabase from '@/components/WorkspaceExploreBarDatabase'; import DatabaseContext from '@/components/WorkspaceExploreBarDatabaseContext'; +import TableContext from '@/components/WorkspaceExploreBarTableContext'; import ModalNewDatabase from '@/components/ModalNewDatabase'; import ModalNewTable from '@/components/ModalNewTable'; @@ -81,6 +89,7 @@ export default { WorkspaceConnectPanel, WorkspaceExploreBarDatabase, DatabaseContext, + TableContext, ModalNewDatabase, ModalNewTable }, @@ -205,7 +214,7 @@ export default { this.isTableContext = true; }, closeTableContext () { - this.isDatabaseContext = false; + this.isTableContext = false; } } }; diff --git a/src/renderer/components/WorkspaceExploreBarDatabaseContext.vue b/src/renderer/components/WorkspaceExploreBarDatabaseContext.vue index 26e404c5..0e3ea2f6 100644 --- a/src/renderer/components/WorkspaceExploreBarDatabaseContext.vue +++ b/src/renderer/components/WorkspaceExploreBarDatabaseContext.vue @@ -13,10 +13,10 @@
- {{ $t('word.edit') }} + {{ $t('word.edit') }}
- {{ $t('word.delete') }} + {{ $t('word.delete') }}
-
- {{ $t('word.add') }} - -
-
- {{ $t('word.table') }} -
-
-
-
- {{ $t('word.edit') }} +
+ {{ $t('message.emptyTable') }}
- {{ $t('word.delete') }} + {{ $t('word.delete') }}
+ + +
+
+ {{ $t('message.emptyCorfirm') }} "{{ selectedTable }}"? +
+
+
- {{ $t('message.deleteCorfirm') }} "{{ selectedDatabase }}"? + {{ $t('message.deleteCorfirm') }} "{{ selectedTable }}"?
- @@ -61,17 +63,23 @@ export default { }, data () { return { - isDeleteModal: false + isDeleteModal: false, + isEmptyModal: false }; }, computed: { ...mapGetters({ - selectedWorkspace: 'workspaces/getSelected' - }) + selectedWorkspace: 'workspaces/getSelected', + getWorkspace: 'workspaces/getWorkspace' + }), + workspace () { + return this.getWorkspace(this.selectedWorkspace); + } }, methods: { ...mapActions({ - addNotification: 'notifications/addNotification' + addNotification: 'notifications/addNotification', + changeBreadcrumbs: 'workspaces/changeBreadcrumbs' }), showCreateTableModal () { this.$emit('show-create-table-modal'); @@ -82,24 +90,47 @@ export default { hideDeleteModal () { this.isDeleteModal = false; }, - showEditModal () { - this.isEditModal = true; + showEmptyModal () { + this.isEmptyModal = true; }, - hideEditModal () { - this.isEditModal = false; - this.closeContext(); + hideEmptyModal () { + this.isEmptyModal = false; }, closeContext () { this.$emit('close-context'); }, - async deleteDatabase () { + async emptyTable () { try { - const { status, response } = await Database.deleteDatabase({ + const { status, response } = await Tables.truncateTable({ uid: this.selectedWorkspace, - database: this.selectedDatabase + table: this.selectedTable }); if (status === 'success') { + if (this.selectedTable === this.workspace.breadcrumbs.table) + this.changeBreadcrumbs({ table: null }); + + this.closeContext(); + this.$emit('reload'); + } + else + this.addNotification({ status: 'error', message: response }); + } + catch (err) { + this.addNotification({ status: 'error', message: err.stack }); + } + }, + async deleteTable () { + try { + const { status, response } = await Tables.dropTable({ + uid: this.selectedWorkspace, + table: this.selectedTable + }); + + if (status === 'success') { + if (this.selectedTable === this.workspace.breadcrumbs.table) + this.changeBreadcrumbs({ table: null }); + this.closeContext(); this.$emit('reload'); } diff --git a/src/renderer/components/WorkspacePropsTableContext.vue b/src/renderer/components/WorkspacePropsTableContext.vue index 3cd1a74f..9d6f3545 100644 --- a/src/renderer/components/WorkspacePropsTableContext.vue +++ b/src/renderer/components/WorkspacePropsTableContext.vue @@ -18,7 +18,7 @@
-
+
{{ $t('message.addToIndex') }}
diff --git a/src/renderer/components/WorkspaceTableTab.vue b/src/renderer/components/WorkspaceTableTab.vue index 2b3fa147..c72513c1 100644 --- a/src/renderer/components/WorkspaceTableTab.vue +++ b/src/renderer/components/WorkspaceTableTab.vue @@ -122,7 +122,12 @@ export default { return this.results.length ? this.results[0].keys : []; }, tableInfo () { - return this.workspace.structure.find(db => db.name === this.schema).tables.find(table => table.name === this.table); + try { + return this.workspace.structure.find(db => db.name === this.schema).tables.find(table => table.name === this.table); + } + catch (err) { + return { rows: 0 }; + } } }, watch: { diff --git a/src/renderer/i18n/en-US.js b/src/renderer/i18n/en-US.js index de84b7b8..35f4c62c 100644 --- a/src/renderer/i18n/en-US.js +++ b/src/renderer/i18n/en-US.js @@ -109,7 +109,9 @@ module.exports = { createNewIndex: 'Create new index', addToIndex: 'Add to index', createNewTable: 'Create new table', - emptyTable: 'Empty table' + emptyTable: 'Empty table', + deleteTable: 'Delete table', + emptyCorfirm: 'Do you confirm to empty' }, // Date and Time short: { diff --git a/src/renderer/ipc-api/Tables.js b/src/renderer/ipc-api/Tables.js index 1e48da8b..087ba8a6 100644 --- a/src/renderer/ipc-api/Tables.js +++ b/src/renderer/ipc-api/Tables.js @@ -41,4 +41,12 @@ export default class { static alterTable (params) { return ipcRenderer.invoke('alter-table', params); } + + static truncateTable (params) { + return ipcRenderer.invoke('truncate-table', params); + } + + static dropTable (params) { + return ipcRenderer.invoke('drop-table', params); + } }