Skip to content

Commit 4bfdc04

Browse files
committed
Moved type override code into a new class.
1 parent 8574085 commit 4bfdc04

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

Diff for: lib/client.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +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');
5+
var TypeOverrides = require('./type-overrides');
66

77
var ConnectionParameters = require(__dirname + '/connection-parameters');
88
var Query = require(__dirname + '/query');
@@ -21,6 +21,8 @@ var Client = function(config) {
2121

2222
var c = config || {};
2323

24+
this._types = new TypeOverrides(c.types);
25+
2426
this.connection = c.connection || new Connection({
2527
stream: c.stream,
2628
ssl: this.connectionParameters.ssl
@@ -31,12 +33,6 @@ var Client = function(config) {
3133
this.processID = null;
3234
this.secretKey = null;
3335
this.ssl = this.connectionParameters.ssl || false;
34-
35-
this._types = c.types || types;
36-
this._parserOverrides = {
37-
text: {},
38-
binary: {}
39-
};
4036
};
4137

4238
util.inherits(Client, EventEmitter);
@@ -235,17 +231,11 @@ Client.prototype.cancel = function(client, query) {
235231
};
236232

237233
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;
234+
return this._types.setTypeParser(oid, format, parseFn);
243235
};
244236

245237
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);
238+
return this._types.getTypeParser(oid, format);
249239
};
250240

251241
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
@@ -323,7 +313,7 @@ Client.prototype.query = function(config, values, callback) {
323313
if(this.binary && !query.binary) {
324314
query.binary = true;
325315
}
326-
query._result._getTypeParser = this.getTypeParser.bind(this);
316+
query._result._getTypeParser = this._types.getTypeParser.bind(this._types);
327317

328318
this.queryQueue.push(query);
329319
this._pulseQueryQueue();

Diff for: lib/native/index.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var Native = require('pg-native');
2-
var types = require('pg-types');
2+
var TypeOverrides = require('../type-overrides');
33
var semver = require('semver');
44
var pkg = require('../../package.json');
55
var assert = require('assert');
@@ -16,8 +16,10 @@ var Client = module.exports = function(config) {
1616
EventEmitter.call(this);
1717
config = config || {};
1818

19+
this._types = new TypeOverrides(config.types);
20+
1921
this.native = new Native({
20-
types: {getTypeParser: this.getTypeParser.bind(this)}
22+
types: this._types
2123
});
2224

2325
this._queryQueue = [];
@@ -34,12 +36,6 @@ var Client = module.exports = function(config) {
3436

3537
//a hash to hold named queries
3638
this.namedQueries = {};
37-
38-
this._types = config.types || types;
39-
this._parserOverrides = {
40-
text: {},
41-
binary: {}
42-
};
4339
};
4440

4541
util.inherits(Client, EventEmitter);
@@ -189,15 +185,9 @@ Client.prototype.cancel = function(query) {
189185
};
190186

191187
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;
188+
return this._types.setTypeParser(oid, format, parseFn);
197189
};
198190

199191
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);
192+
return this._types.getTypeParser(oid, format);
203193
};

Diff for: lib/type-overrides.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var types = require('pg-types');
2+
3+
function TypeOverrides(userTypes) {
4+
this._types = userTypes || types;
5+
this.text = {};
6+
this.binary = {};
7+
}
8+
9+
TypeOverrides.prototype.getOverrides = function(format) {
10+
switch(format) {
11+
case 'text': return this.text;
12+
case 'binary': return this.binary;
13+
default: return {};
14+
}
15+
};
16+
17+
TypeOverrides.prototype.setTypeParser = function(oid, format, parseFn) {
18+
if(typeof format == 'function') {
19+
parseFn = format;
20+
format = 'text';
21+
}
22+
this.getOverrides(format)[oid] = parseFn;
23+
};
24+
25+
TypeOverrides.prototype.getTypeParser = function(oid, format) {
26+
format = format || 'text';
27+
return this.getOverrides(format)[oid] || this._types.getTypeParser(oid, format);
28+
};
29+
30+
module.exports = TypeOverrides;

0 commit comments

Comments
 (0)