dbgate/packages/engines/mysql/MySqlAnalyser.js

48 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);
}
async 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;
}
async runAnalysis() {
2020-03-14 09:38:10 +00:00
const tables = await this.driver.query(this.pool, await this.createQuery('tables'));
const columns = await this.driver.query(this.pool, await this.createQuery('columns'));
2020-02-02 18:27:25 +00:00
// const pkColumns = await this.driver.query(this.pool, await this.createQuery('primary_keys.sql'));
// const fkColumns = await this.driver.query(this.pool, await this.createQuery('foreign_keys.sql'));
this.result.tables = tables.rows.map(table => ({
...table,
columns: columns.rows
2020-02-02 19:28:27 +00:00
.filter(col => col.pureName == table.pureName)
2020-02-02 18:27:25 +00:00
.map(({ isNullable, extra, ...col }) => ({
...col,
notNull: !isNullable,
2020-03-14 09:38:10 +00:00
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
2020-02-02 18:27:25 +00:00
})),
2020-03-14 09:38:10 +00:00
foreignKeys: [],
2020-02-02 18:27:25 +00:00
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
}));
}
}
module.exports = MySqlAnalyser;