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

Commit e30b283

Browse files
committed
Attached is my attempt to clean up the horrors of the ExecSQL() method in
the JDBC driver. I've done this by extracting it into a new method object called QueryExecutor (should go into org/postgresql/core/) and then taking it apart into different methods in that class. A short summary: * Extracted ExecSQL() from Connection into a method object called QueryExecutor. * Moved ReceiveFields() from Connection to QueryExecutor. * Extracted parts of the original ExecSQL() method body into smaller methods on QueryExecutor. * Bug fix: The instance variable "pid" in Connection was used in two places with different meaning. Both were probably in dead code, but it's fixed anyway. Anders Bengtsson
1 parent d99794e commit e30b283

File tree

3 files changed

+9
-166
lines changed

3 files changed

+9
-166
lines changed

src/interfaces/jdbc/org/postgresql/Connection.java

Lines changed: 5 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import org.postgresql.fastpath.*;
99
import org.postgresql.largeobject.*;
1010
import org.postgresql.util.*;
11-
import org.postgresql.core.Encoding;
11+
import org.postgresql.core.*;
1212

1313
/**
14-
* $Id: Connection.java,v 1.26 2001/08/24 16:50:12 momjian Exp $
14+
* $Id: Connection.java,v 1.27 2001/09/06 03:13:34 momjian Exp $
1515
*
1616
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
1717
* JDBC2 versions of the Connection class.
@@ -348,166 +348,9 @@ public java.sql.ResultSet ExecSQL(String sql) throws SQLException
348348
* @return a ResultSet holding the results
349349
* @exception SQLException if a database error occurs
350350
*/
351-
public java.sql.ResultSet ExecSQL(String sql,java.sql.Statement stat) throws SQLException
351+
public java.sql.ResultSet ExecSQL(String sql, java.sql.Statement stat) throws SQLException
352352
{
353-
// added Jan 30 2001 to correct maxrows per statement
354-
int maxrows=0;
355-
if(stat!=null)
356-
maxrows=stat.getMaxRows();
357-
358-
// added Oct 7 1998 to give us thread safety.
359-
synchronized(pg_stream) {
360-
// Deallocate all resources in the stream associated
361-
// with a previous request.
362-
// This will let the driver reuse byte arrays that has already
363-
// been allocated instead of allocating new ones in order
364-
// to gain performance improvements.
365-
// PM 17/01/01: Commented out due to race bug. See comments in
366-
// PG_Stream
367-
//pg_stream.deallocate();
368-
369-
Field[] fields = null;
370-
Vector tuples = new Vector();
371-
byte[] buf = null;
372-
int fqp = 0;
373-
boolean hfr = false;
374-
String recv_status = null, msg;
375-
int update_count = 1;
376-
int insert_oid = 0;
377-
SQLException final_error = null;
378-
379-
buf = encoding.encode(sql);
380-
try
381-
{
382-
pg_stream.SendChar('Q');
383-
pg_stream.Send(buf);
384-
pg_stream.SendChar(0);
385-
pg_stream.flush();
386-
} catch (IOException e) {
387-
throw new PSQLException("postgresql.con.ioerror",e);
388-
}
389-
390-
while (!hfr || fqp > 0)
391-
{
392-
Object tup=null; // holds rows as they are recieved
393-
394-
int c = pg_stream.ReceiveChar();
395-
396-
switch (c)
397-
{
398-
case 'A': // Asynchronous Notify
399-
pid = pg_stream.ReceiveInteger(4);
400-
msg = pg_stream.ReceiveString(encoding);
401-
break;
402-
case 'B': // Binary Data Transfer
403-
if (fields == null)
404-
throw new PSQLException("postgresql.con.tuple");
405-
tup = pg_stream.ReceiveTuple(fields.length, true);
406-
// This implements Statement.setMaxRows()
407-
if(maxrows==0 || tuples.size()<maxrows)
408-
tuples.addElement(tup);
409-
break;
410-
case 'C': // Command Status
411-
recv_status = pg_stream.ReceiveString(encoding);
412-
413-
// Now handle the update count correctly.
414-
if(recv_status.startsWith("INSERT") || recv_status.startsWith("UPDATE") || recv_status.startsWith("DELETE") || recv_status.startsWith("MOVE")) {
415-
try {
416-
update_count = Integer.parseInt(recv_status.substring(1+recv_status.lastIndexOf(' ')));
417-
} catch(NumberFormatException nfe) {
418-
throw new PSQLException("postgresql.con.fathom",recv_status);
419-
}
420-
if(recv_status.startsWith("INSERT")) {
421-
try {
422-
insert_oid = Integer.parseInt(recv_status.substring(1+recv_status.indexOf(' '),recv_status.lastIndexOf(' ')));
423-
} catch(NumberFormatException nfe) {
424-
throw new PSQLException("postgresql.con.fathom",recv_status);
425-
}
426-
}
427-
}
428-
if (fields != null)
429-
hfr = true;
430-
else
431-
{
432-
try
433-
{
434-
pg_stream.SendChar('Q');
435-
pg_stream.SendChar(' ');
436-
pg_stream.SendChar(0);
437-
pg_stream.flush();
438-
} catch (IOException e) {
439-
throw new PSQLException("postgresql.con.ioerror",e);
440-
}
441-
fqp++;
442-
}
443-
break;
444-
case 'D': // Text Data Transfer
445-
if (fields == null)
446-
throw new PSQLException("postgresql.con.tuple");
447-
tup = pg_stream.ReceiveTuple(fields.length, false);
448-
// This implements Statement.setMaxRows()
449-
if(maxrows==0 || tuples.size()<maxrows)
450-
tuples.addElement(tup);
451-
break;
452-
case 'E': // Error Message
453-
msg = pg_stream.ReceiveString(encoding);
454-
final_error = new SQLException(msg);
455-
hfr = true;
456-
break;
457-
case 'I': // Empty Query
458-
int t = pg_stream.ReceiveChar();
459-
460-
if (t != 0)
461-
throw new PSQLException("postgresql.con.garbled");
462-
if (fqp > 0)
463-
fqp--;
464-
if (fqp == 0)
465-
hfr = true;
466-
break;
467-
case 'N': // Error Notification
468-
addWarning(pg_stream.ReceiveString(encoding));
469-
break;
470-
case 'P': // Portal Name
471-
String pname = pg_stream.ReceiveString(encoding);
472-
break;
473-
case 'T': // MetaData Field Description
474-
if (fields != null)
475-
throw new PSQLException("postgresql.con.multres");
476-
fields = ReceiveFields();
477-
break;
478-
case 'Z': // backend ready for query, ignore for now :-)
479-
break;
480-
default:
481-
throw new PSQLException("postgresql.con.type",new Character((char)c));
482-
}
483-
}
484-
if (final_error != null)
485-
throw final_error;
486-
487-
return getResultSet(this, stat, fields, tuples, recv_status, update_count, insert_oid);
488-
}
489-
}
490-
491-
/**
492-
* Receive the field descriptions from the back end
493-
*
494-
* @return an array of the Field object describing the fields
495-
* @exception SQLException if a database error occurs
496-
*/
497-
private Field[] ReceiveFields() throws SQLException
498-
{
499-
int nf = pg_stream.ReceiveIntegerR(2), i;
500-
Field[] fields = new Field[nf];
501-
502-
for (i = 0 ; i < nf ; ++i)
503-
{
504-
String typname = pg_stream.ReceiveString(encoding);
505-
int typid = pg_stream.ReceiveIntegerR(4);
506-
int typlen = pg_stream.ReceiveIntegerR(2);
507-
int typmod = pg_stream.ReceiveIntegerR(4);
508-
fields[i] = new Field(this, typname, typid, typlen, typmod);
509-
}
510-
return fields;
353+
return new QueryExecutor(sql, stat, pg_stream, this).execute();
511354
}
512355

513356
/**
@@ -793,7 +636,7 @@ private void initObjectTypes()
793636
* This returns a resultset. It must be overridden, so that the correct
794637
* version (from jdbc1 or jdbc2) are returned.
795638
*/
796-
protected abstract java.sql.ResultSet getResultSet(org.postgresql.Connection conn,java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount,int insertOID) throws SQLException;
639+
public abstract java.sql.ResultSet getResultSet(org.postgresql.Connection conn,java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount,int insertOID) throws SQLException;
797640

798641
/**
799642
* In some cases, it is desirable to immediately release a Connection's

src/interfaces/jdbc/org/postgresql/jdbc1/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.postgresql.util.*;
1818

1919
/**
20-
* $Id: Connection.java,v 1.8 2001/08/24 16:50:15 momjian Exp $
20+
* $Id: Connection.java,v 1.9 2001/09/06 03:13:34 momjian Exp $
2121
*
2222
* A Connection represents a session with a specific database. Within the
2323
* context of a Connection, SQL statements are executed and results are
@@ -131,7 +131,7 @@ public java.sql.DatabaseMetaData getMetaData() throws SQLException
131131
* This overides the method in org.postgresql.Connection and returns a
132132
* ResultSet.
133133
*/
134-
protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn,java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount,int insertOID) throws SQLException
134+
public java.sql.ResultSet getResultSet(org.postgresql.Connection conn,java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount,int insertOID) throws SQLException
135135
{
136136
// in jdbc1 stat is ignored.
137137
return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn,fields,tuples,status,updateCount,insertOID);

src/interfaces/jdbc/org/postgresql/jdbc2/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.postgresql.util.*;
1818

1919
/**
20-
* $Id: Connection.java,v 1.10 2001/08/24 16:50:16 momjian Exp $
20+
* $Id: Connection.java,v 1.11 2001/09/06 03:13:34 momjian Exp $
2121
*
2222
* A Connection represents a session with a specific database. Within the
2323
* context of a Connection, SQL statements are executed and results are
@@ -204,7 +204,7 @@ public java.sql.DatabaseMetaData getMetaData() throws SQLException
204204
* This overides the method in org.postgresql.Connection and returns a
205205
* ResultSet.
206206
*/
207-
protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat,Field[] fields, Vector tuples, String status, int updateCount, int insertOID) throws SQLException
207+
public java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat,Field[] fields, Vector tuples, String status, int updateCount, int insertOID) throws SQLException
208208
{
209209
// In 7.1 we now test concurrency to see which class to return. If we are not working with a
210210
// Statement then default to a normal ResultSet object.

0 commit comments

Comments
 (0)