Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed bug in the closeStatement handler. Fixed callback getting a pre…

…defined number of arguments for queries.
  • Loading branch information...
commit ecc94a147961970ae20ce8c059a915bb46253b45 1 parent 0e59fd4
@Frans-Willem authored
Showing with 48 additions and 8 deletions.
  1. +7 −8 lib/PostgresClient.js
  2. +41 −0 test.js
View
15 lib/PostgresClient.js
@@ -9,6 +9,7 @@ var sys=require("sys"),
BufferQueueReader=require("bufferlib/BufferQueueReader").BufferQueueReader,
EventEmitter=require("events").EventEmitter,
Constants=require("./PostgresConstants");
+
/*
TODO:
- Make sure on commands without rows that rows is undefined (or maybe []), make sure that the result is always in the same place.
@@ -16,6 +17,7 @@ TODO:
SELECT 1; => callback(undefined,[[1]],"SELECT");
DELETE ...; => callback(undefined,undefined,"DELETE 123");
UPDATE ...; => callback(undefined,undefined,"UPDATE 123");
+- COPY STDIN/STDOUT support
*/
function createPostgresConnectHandler(self) {
@@ -283,7 +285,6 @@ function createQueryHandler(callback) {
rowCallback=callback.row,
commandCallback=callback.commandComplete,
finalCallback=(callback.complete || callback);
- rowinfoCallback
//rowinfoCallback,rowCallback,commandCallback,finalCallback
return function(err,type,data) {
if (err) {
@@ -311,23 +312,21 @@ function createQueryHandler(callback) {
rowinfoCallback(latestRowInfo);
}
if (!rowCallback) {
- savedArgs.push(latestRows=[]);
+ latestRows=[];
}
} else if (type===Constants.MessageTypes.Backend.DataRow) {
var p=(new PostgresReader(data)).popDataRow();
if (rowCallback) {
rowCallback((latestRowInfo && !rowinfoCallback)?parseDataRow(p,latestRowInfo):p.map(String));
} else {
- if (!latestRows) {
- savedArgs.push(latestRows=[]);
- }
- latestRows.push(latestRowInfo?parseDataRow(p,latestRowInfo):p.map(String));
+ (latestRows || (latestRows=[])).push(latestRowInfo?parseDataRow(p,latestRowInfo):p.map(String));
}
} else if (type===Constants.MessageTypes.Backend.CommandComplete) {
temp=(new PostgresReader(data)).popCommandComplete();
if (commandCallback) {
commandCallback(temp);
} else {
+ savedArgs.push(latestRows);
savedArgs.push(temp);
latestRowInfo=latestRows=undefined;
}
@@ -447,7 +446,7 @@ function createCloseHandler(finalCallback) {
}
return false;
case Constants.MessageTypes.Backend.CloseComplete:
- return false; //Ignore
+ return true; //Done
default:
if (!returned) {
returned=true;
@@ -463,7 +462,7 @@ PostgresClient.prototype.closeStatement=function(statementName,callback) {
b=PostgresEncoder.create()
.pushClose(Constants.Close.Statement,statementName)
.toBuffer();
- this.handlerQueue.push(createParseHandler(callback,name));
+ this.handlerQueue.push(createCloseHandler(callback,name));
return this.write(b);
};
View
41 test.js
@@ -88,4 +88,45 @@ client.on("notice",function(notice) {
//Example:
// {"Message":"Hello world","Severity":"NOTICE"}
//See http://developer.postgresql.org/pgdocs/postgres/protocol-error-fields.html for all fields
+});
+
+//CREATE/DELETE/INSERT/DROP support
+client.simpleQuery("CREATE TABLE testtable (first int4, second int4);",function(err,rows,result) {
+ if (err) {
+ sys.puts("CREATE TABLE Error: "+err);
+ return;
+ }
+ sys.puts("CREATE TABLE Callback Arguments:");
+ sys.puts("\trows: "+sys.inspect(rows));
+ sys.puts("\tresult: "+sys.inspect(result));
+ client.simpleQuery("INSERT INTO testtable (first,second) VALUES(1,2);INSERT INTO testtable (first,second) VALUES (3,4),(5,6);",function(err,rows1,result1,rows2,result2) {
+ if (err) {
+ sys.puts("INSERT INTO Error: "+err);
+ return;
+ }
+ sys.puts("INSERT INTO Callback Arguments:");
+ sys.puts("\trows1: "+sys.inspect(rows1));
+ sys.puts("\tresult1: "+sys.inspect(result1));
+ sys.puts("\trows2: "+sys.inspect(rows2));
+ sys.puts("\tresult2: "+sys.inspect(result2));
+
+ client.simpleQuery("SELECT * FROM testtable;",function(err,rows,result) {
+ if (err) {
+ sys.puts("SELECT Error: "+err);
+ return;
+ }
+ sys.puts("SELECT Callback Arguments:");
+ sys.puts("\trows: "+sys.inspect(rows));
+ sys.puts("\tresult: "+sys.inspect(result));
+ client.simpleQuery("DROP TABLE testtable;",function(err,rows,result) {
+ if (err) {
+ sys.puts("DROP TABLE Error: "+err);
+ return;
+ }
+ sys.puts("DROP TABLE Callback Arguments:");
+ sys.puts("\trows: "+sys.inspect(rows));
+ sys.puts("\tresult: "+sys.inspect(result));
+ });
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.