Moved type override code into a new class.

This commit is contained in:
David H. Bronke 2014-12-03 11:00:27 -06:00
parent 857408540c
commit 4bfdc041ef
3 changed files with 42 additions and 32 deletions

View File

@ -2,7 +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 TypeOverrides = require('./type-overrides');
var ConnectionParameters = require(__dirname + '/connection-parameters');
var Query = require(__dirname + '/query');
@ -21,6 +21,8 @@ var Client = function(config) {
var c = config || {};
this._types = new TypeOverrides(c.types);
this.connection = c.connection || new Connection({
stream: c.stream,
ssl: this.connectionParameters.ssl
@ -31,12 +33,6 @@ var Client = function(config) {
this.processID = null;
this.secretKey = null;
this.ssl = this.connectionParameters.ssl || false;
this._types = c.types || types;
this._parserOverrides = {
text: {},
binary: {}
};
};
util.inherits(Client, EventEmitter);
@ -235,17 +231,11 @@ 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;
return this._types.setTypeParser(oid, format, parseFn);
};
Client.prototype.getTypeParser = function(oid, format) {
format = format || 'text';
var formatParserOverrides = this._parserOverrides[format] || {};
return formatParserOverrides[oid] || this._types.getTypeParser(oid, format);
return this._types.getTypeParser(oid, format);
};
// 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) {
if(this.binary && !query.binary) {
query.binary = true;
}
query._result._getTypeParser = this.getTypeParser.bind(this);
query._result._getTypeParser = this._types.getTypeParser.bind(this._types);
this.queryQueue.push(query);
this._pulseQueryQueue();

View File

@ -1,5 +1,5 @@
var Native = require('pg-native');
var types = require('pg-types');
var TypeOverrides = require('../type-overrides');
var semver = require('semver');
var pkg = require('../../package.json');
var assert = require('assert');
@ -16,8 +16,10 @@ var Client = module.exports = function(config) {
EventEmitter.call(this);
config = config || {};
this._types = new TypeOverrides(config.types);
this.native = new Native({
types: {getTypeParser: this.getTypeParser.bind(this)}
types: this._types
});
this._queryQueue = [];
@ -34,12 +36,6 @@ 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);
@ -189,15 +185,9 @@ Client.prototype.cancel = function(query) {
};
Client.prototype.setTypeParser = function(oid, format, parseFn) {
if(typeof format == 'function') {
parseFn = format;
format = 'text';
}
this._parserOverrides[format][oid] = parseFn;
return this._types.setTypeParser(oid, format, parseFn);
};
Client.prototype.getTypeParser = function(oid, format) {
format = format || 'text';
var formatParserOverrides = this._parserOverrides[format] || {};
return formatParserOverrides[oid] || this._types.getTypeParser(oid, format);
return this._types.getTypeParser(oid, format);
};

30
lib/type-overrides.js Normal file
View File

@ -0,0 +1,30 @@
var types = require('pg-types');
function TypeOverrides(userTypes) {
this._types = userTypes || types;
this.text = {};
this.binary = {};
}
TypeOverrides.prototype.getOverrides = function(format) {
switch(format) {
case 'text': return this.text;
case 'binary': return this.binary;
default: return {};
}
};
TypeOverrides.prototype.setTypeParser = function(oid, format, parseFn) {
if(typeof format == 'function') {
parseFn = format;
format = 'text';
}
this.getOverrides(format)[oid] = parseFn;
};
TypeOverrides.prototype.getTypeParser = function(oid, format) {
format = format || 'text';
return this.getOverrides(format)[oid] || this._types.getTypeParser(oid, format);
};
module.exports = TypeOverrides;