Implemented per-client type parser overrides.

Adds Client#getTypeParser() and Client#setTypeParser().
This commit is contained in:
David H. Bronke 2014-11-13 17:57:00 -06:00
parent 5fff5fc61f
commit a0bf25e308
3 changed files with 47 additions and 2 deletions

View File

@ -2,6 +2,7 @@ var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var pgPass = require('pgpass');
var types = require('pg-types');
var ConnectionParameters = require(__dirname + '/connection-parameters');
var Query = require(__dirname + '/query');
@ -30,6 +31,12 @@ var Client = function(config) {
this.processID = null;
this.secretKey = null;
this.ssl = this.connectionParameters.ssl || false;
this._types = config.types || types;
this._parserOverrides = {
text: {},
binary: {}
};
};
util.inherits(Client, EventEmitter);
@ -227,6 +234,20 @@ Client.prototype.cancel = function(client, query) {
}
};
Client.prototype.setTypeParser = function(oid, format, parseFn) {
if(typeof format == 'function') {
parseFn = format;
format = 'text';
}
this._parserOverrides[format][oid] = parseFn;
};
Client.prototype.getTypeParser = function(oid, format) {
format = format || 'text';
var formatParserOverrides = this._parserOverrides[format] || {};
return formatParserOverrides[oid] || this._types.getTypeParser(oid, format);
};
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
Client.prototype.escapeIdentifier = function(str) {
@ -302,6 +323,7 @@ Client.prototype.query = function(config, values, callback) {
if(this.binary && !query.binary) {
query.binary = true;
}
query._result._getTypeParser = this.getTypeParser.bind(this);
this.queryQueue.push(query);
this._pulseQueryQueue();

View File

@ -1,4 +1,5 @@
var Native = require('pg-native');
var types = require('pg-types');
var semver = require('semver');
var pkg = require('../../package.json');
var assert = require('assert');
@ -16,7 +17,7 @@ var Client = module.exports = function(config) {
config = config || {};
this.native = new Native({
types: config.types || require('pg-types')
types: {getTypeParser: this.getTypeParser.bind(this)}
});
this._queryQueue = [];
@ -33,6 +34,12 @@ var Client = module.exports = function(config) {
//a hash to hold named queries
this.namedQueries = {};
this._types = config.types || types;
this._parserOverrides = {
text: {},
binary: {}
};
};
util.inherits(Client, EventEmitter);
@ -180,3 +187,17 @@ Client.prototype.cancel = function(query) {
this._queryQueue.splice(this._queryQueue.indexOf(query), 1);
}
};
Client.prototype.setTypeParser = function(oid, format, parseFn) {
if(typeof format == 'function') {
parseFn = format;
format = 'text';
}
this._parserOverrides[format][oid] = parseFn;
};
Client.prototype.getTypeParser = function(oid, format) {
format = format || 'text';
var formatParserOverrides = this._parserOverrides[format] || {};
return formatParserOverrides[oid] || this._types.getTypeParser(oid, format);
};

View File

@ -88,7 +88,7 @@ Result.prototype.addFields = function(fieldDescriptions) {
for(var i = 0; i < fieldDescriptions.length; i++) {
var desc = fieldDescriptions[i];
this.fields.push(desc);
var parser = types.getTypeParser(desc.dataTypeID, desc.format || 'text');
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text');
this._parsers.push(parser);
//this is some craziness to compile the row result parsing
//results in ~60% speedup on large query result sets
@ -99,4 +99,6 @@ Result.prototype.addFields = function(fieldDescriptions) {
}
};
Result.prototype._getTypeParser = types.getTypeParser;
module.exports = Result;