Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #679 from whitelynx/master
Added per-client type parser overrides.
- Loading branch information
Showing
5 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var helper = require(__dirname + '/test-helper'); | ||
|
||
function testTypeParser(client, expectedResult, done) { | ||
var boolValue = true; | ||
client.query('CREATE TEMP TABLE parserOverrideTest(id bool)'); | ||
client.query('INSERT INTO parserOverrideTest(id) VALUES ($1)', [boolValue]); | ||
client.query('SELECT * FROM parserOverrideTest', assert.success(function(result) { | ||
assert.equal(result.rows[0].id, expectedResult); | ||
helper.pg.end(); | ||
done(); | ||
})); | ||
} | ||
|
||
helper.pg.connect(helper.config, assert.success(function(client1, done1) { | ||
helper.pg.connect(helper.config, assert.success(function(client2, done2) { | ||
var boolTypeOID = 16; | ||
client1.setTypeParser(boolTypeOID, function(){ | ||
return 'first client'; | ||
}); | ||
client2.setTypeParser(boolTypeOID, function(){ | ||
return 'second client'; | ||
}); | ||
|
||
client1.setTypeParser(boolTypeOID, 'binary', function(){ | ||
return 'first client binary'; | ||
}); | ||
client2.setTypeParser(boolTypeOID, 'binary', function(){ | ||
return 'second client binary'; | ||
}); | ||
|
||
testTypeParser(client1, 'first client', done1); | ||
testTypeParser(client2, 'second client', done2); | ||
})); | ||
})); |