refactor(mysql): moved specific queries inside MySQLClient class

This commit is contained in:
Fabio 2020-10-16 17:26:47 +02:00
parent 426628f268
commit 053418ee90
4 changed files with 96 additions and 68 deletions

View File

@ -38,7 +38,7 @@ export default connections => {
}
});
ipcMain.handle('get-database-collation', async (event, params) => {
ipcMain.handle('get-database-collation', async (event, params) => { // TODO: move to mysql class
try {
const query = `SELECT \`DEFAULT_COLLATION_NAME\` FROM \`information_schema\`.\`SCHEMATA\` WHERE \`SCHEMA_NAME\`='${params.database}'`;
const collation = await connections[params.uid].raw(query);

View File

@ -3,37 +3,10 @@ import { sqlEscaper } from 'common/libs/sqlEscaper';
import { TEXT, LONG_TEXT, NUMBER, BLOB } from 'common/fieldTypes';
import fs from 'fs';
// TODO: remap objects based on client
export default (connections) => {
ipcMain.handle('get-table-columns', async (event, { uid, schema, table }) => {
ipcMain.handle('get-table-columns', async (event, params) => {
try {
const { rows } = await connections[uid]
.select('*')
.schema('information_schema')
.from('COLUMNS')
.where({ TABLE_SCHEMA: `= '${schema}'`, TABLE_NAME: `= '${table}'` })
.orderBy({ ORDINAL_POSITION: 'ASC' })
.run();
const result = rows.map(field => {
return {
name: field.COLUMN_NAME,
key: field.COLUMN_KEY.toLowerCase(),
type: field.DATA_TYPE,
schema: field.TABLE_SCHEMA,
table: field.TABLE_NAME,
numPrecision: field.NUMERIC_PRECISION,
datePrecision: field.DATETIME_PRECISION,
charLength: field.CHARACTER_MAXIMUM_LENGTH,
isNullable: field.IS_NULLABLE,
default: field.COLUMN_DEFAULT,
charset: field.CHARACTER_SET_NAME,
collation: field.COLLATION_NAME,
autoIncrement: field.EXTRA.includes('auto_increment'),
comment: field.COLUMN_COMMENT
};
});
const result = await connections[params.uid].getTableColumns(params);
return { status: 'success', response: result };
}
catch (err) {
@ -57,28 +30,9 @@ export default (connections) => {
}
});
ipcMain.handle('get-key-usage', async (event, { uid, schema, table }) => {
ipcMain.handle('get-key-usage', async (event, params) => {
try {
const { rows } = await connections[uid]
.select('*')
.schema('information_schema')
.from('KEY_COLUMN_USAGE')
.where({ TABLE_SCHEMA: `= '${schema}'`, TABLE_NAME: `= '${table}'`, REFERENCED_TABLE_NAME: 'IS NOT NULL' })
.run();
const result = rows.map(field => {
return {
schema: field.TABLE_SCHEMA,
table: field.TABLE_NAME,
column: field.COLUMN_NAME,
position: field.ORDINAL_POSITION,
constraintPosition: field.POSITION_IN_UNIQUE_CONSTRAINT,
constraintName: field.CONSTRAINT_NAME,
refSchema: field.REFERENCED_TABLE_SCHEMA,
refTable: field.REFERENCED_TABLE_NAME,
refColumn: field.REFERENCED_COLUMN_NAME
};
});
const result = await connections[params.uid].getKeyUsage(params);
return { status: 'success', response: result };
}
@ -180,16 +134,16 @@ export default (connections) => {
}
});
ipcMain.handle('get-foreign-list', async (event, params) => {
ipcMain.handle('get-foreign-list', async (event, { uid, schema, table, description }) => {
try {
const query = connections[params.uid]
.select(`${params.column} AS foreignColumn`)
.schema(params.schema)
.from(params.table)
const query = connections[uid]
.select(`${column} AS foreignColumn`)
.schema(schema)
.from(table)
.orderBy('foreignColumn ASC');
if (params.description)
query.select(`LEFT(${params.description}, 20) AS foreignDescription`);
if (description)
query.select(`LEFT(${description}, 20) AS foreignDescription`);
const results = await query.run();

View File

@ -154,6 +154,72 @@ export class MySQLClient extends AntaresCore {
});
}
/**
* @param {Object} params
* @param {String} params.schema
* @param {String} params.table
* @returns {Object} table scructure
* @memberof MySQLClient
*/
async getTableColumns ({ schema, table }) {
const { rows } = await this
.select('*')
.schema('information_schema')
.from('COLUMNS')
.where({ TABLE_SCHEMA: `= '${schema}'`, TABLE_NAME: `= '${table}'` })
.orderBy({ ORDINAL_POSITION: 'ASC' })
.run();
return rows.map(field => {
return {
name: field.COLUMN_NAME,
key: field.COLUMN_KEY.toLowerCase(),
type: field.DATA_TYPE,
schema: field.TABLE_SCHEMA,
table: field.TABLE_NAME,
numPrecision: field.NUMERIC_PRECISION,
datePrecision: field.DATETIME_PRECISION,
charLength: field.CHARACTER_MAXIMUM_LENGTH,
isNullable: field.IS_NULLABLE,
default: field.COLUMN_DEFAULT,
charset: field.CHARACTER_SET_NAME,
collation: field.COLLATION_NAME,
autoIncrement: field.EXTRA.includes('auto_increment'),
comment: field.COLUMN_COMMENT
};
});
}
/**
* @param {Object} params
* @param {String} params.schema
* @param {String} params.table
* @returns {Object} table key usage
* @memberof MySQLClient
*/
async getKeyUsage ({ schema, table }) {
const { rows } = await this
.select('*')
.schema('information_schema')
.from('KEY_COLUMN_USAGE')
.where({ TABLE_SCHEMA: `= '${schema}'`, TABLE_NAME: `= '${table}'`, REFERENCED_TABLE_NAME: 'IS NOT NULL' })
.run();
return rows.map(field => {
return {
schema: field.TABLE_SCHEMA,
table: field.TABLE_NAME,
column: field.COLUMN_NAME,
position: field.ORDINAL_POSITION,
constraintPosition: field.POSITION_IN_UNIQUE_CONSTRAINT,
constraintName: field.CONSTRAINT_NAME,
refSchema: field.REFERENCED_TABLE_SCHEMA,
refTable: field.REFERENCED_TABLE_NAME,
refColumn: field.REFERENCED_COLUMN_NAME
};
});
}
/**
* SHOW COLLATION
*

View File

@ -26,9 +26,7 @@
<span>{{ table.name }}</span>
</a>
<div class="table-size tooltip tooltip-left mr-1" :data-tooltip="formatBytes(table.size)">
<div class="pie" :style="piePercentage(table.size)">
<div class="pie-center" />
</div>
<div class="pie" :style="piePercentage(table.size)" />
</div>
</li>
</ul>
@ -86,7 +84,7 @@ export default {
a.table-name {
display: flex;
align-items: center;
padding: 0.1rem;
padding: 0.1rem 1rem 0.1rem 0.1rem;
cursor: pointer;
font-size: 0.7rem;
@ -97,17 +95,27 @@ export default {
text-overflow: ellipsis;
}
&:hover {
color: inherit;
background: inherit;
}
.database-icon,
.table-icon {
opacity: 0.7;
}
}
.database-name {
&:hover {
color: $body-font-color;
background: rgba($color: #fff, $alpha: 0.05);
border-radius: 2px;
}
}
a.table-name {
&:hover {
color: inherit;
background: inherit;
}
}
.menu-item {
line-height: 1.2;
position: relative;
@ -131,7 +139,7 @@ export default {
display: flex;
align-items: center;
height: 100%;
opacity: 0.1;
opacity: 0.15;
transition: opacity 0.2s;
&:hover {