From 134fe5ec61cb280459a4782b6d465e115c5ac5a8 Mon Sep 17 00:00:00 2001
From: Dave Cramer <davec@fastcrypt.com>
Date: Sat, 16 Mar 2002 02:15:23 +0000
Subject: [PATCH] fixed QueryExecuter to deal with multiple errors previously
 it was throwing a SQLException as soon as the error message was received from
 the backend. This did not allow the protocol to finish properly now, simply
 collects error messages from the backend until the query is done and throws
 exception at the end Also added setLogLevel to Driver.java, and made the log
 levels public

---
 .../jdbc/org/postgresql/Driver.java.in        | 24 +++++++++++++++----
 .../org/postgresql/core/QueryExecutor.java    | 22 +++++++++++++++--
 2 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/src/interfaces/jdbc/org/postgresql/Driver.java.in b/src/interfaces/jdbc/org/postgresql/Driver.java.in
index 5263cf9d0b6..a6d0fb03a25 100644
--- a/src/interfaces/jdbc/org/postgresql/Driver.java.in
+++ b/src/interfaces/jdbc/org/postgresql/Driver.java.in
@@ -27,11 +27,13 @@ import org.postgresql.util.PSQLException;
 public class Driver implements java.sql.Driver
 {
 
-	protected static final int DEBUG = 0;
-	protected static final int INFO = 1;
-	protected static final int WARN = 2;
-	protected static final int ERROR = 3;
-	protected static final int FATAL = 4;
+	// make these public so they can be used in setLogLevel below
+
+	public static final int DEBUG = 0;
+	public static final int INFO = 1;
+	public static final int WARN = 2;
+	public static final int ERROR = 3;
+	public static final int FATAL = 4;
 
 	private static int logLevel = FATAL;
 
@@ -439,6 +441,18 @@ public class Driver implements java.sql.Driver
 	{
 		return new PSQLException("postgresql.unimplemented");
 	}
+
+	/**
+	*	used to turn logging on to a certain level, can be called 
+	*	by specifying fully qualified class ie org.postgresql.Driver.setLogLevel()
+	*	@param int logLevel sets the level which logging will respond to
+	* 	FATAL being almost no messages
+	*	DEBUG most verbose
+	*/
+	public static void setLogLevel(int logLevel)
+	{
+		Driver.logLevel = logLevel;
+	}
 	/*
 	 * logging message at the debug level
 	 * messages will be printed if the logging level is less or equal to DEBUG
diff --git a/src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java b/src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java
index f5b9aef492f..72877e5e4ce 100644
--- a/src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java
+++ b/src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java
@@ -13,7 +13,7 @@ import org.postgresql.util.PSQLException;
  * <p>The lifetime of a QueryExecutor object is from sending the query
  * until the response has been received from the backend.
  *
- * $Id: QueryExecutor.java,v 1.8 2002/03/05 20:11:57 davec Exp $
+ * $Id: QueryExecutor.java,v 1.9 2002/03/16 02:15:23 davec Exp $
  */
 
 public class QueryExecutor
@@ -58,6 +58,8 @@ public class QueryExecutor
 		int fqp = 0;
 		boolean hfr = false;
 
+		StringBuffer errorMessage = null;
+
 		synchronized (pg_stream)
 		{
 
@@ -91,7 +93,19 @@ public class QueryExecutor
 						receiveTuple(false);
 						break;
 					case 'E':	// Error Message
-						throw new SQLException(pg_stream.ReceiveString(connection.getEncoding()));
+
+						// it's possible to get more than one error message for a query
+						// see libpq comments wrt backend closing a connection 
+						// so, append messages to a string buffer and keep processing
+						// check at the bottom to see if we need to throw an exception
+						
+						if ( errorMessage == null )
+							errorMessage = new StringBuffer();
+
+						errorMessage.append(pg_stream.ReceiveString(connection.getEncoding()));
+						// keep processing
+						break;
+
 					case 'I':	// Empty Query
 						int t = pg_stream.ReceiveChar();
 						if (t != 0)
@@ -117,6 +131,10 @@ public class QueryExecutor
 						throw new PSQLException("postgresql.con.type",
 												new Character((char) c));
 				}
+
+				// did we get an error during this query?
+				if ( errorMessage != null )
+					throw new SQLException( errorMessage.toString() );
 			}
 			return connection.getResultSet(connection, statement, fields, tuples, status, update_count, insert_oid, binaryCursor);
 		}
-- 
GitLab