dbgate/api/src/engines/mysql/index.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-01-04 20:59:53 +00:00
const mysql = require('mysql');
2020-02-02 18:27:25 +00:00
const MySqlAnalyser = require('./MySqlAnalyser');
const MySqlDumper = require('./MySqlDumper');
2020-01-04 20:59:53 +00:00
2020-02-02 18:27:25 +00:00
/** @type {import('dbgate').SqlDialect} */
const dialect = {
rangeSelect: true,
quoteIdentifier(s) {
return '`' + s + '`';
},
};
/** @type {import('dbgate').EngineDriver} */
const driver = {
2020-01-19 20:01:48 +00:00
async connect({ server, port, user, password, database }) {
const connection = mysql.createConnection({ host: server, port, user, password, database });
2020-02-02 18:27:25 +00:00
connection._database_name = database;
2020-01-04 20:59:53 +00:00
return connection;
},
async query(connection, sql) {
return new Promise((resolve, reject) => {
connection.query(sql, function(error, results, fields) {
if (error) reject(error);
2020-01-25 16:26:51 +00:00
resolve({ rows: results });
2020-01-04 20:59:53 +00:00
});
});
},
async getVersion(connection) {
2020-01-25 16:26:51 +00:00
const { rows } = await this.query(connection, "show variables like 'version'");
2020-01-04 20:59:53 +00:00
const version = rows[0].Value;
return { version };
},
2020-02-02 18:27:25 +00:00
async analyseFull(pool) {
const analyser = new MySqlAnalyser(pool, this);
await analyser.runAnalysis();
return analyser.result;
},
2020-01-04 20:59:53 +00:00
async listDatabases(connection) {
2020-01-25 16:26:51 +00:00
const { rows } = await this.query(connection, 'show databases');
return rows.map(x => ({ name: x.Database }));
2020-01-04 20:59:53 +00:00
},
2020-02-02 18:27:25 +00:00
createDumper() {
return new MySqlDumper(this);
},
dialect,
2020-01-04 20:59:53 +00:00
};
2020-02-02 18:27:25 +00:00
module.exports = driver;