Skip to content

Commit cc2ff04

Browse files
committed
failing test for parameterized queries
1 parent c58037b commit cc2ff04

3 files changed

Lines changed: 57 additions & 8 deletions

File tree

lib/native.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ p._pulseQueryQueue = function() {
6666
return;
6767
}
6868
this._activeQuery = query;
69-
this._sendQuery(query.text);
69+
if(query.values) {
70+
//call native function
71+
this._sendQueryWithParams(query.text, query.values)
72+
} else {
73+
//call native function
74+
this._sendQuery(query.text);
75+
}
7076
}
7177

7278
var ctor = function(config) {
@@ -107,11 +113,13 @@ var connect = function(config, callback) {
107113
};
108114

109115
//event emitter proxy
110-
var NativeQuery = function(text) {
116+
var NativeQuery = function(text, values) {
111117
if(typeof text == 'object') {
112118
this.text = text.text;
119+
this.values = text.values;
113120
} else {
114121
this.text = text;
122+
this.values = values;
115123
}
116124
EventEmitter.call(this);
117125
};

src/binding.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Connection : public EventEmitter {
4040

4141
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
4242
NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery);
43+
NODE_SET_PROTOTYPE_METHOD(t, "_sendQueryWithParams", SendQueryWithParams);
4344
NODE_SET_PROTOTYPE_METHOD(t, "end", End);
4445

4546
target->Set(String::NewSymbol("Connection"), t->GetFunction());
@@ -89,6 +90,16 @@ class Connection : public EventEmitter {
8990
return Undefined();
9091
}
9192

93+
//v8 entry point into Connection#_sendQueryWithParams
94+
static Handle<Value>
95+
SendQueryWithParams(const Arguments& args)
96+
{
97+
HandleScope scope;
98+
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
99+
printf("%d\n", args.Length());
100+
return Undefined();
101+
}
102+
92103
//v8 entry point into Connection#end
93104
static Handle<Value>
94105
End(const Arguments& args)

test/native/evented-api-tests.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ test('connects', function() {
2626
})
2727
})
2828
})
29-
30-
test('multiple results', function() {
29+
var setupClient = function() {
3130
var client = new Client(conString);
3231
client.connect();
32+
client.query("CREATE TEMP TABLE boom(name varchar(10))");
33+
client.query("INSERT INTO boom(name) VALUES('Aaron')");
34+
client.query("INSERT INTO boom(name) VALUES('Brian')");
35+
return client;
36+
}
37+
test('multiple results', function() {
3338
test('queued queries', function() {
34-
client.query("CREATE TEMP TABLE boom(name varchar(10))");
35-
client.query("INSERT INTO boom(name) VALUES('Aaron')");
36-
client.query("INSERT INTO boom(name) VALUES('Brian')");
39+
var client = setupClient();
3740
var q = client.query("SELECT * from BOOM");
3841
assert.emits(q, 'row', function(row) {
3942
assert.equal(row.name, 'Aaron');
4043
assert.emits(q, 'row', function(row) {
4144
assert.equal(row.name, "Brian");
42-
4345
})
4446
})
4547
assert.emits(q, 'end', function() {
@@ -55,3 +57,31 @@ test('multiple results', function() {
5557
})
5658
})
5759
})
60+
61+
test('parameterized queries', function() {
62+
test('with a single string param', function() {
63+
var client = setupClient();
64+
var q = client.query("SELECT name FROM boom WHERE name = $1", ['Brian']);
65+
assert.emits(q, 'row', function(row) {
66+
assert.equal(row.name, 'Brian')
67+
})
68+
assert.emits(q, 'end', function() {
69+
client.end();
70+
});
71+
})
72+
test('with object config for query', function() {
73+
var client = setupClient();
74+
var q = client.query({
75+
text: "SELECT name FROM boom WHERE name = $1",
76+
values: ['Brian']
77+
});
78+
assert.emits(q, 'row', function(row) {
79+
assert.equal(row.name, 'Brian');
80+
})
81+
assert.emits(q, 'end', function() {
82+
client.end();
83+
})
84+
})
85+
86+
})
87+

0 commit comments

Comments
 (0)