dbgate/packages/engines/mysql/MySqlAnalyser.js

42 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-03-14 09:38:10 +00:00
const fp = require('lodash/fp');
const _ = require('lodash');
const sql = require('./sql');
2020-02-02 18:27:25 +00:00
2020-03-14 09:38:10 +00:00
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
2020-02-02 18:27:25 +00:00
class MySqlAnalyser extends DatabaseAnalayser {
constructor(pool, driver) {
super(pool, driver);
}
2020-04-11 18:24:30 +00:00
createQuery(resFileName, tables = false, views = false, procedures = false, functions = false, triggers = false) {
2020-03-14 09:38:10 +00:00
let res = sql[resFileName];
res = res.replace('=[OBJECT_NAME_CONDITION]', ' is not null');
res = res.replace('#DATABASE#', this.pool._database_name);
2020-02-02 18:27:25 +00:00
return res;
}
2020-04-12 08:16:33 +00:00
async _runAnalysis() {
2020-04-11 18:24:30 +00:00
const tables = await this.driver.query(this.pool, this.createQuery('tables'));
const columns = await this.driver.query(this.pool, this.createQuery('columns'));
2020-05-16 06:58:16 +00:00
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys'));
2020-05-16 07:34:28 +00:00
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys'));
2020-02-02 18:27:25 +00:00
2020-04-12 08:16:33 +00:00
return this.mergeAnalyseResult({
tables: tables.rows.map((table) => ({
...table,
columns: columns.rows
.filter((col) => col.pureName == table.pureName)
.map(({ isNullable, extra, ...col }) => ({
...col,
notNull: !isNullable,
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
})),
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
2020-04-12 08:16:33 +00:00
})),
});
2020-02-02 18:27:25 +00:00
}
}
module.exports = MySqlAnalyser;