dbgate/api/src/engines/mssql/MsSqlAnalyser.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

const fs = require('fs-extra');
const path = require('path');
2020-02-02 10:31:41 +00:00
const _ = require('lodash');
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
2020-01-19 20:01:48 +00:00
/** @returns {Promise<string>} */
async function loadQuery(name) {
return await fs.readFile(path.join(__dirname, name), 'utf-8');
}
class MsSqlAnalyser extends DatabaseAnalayser {
constructor(pool, driver) {
super(pool, driver);
}
2020-01-19 20:01:48 +00:00
async createQuery(
resFileName,
tables = false,
views = false,
procedures = false,
functions = false,
triggers = false
) {
let res = await loadQuery(resFileName);
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
return res;
}
async runAnalysis() {
2020-01-19 20:01:48 +00:00
const tables = await this.driver.query(this.pool, await this.createQuery('tables.sql'));
2020-02-02 10:31:41 +00:00
const columns = await this.driver.query(this.pool, await this.createQuery('columns.sql'));
this.result.tables = tables.rows.map(table => ({
...table,
columns: columns.rows
.filter(col => col.objectId == table.objectId)
.map(({ isNullable, isIdentity, ...col }) => ({
...col,
notNull: isNullable != 'True',
autoIncrement: isIdentity == 'True',
})),
}));
}
}
module.exports = MsSqlAnalyser;