expose type converter overrides & warn on giant numeric values
This commit is contained in:
parent
90d4d2d070
commit
96f7179094
@ -107,12 +107,19 @@ var parseByteA = function(val) {
|
|||||||
}).replace(/\\\\/g, "\\"), "binary");
|
}).replace(/\\\\/g, "\\"), "binary");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var maxLen = Number.MAX_VALUE.toString().length
|
||||||
|
|
||||||
var init = function(register) {
|
var init = function(register) {
|
||||||
register(20, parseInt);
|
register(20, parseInt);
|
||||||
register(21, parseInt);
|
register(21, parseInt);
|
||||||
register(23, parseInt);
|
register(23, parseInt);
|
||||||
register(26, parseInt);
|
register(26, parseInt);
|
||||||
register(1700, parseFloat);
|
register(1700, function(val){
|
||||||
|
if(val.length > maxLen) {
|
||||||
|
console.warn('WARNING: value %s is longer than max supported numeric value in javascript. Possible data loss', val)
|
||||||
|
}
|
||||||
|
return parseFloat(val);
|
||||||
|
});
|
||||||
register(700, parseFloat);
|
register(700, parseFloat);
|
||||||
register(701, parseFloat);
|
register(701, parseFloat);
|
||||||
register(16, parseBool);
|
register(16, parseBool);
|
||||||
|
@ -20,6 +20,14 @@ var getTypeParser = function(oid, format) {
|
|||||||
return typeParsers[format][oid] || noParse;
|
return typeParsers[format][oid] || noParse;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var setTypeParser = function(oid, format, parseFn) {
|
||||||
|
if(typeof format == 'function') {
|
||||||
|
parseFn = format;
|
||||||
|
format = 'text';
|
||||||
|
}
|
||||||
|
typeParsers[format][oid] = parseFn;
|
||||||
|
}
|
||||||
|
|
||||||
textParsers.init(function(oid, converter) {
|
textParsers.init(function(oid, converter) {
|
||||||
typeParsers.text[oid] = function(value) {
|
typeParsers.text[oid] = function(value) {
|
||||||
return converter(String(value));
|
return converter(String(value));
|
||||||
@ -32,4 +40,5 @@ binaryParsers.init(function(oid, converter) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getTypeParser: getTypeParser,
|
getTypeParser: getTypeParser,
|
||||||
|
setTypeParser: setTypeParser
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
var helper = require(__dirname + "/../test/integration/test-helper");
|
var helper = require(__dirname + "/../test/integration/test-helper");
|
||||||
var pg = helper.pg;
|
var pg = helper.pg;
|
||||||
pg.connect(helper.connectionString(), assert.success(function(client) {
|
pg.connect(helper.config, assert.success(function(client) {
|
||||||
var query = client.query('select oid, typname from pg_type where typtype = \'b\' order by oid');
|
var query = client.query('select oid, typname from pg_type where typtype = \'b\' order by oid');
|
||||||
query.on('row', console.log);
|
query.on('row', console.log);
|
||||||
}))
|
}))
|
||||||
|
21
test/integration/client/huge-numeric-tests.js
Normal file
21
test/integration/client/huge-numeric-tests.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
var helper = require(__dirname + '/test-helper');
|
||||||
|
|
||||||
|
helper.pg.connect(helper.config, assert.success(function(client) {
|
||||||
|
var types = require(__dirname + '/../../../lib/types');
|
||||||
|
//1231 = numericOID
|
||||||
|
types.setTypeParser(1700, function(){
|
||||||
|
return 'yes';
|
||||||
|
})
|
||||||
|
types.setTypeParser(1700, 'binary', function(){
|
||||||
|
return 'yes';
|
||||||
|
})
|
||||||
|
var bignum = '294733346389144765940638005275322203805';
|
||||||
|
client.query('CREATE TEMP TABLE bignumz(id numeric)');
|
||||||
|
client.query('INSERT INTO bignumz(id) VALUES ($1)', [bignum]);
|
||||||
|
client.query('SELECT * FROM bignumz', assert.success(function(result) {
|
||||||
|
assert.equal(result.rows[0].id, 'yes')
|
||||||
|
helper.pg.end();
|
||||||
|
}))
|
||||||
|
}));
|
||||||
|
|
||||||
|
//custom type converter
|
Loading…
Reference in New Issue
Block a user