mirror of
https://github.com/knex/knex.git
synced 2025-12-28 15:38:41 +00:00
Mssql decimal fix (#3910)
This commit is contained in:
parent
8bc89b062d
commit
8029396d2d
@ -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(
|
||||
|
||||
95
test/integration/datatype/decimal.js
Normal file
95
test/integration/datatype/decimal.js
Normal 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');
|
||||
});
|
||||
});
|
||||
};
|
||||
95
test/integration/datatype/double.js
Normal file
95
test/integration/datatype/double.js
Normal 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');
|
||||
});
|
||||
});
|
||||
};
|
||||
@ -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 () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user