2020-03-24 18:47:53 +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-03-12 11:46:07 +00:00
|
|
|
stringEscapeChar: "'",
|
2020-02-02 18:27:25 +00:00
|
|
|
quoteIdentifier(s) {
|
|
|
|
return `[${s}]`;
|
2020-03-24 18:47:53 +00:00
|
|
|
},
|
2020-02-02 18:27:25 +00:00
|
|
|
};
|
|
|
|
|
2020-04-07 19:37:00 +00:00
|
|
|
function extractColumns(columns) {
|
2020-04-10 07:50:20 +00:00
|
|
|
return _.sortBy(_.values(columns), 'index').map((col) => ({
|
|
|
|
...col,
|
|
|
|
columnName: col.name,
|
|
|
|
notNull: !col.nullable,
|
|
|
|
autoIncrement: !!col.identity,
|
|
|
|
}));
|
2020-04-07 19:37:00 +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: {
|
2020-03-24 18:47:53 +00:00
|
|
|
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-03-24 18:47:53 +00:00
|
|
|
// console.log(resp);
|
|
|
|
const res = {};
|
|
|
|
|
|
|
|
if (resp.recordset) {
|
2020-04-07 19:37:00 +00:00
|
|
|
res.columns = extractColumns(resp.recordset.columns);
|
2020-03-24 18:47:53 +00:00
|
|
|
res.rows = resp.recordset;
|
|
|
|
}
|
|
|
|
if (resp.rowsAffected) {
|
|
|
|
res.rowsAffected = _.sum(resp.rowsAffected);
|
|
|
|
}
|
|
|
|
return res;
|
2020-01-04 20:59:53 +00:00
|
|
|
},
|
2020-04-05 18:48:04 +00:00
|
|
|
async stream(pool, sql, options) {
|
|
|
|
const request = await pool.request();
|
|
|
|
|
|
|
|
const handleInfo = (info) => {
|
|
|
|
const { message, lineNumber, procName } = info;
|
|
|
|
options.info({
|
|
|
|
message,
|
|
|
|
line: lineNumber,
|
|
|
|
procedure: procName,
|
|
|
|
time: new Date(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDone = (result) => {
|
2020-04-07 19:37:00 +00:00
|
|
|
// console.log('RESULT', result);
|
|
|
|
options.done(result);
|
2020-04-05 18:48:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleRow = (row) => {
|
2020-04-07 19:37:00 +00:00
|
|
|
options.row(row);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRecordset = (columns) => {
|
|
|
|
options.recordset(extractColumns(columns));
|
2020-04-05 18:48:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
request.stream = true;
|
2020-04-07 19:37:00 +00:00
|
|
|
request.on('recordset', handleRecordset);
|
2020-04-05 18:48:04 +00:00
|
|
|
request.on('row', handleRow);
|
|
|
|
request.on('error', options.error);
|
|
|
|
request.on('done', handleDone);
|
|
|
|
request.on('info', handleInfo);
|
|
|
|
request.query(sql);
|
|
|
|
},
|
2020-01-04 20:59:53 +00:00
|
|
|
async getVersion(pool) {
|
2020-03-24 18:47:53 +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-03-24 18:47:53 +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-03-23 19:41:40 +00:00
|
|
|
dialect,
|
|
|
|
engine: 'mssql',
|
2020-01-04 20:59:53 +00:00
|
|
|
};
|
2020-02-02 18:27:25 +00:00
|
|
|
|
|
|
|
module.exports = driver;
|