不要怂,就是干,撸起袖子干!

Commit 3657f32e by Ratanak Lun Committed by Sushant

fix(mssql): improper parameter binding in sql generation (#8782)

1 parent ccee348c
......@@ -144,16 +144,9 @@ class Query extends AbstractQuery {
static formatBindParameters(sql, values, dialect) {
const bindParam = {};
let i = 0;
const seen = {};
const replacementFunc = (match, key, values) => {
if (seen[key] !== undefined) {
return seen[key];
}
if (values[key] !== undefined) {
i = i + 1;
bindParam[key] = values[key];
seen[key] = '$' + i;
return '@' + key;
}
return undefined;
......
......@@ -3,13 +3,16 @@
const chai = require('chai'),
expect = chai.expect,
Sequelize = require(__dirname + '/../../../../index'),
Support = require(__dirname + '/../../support'),
dialect = Support.getTestDialect(),
tedious = require('tedious'),
sinon = require('sinon'),
connectionStub = sinon.stub(tedious, 'Connection');
connectionStub.returns({on() {}});
describe('[MSSQL] Connection Manager', () => {
if (dialect === 'mssql') {
describe('[MSSQL Specific] Connection Manager', () => {
let instance,
config;
beforeEach(() => {
......@@ -37,4 +40,5 @@ describe('[MSSQL] Connection Manager', () => {
instance.dialect.connectionManager._connect(config);
expect(config.dialectOptions.domain).to.equal('TEST.COM');
});
});
});
}
......@@ -2,7 +2,8 @@
const path = require('path');
const Query = require(path.resolve('./lib/dialects/mssql/query.js'));
const Support = require(path.resolve('./test/support'));
const Support = require(__dirname + '/../../support');
const dialect = Support.getTestDialect();
const sequelize = Support.sequelize;
const sinon = require('sinon');
const expect = require('chai').expect;
......@@ -12,7 +13,8 @@ const connectionStub = { beginTransaction: () => {}, lib: tedious };
let sandbox, query;
describe('[MSSQL]', () => {
if (dialect === 'mssql') {
describe('[MSSQL Specific] Query', () => {
describe('beginTransaction', () => {
beforeEach(() => {
sandbox = sinon.sandbox.create();
......@@ -40,4 +42,29 @@ describe('[MSSQL]', () => {
sandbox.restore();
});
});
});
describe('formatBindParameters', () => {
it('should convert Sequelize named binding format to MSSQL format', () => {
const sql = 'select $one as a, $two as b, $one as c, $three as d, $one as e';
const values = { one: 1, two: 2, three: 3 };
const expected = 'select @one as a, @two as b, @one as c, @three as d, @one as e';
const result = Query.formatBindParameters(sql, values, dialect);
expect(result[0]).to.be.a('string');
expect(result[0]).to.equal(expected);
});
it('should convert Sequelize numbered binding format to MSSQL format', () => {
const sql = 'select $1 as a, $2 as b, $1 as c, $3 as d, $1 as e';
const values = [1, 2, 3];
const expected = 'select @0 as a, @1 as b, @0 as c, @2 as d, @0 as e';
const result = Query.formatBindParameters(sql, values, dialect);
expect(result[0]).to.be.a('string');
expect(result[0]).to.equal(expected);
});
});
});
}
......@@ -2,9 +2,12 @@
const ResourceLock = require('../../../../lib/dialects/mssql/resource-lock'),
Promise = require('../../../../lib/promise'),
assert = require('assert');
assert = require('assert'),
Support = require(__dirname + '/../../support'),
dialect = Support.getTestDialect();
describe('[MSSQL Specific] ResourceLock', () => {
if (dialect === 'mssql') {
describe('[MSSQL Specific] ResourceLock', () => {
it('should process requests serially', () => {
const expected = {};
const lock = new ResourceLock(expected);
......@@ -61,4 +64,5 @@ describe('[MSSQL Specific] ResourceLock', () => {
assert.equal(lock.unwrap(), expected);
});
});
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!