diff --git a/lib/native.js b/lib/native.js index 31143a2b..b8ff9e05 100644 --- a/lib/native.js +++ b/lib/native.js @@ -66,7 +66,13 @@ p._pulseQueryQueue = function() { return; } this._activeQuery = query; - this._sendQuery(query.text); + if(query.values) { + //call native function + this._sendQueryWithParams(query.text, query.values) + } else { + //call native function + this._sendQuery(query.text); + } } var ctor = function(config) { @@ -107,11 +113,13 @@ var connect = function(config, callback) { }; //event emitter proxy -var NativeQuery = function(text) { +var NativeQuery = function(text, values) { if(typeof text == 'object') { this.text = text.text; + this.values = text.values; } else { this.text = text; + this.values = values; } EventEmitter.call(this); }; diff --git a/src/binding.cc b/src/binding.cc index d6afebaf..30627224 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -40,6 +40,7 @@ public: NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect); NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery); + NODE_SET_PROTOTYPE_METHOD(t, "_sendQueryWithParams", SendQueryWithParams); NODE_SET_PROTOTYPE_METHOD(t, "end", End); target->Set(String::NewSymbol("Connection"), t->GetFunction()); @@ -89,6 +90,16 @@ public: return Undefined(); } + //v8 entry point into Connection#_sendQueryWithParams + static Handle + SendQueryWithParams(const Arguments& args) + { + HandleScope scope; + Connection *self = ObjectWrap::Unwrap(args.This()); + printf("%d\n", args.Length()); + return Undefined(); + } + //v8 entry point into Connection#end static Handle End(const Arguments& args) diff --git a/test/native/evented-api-tests.js b/test/native/evented-api-tests.js index ba7282b1..f407404e 100644 --- a/test/native/evented-api-tests.js +++ b/test/native/evented-api-tests.js @@ -26,20 +26,22 @@ test('connects', function() { }) }) }) - -test('multiple results', function() { +var setupClient = function() { var client = new Client(conString); client.connect(); + client.query("CREATE TEMP TABLE boom(name varchar(10))"); + client.query("INSERT INTO boom(name) VALUES('Aaron')"); + client.query("INSERT INTO boom(name) VALUES('Brian')"); + return client; +} +test('multiple results', function() { test('queued queries', function() { - client.query("CREATE TEMP TABLE boom(name varchar(10))"); - client.query("INSERT INTO boom(name) VALUES('Aaron')"); - client.query("INSERT INTO boom(name) VALUES('Brian')"); + var client = setupClient(); var q = client.query("SELECT * from BOOM"); assert.emits(q, 'row', function(row) { assert.equal(row.name, 'Aaron'); assert.emits(q, 'row', function(row) { assert.equal(row.name, "Brian"); - }) }) assert.emits(q, 'end', function() { @@ -55,3 +57,31 @@ test('multiple results', function() { }) }) }) + +test('parameterized queries', function() { + test('with a single string param', function() { + var client = setupClient(); + var q = client.query("SELECT name FROM boom WHERE name = $1", ['Brian']); + assert.emits(q, 'row', function(row) { + assert.equal(row.name, 'Brian') + }) + assert.emits(q, 'end', function() { + client.end(); + }); + }) + test('with object config for query', function() { + var client = setupClient(); + var q = client.query({ + text: "SELECT name FROM boom WHERE name = $1", + values: ['Brian'] + }); + assert.emits(q, 'row', function(row) { + assert.equal(row.name, 'Brian'); + }) + assert.emits(q, 'end', function() { + client.end(); + }) + }) + +}) +