node-postgres/lib/types.js

46 lines
984 B
JavaScript
Raw Normal View History

var textParsers = require(__dirname + "/textParsers"),
binaryParsers = require(__dirname + "/binaryParsers");
var typeParsers = {
text: {},
binary: {}
};
//the empty parse function
var noParse = function(val) {
return String(val);
2013-01-21 21:53:46 +08:00
};
//returns a function used to convert a specific type (specified by
//oid) into a result javascript type
var getTypeParser = function(oid, format) {
2013-01-21 21:53:46 +08:00
if (!typeParsers[format]) {
return noParse;
2013-01-21 21:53:46 +08:00
}
return typeParsers[format][oid] || noParse;
};
var setTypeParser = function(oid, format, parseFn) {
if(typeof format == 'function') {
parseFn = format;
format = 'text';
}
typeParsers[format][oid] = parseFn;
2013-01-21 21:53:46 +08:00
};
textParsers.init(function(oid, converter) {
typeParsers.text[oid] = function(value) {
return converter(String(value));
};
});
binaryParsers.init(function(oid, converter) {
typeParsers.binary[oid] = converter;
});
module.exports = {
getTypeParser: getTypeParser,
setTypeParser: setTypeParser
2013-01-21 21:53:46 +08:00
};