Skip to content

Commit

Permalink
Implemented per-client type parser overrides.
Browse files Browse the repository at this point in the history
Adds Client#getTypeParser() and Client#setTypeParser().
  • Loading branch information
whitelynx committed Dec 3, 2014
1 parent 5fff5fc commit a0bf25e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
22 changes: 22 additions & 0 deletions lib/client.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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();
Expand Down
23 changes: 22 additions & 1 deletion lib/native/index.js
@@ -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');
Expand All @@ -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 = [];
Expand All @@ -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);
Expand Down Expand Up @@ -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);
};
4 changes: 3 additions & 1 deletion lib/result.js
Expand Up @@ -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
Expand All @@ -99,4 +99,6 @@ Result.prototype.addFields = function(fieldDescriptions) {
}
};

Result.prototype._getTypeParser = types.getTypeParser;

module.exports = Result;

0 comments on commit a0bf25e

Please sign in to comment.