mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-28 04:56:00 +00:00
refactor - default dbaget engine mvoed to dbgate-tools
This commit is contained in:
@@ -2,7 +2,7 @@ const uuidv1 = require('uuid/v1');
|
||||
const connections = require('./connections');
|
||||
const socket = require('../utility/socket');
|
||||
const { fork } = require('child_process');
|
||||
const DatabaseAnalyser = require('dbgate-engines/default/DatabaseAnalyser');
|
||||
const { DatabaseAnalyser } = require('dbgate-tools');
|
||||
|
||||
module.exports = {
|
||||
/** @type {import('dbgate-types').OpenedDatabaseConnection[]} */
|
||||
|
||||
@@ -2,7 +2,7 @@ const fp = require('lodash/fp');
|
||||
const _ = require('lodash');
|
||||
const sql = require('./sql');
|
||||
|
||||
const DatabaseAnalyser = require('../default/DatabaseAnalyser');
|
||||
const { DatabaseAnalyser } = require('dbgate-tools');
|
||||
const { filter } = require('lodash');
|
||||
const { isTypeString, isTypeNumeric } = require('dbgate-tools');
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const SqlDumper = require('../default/SqlDumper');
|
||||
const { SqlDumper } = require('dbgate-tools');
|
||||
|
||||
class MsSqlDumper extends SqlDumper {
|
||||
autoIncrement() {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const createBulkInsertStreamBase = require('../default/createBulkInsertStreamBase');
|
||||
const { createBulkInsertStreamBase } = require('dbgate-tools');
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const _ = require('lodash');
|
||||
const { driverBase } = require('dbgate-tools');
|
||||
const MsSqlAnalyser = require('./MsSqlAnalyser');
|
||||
const MsSqlDumper = require('./MsSqlDumper');
|
||||
const createBulkInsertStream = require('./createBulkInsertStream');
|
||||
const driverBase = require('../default/driverBase');
|
||||
|
||||
/** @type {import('dbgate-types').SqlDialect} */
|
||||
const dialect = {
|
||||
|
||||
@@ -2,7 +2,7 @@ const fp = require('lodash/fp');
|
||||
const _ = require('lodash');
|
||||
const sql = require('./sql');
|
||||
|
||||
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
||||
const { DatabaseAnalyser } = require('dbgate-tools');
|
||||
const { isTypeString, isTypeNumeric } = require('dbgate-tools');
|
||||
const { rangeStep } = require('lodash/fp');
|
||||
|
||||
@@ -35,7 +35,7 @@ function objectTypeToField(type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
class MySqlAnalyser extends DatabaseAnalayser {
|
||||
class MySqlAnalyser extends DatabaseAnalyser {
|
||||
constructor(pool, driver) {
|
||||
super(pool, driver);
|
||||
}
|
||||
@@ -103,8 +103,8 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
||||
tables: tables.rows.map((table) => ({
|
||||
...table,
|
||||
columns: columns.rows.filter((col) => col.pureName == table.pureName).map(getColumnInfo),
|
||||
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
||||
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
|
||||
primaryKey: DatabaseAnalyser.extractPrimaryKeys(table, pkColumns.rows),
|
||||
foreignKeys: DatabaseAnalyser.extractForeignKeys(table, fkColumns.rows),
|
||||
})),
|
||||
views: views.rows.map((view) => ({
|
||||
...view,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const SqlDumper = require('../default/SqlDumper');
|
||||
const { SqlDumper } = require('dbgate-tools');
|
||||
|
||||
class MySqlDumper extends SqlDumper {
|
||||
/** @param type {import('dbgate-types').TransformType} */
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const driverBase = require('../default/driverBase');
|
||||
const { driverBase } = require('dbgate-tools');
|
||||
const MySqlAnalyser = require('./MySqlAnalyser');
|
||||
const MySqlDumper = require('./MySqlDumper');
|
||||
|
||||
@@ -143,6 +143,12 @@ const driver = {
|
||||
const { rows } = await this.query(connection, 'show databases');
|
||||
return rows.map((x) => ({ name: x.Database }));
|
||||
},
|
||||
async writeTable(pool, name, options) {
|
||||
const { stream } = pool._nativeModules;
|
||||
// @ts-ignore
|
||||
return createBulkInsertStreamBase(this, stream, pool, name, options);
|
||||
},
|
||||
|
||||
// createDumper() {
|
||||
// return new MySqlDumper(this);
|
||||
// },
|
||||
|
||||
@@ -2,7 +2,7 @@ const fp = require('lodash/fp');
|
||||
const _ = require('lodash');
|
||||
const sql = require('./sql');
|
||||
|
||||
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
||||
const { DatabaseAnalyser } = require('dbgate-tools');
|
||||
const { isTypeString, isTypeNumeric } = require('dbgate-tools');
|
||||
|
||||
function normalizeTypeName(dataType) {
|
||||
@@ -35,7 +35,7 @@ function getColumnInfo({
|
||||
};
|
||||
}
|
||||
|
||||
class PostgreAnalyser extends DatabaseAnalayser {
|
||||
class PostgreAnalyser extends DatabaseAnalyser {
|
||||
constructor(pool, driver) {
|
||||
super(pool, driver);
|
||||
}
|
||||
@@ -84,8 +84,8 @@ class PostgreAnalyser extends DatabaseAnalayser {
|
||||
columns: columns.rows
|
||||
.filter((col) => col.pureName == table.pureName && col.schemaName == table.schemaName)
|
||||
.map(getColumnInfo),
|
||||
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
||||
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
|
||||
primaryKey: DatabaseAnalyser.extractPrimaryKeys(table, pkColumns.rows),
|
||||
foreignKeys: DatabaseAnalyser.extractForeignKeys(table, fkColumns.rows),
|
||||
})),
|
||||
views: views.rows.map((view) => ({
|
||||
...view,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const SqlDumper = require('../default/SqlDumper');
|
||||
const { SqlDumper } = require('dbgate-tools');
|
||||
|
||||
class PostgreDumper extends SqlDumper {
|
||||
/** @param type {import('dbgate-types').TransformType} */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const _ = require('lodash');
|
||||
const driverBase = require('../default/driverBase');
|
||||
const { driverBase } = require('dbgate-tools');
|
||||
const PostgreAnalyser = require('./PostgreAnalyser');
|
||||
const PostgreDumper = require('./PostgreDumper');
|
||||
|
||||
@@ -174,6 +174,11 @@ const driver = {
|
||||
// createDumper() {
|
||||
// return new PostgreDumper(this);
|
||||
// },
|
||||
async writeTable(pool, name, options) {
|
||||
const { stream } = pool._nativeModules;
|
||||
// @ts-ignore
|
||||
return createBulkInsertStreamBase(this, stream, pool, name, options);
|
||||
},
|
||||
async listDatabases(client) {
|
||||
const { rows } = await this.query(client, 'SELECT datname AS name FROM pg_database WHERE datistemplate = false');
|
||||
return rows;
|
||||
|
||||
@@ -1,21 +1,13 @@
|
||||
const _ = require('lodash');
|
||||
const fp = require('lodash/fp');
|
||||
import { DatabaseInfo, DatabaseModification, EngineDriver } from 'dbgate-types';
|
||||
import _ from 'lodash';
|
||||
import fp from 'lodash/fp';
|
||||
|
||||
class DatabaseAnalyser {
|
||||
/**
|
||||
*
|
||||
* @param {import('dbgate-types').EngineDriver} driver
|
||||
*/
|
||||
constructor(pool, driver) {
|
||||
this.pool = pool;
|
||||
this.driver = driver;
|
||||
// this.result = DatabaseAnalyser.createEmptyStructure();
|
||||
/** @type {import('dbgate-types').DatabaseInfo} */
|
||||
this.structure = null;
|
||||
/** import('dbgate-types').DatabaseModification[]) */
|
||||
this.modifications = null;
|
||||
this.singleObjectFilter = null;
|
||||
}
|
||||
export class DatabaseAnalyser {
|
||||
structure: DatabaseInfo;
|
||||
modifications: DatabaseModification[];
|
||||
singleObjectFilter: any;
|
||||
|
||||
constructor(public pool, public driver: EngineDriver) {}
|
||||
|
||||
async _runAnalysis() {
|
||||
return DatabaseAnalyser.createEmptyStructure();
|
||||
@@ -80,46 +72,46 @@ class DatabaseAnalyser {
|
||||
// findObjectById(id) {
|
||||
// return this.structure.tables.find((x) => x.objectId == id);
|
||||
// }
|
||||
|
||||
static createEmptyStructure(): DatabaseInfo {
|
||||
return {
|
||||
tables: [],
|
||||
views: [],
|
||||
functions: [],
|
||||
procedures: [],
|
||||
triggers: [],
|
||||
schemas: [],
|
||||
};
|
||||
}
|
||||
|
||||
static byTableFilter(table) {
|
||||
return (x) => x.pureName == table.pureName && x.schemaName == x.schemaName;
|
||||
}
|
||||
|
||||
static extractPrimaryKeys(table, pkColumns) {
|
||||
const filtered = pkColumns.filter(DatabaseAnalyser.byTableFilter(table));
|
||||
if (filtered.length == 0) return undefined;
|
||||
return {
|
||||
..._.pick(filtered[0], ['constraintName', 'schemaName', 'pureName']),
|
||||
constraintType: 'primaryKey',
|
||||
columns: filtered.map(fp.pick('columnName')),
|
||||
};
|
||||
}
|
||||
static extractForeignKeys(table, fkColumns) {
|
||||
const grouped = _.groupBy(fkColumns.filter(DatabaseAnalyser.byTableFilter(table)), 'constraintName');
|
||||
return _.keys(grouped).map((constraintName) => ({
|
||||
constraintName,
|
||||
constraintType: 'foreignKey',
|
||||
..._.pick(grouped[constraintName][0], [
|
||||
'constraintName',
|
||||
'schemaName',
|
||||
'pureName',
|
||||
'refSchemaName',
|
||||
'refTableName',
|
||||
'updateAction',
|
||||
'deleteAction',
|
||||
]),
|
||||
columns: grouped[constraintName].map(fp.pick(['columnName', 'refColumnName'])),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {import('dbgate-types').DatabaseInfo} */
|
||||
DatabaseAnalyser.createEmptyStructure = () => ({
|
||||
tables: [],
|
||||
views: [],
|
||||
functions: [],
|
||||
procedures: [],
|
||||
triggers: [],
|
||||
schemas: [],
|
||||
});
|
||||
|
||||
DatabaseAnalyser.byTableFilter = (table) => (x) => x.pureName == table.pureName && x.schemaName == x.schemaName;
|
||||
|
||||
DatabaseAnalyser.extractPrimaryKeys = (table, pkColumns) => {
|
||||
const filtered = pkColumns.filter(DatabaseAnalyser.byTableFilter(table));
|
||||
if (filtered.length == 0) return undefined;
|
||||
return {
|
||||
..._.pick(filtered[0], ['constraintName', 'schemaName', 'pureName']),
|
||||
constraintType: 'primaryKey',
|
||||
columns: filtered.map(fp.pick('columnName')),
|
||||
};
|
||||
};
|
||||
|
||||
DatabaseAnalyser.extractForeignKeys = (table, fkColumns) => {
|
||||
const grouped = _.groupBy(fkColumns.filter(DatabaseAnalyser.byTableFilter(table)), 'constraintName');
|
||||
return _.keys(grouped).map((constraintName) => ({
|
||||
constraintName,
|
||||
constraintType: 'foreignKey',
|
||||
..._.pick(grouped[constraintName][0], [
|
||||
'constraintName',
|
||||
'schemaName',
|
||||
'pureName',
|
||||
'refSchemaName',
|
||||
'refTableName',
|
||||
'updateAction',
|
||||
'deleteAction',
|
||||
]),
|
||||
columns: grouped[constraintName].map(fp.pick(['columnName', 'refColumnName'])),
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = DatabaseAnalyser;
|
||||
@@ -1,13 +1,24 @@
|
||||
const _ = require('lodash');
|
||||
const moment = require('moment');
|
||||
import {
|
||||
ColumnInfo,
|
||||
EngineDriver,
|
||||
ForeignKeyInfo,
|
||||
NamedObjectInfo,
|
||||
SqlDialect,
|
||||
TableInfo,
|
||||
TransformType,
|
||||
} from 'dbgate-types';
|
||||
import _ from 'lodash';
|
||||
import moment from 'moment';
|
||||
|
||||
class SqlDumper {
|
||||
/** @param driver {import('dbgate-types').EngineDriver} */
|
||||
constructor(driver) {
|
||||
this.s = '';
|
||||
export class SqlDumper {
|
||||
s = '';
|
||||
driver: EngineDriver;
|
||||
dialect: SqlDialect;
|
||||
indentLevel = 0;
|
||||
|
||||
constructor(driver: EngineDriver) {
|
||||
this.driver = driver;
|
||||
this.dialect = driver.dialect;
|
||||
this.indentLevel = 0;
|
||||
}
|
||||
endCommand() {
|
||||
this.putRaw(';\n');
|
||||
@@ -85,8 +96,7 @@ class SqlDumper {
|
||||
if (!collection) return;
|
||||
this.putCollection(', ', collection, (item) => this.putFormattedValue(c, item));
|
||||
}
|
||||
/** @param format {string} */
|
||||
put(format, ...args) {
|
||||
put(format: string, ...args) {
|
||||
let i = 0;
|
||||
let argIndex = 0;
|
||||
const length = format.length;
|
||||
@@ -149,10 +159,7 @@ class SqlDumper {
|
||||
this.put(' ^auto_increment');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param column {import('dbgate-types').ColumnInfo}
|
||||
*/
|
||||
columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
|
||||
columnDefinition(column: ColumnInfo, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
|
||||
if (column.computedExpression) {
|
||||
this.put('^as %s', column.computedExpression);
|
||||
if (column.isPersisted) this.put(' ^persisted');
|
||||
@@ -175,10 +182,7 @@ class SqlDumper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param column {import('dbgate-types').ColumnInfo}
|
||||
*/
|
||||
columnDefault(column) {
|
||||
columnDefault(column: ColumnInfo) {
|
||||
if (column.defaultConstraint != null) {
|
||||
this.put(' ^constraint %i ^default %s ', column.defaultConstraint, column.defaultValue);
|
||||
} else {
|
||||
@@ -186,13 +190,7 @@ class SqlDumper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {string} delimiter
|
||||
* @param {T[]} collection
|
||||
* @param {(col: T) => void} lambda
|
||||
*/
|
||||
putCollection(delimiter, collection, lambda) {
|
||||
putCollection<T>(delimiter: string, collection: T[], lambda: (col: T) => void) {
|
||||
if (!collection) return;
|
||||
let first = true;
|
||||
for (const item of collection) {
|
||||
@@ -202,8 +200,7 @@ class SqlDumper {
|
||||
}
|
||||
}
|
||||
|
||||
/** @param table {import('dbgate-types').TableInfo} */
|
||||
createTable(table) {
|
||||
createTable(table: TableInfo) {
|
||||
this.put('^create ^table %f ( &>&n', table);
|
||||
this.putCollection(',&n', table.columns, (col) => {
|
||||
this.put('%i ', col.columnName);
|
||||
@@ -245,8 +242,7 @@ class SqlDumper {
|
||||
// }
|
||||
}
|
||||
|
||||
/** @param fk {import('dbgate-types').ForeignKeyInfo} */
|
||||
createForeignKeyFore(fk) {
|
||||
createForeignKeyFore(fk: ForeignKeyInfo) {
|
||||
if (fk.constraintName != null) this.put('^constraint %i ', fk.constraintName);
|
||||
this.put(
|
||||
'^foreign ^key (%,i) ^references %f (%,i)',
|
||||
@@ -258,16 +254,9 @@ class SqlDumper {
|
||||
if (fk.updateAction) this.put(' ^on ^update %k', fk.updateAction);
|
||||
}
|
||||
|
||||
/** @param type {import('dbgate-types').TransformType} */
|
||||
transform(type, dumpExpr) {
|
||||
transform(type: TransformType, dumpExpr) {
|
||||
dumpExpr();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param table {import('dbgate-types').NamedObjectInfo}
|
||||
* @param allow {boolean}
|
||||
*/
|
||||
allowIdentityInsert(table, allow) {}
|
||||
allowIdentityInsert(table: NamedObjectInfo, allow: boolean) {}
|
||||
}
|
||||
|
||||
module.exports = SqlDumper;
|
||||
@@ -1,11 +1,8 @@
|
||||
const { prepareTableForImport } = require('dbgate-tools');
|
||||
const _ = require('lodash');
|
||||
import { EngineDriver } from 'dbgate-types';
|
||||
import _ from 'lodash';
|
||||
import { prepareTableForImport } from './tableTransforms';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('dbgate-types').EngineDriver} driver
|
||||
*/
|
||||
function createBulkInsertStreamBase(driver, stream, pool, name, options) {
|
||||
export function createBulkInsertStreamBase(driver, stream, pool, name, options): any {
|
||||
const fullNameQuoted = name.schemaName
|
||||
? `${driver.dialect.quoteIdentifier(name.schemaName)}.${driver.dialect.quoteIdentifier(name.pureName)}`
|
||||
: driver.dialect.quoteIdentifier(name.pureName);
|
||||
@@ -94,5 +91,3 @@ function createBulkInsertStreamBase(driver, stream, pool, name, options) {
|
||||
|
||||
return writable;
|
||||
}
|
||||
|
||||
module.exports = createBulkInsertStreamBase;
|
||||
@@ -1,6 +1,6 @@
|
||||
const createBulkInsertStreamBase = require('./createBulkInsertStreamBase');
|
||||
import { createBulkInsertStreamBase } from './createBulkInsertStreamBase';
|
||||
|
||||
const driverBase = {
|
||||
export const driverBase = {
|
||||
analyserClass: null,
|
||||
dumperClass: null,
|
||||
|
||||
@@ -24,11 +24,4 @@ const driverBase = {
|
||||
createDumper() {
|
||||
return new this.dumperClass(this);
|
||||
},
|
||||
async writeTable(pool, name, options) {
|
||||
const { stream, mssql } = pool._nativeModules;
|
||||
// @ts-ignore
|
||||
return createBulkInsertStreamBase(this, stream, pool, name, options);
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = driverBase;
|
||||
@@ -2,3 +2,7 @@ export * from './commonTypeParser';
|
||||
export * from './nameTools';
|
||||
export * from './tableTransforms';
|
||||
export * from './packageTools';
|
||||
export * from './createBulkInsertStreamBase';
|
||||
export * from './DatabaseAnalyser';
|
||||
export * from './driverBase';
|
||||
export * from './SqlDumper';
|
||||
|
||||
Reference in New Issue
Block a user