Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 134fe5e

Browse files
author
Dave Cramer
committed
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
1 parent efd45bc commit 134fe5e

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/interfaces/jdbc/org/postgresql/Driver.java.in

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ import org.postgresql.util.PSQLException;
2727
public class Driver implements java.sql.Driver
2828
{
2929

30-
protected static final int DEBUG = 0;
31-
protected static final int INFO = 1;
32-
protected static final int WARN = 2;
33-
protected static final int ERROR = 3;
34-
protected static final int FATAL = 4;
30+
// make these public so they can be used in setLogLevel below
31+
32+
public static final int DEBUG = 0;
33+
public static final int INFO = 1;
34+
public static final int WARN = 2;
35+
public static final int ERROR = 3;
36+
public static final int FATAL = 4;
3537

3638
private static int logLevel = FATAL;
3739

@@ -439,6 +441,18 @@ public class Driver implements java.sql.Driver
439441
{
440442
return new PSQLException("postgresql.unimplemented");
441443
}
444+
445+
/**
446+
* used to turn logging on to a certain level, can be called
447+
* by specifying fully qualified class ie org.postgresql.Driver.setLogLevel()
448+
* @param int logLevel sets the level which logging will respond to
449+
* FATAL being almost no messages
450+
* DEBUG most verbose
451+
*/
452+
public static void setLogLevel(int logLevel)
453+
{
454+
Driver.logLevel = logLevel;
455+
}
442456
/*
443457
* logging message at the debug level
444458
* messages will be printed if the logging level is less or equal to DEBUG

src/interfaces/jdbc/org/postgresql/core/QueryExecutor.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* <p>The lifetime of a QueryExecutor object is from sending the query
1414
* until the response has been received from the backend.
1515
*
16-
* $Id: QueryExecutor.java,v 1.8 2002/03/05 20:11:57 davec Exp $
16+
* $Id: QueryExecutor.java,v 1.9 2002/03/16 02:15:23 davec Exp $
1717
*/
1818

1919
public class QueryExecutor
@@ -58,6 +58,8 @@ public java.sql.ResultSet execute() throws SQLException
5858
int fqp = 0;
5959
boolean hfr = false;
6060

61+
StringBuffer errorMessage = null;
62+
6163
synchronized (pg_stream)
6264
{
6365

@@ -91,7 +93,19 @@ public java.sql.ResultSet execute() throws SQLException
9193
receiveTuple(false);
9294
break;
9395
case 'E': // Error Message
94-
throw new SQLException(pg_stream.ReceiveString(connection.getEncoding()));
96+
97+
// it's possible to get more than one error message for a query
98+
// see libpq comments wrt backend closing a connection
99+
// so, append messages to a string buffer and keep processing
100+
// check at the bottom to see if we need to throw an exception
101+
102+
if ( errorMessage == null )
103+
errorMessage = new StringBuffer();
104+
105+
errorMessage.append(pg_stream.ReceiveString(connection.getEncoding()));
106+
// keep processing
107+
break;
108+
95109
case 'I': // Empty Query
96110
int t = pg_stream.ReceiveChar();
97111
if (t != 0)
@@ -117,6 +131,10 @@ public java.sql.ResultSet execute() throws SQLException
117131
throw new PSQLException("postgresql.con.type",
118132
new Character((char) c));
119133
}
134+
135+
// did we get an error during this query?
136+
if ( errorMessage != null )
137+
throw new SQLException( errorMessage.toString() );
120138
}
121139
return connection.getResultSet(connection, statement, fields, tuples, status, update_count, insert_oid, binaryCursor);
122140
}

0 commit comments

Comments
 (0)