|
| 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 | +} |