Skip to content

Commit 96f7179

Browse files
committed
expose type converter overrides & warn on giant numeric values
1 parent 90d4d2d commit 96f7179

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

lib/textParsers.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,19 @@ var parseByteA = function(val) {
107107
}).replace(/\\\\/g, "\\"), "binary");
108108
}
109109

110+
var maxLen = Number.MAX_VALUE.toString().length
111+
110112
var init = function(register) {
111113
register(20, parseInt);
112114
register(21, parseInt);
113115
register(23, parseInt);
114116
register(26, parseInt);
115-
register(1700, parseFloat);
117+
register(1700, function(val){
118+
if(val.length > maxLen) {
119+
console.warn('WARNING: value %s is longer than max supported numeric value in javascript. Possible data loss', val)
120+
}
121+
return parseFloat(val);
122+
});
116123
register(700, parseFloat);
117124
register(701, parseFloat);
118125
register(16, parseBool);

lib/types.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ var getTypeParser = function(oid, format) {
2020
return typeParsers[format][oid] || noParse;
2121
};
2222

23+
var setTypeParser = function(oid, format, parseFn) {
24+
if(typeof format == 'function') {
25+
parseFn = format;
26+
format = 'text';
27+
}
28+
typeParsers[format][oid] = parseFn;
29+
}
30+
2331
textParsers.init(function(oid, converter) {
2432
typeParsers.text[oid] = function(value) {
2533
return converter(String(value));
@@ -32,4 +40,5 @@ binaryParsers.init(function(oid, converter) {
3240

3341
module.exports = {
3442
getTypeParser: getTypeParser,
43+
setTypeParser: setTypeParser
3544
}

script/list-db-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var helper = require(__dirname + "/../test/integration/test-helper");
22
var pg = helper.pg;
3-
pg.connect(helper.connectionString(), assert.success(function(client) {
3+
pg.connect(helper.config, assert.success(function(client) {
44
var query = client.query('select oid, typname from pg_type where typtype = \'b\' order by oid');
55
query.on('row', console.log);
66
}))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var helper = require(__dirname + '/test-helper');
2+
3+
helper.pg.connect(helper.config, assert.success(function(client) {
4+
var types = require(__dirname + '/../../../lib/types');
5+
//1231 = numericOID
6+
types.setTypeParser(1700, function(){
7+
return 'yes';
8+
})
9+
types.setTypeParser(1700, 'binary', function(){
10+
return 'yes';
11+
})
12+
var bignum = '294733346389144765940638005275322203805';
13+
client.query('CREATE TEMP TABLE bignumz(id numeric)');
14+
client.query('INSERT INTO bignumz(id) VALUES ($1)', [bignum]);
15+
client.query('SELECT * FROM bignumz', assert.success(function(result) {
16+
assert.equal(result.rows[0].id, 'yes')
17+
helper.pg.end();
18+
}))
19+
}));
20+
21+
//custom type converter

0 commit comments

Comments
 (0)