Skip to content

Commit a0bf25e

Browse files
committedDec 3, 2014
Implemented per-client type parser overrides.
Adds Client#getTypeParser() and Client#setTypeParser().
1 parent 5fff5fc commit a0bf25e

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed
 

Diff for: ‎lib/client.js

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var crypto = require('crypto');
22
var EventEmitter = require('events').EventEmitter;
33
var util = require('util');
44
var pgPass = require('pgpass');
5+
var types = require('pg-types');
56

67
var ConnectionParameters = require(__dirname + '/connection-parameters');
78
var Query = require(__dirname + '/query');
@@ -30,6 +31,12 @@ var Client = function(config) {
3031
this.processID = null;
3132
this.secretKey = null;
3233
this.ssl = this.connectionParameters.ssl || false;
34+
35+
this._types = config.types || types;
36+
this._parserOverrides = {
37+
text: {},
38+
binary: {}
39+
};
3340
};
3441

3542
util.inherits(Client, EventEmitter);
@@ -227,6 +234,20 @@ Client.prototype.cancel = function(client, query) {
227234
}
228235
};
229236

237+
Client.prototype.setTypeParser = function(oid, format, parseFn) {
238+
if(typeof format == 'function') {
239+
parseFn = format;
240+
format = 'text';
241+
}
242+
this._parserOverrides[format][oid] = parseFn;
243+
};
244+
245+
Client.prototype.getTypeParser = function(oid, format) {
246+
format = format || 'text';
247+
var formatParserOverrides = this._parserOverrides[format] || {};
248+
return formatParserOverrides[oid] || this._types.getTypeParser(oid, format);
249+
};
250+
230251
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
231252
Client.prototype.escapeIdentifier = function(str) {
232253

@@ -302,6 +323,7 @@ Client.prototype.query = function(config, values, callback) {
302323
if(this.binary && !query.binary) {
303324
query.binary = true;
304325
}
326+
query._result._getTypeParser = this.getTypeParser.bind(this);
305327

306328
this.queryQueue.push(query);
307329
this._pulseQueryQueue();

Diff for: ‎lib/native/index.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Native = require('pg-native');
2+
var types = require('pg-types');
23
var semver = require('semver');
34
var pkg = require('../../package.json');
45
var assert = require('assert');
@@ -16,7 +17,7 @@ var Client = module.exports = function(config) {
1617
config = config || {};
1718

1819
this.native = new Native({
19-
types: config.types || require('pg-types')
20+
types: {getTypeParser: this.getTypeParser.bind(this)}
2021
});
2122

2223
this._queryQueue = [];
@@ -33,6 +34,12 @@ var Client = module.exports = function(config) {
3334

3435
//a hash to hold named queries
3536
this.namedQueries = {};
37+
38+
this._types = config.types || types;
39+
this._parserOverrides = {
40+
text: {},
41+
binary: {}
42+
};
3643
};
3744

3845
util.inherits(Client, EventEmitter);
@@ -180,3 +187,17 @@ Client.prototype.cancel = function(query) {
180187
this._queryQueue.splice(this._queryQueue.indexOf(query), 1);
181188
}
182189
};
190+
191+
Client.prototype.setTypeParser = function(oid, format, parseFn) {
192+
if(typeof format == 'function') {
193+
parseFn = format;
194+
format = 'text';
195+
}
196+
this._parserOverrides[format][oid] = parseFn;
197+
};
198+
199+
Client.prototype.getTypeParser = function(oid, format) {
200+
format = format || 'text';
201+
var formatParserOverrides = this._parserOverrides[format] || {};
202+
return formatParserOverrides[oid] || this._types.getTypeParser(oid, format);
203+
};

Diff for: ‎lib/result.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Result.prototype.addFields = function(fieldDescriptions) {
8888
for(var i = 0; i < fieldDescriptions.length; i++) {
8989
var desc = fieldDescriptions[i];
9090
this.fields.push(desc);
91-
var parser = types.getTypeParser(desc.dataTypeID, desc.format || 'text');
91+
var parser = this._getTypeParser(desc.dataTypeID, desc.format || 'text');
9292
this._parsers.push(parser);
9393
//this is some craziness to compile the row result parsing
9494
//results in ~60% speedup on large query result sets
@@ -99,4 +99,6 @@ Result.prototype.addFields = function(fieldDescriptions) {
9999
}
100100
};
101101

102+
Result.prototype._getTypeParser = types.getTypeParser;
103+
102104
module.exports = Result;

0 commit comments

Comments
 (0)
Please sign in to comment.