dbgate/packages/engines/mysql/index.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-02-02 21:27:49 +00:00
const MySqlAnalyser = require("./MySqlAnalyser");
const MySqlDumper = require("./MySqlDumper");
2020-01-04 20:59:53 +00:00
2020-02-03 18:52:02 +00:00
/** @type {import('@dbgate/types').SqlDialect} */
2020-02-02 18:27:25 +00:00
const dialect = {
rangeSelect: true,
quoteIdentifier(s) {
2020-02-02 21:27:49 +00:00
return "`" + s + "`";
}
2020-02-02 18:27:25 +00:00
};
2020-02-03 18:52:02 +00:00
/** @type {import('@dbgate/types').EngineDriver} */
2020-02-02 18:27:25 +00:00
const driver = {
2020-02-02 21:27:49 +00:00
async connect({ mysql }, { 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-02-02 18:34:04 +00:00
resolve({ rows: results, columns: fields });
2020-01-04 20:59:53 +00:00
});
});
},
async getVersion(connection) {
2020-02-02 21:27:49 +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-02-02 21:27:49 +00:00
const { rows } = await this.query(connection, "show databases");
2020-01-25 16:26:51 +00:00
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);
},
2020-02-02 21:27:49 +00:00
dialect
2020-01-04 20:59:53 +00:00
};
2020-02-02 18:27:25 +00:00
module.exports = driver;