1 5 package org.h2.command; 6 7 import java.io.IOException ; 8 import java.sql.SQLException ; 9 10 import org.h2.engine.Constants; 11 import org.h2.engine.SessionInterface; 12 import org.h2.engine.SessionRemote; 13 import org.h2.expression.ParameterInterface; 14 import org.h2.expression.ParameterRemote; 15 import org.h2.message.Trace; 16 import org.h2.result.ResultInterface; 17 import org.h2.result.ResultRemote; 18 import org.h2.util.ObjectArray; 19 import org.h2.value.Transfer; 20 21 public class CommandRemote implements CommandInterface { 22 23 private SessionRemote session; 24 private ObjectArray transferList; 25 private int id; 26 private boolean isQuery; 27 private boolean readonly; 28 private ObjectArray parameters; 29 private Trace trace; 30 private String sql; 31 private int paramCount; 32 33 private void prepare(SessionRemote session) throws SQLException { 34 id = session.getNextId(); 35 paramCount = 0; 36 for(int i=0; i<transferList.size(); i++) { 37 try { 38 Transfer transfer = (Transfer) transferList.get(i); 39 session.traceOperation("SESSION_PREPARE", id); 40 transfer.writeInt(SessionRemote.SESSION_PREPARE).writeInt(id).writeString(sql); 41 session.done(transfer); 42 isQuery = transfer.readBoolean(); 43 readonly = transfer.readBoolean(); 44 paramCount = transfer.readInt(); 45 } catch(IOException e) { 46 session.removeServer(i); 47 } 48 } 49 } 50 51 public CommandRemote(SessionRemote session, ObjectArray transferList, String sql) throws SQLException { 52 this.transferList = transferList; 53 trace = session.getTrace(); 54 this.sql = sql; 55 parameters = new ObjectArray(); 56 prepare(session); 57 for(int i=0; i<paramCount; i++) { 58 parameters.add(new ParameterRemote(i)); 59 } 60 this.session = session; 62 } 63 64 public boolean isQuery() { 65 return isQuery; 66 } 67 68 public ObjectArray getParameters() { 69 return parameters; 70 } 71 72 public ResultInterface executeQuery(int maxRows, boolean scrollable) throws SQLException { 73 checkParameters(); 74 synchronized(session) { 75 session.checkClosed(); 76 if(id <= session.getCurrentId() - Constants.SERVER_CACHED_OBJECTS) { 77 prepare(session); 79 } 80 int objectId = session.getNextId(); 81 ResultRemote result = null; 82 for(int i=0; i<transferList.size(); i++) { 83 Transfer transfer = (Transfer) transferList.get(i); 84 try { 85 session.traceOperation("COMMAND_EXECUTE_QUERY", id); 87 transfer.writeInt(SessionRemote.COMMAND_EXECUTE_QUERY).writeInt(id).writeInt(objectId).writeInt(maxRows); 88 int readRows; 89 if(session.isClustered() || scrollable) { 90 readRows = Integer.MAX_VALUE; 91 } else { 92 readRows = Constants.SERVER_SMALL_RESULTSET_SIZE; 93 } 94 transfer.writeInt(readRows); 95 sendParameters(transfer); 96 session.done(transfer); 97 int columnCount = transfer.readInt(); 98 if(result != null) { 99 result.close(); 100 result = null; 101 } 102 result = new ResultRemote(session, transfer, objectId, columnCount, readRows); 103 if(readonly) { 104 break; 105 } 106 } catch(IOException e) { 107 session.removeServer(i); 108 } 109 } 110 session.autoCommitIfCluster(); 111 return result; 112 } 113 } 114 115 public int executeUpdate() throws SQLException { 116 checkParameters(); 117 synchronized(session) { 118 session.checkClosed(); 119 if(id <= session.getCurrentId() - Constants.SERVER_CACHED_OBJECTS) { 120 prepare(session); 122 } 123 int updateCount = 0; 124 boolean autoCommit = false; 125 for(int i=0; i<transferList.size(); i++) { 126 try { 127 Transfer transfer = (Transfer) transferList.get(i); 128 session.traceOperation("COMMAND_EXECUTE_UPDATE", id); 129 transfer.writeInt(SessionRemote.COMMAND_EXECUTE_UPDATE).writeInt(id); 130 sendParameters(transfer); 131 session.done(transfer); 132 updateCount = transfer.readInt(); 133 autoCommit = transfer.readBoolean(); 134 } catch(IOException e) { 135 session.removeServer(i); 136 } 137 } 138 session.setAutoCommit(autoCommit); 139 session.autoCommitIfCluster(); 140 return updateCount; 141 } 142 } 143 144 private void checkParameters() throws SQLException { 145 int len = parameters.size(); 146 for(int i=0; i<len; i++) { 147 ParameterInterface p = (ParameterInterface)parameters.get(i); 148 p.checkSet(); 149 } 150 } 151 152 private void sendParameters(Transfer transfer) throws IOException , SQLException { 153 int len = parameters.size(); 154 transfer.writeInt(len); 155 for(int i=0; i<len; i++) { 156 ParameterInterface p = (ParameterInterface)parameters.get(i); 157 transfer.writeValue(p.getParamValue()); 158 } 159 } 160 161 public SessionInterface getSession() { 162 return session; 163 } 164 165 public void close() { 166 if(session == null || session.isClosed()) { 167 return; 168 } 169 synchronized(session) { 170 for(int i=0; i<transferList.size(); i++) { 171 try { 172 Transfer transfer = (Transfer) transferList.get(i); 173 session.traceOperation("COMMAND_CLOSE", id); 174 transfer.writeInt(SessionRemote.COMMAND_CLOSE).writeInt(id); 175 } catch (IOException e) { 176 trace.error("close", e); 178 } 179 } 180 session = null; 181 } 182 } 183 184 191 public void cancel() { 192 } 194 195 } 196 | Popular Tags |