expose type converter overrides & warn on giant numeric values

This commit is contained in:
brianc 2012-03-21 22:36:18 -05:00
parent 90d4d2d070
commit 96f7179094
4 changed files with 39 additions and 2 deletions

View File

@ -107,12 +107,19 @@ var parseByteA = function(val) {
}).replace(/\\\\/g, "\\"), "binary");
}
var maxLen = Number.MAX_VALUE.toString().length
var init = function(register) {
register(20, parseInt);
register(21, parseInt);
register(23, 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(701, parseFloat);
register(16, parseBool);

View File

@ -20,6 +20,14 @@ var getTypeParser = function(oid, format) {
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) {
typeParsers.text[oid] = function(value) {
return converter(String(value));
@ -32,4 +40,5 @@ binaryParsers.init(function(oid, converter) {
module.exports = {
getTypeParser: getTypeParser,
setTypeParser: setTypeParser
}

View File

@ -1,6 +1,6 @@
var helper = require(__dirname + "/../test/integration/test-helper");
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');
query.on('row', console.log);
}))

View 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