Mssql decimal fix (#3910)

This commit is contained in:
hansnull 2020-09-30 09:05:16 +02:00 committed by GitHub
parent 8bc89b062d
commit 8029396d2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 193 additions and 1 deletions

View File

@ -316,7 +316,7 @@ Object.assign(Client_MSSQL.prototype, {
_setReqInput(req, i, binding) {
if (typeof binding == 'number') {
if (binding % 1 !== 0) {
req.input(`p${i}`, this.driver.Decimal(38, 10), binding);
req.input(`p${i}`, this.driver.Float(53), binding);
} else if (binding < SQL_INT4.MIN || binding > SQL_INT4.MAX) {
if (binding < SQL_BIGINT_SAFE.MIN || binding > SQL_BIGINT_SAFE.MAX) {
throw new Error(

View File

@ -0,0 +1,95 @@
'use strict';
const { expect } = require('chai');
module.exports = function (knex) {
it('#test decimal mssql should allow large numbers', function () {
// https://github.com/tediousjs/tedious/issues/1058
if (!/mssql/i.test(knex.client.driverName)) {
return Promise.resolve();
}
const tableName = 'decimal_test';
const testDecimal = 12345678901234.56;
return knex
.transaction(function (tr) {
return tr.schema.dropTableIfExists(tableName).then(function () {
return tr.schema.createTable(tableName, function (table) {
table.decimal('largeDecimal', 38, 10);
});
});
})
.then(function () {
return knex(tableName).insert({
largeDecimal: testDecimal,
});
})
.then(function () {
return knex(tableName).select('*');
})
.then(function (result) {
expect(result[0].largeDecimal).to.equal(testDecimal);
})
.catch(function (err) {
throw new Error('Test should not throw an error');
});
});
it('#test decimal mssql should allow numbers with precision of 10', function () {
if (!/mssql/i.test(knex.client.driverName)) {
return Promise.resolve();
}
const tableName = 'decimal_test';
const testDecimal = 0.1234567891;
return knex
.transaction(function (tr) {
return tr.schema.dropTableIfExists(tableName).then(function () {
return tr.schema.createTable(tableName, function (table) {
table.decimal('largeDecimal', 38, 10);
});
});
})
.then(function () {
return knex(tableName).insert({
largeDecimal: testDecimal,
});
})
.then(function () {
return knex(tableName).select('*');
})
.then(function (result) {
expect(result[0].largeDecimal).to.equal(testDecimal);
})
.catch(function (err) {
throw new Error('Test should not throw an error');
});
});
it('#test decimal mssql should allow numbers with precision of 16', function () {
// Plain JavaScript cannot handle higher numbers without losing precision.
if (!/mssql/i.test(knex.client.driverName)) {
return Promise.resolve();
}
const tableName = 'decimal_test';
const testDecimal = 1234567890123456;
return knex
.transaction(function (tr) {
return tr.schema.dropTableIfExists(tableName).then(function () {
return tr.schema.createTable(tableName, function (table) {
table.decimal('largeDecimal', 38, 10);
});
});
})
.then(function () {
return knex(tableName).insert({
largeDecimal: testDecimal,
});
})
.then(function () {
return knex(tableName).select('*');
})
.then(function (result) {
expect(result[0].largeDecimal).to.equal(testDecimal);
})
.catch(function (err) {
throw new Error('Test should not throw an error');
});
});
};

View File

@ -0,0 +1,95 @@
'use strict';
const { expect } = require('chai');
module.exports = function (knex) {
it('#test decimal mssql should allow large numbers', function () {
// https://github.com/tediousjs/tedious/issues/1058
if (!/mssql/i.test(knex.client.driverName)) {
return Promise.resolve();
}
const tableName = 'decimal_test';
const testDecimal = 12345678901234.56;
return knex
.transaction(function (tr) {
return tr.schema.dropTableIfExists(tableName).then(function () {
return tr.schema.createTable(tableName, function (table) {
table.double('largeDecimal');
});
});
})
.then(function () {
return knex(tableName).insert({
largeDecimal: testDecimal,
});
})
.then(function () {
return knex(tableName).select('*');
})
.then(function (result) {
expect(result[0].largeDecimal).to.equal(testDecimal);
})
.catch(function (err) {
throw new Error('Test should not throw an error');
});
});
it('#test decimal mssql should allow numbers with precision of 10', function () {
if (!/mssql/i.test(knex.client.driverName)) {
return Promise.resolve();
}
const tableName = 'decimal_test';
const testDecimal = 0.1234567891;
return knex
.transaction(function (tr) {
return tr.schema.dropTableIfExists(tableName).then(function () {
return tr.schema.createTable(tableName, function (table) {
table.double('largeDecimal');
});
});
})
.then(function () {
return knex(tableName).insert({
largeDecimal: testDecimal,
});
})
.then(function () {
return knex(tableName).select('*');
})
.then(function (result) {
expect(result[0].largeDecimal).to.equal(testDecimal);
})
.catch(function (err) {
throw new Error('Test should not throw an error');
});
});
it('#test decimal mssql should allow numbers with precision of 16', function () {
// Plain JavaScript cannot handle higher numbers without losing precision.
if (!/mssql/i.test(knex.client.driverName)) {
return Promise.resolve();
}
const tableName = 'decimal_test';
const testDecimal = 1234567890123456;
return knex
.transaction(function (tr) {
return tr.schema.dropTableIfExists(tableName).then(function () {
return tr.schema.createTable(tableName, function (table) {
table.double('largeDecimal');
});
});
})
.then(function () {
return knex(tableName).insert({
largeDecimal: testDecimal,
});
})
.then(function () {
return knex(tableName).select('*');
})
.then(function (result) {
expect(result[0].largeDecimal).to.equal(testDecimal);
})
.catch(function (err) {
throw new Error('Test should not throw an error');
});
});
};

View File

@ -27,6 +27,8 @@ module.exports = function (knex) {
require('./builder/deletes')(knex);
require('./builder/additional')(knex);
require('./datatype/bigint')(knex);
require('./datatype/decimal')(knex);
require('./datatype/double')(knex);
describe('knex.destroy', function () {
it('should allow destroying the pool with knex.destroy', function () {