From e6602d1bfa9ca10c6bb078ee80ddc94fb338763d Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Thu, 3 Dec 2020 13:00:54 +0100 Subject: [PATCH] feat: create new tables --- src/main/ipc-handlers/tables.js | 10 ++ src/main/libs/clients/MySQLClient.js | 24 +++- src/renderer/components/ModalNewTable.vue | 122 ++++++++++++++++++ .../components/WorkspaceExploreBar.vue | 50 ++++++- .../WorkspaceExploreBarDatabase.vue | 4 +- .../WorkspaceExploreBarDatabaseContext.vue | 7 +- .../WorkspaceExploreBarTableContext.vue | 115 +++++++++++++++++ src/renderer/i18n/en-US.js | 4 +- src/renderer/images/svg/sqllite.svg | 2 +- src/renderer/ipc-api/Tables.js | 4 + .../store/modules/workspaces.store.js | 24 +++- 11 files changed, 348 insertions(+), 18 deletions(-) create mode 100644 src/renderer/components/ModalNewTable.vue create mode 100644 src/renderer/components/WorkspaceExploreBarTableContext.vue diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index 3e97a200..0da6cae8 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -165,6 +165,16 @@ export default (connections) => { } }); + ipcMain.handle('create-table', async (event, params) => { + try { + await connections[params.uid].createTable(params); + return { status: 'success' }; + } + catch (err) { + return { status: 'error', response: err.toString() }; + } + }); + ipcMain.handle('alter-table', async (event, params) => { try { await connections[params.uid].alterTable(params); diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 4547bd11..c186e6ed 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -306,13 +306,33 @@ export class MySQLClient extends AntaresCore { name: row.Engine, support: row.Support, comment: row.Comment, - transactions: row.Trasactions, + transactions: row.Transactions, xa: row.XA, - savepoints: row.Savepoints + savepoints: row.Savepoints, + isDefault: row.Support.includes('DEFAULT') }; }); } + /** + * CREATE TABLE + * + * @returns {Array.} parameters + * @memberof MySQLClient + */ + async createTable (params) { + const { + name, + collation, + comment, + engine + } = params; + + const sql = `CREATE TABLE \`${name}\` (\`${name}_ID\` INT NULL) COMMENT='${comment}', COLLATE='${collation}', ENGINE=${engine}`; + + return await this.raw(sql); + } + /** * ALTER TABLE * diff --git a/src/renderer/components/ModalNewTable.vue b/src/renderer/components/ModalNewTable.vue new file mode 100644 index 00000000..d337c895 --- /dev/null +++ b/src/renderer/components/ModalNewTable.vue @@ -0,0 +1,122 @@ + + + diff --git a/src/renderer/components/WorkspaceExploreBar.vue b/src/renderer/components/WorkspaceExploreBar.vue index fb3c9502..f1ff98c5 100644 --- a/src/renderer/components/WorkspaceExploreBar.vue +++ b/src/renderer/components/WorkspaceExploreBar.vue @@ -39,6 +39,7 @@ :database="db" :connection="connection" @show-database-context="openDatabaseContext" + @show-table-context="openTableContext" /> @@ -47,11 +48,18 @@ @close="hideNewDBModal" @reload="refresh" /> + @@ -60,10 +68,12 @@ diff --git a/src/renderer/i18n/en-US.js b/src/renderer/i18n/en-US.js index 0122a479..de84b7b8 100644 --- a/src/renderer/i18n/en-US.js +++ b/src/renderer/i18n/en-US.js @@ -107,7 +107,9 @@ module.exports = { onUpdate: 'On update', deleteField: 'Delete field', createNewIndex: 'Create new index', - addToIndex: 'Add to index' + addToIndex: 'Add to index', + createNewTable: 'Create new table', + emptyTable: 'Empty table' }, // Date and Time short: { diff --git a/src/renderer/images/svg/sqllite.svg b/src/renderer/images/svg/sqllite.svg index 4ce361b9..4ad68e78 100644 --- a/src/renderer/images/svg/sqllite.svg +++ b/src/renderer/images/svg/sqllite.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/renderer/ipc-api/Tables.js b/src/renderer/ipc-api/Tables.js index fe3eb5b0..1e48da8b 100644 --- a/src/renderer/ipc-api/Tables.js +++ b/src/renderer/ipc-api/Tables.js @@ -34,6 +34,10 @@ export default class { return ipcRenderer.invoke('get-foreign-list', params); } + static createTable (params) { + return ipcRenderer.invoke('create-table', params); + } + static alterTable (params) { return ipcRenderer.invoke('alter-table', params); } diff --git a/src/renderer/store/modules/workspaces.store.js b/src/renderer/store/modules/workspaces.store.js index b0ccbe73..9d81c6ad 100644 --- a/src/renderer/store/modules/workspaces.store.js +++ b/src/renderer/store/modules/workspaces.store.js @@ -3,14 +3,15 @@ import Connection from '@/ipc-api/Connection'; import Database from '@/ipc-api/Database'; import { uidGen } from 'common/libs/uidGen'; const tabIndex = []; -let lastBreadcrumb = ''; +let lastBreadcrumbs = {}; export default { namespaced: true, strict: true, state: { workspaces: [], - selected_workspace: null + selected_workspace: null, + has_unsaved_changes: false }, getters: { getSelected: state => { @@ -295,13 +296,24 @@ export default { dispatch('newTab', uid); }, changeBreadcrumbs ({ commit, getters }, payload) { - if (lastBreadcrumb.schema === payload.schema && lastBreadcrumb.table !== null && payload.table === null) return; + const breadcrumbsObj = { + schema: null, + table: null, + trigger: null, + procedure: null, + scheduler: null + }; + + const hasLastChildren = Object.keys(lastBreadcrumbs).filter(b => b !== 'schema').some(b => lastBreadcrumbs[b]); + const hasChildren = Object.keys(payload).filter(b => b !== 'schema').some(b => payload[b]); + + if (lastBreadcrumbs.schema === payload.schema && hasLastChildren && !hasChildren) return; - if (lastBreadcrumb.schema !== payload.schema) + if (lastBreadcrumbs.schema !== payload.schema) Database.useSchema({ uid: getters.getSelected, schema: payload.schema }); - commit('CHANGE_BREADCRUMBS', { uid: getters.getSelected, breadcrumbs: payload }); - lastBreadcrumb = { ...payload }; + commit('CHANGE_BREADCRUMBS', { uid: getters.getSelected, breadcrumbs: { ...breadcrumbsObj, ...payload } }); + lastBreadcrumbs = { ...breadcrumbsObj, ...payload }; }, newTab ({ commit }, uid) { commit('NEW_TAB', uid);