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

Commit78d40a2

Browse files
author
Dave Cramer
committed
Two versions of QueryExecutor, currently only version 2 works 100%
these versions adhere to the backend protocol better than previous versionfixes problem when an error occurs on the backend, and the connection is still usedprevious versions were throwing an exception half way through the protocol, leaving itindeterminate.also removes empty query code, should speed things up a bit
1 parent54cc549 commit78d40a2

File tree

2 files changed

+260
-2
lines changed

2 files changed

+260
-2
lines changed

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

Lines changed: 1 addition & 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.10 2002/03/18 04:16:33 davec Exp $
16+
* $Id: QueryExecutor.java,v 1.11 2002/03/21 03:20:30 davec Exp $
1717
*/
1818

1919
publicclassQueryExecutor
@@ -104,7 +104,6 @@ public java.sql.ResultSet execute() throws SQLException
104104

105105
errorMessage.append(pg_stream.ReceiveString(connection.getEncoding()));
106106
// keep processing
107-
hfr =true;
108107
break;
109108

110109
case'I':// Empty Query
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
2+
packageorg.postgresql.core;
3+
4+
importjava.util.Vector;
5+
importjava.io.IOException;
6+
importjava.sql.*;
7+
importorg.postgresql.*;
8+
importorg.postgresql.util.PSQLException;
9+
10+
/*
11+
* Executes a query on the backend.
12+
*
13+
* <p>The lifetime of a QueryExecutor object is from sending the query
14+
* until the response has been received from the backend.
15+
*
16+
* $Id: QueryExecutor2.java,v 1.1 2002/03/21 03:20:29 davec Exp $
17+
*/
18+
19+
publicclassQueryExecutor2
20+
{
21+
22+
privatefinalStringsql;
23+
privatefinaljava.sql.Statementstatement;
24+
privatefinalPG_Streampg_stream;
25+
privatefinalorg.postgresql.Connectionconnection;
26+
27+
publicQueryExecutor2(Stringsql,
28+
java.sql.Statementstatement,
29+
PG_Streampg_stream,
30+
org.postgresql.Connectionconnection)
31+
throwsSQLException
32+
{
33+
this.sql =sql;
34+
this.statement =statement;
35+
this.pg_stream =pg_stream;
36+
this.connection =connection;
37+
38+
if (statement !=null)
39+
maxRows =statement.getMaxRows();
40+
else
41+
maxRows =0;
42+
}
43+
44+
privateField[]fields =null;
45+
privateVectortuples =newVector();
46+
privatebooleanbinaryCursor =false;
47+
privateStringstatus =null;
48+
privateintupdate_count =1;
49+
privatelonginsert_oid =0;
50+
privateintmaxRows;
51+
52+
/*
53+
* Execute a query on the backend.
54+
*/
55+
publicjava.sql.ResultSetexecute()throwsSQLException
56+
{
57+
58+
StringBuffererrorMessage =null;
59+
60+
synchronized (pg_stream)
61+
{
62+
63+
sendQuery(sql);
64+
connection.asyncStatus =org.postgresql.Connection.PGASYNC_BUSY;
65+
66+
while (connection.asyncStatus !=org.postgresql.Connection.PGASYNC_IDLE )
67+
{
68+
intc =pg_stream.ReceiveChar();
69+
70+
if (c =='A' )
71+
{
72+
73+
intpid =pg_stream.ReceiveIntegerR(4);
74+
Stringmsg =pg_stream.ReceiveString(connection.getEncoding());
75+
76+
org.postgresql.Driver.debug(msg);
77+
continue;
78+
}
79+
elseif (c =='N' )
80+
{
81+
Stringnotification =pg_stream.ReceiveString(connection.getEncoding());
82+
org.postgresql.Driver.debug(notification);
83+
connection.addWarning(notification);
84+
continue;
85+
}
86+
elseif (connection.asyncStatus !=org.postgresql.Connection.PGASYNC_BUSY )
87+
{
88+
if (connection.asyncStatus !=org.postgresql.Connection.PGASYNC_IDLE )
89+
{
90+
// only one possibility left which is PGASYNC_READY, so let's get out
91+
break;
92+
}
93+
if (c =='E' ) {
94+
Stringerror =pg_stream.ReceiveString(connection.getEncoding());
95+
org.postgresql.Driver.debug(error);
96+
97+
// no sense in creating this object until we really need it
98+
if (errorMessage ==null ) {
99+
errorMessage =newStringBuffer();
100+
}
101+
102+
errorMessage.append(error);
103+
break;
104+
}
105+
}else{
106+
107+
switch (c)
108+
{
109+
case'C':// Command Status
110+
receiveCommandStatus();
111+
break;
112+
113+
case'E':// Error Message
114+
115+
// it's possible to get multiple error messages from one query
116+
// see libpq, there are some comments about a connection being closed
117+
// by the backend after real error occurs, so append error messages here
118+
// so append them and just remember that an error occured
119+
// throw the exception at the end of processing
120+
121+
Stringerror =pg_stream.ReceiveString(connection.getEncoding());
122+
org.postgresql.Driver.debug(error);
123+
124+
// no sense in creating this object until we really need it
125+
if (errorMessage ==null ) {
126+
errorMessage =newStringBuffer();
127+
}
128+
129+
errorMessage.append(error);
130+
connection.asyncStatus =org.postgresql.Connection.PGASYNC_READY;
131+
break;
132+
133+
case'Z':// backend ready for query, ignore for now :-)
134+
connection.asyncStatus =org.postgresql.Connection.PGASYNC_IDLE;
135+
break;
136+
137+
case'I':// Empty Query
138+
intt =pg_stream.ReceiveChar();
139+
if (t !=0)
140+
thrownewPSQLException("postgresql.con.garbled");
141+
142+
connection.asyncStatus =org.postgresql.Connection.PGASYNC_READY;
143+
break;
144+
145+
case'P':// Portal Name
146+
Stringpname =pg_stream.ReceiveString(connection.getEncoding());
147+
org.postgresql.Driver.debug(pname);
148+
break;
149+
150+
case'T':// MetaData Field Description
151+
receiveFields();
152+
break;
153+
154+
case'B':// Binary Data Transfer
155+
receiveTuple(true);
156+
break;
157+
158+
case'D':// Text Data Transfer
159+
receiveTuple(false);
160+
break;
161+
162+
default:
163+
thrownewPSQLException("postgresql.con.type",
164+
newCharacter((char)c));
165+
}
166+
}
167+
}
168+
// did we get an error message?
169+
170+
if (errorMessage !=null ) {
171+
// yes, throw an exception
172+
thrownewSQLException(errorMessage.toString());
173+
}
174+
returnconnection.getResultSet(connection,statement,fields,tuples,status,update_count,insert_oid,binaryCursor);
175+
}
176+
}
177+
178+
/*
179+
* Send a query to the backend.
180+
*/
181+
privatevoidsendQuery(Stringquery)throwsSQLException
182+
{
183+
try
184+
{
185+
pg_stream.SendChar('Q');
186+
pg_stream.Send(connection.getEncoding().encode(query));
187+
pg_stream.SendChar(0);
188+
pg_stream.flush();
189+
190+
}
191+
catch (IOExceptione)
192+
{
193+
thrownewPSQLException("postgresql.con.ioerror",e);
194+
}
195+
}
196+
197+
/*
198+
* Receive a tuple from the backend.
199+
*
200+
* @param isBinary set if the tuple should be treated as binary data
201+
*/
202+
privatevoidreceiveTuple(booleanisBinary)throwsSQLException
203+
{
204+
if (fields ==null)
205+
thrownewPSQLException("postgresql.con.tuple");
206+
Objecttuple =pg_stream.ReceiveTuple(fields.length,isBinary);
207+
if (isBinary)
208+
binaryCursor =true;
209+
if (maxRows ==0 ||tuples.size() <maxRows)
210+
tuples.addElement(tuple);
211+
}
212+
213+
/*
214+
* Receive command status from the backend.
215+
*/
216+
privatevoidreceiveCommandStatus()throwsSQLException
217+
{
218+
status =pg_stream.ReceiveString(connection.getEncoding());
219+
220+
try
221+
{
222+
// Now handle the update count correctly.
223+
if (status.startsWith("INSERT") ||status.startsWith("UPDATE") ||status.startsWith("DELETE") ||status.startsWith("MOVE"))
224+
{
225+
update_count =Integer.parseInt(status.substring(1 +status.lastIndexOf(' ')));
226+
}
227+
if (status.startsWith("INSERT"))
228+
{
229+
insert_oid =Long.parseLong(status.substring(1 +status.indexOf(' '),
230+
status.lastIndexOf(' ')));
231+
}
232+
}
233+
catch (NumberFormatExceptionnfe)
234+
{
235+
thrownewPSQLException("postgresql.con.fathom",status);
236+
}
237+
}
238+
239+
/*
240+
* Receive the field descriptions from the back end.
241+
*/
242+
privatevoidreceiveFields()throwsSQLException
243+
{
244+
if (fields !=null)
245+
thrownewPSQLException("postgresql.con.multres");
246+
247+
intsize =pg_stream.ReceiveIntegerR(2);
248+
fields =newField[size];
249+
250+
for (inti =0;i <fields.length;i++)
251+
{
252+
StringtypeName =pg_stream.ReceiveString(connection.getEncoding());
253+
inttypeOid =pg_stream.ReceiveIntegerR(4);
254+
inttypeLength =pg_stream.ReceiveIntegerR(2);
255+
inttypeModifier =pg_stream.ReceiveIntegerR(4);
256+
fields[i] =newField(connection,typeName,typeOid,typeLength,typeModifier);
257+
}
258+
}
259+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp