Merge pull request #555 from benesch/custom-type-coercion

Supercharge `prepareValue`
This commit is contained in:
Brian C 2014-04-06 11:16:33 -05:00
commit 82486929a9
4 changed files with 220 additions and 31 deletions

View File

@ -0,0 +1,46 @@
var utils = require("../lib/utils");
var numArr = [];
for (var i = 0; i < 1000; i++) numArr[i] = i;
console.time("prepare-number-array");
for (var i = 0; i < 100; i++) {
utils.prepareValue(numArr);
}
console.timeEnd("prepare-number-array");
var strArr = new Array(10000);
console.time("prepare-string-array");
for (var i = 0; i < 100; i++) {
utils.prepareValue(strArr);
}
console.timeEnd("prepare-string-array");
var objArr = [];
for (var i = 0; i < 1000; i++) objArr[i] = { x: { y: 42 }};
console.time("prepare-object-array");
for (var i = 0; i < 100; i++) {
utils.prepareValue(objArr);
}
console.timeEnd("prepare-object-array");
var obj = { x: { y: 42 }};
console.time("prepare-object");
for (var i = 0; i < 100000; i++) {
utils.prepareValue(obj);
}
console.timeEnd("prepare-object");
var customType = {
toPostgres: function () {
return { toPostgres: function () { return new Date(); } };
}
};
console.time("prepare-custom-type");
for (var i = 0; i < 100000; i++) {
utils.prepareValue(customType);
}
console.timeEnd("prepare-custom-type");

View File

@ -17,52 +17,61 @@ if(typeof events.EventEmitter.prototype.once !== 'function') {
// uses comma separator so won't work for types like box that use // uses comma separator so won't work for types like box that use
// a different array separator. // a different array separator.
function arrayString(val) { function arrayString(val) {
var result = '{'; var result = '{';
for (var i = 0 ; i < val.length; i++) { for (var i = 0 ; i < val.length; i++) {
if(i > 0) { if(i > 0) {
result = result + ','; result = result + ',';
} }
if(val[i] instanceof Date) { if(val[i] === null || typeof val[i] === 'undefined') {
result = result + JSON.stringify(val[i]); result = result + 'NULL';
} }
else if(typeof val[i] === 'undefined') { else if(Array.isArray(val[i])) {
result = result + 'NULL'; result = result + arrayString(val[i]);
} }
else if(Array.isArray(val[i])) { else
result = result + arrayString(val[i]); {
} result = result + JSON.stringify(prepareValue(val[i]));
else }
{ }
result = result + result = result + '}';
(val[i] === null ? 'NULL' : JSON.stringify(val[i])); return result;
}
}
result = result + '}';
return result;
} }
//converts values from javascript types //converts values from javascript types
//to their 'raw' counterparts for use as a postgres parameter //to their 'raw' counterparts for use as a postgres parameter
//note: you can override this function to provide your own conversion mechanism //note: you can override this function to provide your own conversion mechanism
//for complex types, etc... //for complex types, etc...
var prepareValue = function(val) { var prepareValue = function(val, seen) {
if (val instanceof Buffer) { if (val instanceof Buffer) {
return val; return val;
} }
if(val instanceof Date) { if(val instanceof Date) {
return dateToString(val); return dateToString(val);
} }
if(typeof val === 'undefined') {
return null;
}
if(Array.isArray(val)) { if(Array.isArray(val)) {
return arrayString(val); return arrayString(val);
} }
if(!val || typeof val !== 'object') { if(val === null || typeof val === 'undefined') {
return val === null ? null : val.toString(); return null;
}
if(typeof val === 'object') {
return prepareObject(val, seen);
}
return val.toString();
};
function prepareObject(val, seen) {
if(val.toPostgres && typeof val.toPostgres === 'function') {
seen = seen || [];
if (seen.indexOf(val) !== -1) {
throw new Error('circular reference detected while preparing "' + val + '" for query');
}
seen.push(val);
return prepareValue(val.toPostgres(prepareValue), seen);
} }
return JSON.stringify(val); return JSON.stringify(val);
}; }
function dateToString(date) { function dateToString(date) {
function pad(number, digits) { function pad(number, digits) {

View File

@ -222,6 +222,15 @@ var Sink = function(expected, timeout, callback) {
} }
} }
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
var setTimezoneOffset = function(minutesOffset) {
Date.prototype.getTimezoneOffset = function () { return minutesOffset; };
}
var resetTimezoneOffset = function() {
Date.prototype.getTimezoneOffset = getTimezoneOffset;
}
module.exports = { module.exports = {
Sink: Sink, Sink: Sink,
@ -229,7 +238,9 @@ module.exports = {
args: args, args: args,
config: args, config: args,
sys: sys, sys: sys,
Client: Client Client: Client,
setTimezoneOffset: setTimezoneOffset,
resetTimezoneOffset: resetTimezoneOffset
}; };

View File

@ -1,4 +1,4 @@
require(__dirname + '/test-helper'); var helper = require(__dirname + '/test-helper');
var utils = require(__dirname + "/../../lib/utils"); var utils = require(__dirname + "/../../lib/utils");
var defaults = require(__dirname + "/../../lib").defaults; var defaults = require(__dirname + "/../../lib").defaults;
@ -48,3 +48,126 @@ test('normalizing query configs', function() {
config = utils.normalizeQueryConfig({text: 'TEXT', values: [10]}, callback) config = utils.normalizeQueryConfig({text: 'TEXT', values: [10]}, callback)
assert.deepEqual(config, {text: 'TEXT', values: [10], callback: callback}) assert.deepEqual(config, {text: 'TEXT', values: [10], callback: callback})
}) })
test('prepareValues: buffer prepared properly', function() {
var buf = new Buffer("quack");
var out = utils.prepareValue(buf);
assert.strictEqual(buf, out);
});
test('prepareValues: date prepared properly', function() {
helper.setTimezoneOffset(-330);
var date = new Date(2014, 1, 1, 11, 11, 1, 7);
var out = utils.prepareValue(date);
assert.strictEqual(out, "2014-02-01T11:11:01.007+05:30");
helper.resetTimezoneOffset();
});
test('prepareValues: undefined prepared properly', function() {
var out = utils.prepareValue(void 0);
assert.strictEqual(out, null);
});
test('prepareValue: null prepared properly', function() {
var out = utils.prepareValue(null);
assert.strictEqual(out, null);
});
test('prepareValue: true prepared properly', function() {
var out = utils.prepareValue(true);
assert.strictEqual(out, 'true');
});
test('prepareValue: false prepared properly', function() {
var out = utils.prepareValue(false);
assert.strictEqual(out, 'false');
});
test('prepareValue: number prepared properly', function () {
var out = utils.prepareValue(3.042);
assert.strictEqual(out, '3.042');
});
test('prepareValue: string prepared properly', function() {
var out = utils.prepareValue('big bad wolf');
assert.strictEqual(out, 'big bad wolf');
});
test('prepareValue: simple array prepared properly', function() {
var out = utils.prepareValue([1, null, 3, undefined, [5, 6, "squ,awk"]]);
assert.strictEqual(out, '{"1",NULL,"3",NULL,{"5","6","squ,awk"}}');
});
test('prepareValue: complex array prepared properly', function() {
var out = utils.prepareValue([{ x: 42 }, { y: 84 }]);
assert.strictEqual(out, '{"{\\"x\\":42}","{\\"y\\":84}"}');
});
test('prepareValue: date array prepared properly', function() {
helper.setTimezoneOffset(-330);
var date = new Date(2014, 1, 1, 11, 11, 1, 7);
var out = utils.prepareValue([date]);
assert.strictEqual(out, '{"2014-02-01T11:11:01.007+05:30"}');
helper.resetTimezoneOffset();
});
test('prepareValue: arbitrary objects prepared properly', function() {
var out = utils.prepareValue({ x: 42 });
assert.strictEqual(out, '{"x":42}');
});
test('prepareValue: objects with simple toPostgres prepared properly', function() {
var customType = {
toPostgres: function() {
return "zomgcustom!";
}
};
var out = utils.prepareValue(customType);
assert.strictEqual(out, "zomgcustom!");
});
test('prepareValue: objects with complex toPostgres prepared properly', function() {
var buf = new Buffer("zomgcustom!");
var customType = {
toPostgres: function() {
return [1, 2];
}
};
var out = utils.prepareValue(customType);
assert.strictEqual(out, '{"1","2"}');
});
test('prepareValue: objects with toPostgres receive prepareValue', function() {
var customRange = {
lower: { toPostgres: function() { return 5; } },
upper: { toPostgres: function() { return 10; } },
toPostgres: function(prepare) {
return "[" + prepare(this.lower) + "," + prepare(this.upper) + "]";
}
};
var out = utils.prepareValue(customRange);
assert.strictEqual(out, "[5,10]");
});
test('prepareValue: objects with circular toPostgres rejected', function() {
var buf = new Buffer("zomgcustom!");
var customType = {
toPostgres: function() {
return { toPostgres: function () { return customType; } };
}
};
//can't use `assert.throws` since we need to distinguish circular reference
//errors from call stack exceeded errors
try {
utils.prepareValue(customType);
} catch (e) {
assert.ok(e.message.match(/circular/), "Expected circular reference error but got " + e);
return;
}
throw new Error("Expected prepareValue to throw exception");
});