Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite30b283

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 calledQueryExecutor (should go into org/postgresql/core/) and then taking itapart 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 parentd99794e commite30b283

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
importorg.postgresql.fastpath.*;
99
importorg.postgresql.largeobject.*;
1010
importorg.postgresql.util.*;
11-
importorg.postgresql.core.Encoding;
11+
importorg.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-
publicjava.sql.ResultSetExecSQL(Stringsql,java.sql.Statementstat)throwsSQLException
351+
publicjava.sql.ResultSetExecSQL(Stringsql,java.sql.Statementstat)throwsSQLException
352352
{
353-
// added Jan 30 2001 to correct maxrows per statement
354-
intmaxrows=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-
Vectortuples =newVector();
371-
byte[]buf =null;
372-
intfqp =0;
373-
booleanhfr =false;
374-
Stringrecv_status =null,msg;
375-
intupdate_count =1;
376-
intinsert_oid =0;
377-
SQLExceptionfinal_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 (IOExceptione) {
387-
thrownewPSQLException("postgresql.con.ioerror",e);
388-
}
389-
390-
while (!hfr ||fqp >0)
391-
{
392-
Objecttup=null;// holds rows as they are recieved
393-
394-
intc =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-
thrownewPSQLException("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(NumberFormatExceptionnfe) {
418-
thrownewPSQLException("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(NumberFormatExceptionnfe) {
424-
thrownewPSQLException("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 (IOExceptione) {
439-
thrownewPSQLException("postgresql.con.ioerror",e);
440-
}
441-
fqp++;
442-
}
443-
break;
444-
case'D':// Text Data Transfer
445-
if (fields ==null)
446-
thrownewPSQLException("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 =newSQLException(msg);
455-
hfr =true;
456-
break;
457-
case'I':// Empty Query
458-
intt =pg_stream.ReceiveChar();
459-
460-
if (t !=0)
461-
thrownewPSQLException("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-
Stringpname =pg_stream.ReceiveString(encoding);
472-
break;
473-
case'T':// MetaData Field Description
474-
if (fields !=null)
475-
thrownewPSQLException("postgresql.con.multres");
476-
fields =ReceiveFields();
477-
break;
478-
case'Z':// backend ready for query, ignore for now :-)
479-
break;
480-
default:
481-
thrownewPSQLException("postgresql.con.type",newCharacter((char)c));
482-
}
483-
}
484-
if (final_error !=null)
485-
throwfinal_error;
486-
487-
returngetResultSet(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-
privateField[]ReceiveFields()throwsSQLException
498-
{
499-
intnf =pg_stream.ReceiveIntegerR(2),i;
500-
Field[]fields =newField[nf];
501-
502-
for (i =0 ;i <nf ; ++i)
503-
{
504-
Stringtypname =pg_stream.ReceiveString(encoding);
505-
inttypid =pg_stream.ReceiveIntegerR(4);
506-
inttyplen =pg_stream.ReceiveIntegerR(2);
507-
inttypmod =pg_stream.ReceiveIntegerR(4);
508-
fields[i] =newField(this,typname,typid,typlen,typmod);
509-
}
510-
returnfields;
353+
returnnewQueryExecutor(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-
protectedabstractjava.sql.ResultSetgetResultSet(org.postgresql.Connectionconn,java.sql.Statementstat,Field[]fields,Vectortuples,Stringstatus,intupdateCount,intinsertOID)throwsSQLException;
639+
publicabstractjava.sql.ResultSetgetResultSet(org.postgresql.Connectionconn,java.sql.Statementstat,Field[]fields,Vectortuples,Stringstatus,intupdateCount,intinsertOID)throwsSQLException;
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
importorg.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-
protectedjava.sql.ResultSetgetResultSet(org.postgresql.Connectionconn,java.sql.Statementstat,Field[]fields,Vectortuples,Stringstatus,intupdateCount,intinsertOID)throwsSQLException
134+
publicjava.sql.ResultSetgetResultSet(org.postgresql.Connectionconn,java.sql.Statementstat,Field[]fields,Vectortuples,Stringstatus,intupdateCount,intinsertOID)throwsSQLException
135135
{
136136
// in jdbc1 stat is ignored.
137137
returnneworg.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
importorg.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-
protectedjava.sql.ResultSetgetResultSet(org.postgresql.Connectionconn,java.sql.Statementstat,Field[]fields,Vectortuples,Stringstatus,intupdateCount,intinsertOID)throwsSQLException
207+
publicjava.sql.ResultSetgetResultSet(org.postgresql.Connectionconn,java.sql.Statementstat,Field[]fields,Vectortuples,Stringstatus,intupdateCount,intinsertOID)throwsSQLException
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)

[8]ページ先頭

©2009-2025 Movatter.jp