diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index a153452a..3e97a200 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -30,6 +30,17 @@ export default (connections) => { } }); + ipcMain.handle('get-table-indexes', async (event, params) => { + try { + const result = await connections[params.uid].getTableIndexes(params); + + return { status: 'success', response: result }; + } + catch (err) { + return { status: 'error', response: err.toString() }; + } + }); + ipcMain.handle('get-key-usage', async (event, params) => { try { const result = await connections[params.uid].getKeyUsage(params); diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 59a387fc..64b6b9fb 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -198,6 +198,30 @@ export class MySQLClient extends AntaresCore { }); } + /** + * @param {Object} params + * @param {String} params.schema + * @param {String} params.table + * @returns {Object} table indexes + * @memberof MySQLClient + */ + async getTableIndexes ({ schema, table }) { + const { rows } = await this.raw(`SHOW INDEXES FROM \`${table}\` FROM \`${schema}\``); + + return rows.map(row => { + return { + unique: !row.Non_unique, + name: row.Key_name, + column: row.Column_name, + indexType: row.Index_type, + type: row.Key_name === 'PRIMARY' ? 'PRIMARY' : !row.Non_unique ? 'UNIQUE' : row.Index_type === 'FULLTEXT' ? 'FULLTEXT' : 'INDEX', + cardinality: row.Cardinality, + comment: row.Comment, + indexComment: row.Index_comment + }; + }); + } + /** * @param {Object} params * @param {String} params.schema diff --git a/src/renderer/components/WorkspacePropsTab.vue b/src/renderer/components/WorkspacePropsTab.vue index 39af2933..cc0d79c1 100644 --- a/src/renderer/components/WorkspacePropsTab.vue +++ b/src/renderer/components/WorkspacePropsTab.vue @@ -32,7 +32,7 @@ {{ $t('word.add') }} - @@ -52,6 +52,7 @@ v-if="localFields" ref="queryTable" :fields="localFields" + :indexes="localIndexes" :tab-uid="tabUid" :conn-uid="connection.uid" :table="table" @@ -178,6 +179,8 @@ export default { localFields: [], originalKeyUsage: [], localKeyUsage: [], + originalIndexes: [], + localIndexes: [], localOptions: [], lastTable: null }; @@ -257,6 +260,20 @@ export default { this.addNotification({ status: 'error', message: err.stack }); } + try { // Indexes + const { status, response } = await Tables.getTableIndexes(params); + + if (status === 'success') { + this.originalIndexes = response; + this.localIndexes = JSON.parse(JSON.stringify(response)); + } + else + this.addNotification({ status: 'error', message: response }); + } + catch (err) { + this.addNotification({ status: 'error', message: err.stack }); + } + try { // Key usage (foreign keys) const { status, response } = await Tables.getKeyUsage(params); diff --git a/src/renderer/components/WorkspacePropsTable.vue b/src/renderer/components/WorkspacePropsTable.vue index 434417a3..e8505805 100644 --- a/src/renderer/components/WorkspacePropsTable.vue +++ b/src/renderer/components/WorkspacePropsTable.vue @@ -99,6 +99,7 @@ v-for="row in fields" :key="row._id" :row="row" + :indexes="getIndexes(row.name)" :data-types="dataTypes" @contextmenu="contextMenu" /> @@ -122,6 +123,7 @@ export default { }, props: { fields: Array, + indexes: Array, tabUid: [String, Number], connUid: String, table: String, @@ -195,6 +197,9 @@ export default { }, removeField () { this.$emit('remove-field', this.selectedField); + }, + getIndexes (field) { + return this.indexes.filter(index => index.column === field); } } }; diff --git a/src/renderer/components/WorkspacePropsTableRow.vue b/src/renderer/components/WorkspacePropsTableRow.vue index 23338e71..c2ecfdff 100644 --- a/src/renderer/components/WorkspacePropsTableRow.vue +++ b/src/renderer/components/WorkspacePropsTableRow.vue @@ -7,12 +7,15 @@
- +
+ +