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

Commit 41237ae1 by Pablo Soares Committed by GitHub

fix(mssql): set correct scale for float (#12340)

1 parent 5c733ef7
...@@ -9,6 +9,13 @@ const { logger } = require('../../utils/logger'); ...@@ -9,6 +9,13 @@ const { logger } = require('../../utils/logger');
const debug = logger.debugContext('sql:mssql'); const debug = logger.debugContext('sql:mssql');
function getScale(aNum) {
if (!Number.isFinite(aNum)) return 0;
let e = 1;
while (Math.round(aNum * e) / e !== aNum) e *= 10;
return Math.log10(e);
}
class Query extends AbstractQuery { class Query extends AbstractQuery {
getInsertIdField() { getInsertIdField() {
return 'id'; return 'id';
...@@ -27,7 +34,7 @@ class Query extends AbstractQuery { ...@@ -27,7 +34,7 @@ class Query extends AbstractQuery {
} else { } else {
paramType.type = TYPES.Numeric; paramType.type = TYPES.Numeric;
//Default to a reasonable numeric precision/scale pending more sophisticated logic //Default to a reasonable numeric precision/scale pending more sophisticated logic
paramType.typeOptions = { precision: 30, scale: 15 }; paramType.typeOptions = { precision: 30, scale: getScale(value) };
} }
} }
if (Buffer.isBuffer(value)) { if (Buffer.isBuffer(value)) {
......
...@@ -15,18 +15,22 @@ let sandbox, query; ...@@ -15,18 +15,22 @@ let sandbox, query;
if (dialect === 'mssql') { if (dialect === 'mssql') {
describe('[MSSQL Specific] Query', () => { describe('[MSSQL Specific] Query', () => {
describe('beginTransaction', () => { beforeEach(() => {
beforeEach(() => { sandbox = sinon.createSandbox();
sandbox = sinon.createSandbox(); const options = {
const options = { transaction: { name: 'transactionName' },
transaction: { name: 'transactionName' }, isolationLevel: 'REPEATABLE_READ',
isolationLevel: 'REPEATABLE_READ', logging: false
logging: false };
}; sandbox.stub(connectionStub, 'beginTransaction').callsArg(0);
sandbox.stub(connectionStub, 'beginTransaction').callsArg(0); query = new Query(connectionStub, sequelize, options);
query = new Query(connectionStub, sequelize, options); });
});
afterEach(() => {
sandbox.restore();
});
describe('beginTransaction', () => {
it('should call beginTransaction with correct arguments', () => { it('should call beginTransaction with correct arguments', () => {
return query._run(connectionStub, 'BEGIN TRANSACTION') return query._run(connectionStub, 'BEGIN TRANSACTION')
.then(() => { .then(() => {
...@@ -35,10 +39,6 @@ if (dialect === 'mssql') { ...@@ -35,10 +39,6 @@ if (dialect === 'mssql') {
expect(connectionStub.beginTransaction.args[0][2]).to.equal(tediousIsolationLevel.REPEATABLE_READ); expect(connectionStub.beginTransaction.args[0][2]).to.equal(tediousIsolationLevel.REPEATABLE_READ);
}); });
}); });
afterEach(() => {
sandbox.restore();
});
}); });
describe('formatBindParameters', () => { describe('formatBindParameters', () => {
...@@ -64,5 +64,25 @@ if (dialect === 'mssql') { ...@@ -64,5 +64,25 @@ if (dialect === 'mssql') {
expect(result[0]).to.equal(expected); expect(result[0]).to.equal(expected);
}); });
}); });
describe('getSQLTypeFromJsType', () => {
const TYPES = tedious.TYPES;
it('should return correct parameter type', () => {
expect(query.getSQLTypeFromJsType(2147483647, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {} });
expect(query.getSQLTypeFromJsType(-2147483648, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {} });
expect(query.getSQLTypeFromJsType(2147483648, TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {} });
expect(query.getSQLTypeFromJsType(-2147483649, TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {} });
expect(query.getSQLTypeFromJsType(Buffer.from('abc'), TYPES)).to.eql({ type: TYPES.VarBinary, typeOptions: {} });
});
it('should return parameter type correct scale for float', () => {
expect(query.getSQLTypeFromJsType(1.23, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 2 } });
expect(query.getSQLTypeFromJsType(0.30000000000000004, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 17 } });
expect(query.getSQLTypeFromJsType(2.5e-15, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 16 } });
});
});
}); });
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!