From f64521fcd70ef5668a01576e4297055fb75ab658 Mon Sep 17 00:00:00 2001 From: Derek Arnold Date: Tue, 6 Mar 2012 18:34:26 -0600 Subject: [PATCH] Add custom row object class support A nice feature of PHP's PDO layer is you can specify an object class to fetch rows as. This commit adds support for specify either a client-level or query-level row object to fetch them into, with a default of a plain-jane Object. --- lib/client.js | 3 +++ lib/defaults.js | 5 ++++- lib/query.js | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 5d1af39aa..a78224520 100644 --- a/lib/client.js +++ b/lib/client.js @@ -24,6 +24,7 @@ var Client = function(config) { this.encoding = 'utf8'; this.processID = null; this.secretKey = null; + this.rowClass = config.rowClass || defaults.rowClass; var self = this; }; @@ -187,6 +188,8 @@ p.query = function(config, values, callback) { } config.callback = callback; + if (!('rowClass' in config) && ('rowClass' in this)) + config.rowClass = this.rowClass; var query = new Query(config); this.queryQueue.push(query); diff --git a/lib/defaults.js b/lib/defaults.js index 249ad126e..c8619758b 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -27,5 +27,8 @@ module.exports = { reapIntervalMillis: 1000, // binary result mode - binary: false + binary: false, + + // row prototype + rowClass: Object } diff --git a/lib/query.js b/lib/query.js index 3d5dfc08f..41bea5bb0 100644 --- a/lib/query.js +++ b/lib/query.js @@ -18,6 +18,7 @@ var Query = function(config) { this._fieldConverters = []; this._result = new Result(); this.isPreparedStatement = false; + this.rowClass = config.rowClass || Object; EventEmitter.call(this); }; @@ -50,7 +51,7 @@ p.handleRowDescription = function(msg) { p.handleDataRow = function(msg) { var self = this; - var row = {}; + var row = new this.rowClass() for(var i = 0; i < msg.fields.length; i++) { var rawValue = msg.fields[i]; if(rawValue === null) {