diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index 7716d4ac..46e1558b 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -48,6 +48,16 @@ export default (connections) => { } }); + ipcMain.handle('get-table-options', async (event, params) => { + try { + const result = await connections[params.uid].getTableOptions(params); + return { status: 'success', response: result }; + } + catch (err) { + return { status: 'error', response: err.toString() }; + } + }); + ipcMain.handle('get-table-indexes', async (event, params) => { try { const result = await connections[params.uid].getTableIndexes(params); diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 03fe2ebf..d31e490e 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -437,6 +437,43 @@ export class MySQLClient extends AntaresCore { return rows.length ? rows[0].count : 0; } + /** + * @param {Object} params + * @param {String} params.schema + * @param {String} params.table + * @returns {Object} table options + * @memberof MySQLClient + */ + async getTableOptions ({ schema, table }) { + const { rows } = await this.raw(`SHOW TABLE STATUS FROM \`${schema}\` WHERE Name = '${table}'`); + + if (rows.length) { + let tableType; + switch (rows[0].Comment) { + case 'VIEW': + tableType = 'view'; + break; + default: + tableType = 'table'; + break; + } + + return { + name: rows[0].Name, + type: tableType, + rows: rows[0].Rows, + created: rows[0].Create_time, + updated: rows[0].Update_time, + engine: rows[0].Engine, + comment: rows[0].Comment, + size: rows[0].Data_length + rows[0].Index_length, + autoIncrement: rows[0].Auto_increment, + collation: rows[0].Collation + }; + }; + return {}; + } + /** * @param {Object} params * @param {String} params.schema diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index 5bc1f6e5..d7a9263b 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -306,6 +306,40 @@ export class PostgreSQLClient extends AntaresCore { return rows.length ? rows[0].count : 0; } + /** + * @param {Object} params + * @param {String} params.schema + * @param {String} params.table + * @returns {Object} table options + * @memberof MySQLClient + */ + async getTableOptions ({ schema, table }) { + const { rows } = await this.raw(` + SELECT *, + pg_table_size(QUOTE_IDENT(t.TABLE_SCHEMA) || '.' || QUOTE_IDENT(t.TABLE_NAME))::bigint AS data_length, + pg_relation_size(QUOTE_IDENT(t.TABLE_SCHEMA) || '.' || QUOTE_IDENT(t.TABLE_NAME))::bigint AS index_length, + c.reltuples, obj_description(c.oid) AS comment + FROM "information_schema"."tables" AS t + LEFT JOIN "pg_namespace" n ON t.table_schema = n.nspname + LEFT JOIN "pg_class" c ON n.oid = c.relnamespace AND c.relname=t.table_name + WHERE t."table_schema" = '${schema}' + AND table_name = '${table}' + `); + + if (rows.length) { + return { + name: rows[0].table_name, + type: rows[0].table_type === 'VIEW' ? 'view' : 'table', + rows: rows[0].reltuples, + size: +rows[0].data_length + +rows[0].index_length, + collation: rows[0].Collation, + comment: rows[0].comment, + engine: '' + }; + }; + return {}; + } + /** * @param {Object} params * @param {String} params.schema diff --git a/src/renderer/components/WorkspacePropsTab.vue b/src/renderer/components/WorkspacePropsTab.vue index a91aad90..a7312fd4 100644 --- a/src/renderer/components/WorkspacePropsTab.vue +++ b/src/renderer/components/WorkspacePropsTab.vue @@ -232,6 +232,7 @@ export default { localKeyUsage: [], originalIndexes: [], localIndexes: [], + tableOptions: {}, localOptions: {}, lastTable: null, newFieldsCounter: 0 @@ -249,10 +250,6 @@ export default { tabUid () { return this.$vnode.key; }, - tableOptions () { - const db = this.workspace.structure.find(db => db.name === this.schema); - return db && this.table ? db.tables.find(table => table.name === this.table) : {}; - }, defaultEngine () { const engine = this.getDatabaseVariable(this.connection.uid, 'default_storage_engine'); return engine ? engine.value : ''; @@ -311,6 +308,20 @@ export default { renameTabs: 'workspaces/renameTabs', changeBreadcrumbs: 'workspaces/changeBreadcrumbs' }), + async getTableOptions (params) { + const db = this.workspace.structure.find(db => db.name === this.schema); + + if (db && db.tables.length && this.table) + this.tableOptions = db.tables.find(table => table.name === this.table); + else { + const { status, response } = await Tables.getTableOptions(params); + + if (status === 'success') + this.tableOptions = response; + else + this.addNotification({ status: 'error', message: response }); + } + }, async getFieldsData () { if (!this.table) return; @@ -318,10 +329,6 @@ export default { this.lastTable = this.table; this.newFieldsCounter = 0; this.isLoading = true; - try { - this.localOptions = JSON.parse(JSON.stringify(this.tableOptions)); - } - catch (err) {} const params = { uid: this.connection.uid, @@ -329,6 +336,14 @@ export default { table: this.table }; + try { + await this.getTableOptions(params); + this.localOptions = JSON.parse(JSON.stringify(this.tableOptions)); + } + catch (err) { + console.error(err); + } + try { // Columns data const { status, response } = await Tables.getTableColumns(params); if (status === 'success') { diff --git a/src/renderer/components/WorkspacePropsTabScheduler.vue b/src/renderer/components/WorkspacePropsTabScheduler.vue index 998f8040..824a5463 100644 --- a/src/renderer/components/WorkspacePropsTabScheduler.vue +++ b/src/renderer/components/WorkspacePropsTabScheduler.vue @@ -77,7 +77,7 @@ -
+