2010-10-28 13:50:45 +08:00
|
|
|
var helper = require(__dirname +'/test-helper');
|
|
|
|
|
2010-10-29 08:09:40 +08:00
|
|
|
test("simple, unnamed prepared statement", function(){
|
2010-10-28 13:50:45 +08:00
|
|
|
var client = helper.client();
|
2010-10-28 14:04:55 +08:00
|
|
|
|
2010-10-28 13:50:45 +08:00
|
|
|
var query = client.query({
|
|
|
|
text: 'select age from person where name = $1',
|
|
|
|
values: ['Brian']
|
|
|
|
});
|
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row.age, 20);
|
2010-10-28 13:50:45 +08:00
|
|
|
});
|
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'end', function() {
|
2010-10-28 13:50:45 +08:00
|
|
|
client.end();
|
|
|
|
});
|
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
|
|
|
test("named prepared statement", function() {
|
|
|
|
|
|
|
|
var client = helper.client();
|
2010-10-30 09:05:35 +08:00
|
|
|
client.on('drain', client.end.bind(client));
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-10-30 09:05:35 +08:00
|
|
|
var queryName = "user by age and like name";
|
2010-10-29 08:09:40 +08:00
|
|
|
var parseCount = 0;
|
|
|
|
|
2010-10-30 09:05:35 +08:00
|
|
|
test("first named prepared statement",function() {
|
|
|
|
var query = client.query({
|
|
|
|
text: 'select name from person where age <= $1 and name LIKE $2',
|
|
|
|
values: [20, 'Bri%'],
|
|
|
|
name: queryName
|
2010-10-29 08:09:40 +08:00
|
|
|
});
|
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row.name, 'Brian');
|
2010-10-30 09:05:35 +08:00
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'end', function() {
|
2010-10-29 08:09:40 +08:00
|
|
|
});
|
2010-10-30 09:05:35 +08:00
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-10-30 09:05:35 +08:00
|
|
|
test("second named prepared statement with same name & text", function() {
|
|
|
|
var cachedQuery = client.query({
|
|
|
|
text: 'select name from person where age <= $1 and name LIKE $2',
|
|
|
|
name: queryName,
|
|
|
|
values: [10, 'A%']
|
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(cachedQuery, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row.name, 'Aaron');
|
2010-10-30 09:05:35 +08:00
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(cachedQuery, 'end', function() {
|
2010-10-30 09:05:35 +08:00
|
|
|
});
|
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-10-30 09:05:35 +08:00
|
|
|
test("with same name, but the query text not even there batman!", function() {
|
|
|
|
var q = client.query({
|
|
|
|
name: queryName,
|
|
|
|
values: [30, '%n%']
|
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-10-30 09:05:35 +08:00
|
|
|
test("gets first row", function() {
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(q, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row.name, "Aaron");
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-10-30 09:05:35 +08:00
|
|
|
test("gets second row", function() {
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(q, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row.name, "Brian");
|
2010-10-30 09:05:35 +08:00
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2010-10-30 09:05:35 +08:00
|
|
|
});
|
2010-10-29 08:09:40 +08:00
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(q, 'end', function() {
|
2011-02-23 09:16:05 +08:00
|
|
|
|
2010-10-29 08:09:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("prepared statements on different clients", function() {
|
|
|
|
var statementName = "differ";
|
2013-07-31 02:15:31 +08:00
|
|
|
var statement1 = "select count(*)::int4 as count from person";
|
|
|
|
var statement2 = "select count(*)::int4 as count from person where age < $1";
|
2010-10-29 08:09:40 +08:00
|
|
|
|
|
|
|
var client1Finished = false;
|
|
|
|
var client2Finished = false;
|
|
|
|
|
|
|
|
var client1 = helper.client();
|
|
|
|
|
|
|
|
var client2 = helper.client();
|
|
|
|
|
|
|
|
test("client 1 execution", function() {
|
|
|
|
|
|
|
|
var query = client1.query({
|
|
|
|
name: statementName,
|
|
|
|
text: statement1
|
|
|
|
});
|
|
|
|
test('gets right data back', function() {
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row.count, 26);
|
2010-10-29 08:09:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'end', function() {
|
2010-10-29 08:09:40 +08:00
|
|
|
if(client2Finished) {
|
|
|
|
client1.end();
|
|
|
|
client2.end();
|
|
|
|
} else {
|
|
|
|
client1Finished = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test('client 2 execution', function() {
|
|
|
|
var query = client2.query({
|
|
|
|
name: statementName,
|
|
|
|
text: statement2,
|
|
|
|
values: [11]
|
|
|
|
});
|
|
|
|
|
|
|
|
test('gets right data', function() {
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'row', function(row) {
|
2010-11-04 13:21:29 +08:00
|
|
|
assert.equal(row.count, 1);
|
2010-10-29 08:09:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-11-01 03:43:10 +08:00
|
|
|
assert.emits(query, 'end', function() {
|
2010-10-29 08:09:40 +08:00
|
|
|
if(client1Finished) {
|
|
|
|
client1.end();
|
|
|
|
client2.end();
|
|
|
|
} else {
|
|
|
|
client2Finished = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2010-10-29 13:46:08 +08:00
|
|
|
|
2010-11-15 07:53:49 +08:00
|
|
|
test('prepared statement', function() {
|
|
|
|
var client = helper.client();
|
|
|
|
client.on('drain', client.end.bind(client));
|
|
|
|
client.query('CREATE TEMP TABLE zoom(name varchar(100));');
|
|
|
|
client.query("INSERT INTO zoom (name) VALUES ('zed')");
|
|
|
|
client.query("INSERT INTO zoom (name) VALUES ('postgres')");
|
|
|
|
client.query("INSERT INTO zoom (name) VALUES ('node postgres')");
|
|
|
|
|
2010-11-15 07:56:18 +08:00
|
|
|
var checkForResults = function(q) {
|
2010-11-15 07:53:49 +08:00
|
|
|
test('row callback fires for each result', function() {
|
|
|
|
assert.emits(q, 'row', function(row) {
|
|
|
|
assert.equal(row.name, 'node postgres');
|
2010-11-15 07:56:18 +08:00
|
|
|
|
2010-11-15 07:53:49 +08:00
|
|
|
assert.emits(q, 'row', function(row) {
|
|
|
|
assert.equal(row.name, 'postgres');
|
2010-11-15 07:56:18 +08:00
|
|
|
|
2010-11-15 07:53:49 +08:00
|
|
|
assert.emits(q, 'row', function(row) {
|
|
|
|
assert.equal(row.name, 'zed');
|
|
|
|
})
|
|
|
|
});
|
|
|
|
})
|
|
|
|
})
|
2010-11-15 07:56:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
test('with small row count', function() {
|
|
|
|
var query = client.query({
|
|
|
|
name: 'get names',
|
|
|
|
text: "SELECT name FROM zoom ORDER BY name",
|
|
|
|
rows: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
checkForResults(query);
|
2010-11-15 07:53:49 +08:00
|
|
|
|
|
|
|
})
|
2010-11-15 07:56:18 +08:00
|
|
|
|
|
|
|
test('with large row count', function() {
|
|
|
|
var query = client.query({
|
|
|
|
name: 'get names',
|
|
|
|
text: 'SELECT name FROM zoom ORDER BY name',
|
|
|
|
rows: 1000
|
|
|
|
})
|
|
|
|
checkForResults(query);
|
|
|
|
})
|
|
|
|
|
2010-11-15 07:53:49 +08:00
|
|
|
})
|