mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 21:46:00 +00:00
engines moved to separate library
This commit is contained in:
53
lib/engines/mysql/MySqlAnalyser.js
Normal file
53
lib/engines/mysql/MySqlAnalyser.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const fs = require('fs-extra');
|
||||
const fp = require('lodash/fp');
|
||||
const path = require('path');
|
||||
const _ = require('lodash');
|
||||
|
||||
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
||||
|
||||
/** @returns {Promise<string>} */
|
||||
async function loadQuery(name) {
|
||||
return await fs.readFile(path.join(__dirname, name), 'utf-8');
|
||||
}
|
||||
|
||||
class MySqlAnalyser extends DatabaseAnalayser {
|
||||
constructor(pool, driver) {
|
||||
super(pool, driver);
|
||||
}
|
||||
|
||||
async createQuery(
|
||||
resFileName,
|
||||
tables = false,
|
||||
views = false,
|
||||
procedures = false,
|
||||
functions = false,
|
||||
triggers = false
|
||||
) {
|
||||
let res = await loadQuery(resFileName);
|
||||
res = res.replace('=[OBJECT_NAME_CONDITION]', ' is not null');
|
||||
res = res.replace('#DATABASE#', this.pool._database_name);
|
||||
return res;
|
||||
}
|
||||
async runAnalysis() {
|
||||
const tables = await this.driver.query(this.pool, await this.createQuery('tables.sql'));
|
||||
const columns = await this.driver.query(this.pool, await this.createQuery('columns.sql'));
|
||||
// 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
|
||||
.filter(col => col.pureName == table.pureName)
|
||||
.map(({ isNullable, extra, ...col }) => ({
|
||||
...col,
|
||||
notNull: !isNullable,
|
||||
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
|
||||
})),
|
||||
foreignKeys: [],
|
||||
// primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
||||
// foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MySqlAnalyser;
|
||||
5
lib/engines/mysql/MySqlDumper.js
Normal file
5
lib/engines/mysql/MySqlDumper.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const SqlDumper = require('../default/SqlDumper');
|
||||
|
||||
class MySqlDumper extends SqlDumper {}
|
||||
|
||||
module.exports = MySqlDumper;
|
||||
13
lib/engines/mysql/columns.sql
Normal file
13
lib/engines/mysql/columns.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
select
|
||||
TABLE_NAME as pureName,
|
||||
COLUMN_NAME as columnName,
|
||||
IS_NULLABLE as isNullable,
|
||||
DATA_TYPE as dataType,
|
||||
CHARACTER_MAXIMUM_LENGTH,
|
||||
NUMERIC_PRECISION,
|
||||
NUMERIC_SCALE,
|
||||
COLUMN_DEFAULT,
|
||||
EXTRA as extra
|
||||
from INFORMATION_SCHEMA.COLUMNS
|
||||
where TABLE_SCHEMA = '#DATABASE#' and TABLE_NAME =[OBJECT_NAME_CONDITION]
|
||||
order by ORDINAL_POSITION
|
||||
56
lib/engines/mysql/index.js
Normal file
56
lib/engines/mysql/index.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const MySqlAnalyser = require("./MySqlAnalyser");
|
||||
const MySqlDumper = require("./MySqlDumper");
|
||||
|
||||
/** @type {import('dbgate').SqlDialect} */
|
||||
const dialect = {
|
||||
rangeSelect: true,
|
||||
quoteIdentifier(s) {
|
||||
return "`" + s + "`";
|
||||
}
|
||||
};
|
||||
|
||||
/** @type {import('dbgate').EngineDriver} */
|
||||
const driver = {
|
||||
async connect({ mysql }, { server, port, user, password, database }) {
|
||||
const connection = mysql.createConnection({
|
||||
host: server,
|
||||
port,
|
||||
user,
|
||||
password,
|
||||
database
|
||||
});
|
||||
connection._database_name = database;
|
||||
return connection;
|
||||
},
|
||||
async query(connection, sql) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(sql, function(error, results, fields) {
|
||||
if (error) reject(error);
|
||||
resolve({ rows: results, columns: fields });
|
||||
});
|
||||
});
|
||||
},
|
||||
async getVersion(connection) {
|
||||
const { rows } = await this.query(
|
||||
connection,
|
||||
"show variables like 'version'"
|
||||
);
|
||||
const version = rows[0].Value;
|
||||
return { version };
|
||||
},
|
||||
async analyseFull(pool) {
|
||||
const analyser = new MySqlAnalyser(pool, this);
|
||||
await analyser.runAnalysis();
|
||||
return analyser.result;
|
||||
},
|
||||
async listDatabases(connection) {
|
||||
const { rows } = await this.query(connection, "show databases");
|
||||
return rows.map(x => ({ name: x.Database }));
|
||||
},
|
||||
createDumper() {
|
||||
return new MySqlDumper(this);
|
||||
},
|
||||
dialect
|
||||
};
|
||||
|
||||
module.exports = driver;
|
||||
5
lib/engines/mysql/tables.sql
Normal file
5
lib/engines/mysql/tables.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
select
|
||||
TABLE_NAME as pureName,
|
||||
case when ENGINE='InnoDB' then CREATE_TIME else coalesce(UPDATE_TIME, CREATE_TIME) end as alterTime
|
||||
from information_schema.tables
|
||||
where TABLE_SCHEMA = '#DATABASE#' and TABLE_NAME =[OBJECT_NAME_CONDITION];
|
||||
Reference in New Issue
Block a user