Skip to content

Commit

Permalink
feat: drop and truncate tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Dec 3, 2020
1 parent e6602d1 commit a4122b4
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 41 deletions.
20 changes: 20 additions & 0 deletions src/main/ipc-handlers/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}
});
};
22 changes: 22 additions & 0 deletions src/main/libs/clients/MySQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,28 @@ export class MySQLClient extends AntaresCore {
return await this.raw(sql);
}

/**
* TRUNCATE TABLE
*
* @returns {Array.<Object>} parameters
* @memberof MySQLClient
*/
async truncateTable (params) {
const sql = `TRUNCATE TABLE \`${params.table}\``;
return await this.raw(sql);
}

/**
* DROP TABLE
*
* @returns {Array.<Object>} parameters
* @memberof MySQLClient
*/
async dropTable (params) {
const sql = `DROP TABLE \`${params.table}\``;
return await this.raw(sql);
}

/**
* @returns {String} SQL string
* @memberof MySQLClient
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/components/WorkspaceExploreBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
@show-create-table-modal="showCreateTableModal"
@reload="refresh"
/>
<TableContext
v-if="isTableContext"
:selected-table="selectedTable"
:context-event="tableContextEvent"
@close-context="closeTableContext"
@reload="refresh"
/>
</div>
</template>

Expand All @@ -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';
Expand All @@ -81,6 +89,7 @@ export default {
WorkspaceConnectPanel,
WorkspaceExploreBarDatabase,
DatabaseContext,
TableContext,
ModalNewDatabase,
ModalNewTable
},
Expand Down Expand Up @@ -205,7 +214,7 @@ export default {
this.isTableContext = true;
},
closeTableContext () {
this.isDatabaseContext = false;
this.isTableContext = false;
}
}
};
Expand Down
18 changes: 13 additions & 5 deletions src/renderer/components/WorkspaceExploreBarDatabaseContext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
</div>
</div>
<div class="context-element" @click="showEditModal">
<span class="d-flex"><i class="mdi mdi-18px mdi-pencil text-light pr-1" /> {{ $t('word.edit') }}</span>
<span class="d-flex"><i class="mdi mdi-18px mdi-database-edit text-light pr-1" /> {{ $t('word.edit') }}</span>
</div>
<div class="context-element" @click="showDeleteModal">
<span class="d-flex"><i class="mdi mdi-18px mdi-delete text-light pr-1" /> {{ $t('word.delete') }}</span>
<span class="d-flex"><i class="mdi mdi-18px mdi-database-remove text-light pr-1" /> {{ $t('word.delete') }}</span>
</div>

<ConfirmModal
Expand Down Expand Up @@ -69,12 +69,17 @@ export default {
},
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');
Expand Down Expand Up @@ -103,6 +108,9 @@ export default {
});
if (status === 'success') {
if (this.selectedDatabase === this.workspace.breadcrumbs.schema)
this.changeBreadcrumbs({ schema: null });
this.closeContext();
this.$emit('reload');
}
Expand Down
95 changes: 63 additions & 32 deletions src/renderer/components/WorkspaceExploreBarTableContext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,45 @@
:context-event="contextEvent"
@close-context="closeContext"
>
<div class="context-element">
<span class="d-flex"><i class="mdi mdi-18px mdi-plus text-light pr-1" /> {{ $t('word.add') }}</span>
<i class="mdi mdi-18px mdi-chevron-right text-light pl-1" />
<div class="context-submenu">
<div class="context-element" @click="showCreateTableModal">
<span class="d-flex"><i class="mdi mdi-18px mdi-table text-light pr-1" /> {{ $t('word.table') }}</span>
</div>
</div>
</div>
<div class="context-element" @click="showEditModal">
<span class="d-flex"><i class="mdi mdi-18px mdi-pencil text-light pr-1" /> {{ $t('word.edit') }}</span>
<div class="context-element" @click="showEmptyModal">
<span class="d-flex"><i class="mdi mdi-18px mdi-table-off text-light pr-1" /> {{ $t('message.emptyTable') }}</span>
</div>
<div class="context-element" @click="showDeleteModal">
<span class="d-flex"><i class="mdi mdi-18px mdi-delete text-light pr-1" /> {{ $t('word.delete') }}</span>
<span class="d-flex"><i class="mdi mdi-18px mdi-table-remove text-light pr-1" /> {{ $t('word.delete') }}</span>
</div>

<ConfirmModal
v-if="isEmptyModal"
@confirm="emptyTable"
@hide="hideEmptyModal"
>
<template slot="header">
<div class="d-flex">
<i class="mdi mdi-24px mdi-table-off mr-1" /> {{ $t('message.emptyTable') }}
</div>
</template>
<div slot="body">
<div class="mb-2">
{{ $t('message.emptyCorfirm') }} "<b>{{ selectedTable }}</b>"?
</div>
</div>
</ConfirmModal>
<ConfirmModal
v-if="isDeleteModal"
@confirm="deleteDatabase"
@confirm="deleteTable"
@hide="hideDeleteModal"
>
<template slot="header">
<div class="d-flex">
<i class="mdi mdi-24px mdi-database-remove mr-1" /> {{ $t('message.deleteDatabase') }}
<i class="mdi mdi-24px mdi-table-remove mr-1" /> {{ $t('message.deleteTable') }}
</div>
</template>
<div slot="body">
<div class="mb-2">
{{ $t('message.deleteCorfirm') }} "<b>{{ selectedDatabase }}</b>"?
{{ $t('message.deleteCorfirm') }} "<b>{{ selectedTable }}</b>"?
</div>
</div>
</ConfirmModal>
<ModalEditDatabase
v-if="isEditModal"
:selected-database="selectedDatabase"
@close="hideEditModal"
/>
</BaseContextMenu>
</template>

Expand All @@ -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');
Expand All @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/WorkspacePropsTableContext.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</div>
</div>
</div>
<div class="context-element">
<div v-if="indexes.length" class="context-element">
<span class="d-flex"><i class="mdi mdi-18px mdi-key-arrow-right text-light pr-1" /> {{ $t('message.addToIndex') }}</span>
<i class="mdi mdi-18px mdi-chevron-right text-light pl-1" />
<div class="context-submenu">
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/components/WorkspaceTableTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/i18n/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/ipc-api/Tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a4122b4

Please sign in to comment.