@@ -2,6 +2,7 @@ var crypto = require('crypto');
22var EventEmitter = require ( 'events' ) . EventEmitter ;
33var util = require ( 'util' ) ;
44var pgPass = require ( 'pgpass' ) ;
5+ var types = require ( 'pg-types' ) ;
56
67var ConnectionParameters = require ( __dirname + '/connection-parameters' ) ;
78var 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
3542util . 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
231252Client . 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 ( ) ;
0 commit comments