|
| 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: QueryExecutor.java,v 1.1 2001/09/06 03:58:59 momjian Exp $ |
| 17 | + */ |
| 18 | + |
| 19 | +publicclassQueryExecutor { |
| 20 | + |
| 21 | +privatefinalStringsql; |
| 22 | +privatefinaljava.sql.Statementstatement; |
| 23 | +privatefinalPG_Streampg_stream; |
| 24 | +privatefinalorg.postgresql.Connectionconnection; |
| 25 | + |
| 26 | +publicQueryExecutor(Stringsql, |
| 27 | +java.sql.Statementstatement, |
| 28 | +PG_Streampg_stream, |
| 29 | +org.postgresql.Connectionconnection) |
| 30 | +throwsSQLException |
| 31 | + { |
| 32 | +this.sql =sql; |
| 33 | +this.statement =statement; |
| 34 | +this.pg_stream =pg_stream; |
| 35 | +this.connection =connection; |
| 36 | + |
| 37 | +if (statement !=null) |
| 38 | +maxRows =statement.getMaxRows(); |
| 39 | +else |
| 40 | +maxRows =0; |
| 41 | + } |
| 42 | + |
| 43 | +privateField[]fields =null; |
| 44 | +privateVectortuples =newVector(); |
| 45 | +privateStringstatus =null; |
| 46 | +privateintupdate_count =1; |
| 47 | +privateintinsert_oid =0; |
| 48 | +privateintmaxRows; |
| 49 | + |
| 50 | +/** |
| 51 | + * Execute a query on the backend. |
| 52 | + */ |
| 53 | +publicjava.sql.ResultSetexecute()throwsSQLException { |
| 54 | + |
| 55 | +intfqp =0; |
| 56 | +booleanhfr =false; |
| 57 | + |
| 58 | +synchronized(pg_stream) { |
| 59 | + |
| 60 | +sendQuery(sql); |
| 61 | + |
| 62 | +while (!hfr ||fqp >0) { |
| 63 | +intc =pg_stream.ReceiveChar(); |
| 64 | + |
| 65 | +switch (c) |
| 66 | + { |
| 67 | +case'A':// Asynchronous Notify |
| 68 | +intpid =pg_stream.ReceiveInteger(4); |
| 69 | +Stringmsg =pg_stream.ReceiveString(connection.getEncoding()); |
| 70 | +break; |
| 71 | +case'B':// Binary Data Transfer |
| 72 | +receiveTuple(true); |
| 73 | +break; |
| 74 | +case'C':// Command Status |
| 75 | +receiveCommandStatus(); |
| 76 | + |
| 77 | +if (fields !=null) |
| 78 | +hfr =true; |
| 79 | +else { |
| 80 | +sendQuery(" "); |
| 81 | +fqp++; |
| 82 | +} |
| 83 | +break; |
| 84 | +case'D':// Text Data Transfer |
| 85 | +receiveTuple(false); |
| 86 | +break; |
| 87 | +case'E':// Error Message |
| 88 | +thrownewSQLException(pg_stream.ReceiveString(connection.getEncoding())); |
| 89 | +case'I':// Empty Query |
| 90 | +intt =pg_stream.ReceiveChar(); |
| 91 | +if (t !=0) |
| 92 | +thrownewPSQLException("postgresql.con.garbled"); |
| 93 | + |
| 94 | +if (fqp >0) |
| 95 | +fqp--; |
| 96 | +if (fqp ==0) |
| 97 | +hfr =true; |
| 98 | +break; |
| 99 | +case'N':// Error Notification |
| 100 | +connection.addWarning(pg_stream.ReceiveString(connection.getEncoding())); |
| 101 | +break; |
| 102 | +case'P':// Portal Name |
| 103 | +Stringpname =pg_stream.ReceiveString(connection.getEncoding()); |
| 104 | +break; |
| 105 | +case'T':// MetaData Field Description |
| 106 | +receiveFields(); |
| 107 | +break; |
| 108 | +case'Z':// backend ready for query, ignore for now :-) |
| 109 | +break; |
| 110 | +default: |
| 111 | +thrownewPSQLException("postgresql.con.type", |
| 112 | +newCharacter((char)c)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +returnconnection.getResultSet(connection,statement,fields,tuples,status,update_count,insert_oid); |
| 117 | +} |
| 118 | + } |
| 119 | + |
| 120 | +/** |
| 121 | + * Send a query to the backend. |
| 122 | + */ |
| 123 | +privatevoidsendQuery(Stringquery)throwsSQLException { |
| 124 | +try { |
| 125 | +pg_stream.SendChar('Q'); |
| 126 | +pg_stream.Send(connection.getEncoding().encode(query)); |
| 127 | +pg_stream.SendChar(0); |
| 128 | +pg_stream.flush(); |
| 129 | + |
| 130 | +}catch (IOExceptione) { |
| 131 | +thrownewPSQLException("postgresql.con.ioerror",e); |
| 132 | +} |
| 133 | + } |
| 134 | + |
| 135 | +/** |
| 136 | + * Receive a tuple from the backend. |
| 137 | + * |
| 138 | + * @param isBinary set if the tuple should be treated as binary data |
| 139 | + */ |
| 140 | +privatevoidreceiveTuple(booleanisBinary)throwsSQLException { |
| 141 | +if (fields ==null) |
| 142 | +thrownewPSQLException("postgresql.con.tuple"); |
| 143 | +Objecttuple =pg_stream.ReceiveTuple(fields.length,isBinary); |
| 144 | +if (maxRows ==0 ||tuples.size() <maxRows) |
| 145 | +tuples.addElement(tuple); |
| 146 | + } |
| 147 | + |
| 148 | +/** |
| 149 | + * Receive command status from the backend. |
| 150 | + */ |
| 151 | +privatevoidreceiveCommandStatus()throwsSQLException { |
| 152 | + |
| 153 | +status =pg_stream.ReceiveString(connection.getEncoding()); |
| 154 | + |
| 155 | +try { |
| 156 | +// Now handle the update count correctly. |
| 157 | +if (status.startsWith("INSERT") ||status.startsWith("UPDATE") ||status.startsWith("DELETE") ||status.startsWith("MOVE")) { |
| 158 | +update_count =Integer.parseInt(status.substring(1 +status.lastIndexOf(' '))); |
| 159 | + } |
| 160 | +if (status.startsWith("INSERT")) { |
| 161 | +insert_oid =Integer.parseInt(status.substring(1 +status.indexOf(' '), |
| 162 | +status.lastIndexOf(' '))); |
| 163 | + } |
| 164 | +}catch (NumberFormatExceptionnfe) { |
| 165 | +thrownewPSQLException("postgresql.con.fathom",status); |
| 166 | +} |
| 167 | + } |
| 168 | + |
| 169 | +/** |
| 170 | + * Receive the field descriptions from the back end. |
| 171 | + */ |
| 172 | +privatevoidreceiveFields()throwsSQLException { |
| 173 | +if (fields !=null) |
| 174 | +thrownewPSQLException("postgresql.con.multres"); |
| 175 | + |
| 176 | +intsize =pg_stream.ReceiveIntegerR(2); |
| 177 | +fields =newField[size]; |
| 178 | + |
| 179 | +for (inti =0;i <fields.length;i++) { |
| 180 | +StringtypeName =pg_stream.ReceiveString(connection.getEncoding()); |
| 181 | +inttypeOid =pg_stream.ReceiveIntegerR(4); |
| 182 | +inttypeLength =pg_stream.ReceiveIntegerR(2); |
| 183 | +inttypeModifier =pg_stream.ReceiveIntegerR(4); |
| 184 | +fields[i] =newField(connection,typeName,typeOid,typeLength,typeModifier); |
| 185 | +} |
| 186 | + } |
| 187 | +} |