2020-02-02 21:27:49 +00:00
|
|
|
const _ = require("lodash");
|
|
|
|
const MsSqlAnalyser = require("./MsSqlAnalyser");
|
|
|
|
const MsSqlDumper = require("./MsSqlDumper");
|
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 = {
|
|
|
|
limitSelect: true,
|
2020-03-05 14:04:06 +00:00
|
|
|
rangeSelect: true,
|
|
|
|
offsetFetchRangeSyntax: true,
|
2020-02-02 18:27:25 +00:00
|
|
|
quoteIdentifier(s) {
|
|
|
|
return `[${s}]`;
|
2020-02-02 21:27:49 +00:00
|
|
|
}
|
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-03 19:34:38 +00:00
|
|
|
async connect(nativeModules, { server, port, user, password, database }) {
|
|
|
|
const pool = await nativeModules.mssql.connect({
|
2020-02-02 21:27:49 +00:00
|
|
|
server,
|
|
|
|
port,
|
|
|
|
user,
|
|
|
|
password,
|
2020-03-05 10:42:16 +00:00
|
|
|
database,
|
|
|
|
options: {
|
|
|
|
enableArithAbort: true
|
|
|
|
}
|
2020-02-02 21:27:49 +00:00
|
|
|
});
|
2020-02-03 19:34:38 +00:00
|
|
|
pool._nativeModules = nativeModules;
|
2020-01-04 20:59:53 +00:00
|
|
|
return pool;
|
|
|
|
},
|
|
|
|
async query(pool, sql) {
|
|
|
|
const resp = await pool.request().query(sql);
|
2020-01-25 16:26:51 +00:00
|
|
|
// console.log(Object.keys(resp.recordset));
|
2020-02-02 21:27:49 +00:00
|
|
|
const columns = _.sortBy(_.values(resp.recordset.columns), "index");
|
2020-01-25 16:26:51 +00:00
|
|
|
return { rows: resp.recordset, columns };
|
2020-01-04 20:59:53 +00:00
|
|
|
},
|
|
|
|
async getVersion(pool) {
|
2020-02-02 21:27:49 +00:00
|
|
|
const { version } = (
|
|
|
|
await this.query(pool, "SELECT @@VERSION AS version")
|
|
|
|
).rows[0];
|
2020-01-04 20:59:53 +00:00
|
|
|
return { version };
|
|
|
|
},
|
|
|
|
async listDatabases(pool) {
|
2020-02-02 21:27:49 +00:00
|
|
|
const { rows } = await this.query(
|
|
|
|
pool,
|
|
|
|
"SELECT name FROM sys.databases order by name"
|
|
|
|
);
|
2020-01-25 16:26:51 +00:00
|
|
|
return rows;
|
2020-01-04 20:59:53 +00:00
|
|
|
},
|
2020-01-06 21:33:20 +00:00
|
|
|
async analyseFull(pool) {
|
2020-01-19 20:01:48 +00:00
|
|
|
const analyser = new MsSqlAnalyser(pool, this);
|
|
|
|
await analyser.runAnalysis();
|
|
|
|
return analyser.result;
|
2020-01-06 21:33:20 +00:00
|
|
|
},
|
2020-02-02 18:27:25 +00:00
|
|
|
// async analyseIncremental(pool) {},
|
|
|
|
createDumper() {
|
|
|
|
return new MsSqlDumper(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;
|