|
| 1 | + |
| 2 | +package org.postgresql.core; |
| 3 | + |
| 4 | +import java.util.Vector; |
| 5 | +import java.io.IOException; |
| 6 | +import java.sql.*; |
| 7 | +import org.postgresql.*; |
| 8 | +import org.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 | +public class QueryExecutor { |
| 20 | + |
| 21 | + private final String sql; |
| 22 | + private final java.sql.Statement statement; |
| 23 | + private final PG_Stream pg_stream; |
| 24 | + private final org.postgresql.Connection connection; |
| 25 | + |
| 26 | + public QueryExecutor(String sql, |
| 27 | + java.sql.Statement statement, |
| 28 | + PG_Stream pg_stream, |
| 29 | + org.postgresql.Connection connection) |
| 30 | + throws SQLException |
| 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 | + private Field[] fields = null; |
| 44 | + private Vector tuples = new Vector(); |
| 45 | + private String status = null; |
| 46 | + private int update_count = 1; |
| 47 | + private int insert_oid = 0; |
| 48 | + private int maxRows; |
| 49 | + |
| 50 | + /** |
| 51 | + * Execute a query on the backend. |
| 52 | + */ |
| 53 | + public java.sql.ResultSet execute() throws SQLException { |
| 54 | + |
| 55 | + int fqp = 0; |
| 56 | + boolean hfr = false; |
| 57 | + |
| 58 | + synchronized(pg_stream) { |
| 59 | + |
| 60 | + sendQuery(sql); |
| 61 | + |
| 62 | + while (!hfr || fqp > 0) { |
| 63 | + int c = pg_stream.ReceiveChar(); |
| 64 | + |
| 65 | + switch (c) |
| 66 | + { |
| 67 | + case 'A': // Asynchronous Notify |
| 68 | + int pid = pg_stream.ReceiveInteger(4); |
| 69 | + String msg = 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 | + throw new SQLException(pg_stream.ReceiveString(connection.getEncoding())); |
| 89 | + case 'I': // Empty Query |
| 90 | + int t = pg_stream.ReceiveChar(); |
| 91 | + if (t != 0) |
| 92 | + throw new PSQLException("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 | + String pname = 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 | + throw new PSQLException("postgresql.con.type", |
| 112 | + new Character((char) c)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return connection.getResultSet(connection, statement, fields, tuples, status, update_count, insert_oid); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Send a query to the backend. |
| 122 | + */ |
| 123 | + private void sendQuery(String query) throws SQLException { |
| 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 (IOException e) { |
| 131 | + throw new PSQLException("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 | + private void receiveTuple(boolean isBinary) throws SQLException { |
| 141 | + if (fields == null) |
| 142 | + throw new PSQLException("postgresql.con.tuple"); |
| 143 | + Object tuple = 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 | + private void receiveCommandStatus() throws SQLException { |
| 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 (NumberFormatException nfe) { |
| 165 | + throw new PSQLException("postgresql.con.fathom", status); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Receive the field descriptions from the back end. |
| 171 | + */ |
| 172 | + private void receiveFields() throws SQLException { |
| 173 | + if (fields != null) |
| 174 | + throw new PSQLException("postgresql.con.multres"); |
| 175 | + |
| 176 | + int size = pg_stream.ReceiveIntegerR(2); |
| 177 | + fields = new Field[size]; |
| 178 | + |
| 179 | + for (int i = 0; i < fields.length; i++) { |
| 180 | + String typeName = pg_stream.ReceiveString(connection.getEncoding()); |
| 181 | + int typeOid = pg_stream.ReceiveIntegerR(4); |
| 182 | + int typeLength = pg_stream.ReceiveIntegerR(2); |
| 183 | + int typeModifier = pg_stream.ReceiveIntegerR(4); |
| 184 | + fields[i] = new Field(connection, typeName, typeOid, typeLength, typeModifier); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments