From a0bf25e30830898a3f27d3d87cb311c9cc7099c5 Mon Sep 17 00:00:00 2001 From: "David H. Bronke" Date: Thu, 13 Nov 2014 17:57:00 -0600 Subject: [PATCH] Implemented per-client type parser overrides. Adds Client#getTypeParser() and Client#setTypeParser(). --- lib/client.js | 22 ++++++++++++++++++++++ lib/native/index.js | 23 ++++++++++++++++++++++- lib/result.js | 4 +++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 53b4481d..636275d8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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(); diff --git a/lib/native/index.js b/lib/native/index.js index 4ca87edc..9d21dbcd 100644 --- a/lib/native/index.js +++ b/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'); @@ -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); +}; diff --git a/lib/result.js b/lib/result.js index ae992551..bf05381d 100644 --- a/lib/result.js +++ b/lib/result.js @@ -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;