2020-05-16 16:47:27 +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,
|
2020-05-16 16:47:27 +00:00
|
|
|
stringEscapeChar: '\\',
|
2020-02-02 18:27:25 +00:00
|
|
|
quoteIdentifier(s) {
|
2020-05-16 16:47:27 +00:00
|
|
|
return '`' + s + '`';
|
|
|
|
},
|
2020-02-02 18:27:25 +00:00
|
|
|
};
|
|
|
|
|
2020-05-16 16:47:27 +00:00
|
|
|
function extractColumns(fields) {
|
|
|
|
return fields.map((col) => ({
|
|
|
|
columnName: col.name,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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-03 19:34:38 +00:00
|
|
|
async connect(nativeModules, { server, port, user, password, database }) {
|
|
|
|
const connection = nativeModules.mysql.createConnection({
|
2020-02-02 21:27:49 +00:00
|
|
|
host: server,
|
|
|
|
port,
|
|
|
|
user,
|
|
|
|
password,
|
2020-05-16 16:47:27 +00:00
|
|
|
database,
|
2020-02-02 21:27:49 +00:00
|
|
|
});
|
2020-02-02 18:27:25 +00:00
|
|
|
connection._database_name = database;
|
2020-02-03 19:34:38 +00:00
|
|
|
connection._nativeModules = nativeModules;
|
2020-01-04 20:59:53 +00:00
|
|
|
return connection;
|
|
|
|
},
|
|
|
|
async query(connection, sql) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-05-16 16:47:27 +00:00
|
|
|
connection.query(sql, function (error, results, fields) {
|
2020-01-04 20:59:53 +00:00
|
|
|
if (error) reject(error);
|
2020-05-16 16:47:27 +00:00
|
|
|
resolve({ rows: results, columns: extractColumns(fields) });
|
2020-01-04 20:59:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2020-05-16 16:47:27 +00:00
|
|
|
async stream(connection, sql, options) {
|
|
|
|
const query = connection.query(sql);
|
|
|
|
|
|
|
|
// const handleInfo = (info) => {
|
|
|
|
// const { message, lineNumber, procName } = info;
|
|
|
|
// options.info({
|
|
|
|
// message,
|
|
|
|
// line: lineNumber,
|
|
|
|
// procedure: procName,
|
|
|
|
// time: new Date(),
|
|
|
|
// severity: 'info',
|
|
|
|
// });
|
|
|
|
// };
|
|
|
|
|
|
|
|
const handleEnd = (result) => {
|
|
|
|
// console.log('RESULT', result);
|
|
|
|
options.done(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRow = (row) => {
|
|
|
|
options.row(row);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleFields = (columns) => {
|
|
|
|
console.log('FIELDS', columns[0].name);
|
|
|
|
options.recordset(extractColumns(columns));
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleError = (error) => {
|
|
|
|
console.log('ERROR', error);
|
|
|
|
const { message, lineNumber, procName } = error;
|
|
|
|
options.info({
|
|
|
|
message,
|
|
|
|
line: lineNumber,
|
|
|
|
procedure: procName,
|
|
|
|
time: new Date(),
|
|
|
|
severity: 'error',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
query.on('error', handleError).on('fields', handleFields).on('result', handleRow).on('end', handleEnd);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
},
|
2020-01-04 20:59:53 +00:00
|
|
|
async getVersion(connection) {
|
2020-05-16 16:47:27 +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);
|
2020-04-12 08:16:33 +00:00
|
|
|
return analyser.fullAnalysis();
|
|
|
|
},
|
|
|
|
async analyseIncremental(pool, structure) {
|
|
|
|
const analyser = new MySqlAnalyser(pool, this);
|
|
|
|
return analyser.incrementalAnalysis(structure);
|
2020-02-02 18:27:25 +00:00
|
|
|
},
|
2020-01-04 20:59:53 +00:00
|
|
|
async listDatabases(connection) {
|
2020-05-16 16:47:27 +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);
|
|
|
|
},
|
2020-03-23 19:41:40 +00:00
|
|
|
dialect,
|
|
|
|
engine: 'mysql',
|
2020-01-04 20:59:53 +00:00
|
|
|
};
|
2020-02-02 18:27:25 +00:00
|
|
|
|
|
|
|
module.exports = driver;
|