dbgate/packages/engines/postgres/PostgreAnalyser.js

76 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-04-11 18:24:30 +00:00
const fp = require('lodash/fp');
const _ = require('lodash');
const sql = require('./sql');
2020-02-02 19:28:27 +00:00
2020-04-11 18:24:30 +00:00
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
2020-06-26 13:28:44 +00:00
const { isTypeString, isTypeNumeric } = require('@dbgate/tools');
function normalizeTypeName(dataType) {
if (dataType == 'character varying') return 'varchar';
if (dataType == 'timestamp without time zone') return 'timestamp';
return dataType;
}
function getColumnInfo({
isNullable,
isIdentity,
columnName,
dataType,
charMaxLength,
numericPrecision,
numericScale,
defaultValue,
}) {
const normDataType = normalizeTypeName(dataType);
let fullDataType = normDataType;
if (charMaxLength && isTypeString(normDataType)) fullDataType = `${normDataType}(${charMaxLength})`;
if (numericPrecision && numericScale && isTypeNumeric(normDataType))
fullDataType = `${normDataType}(${numericPrecision},${numericScale})`;
return {
columnName,
dataType: fullDataType,
2020-06-29 17:49:54 +00:00
notNull: !isNullable || isNullable == 'NO' || isNullable == 'no',
2020-06-26 13:28:44 +00:00
autoIncrement: !!isIdentity,
defaultValue,
};
}
2020-02-02 19:28:27 +00:00
2020-05-16 07:34:28 +00:00
class PostgreAnalyser extends DatabaseAnalayser {
2020-02-02 19:28:27 +00:00
constructor(pool, driver) {
super(pool, driver);
}
2020-06-29 17:49:54 +00:00
createQuery(resFileName, typeFields) {
2020-03-14 09:38:10 +00:00
let res = sql[resFileName];
2020-04-11 18:24:30 +00:00
res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null');
2020-02-02 19:28:27 +00:00
return res;
}
2020-04-12 08:16:33 +00:00
async _runAnalysis() {
2020-06-29 17:49:54 +00:00
const tables = await this.driver.query(this.pool, this.createQuery('tableModifications', ['tables']));
const columns = await this.driver.query(this.pool, this.createQuery('columns', ['tables']));
const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables']));
const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables']));
const views = await this.driver.query(this.pool, this.createQuery('views', ['views']));
2020-05-16 07:34:28 +00:00
// console.log('PG fkColumns', fkColumns.rows);
2020-02-02 19:28:27 +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 && col.schemaName == table.schemaName)
2020-06-26 13:28:44 +00:00
.map(getColumnInfo),
2020-05-16 07:34:28 +00:00
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
2020-04-12 08:16:33 +00:00
})),
2020-06-29 17:49:54 +00:00
views: views.rows.map((view) => ({
...view,
columns: columns.rows
.filter((col) => col.pureName == view.pureName && col.schemaName == view.schemaName)
.map(getColumnInfo),
})),
2020-04-12 08:16:33 +00:00
});
2020-02-02 19:28:27 +00:00
}
}
2020-05-16 07:34:28 +00:00
module.exports = PostgreAnalyser;