mirror of https://github.com/Fabio286/antares.git
refactor(mysql): moved specific queries inside MySQLClient class
This commit is contained in:
parent
426628f268
commit
053418ee90
|
@ -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 {
|
try {
|
||||||
const query = `SELECT \`DEFAULT_COLLATION_NAME\` FROM \`information_schema\`.\`SCHEMATA\` WHERE \`SCHEMA_NAME\`='${params.database}'`;
|
const query = `SELECT \`DEFAULT_COLLATION_NAME\` FROM \`information_schema\`.\`SCHEMATA\` WHERE \`SCHEMA_NAME\`='${params.database}'`;
|
||||||
const collation = await connections[params.uid].raw(query);
|
const collation = await connections[params.uid].raw(query);
|
||||||
|
|
|
@ -3,37 +3,10 @@ import { sqlEscaper } from 'common/libs/sqlEscaper';
|
||||||
import { TEXT, LONG_TEXT, NUMBER, BLOB } from 'common/fieldTypes';
|
import { TEXT, LONG_TEXT, NUMBER, BLOB } from 'common/fieldTypes';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
// TODO: remap objects based on client
|
|
||||||
|
|
||||||
export default (connections) => {
|
export default (connections) => {
|
||||||
ipcMain.handle('get-table-columns', async (event, { uid, schema, table }) => {
|
ipcMain.handle('get-table-columns', async (event, params) => {
|
||||||
try {
|
try {
|
||||||
const { rows } = await connections[uid]
|
const result = await connections[params.uid].getTableColumns(params);
|
||||||
.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
|
|
||||||
};
|
|
||||||
});
|
|
||||||
return { status: 'success', response: result };
|
return { status: 'success', response: result };
|
||||||
}
|
}
|
||||||
catch (err) {
|
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 {
|
try {
|
||||||
const { rows } = await connections[uid]
|
const result = await connections[params.uid].getKeyUsage(params);
|
||||||
.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
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return { status: 'success', response: result };
|
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 {
|
try {
|
||||||
const query = connections[params.uid]
|
const query = connections[uid]
|
||||||
.select(`${params.column} AS foreignColumn`)
|
.select(`${column} AS foreignColumn`)
|
||||||
.schema(params.schema)
|
.schema(schema)
|
||||||
.from(params.table)
|
.from(table)
|
||||||
.orderBy('foreignColumn ASC');
|
.orderBy('foreignColumn ASC');
|
||||||
|
|
||||||
if (params.description)
|
if (description)
|
||||||
query.select(`LEFT(${params.description}, 20) AS foreignDescription`);
|
query.select(`LEFT(${description}, 20) AS foreignDescription`);
|
||||||
|
|
||||||
const results = await query.run();
|
const results = await query.run();
|
||||||
|
|
||||||
|
|
|
@ -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
|
* SHOW COLLATION
|
||||||
*
|
*
|
||||||
|
|
|
@ -26,9 +26,7 @@
|
||||||
<span>{{ table.name }}</span>
|
<span>{{ table.name }}</span>
|
||||||
</a>
|
</a>
|
||||||
<div class="table-size tooltip tooltip-left mr-1" :data-tooltip="formatBytes(table.size)">
|
<div class="table-size tooltip tooltip-left mr-1" :data-tooltip="formatBytes(table.size)">
|
||||||
<div class="pie" :style="piePercentage(table.size)">
|
<div class="pie" :style="piePercentage(table.size)" />
|
||||||
<div class="pie-center" />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -86,7 +84,7 @@ export default {
|
||||||
a.table-name {
|
a.table-name {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
padding: 0.1rem;
|
padding: 0.1rem 1rem 0.1rem 0.1rem;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
font-size: 0.7rem;
|
font-size: 0.7rem;
|
||||||
|
|
||||||
|
@ -97,17 +95,27 @@ export default {
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
|
|
||||||
&:hover {
|
|
||||||
color: inherit;
|
|
||||||
background: inherit;
|
|
||||||
}
|
|
||||||
|
|
||||||
.database-icon,
|
.database-icon,
|
||||||
.table-icon {
|
.table-icon {
|
||||||
opacity: 0.7;
|
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 {
|
.menu-item {
|
||||||
line-height: 1.2;
|
line-height: 1.2;
|
||||||
position: relative;
|
position: relative;
|
||||||
|
@ -131,7 +139,7 @@ export default {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
opacity: 0.1;
|
opacity: 0.15;
|
||||||
transition: opacity 0.2s;
|
transition: opacity 0.2s;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
|
|
Loading…
Reference in New Issue