2020-01-06 21:33:20 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2020-02-02 10:31:41 +00:00
|
|
|
const _ = require('lodash');
|
2020-01-06 21:33:20 +00:00
|
|
|
|
|
|
|
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
|
|
|
|
2020-01-19 20:01:48 +00:00
|
|
|
/** @returns {Promise<string>} */
|
2020-01-06 21:33:20 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-01-06 21:33:20 +00:00
|
|
|
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',
|
|
|
|
})),
|
|
|
|
}));
|
2020-01-06 21:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MsSqlAnalyser;
|