Skip to content

Commit

Permalink
fix(mssql): set correct scale for float (#12340)
Browse files Browse the repository at this point in the history
  • Loading branch information
firefueled committed Jun 4, 2020
1 parent 5c733ef commit 41237ae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
9 changes: 8 additions & 1 deletion lib/dialects/mssql/query.js
Expand Up @@ -9,6 +9,13 @@ const { logger } = require('../../utils/logger');

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 {
getInsertIdField() {
return 'id';
Expand All @@ -27,7 +34,7 @@ class Query extends AbstractQuery {
} else {
paramType.type = TYPES.Numeric;
//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)) {
Expand Down
50 changes: 35 additions & 15 deletions test/unit/dialects/mssql/query.test.js
Expand Up @@ -15,18 +15,22 @@ let sandbox, query;

if (dialect === 'mssql') {
describe('[MSSQL Specific] Query', () => {
describe('beginTransaction', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
const options = {
transaction: { name: 'transactionName' },
isolationLevel: 'REPEATABLE_READ',
logging: false
};
sandbox.stub(connectionStub, 'beginTransaction').callsArg(0);
query = new Query(connectionStub, sequelize, options);
});
beforeEach(() => {
sandbox = sinon.createSandbox();
const options = {
transaction: { name: 'transactionName' },
isolationLevel: 'REPEATABLE_READ',
logging: false
};
sandbox.stub(connectionStub, 'beginTransaction').callsArg(0);
query = new Query(connectionStub, sequelize, options);
});

afterEach(() => {
sandbox.restore();
});

describe('beginTransaction', () => {
it('should call beginTransaction with correct arguments', () => {
return query._run(connectionStub, 'BEGIN TRANSACTION')
.then(() => {
Expand All @@ -35,10 +39,6 @@ if (dialect === 'mssql') {
expect(connectionStub.beginTransaction.args[0][2]).to.equal(tediousIsolationLevel.REPEATABLE_READ);
});
});

afterEach(() => {
sandbox.restore();
});
});

describe('formatBindParameters', () => {
Expand All @@ -64,5 +64,25 @@ if (dialect === 'mssql') {
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 } });
});
});

});
}

0 comments on commit 41237ae

Please sign in to comment.