diff --git a/src/main/ipc-handlers/connection.js b/src/main/ipc-handlers/connection.js index 6f9417eb..e6184689 100644 --- a/src/main/ipc-handlers/connection.js +++ b/src/main/ipc-handlers/connection.js @@ -46,12 +46,7 @@ export default connections => { try { await connection.connect(); - const { rows: structure } = await connection - .select('*') - .schema('information_schema') - .from('TABLES') - .orderBy({ TABLE_SCHEMA: 'ASC', TABLE_NAME: 'ASC' }) - .run(); + const structure = await connection.getStructure(); connections[conn.uid] = connection; diff --git a/src/main/ipc-handlers/database.js b/src/main/ipc-handlers/database.js index d4c97c20..82c9d967 100644 --- a/src/main/ipc-handlers/database.js +++ b/src/main/ipc-handlers/database.js @@ -16,12 +16,7 @@ export default connections => { ipcMain.handle('get-structure', async (event, uid) => { try { - const { rows: structure } = await connections[uid] - .select('*') - .schema('information_schema') - .from('TABLES') - .orderBy({ TABLE_SCHEMA: 'ASC', TABLE_NAME: 'ASC' }) - .run(); + const structure = await connections[uid].getStructure(); return { status: 'success', response: structure }; } diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index fd24be9b..1e1382be 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -23,7 +23,7 @@ export class MySQLClient extends AntaresCore { /** * Executes an USE query * - * @param {*} schema + * @param {String} schema * @memberof MySQLClient */ use (schema) { @@ -31,15 +31,36 @@ export class MySQLClient extends AntaresCore { return this.raw(sql); } + /** + * @returns {Array.} databases scructure + * @memberof MySQLClient + */ + async getStructure () { + const { rows: databases } = await this.raw('SHOW DATABASES'); + + const { rows: tables } = await this + .select('*') + .schema('information_schema') + .from('TABLES') + .orderBy({ TABLE_SCHEMA: 'ASC', TABLE_NAME: 'ASC' }) + .run(); + + return databases.map(db => { + return { + name: db.Database, + tables: tables.filter(table => table.TABLE_SCHEMA === db.Database) + }; + }); + } + /** * SHOW COLLATION * - * @returns + * @returns {Array.} collations list * @memberof MySQLClient */ async getCollations () { - const sql = 'SHOW COLLATION'; - const results = await this.raw(sql); + const results = await this.raw('SHOW COLLATION'); return results.rows.map(row => { return { @@ -56,7 +77,7 @@ export class MySQLClient extends AntaresCore { /** * SHOW VARIABLES * - * @returns + * @returns {Array.} variables list * @memberof MySQLClient */ async getVariables () { @@ -72,7 +93,7 @@ export class MySQLClient extends AntaresCore { } /** - * @returns {string} SQL string + * @returns {String} SQL string * @memberof MySQLClient */ getSQL () { diff --git a/src/renderer/store/modules/workspaces.store.js b/src/renderer/store/modules/workspaces.store.js index cc7363eb..9ec3295e 100644 --- a/src/renderer/store/modules/workspaces.store.js +++ b/src/renderer/store/modules/workspaces.store.js @@ -4,18 +4,6 @@ import Database from '@/ipc-api/Database'; import { uidGen } from 'common/libs/uidGen'; const tabIndex = []; -function remapStructure (structure) { // TODO: move to main process and add fields (for autocomplete purpose), also add empty database - const databases = structure.map(table => table.TABLE_SCHEMA) - .filter((value, index, self) => self.indexOf(value) === index); - - return databases.map(db => { - return { - name: db, - tables: structure.filter(table => table.TABLE_SCHEMA === db) - }; - }); -} - export default { namespaced: true, strict: true, @@ -155,7 +143,7 @@ export default { if (status === 'error') dispatch('notifications/addNotification', { status, message: response }, { root: true }); else { - commit('ADD_CONNECTED', { uid: connection.uid, structure: remapStructure(response) }); + commit('ADD_CONNECTED', { uid: connection.uid, structure: response }); dispatch('refreshCollations', connection.uid); dispatch('refreshVariables', connection.uid); } @@ -170,7 +158,7 @@ export default { if (status === 'error') dispatch('notifications/addNotification', { status, message: response }, { root: true }); else - commit('REFRESH_STRUCTURE', { uid, structure: remapStructure(response) }); + commit('REFRESH_STRUCTURE', { uid, structure: response }); } catch (err) { dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });