From 2aa37e1b46dcd9934e66eaf46739452fa66bbb5c Mon Sep 17 00:00:00 2001 From: Stephen Sugden Date: Sun, 19 Aug 2012 12:21:33 -0700 Subject: [PATCH] Make query results act directly like an array --- lib/result.js | 56 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/lib/result.js b/lib/result.js index f46ed4185..d0ebd535d 100644 --- a/lib/result.js +++ b/lib/result.js @@ -2,31 +2,43 @@ //in the 'end' event and also //passed as second argument to provided callback var Result = function() { - this.rows = []; -}; - -var p = Result.prototype; + Array.call(this); + this.rows = this; + // Define properties scoped to the object instance + for (var prop in ['command', 'rowCount', 'oid']) { + Object.defineProperty(this, prop, { enumerable: false, writable: true }) + } +} +Result.prototype.__proto__ = Array.prototype; var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/ -//adds a command complete message -p.addCommandComplete = function(msg) { - var match = matchRegexp.exec(msg.text); - if(match) { - this.command = match[1]; - //match 3 will only be existing on insert commands - if(match[3]) { - this.rowCount = parseInt(match[3]); - this.oid = parseInt(match[2]); - } else { - this.rowCount = parseInt(match[2]); - } - } -}; - -p.addRow = function(row) { - this.rows.push(row); -}; +// Define properties that can safely exist on the prototype. +Object.defineProperties(Result.prototype, { + //adds a command complete message + addCommandComplete: { + value: function addCommandComplete (msg) { + var match = matchRegexp.exec(msg.text); + if(match) { + this.command = match[1]; + //match 3 will only be existing on insert commands + if(match[3]) { + this.rowCount = parseInt(match[3]); + this.oid = parseInt(match[2]); + } else { + this.rowCount = parseInt(match[2]); + } + } + }, + enumerable: false + }, + addRow: { + get: function () { + return this.push + }, + enumerable: false + } +}) module.exports = Result;