1 5 package org.h2.result; 6 7 import java.io.IOException ; 8 import java.sql.SQLException ; 9 10 import org.h2.engine.Constants; 11 import org.h2.engine.SessionRemote; 12 import org.h2.message.Message; 13 import org.h2.util.ObjectArray; 14 import org.h2.value.Transfer; 15 import org.h2.value.Value; 16 17 public class ResultRemote implements ResultInterface { 18 19 private SessionRemote session; 20 private Transfer transfer; 21 private int id; 22 private ResultColumn[] columns; 23 private Value[] currentRow; 24 private int rowId, rowCount; 25 private ObjectArray result; 26 27 private boolean isUpdateCount; 28 private int updateCount; 29 30 public ResultRemote(int updateCount) { 31 this.isUpdateCount = true; 32 this.updateCount = updateCount; 33 } 34 35 public boolean isUpdateCount() { 36 return isUpdateCount; 37 } 38 39 public int getUpdateCount() { 40 return updateCount; 41 } 42 43 public ResultRemote(SessionRemote session, Transfer transfer, int id, int columnCount, int readRows) throws IOException , SQLException { 44 this.session = session; 45 this.transfer = transfer; 46 this.id = id; 47 this.columns = new ResultColumn[columnCount]; 48 rowCount = transfer.readInt(); 49 for (int i = 0; i < columnCount; i++) { 50 columns[i] = new ResultColumn(transfer); 51 } 52 rowId = -1; 53 if(rowCount < readRows) { 54 result = new ObjectArray(); 55 readFully(); 56 sendClose(); 57 } 58 } 59 60 private void readFully() throws SQLException { 61 while(true) { 62 Value[] values = fetchRow(false); 63 if(values == null) { 64 break; 65 } 66 result.add(values); 67 } 68 } 69 70 public String getAlias(int i) { 71 return columns[i].alias; 72 } 73 74 public String getSchemaName(int i) { 75 return columns[i].schemaName; 76 } 77 78 public String getTableName(int i) { 79 return columns[i].tableName; 80 } 81 82 public String getColumnName(int i) { 83 return columns[i].columnName; 84 } 85 86 public int getColumnType(int i) { 87 return columns[i].columnType; 88 } 89 90 public long getColumnPrecision(int i) { 91 return columns[i].precision; 92 } 93 94 public int getColumnScale(int i) { 95 return columns[i].scale; 96 } 97 98 public int getDisplaySize(int i) { 99 return columns[i].displaySize; 100 } 101 102 public boolean isAutoIncrement(int i) { 103 return columns[i].autoIncrement; 104 } 105 106 public int getNullable(int i) { 107 return columns[i].nullable; 108 } 109 110 public void reset() throws SQLException { 111 rowId = -1; 112 currentRow = null; 113 if(session == null) { 114 return; 115 } 116 synchronized (session) { 117 session.checkClosed(); 118 try { 119 session.traceOperation("RESULT_RESET", id); 120 transfer.writeInt(SessionRemote.RESULT_RESET).writeInt(id).flush(); 121 } catch (IOException e) { 122 throw Message.convert(e); 123 } 124 } 125 } 126 127 public Value[] currentRow() { 128 return currentRow; 129 } 130 131 public boolean next() throws SQLException { 132 if (rowId < rowCount) { 134 rowId++; 135 if (rowId < rowCount) { 136 if(session == null) { 137 currentRow = (Value[]) result.get(rowId); 138 } else { 139 currentRow = fetchRow(true); 140 } 141 return true; 142 } 143 currentRow = null; 144 } 145 return false; 146 } 147 148 public int getRowId() { 149 return rowId; 150 } 151 152 public int getVisibleColumnCount() { 153 return columns.length; 154 } 155 156 public int getRowCount() { 157 return rowCount; 158 } 159 160 private void sendClose() { 161 if (session == null) { 162 return; 163 } 164 synchronized (session) { 166 try { 167 session.traceOperation("RESULT_CLOSE", id); 168 transfer.writeInt(SessionRemote.RESULT_CLOSE).writeInt(id); 169 } catch (IOException e) { 170 session.getTrace().error("close", e); 171 } finally { 172 transfer = null; 173 session = null; 174 } 175 } 176 } 177 178 public void close() { 179 result = null; 180 sendClose(); 181 } 182 183 190 private Value[] fetchRow(boolean sendFetch) throws SQLException { 191 synchronized (session) { 192 session.checkClosed(); 193 try { 194 if(id <= session.getCurrentId() - Constants.SERVER_CACHED_OBJECTS / 2) { 195 int newId = session.getNextId(); 197 session.traceOperation("CHANGE_ID", id); 198 transfer.writeInt(SessionRemote.CHANGE_ID).writeInt(id).writeInt(newId); 199 id = newId; 200 } 202 if(sendFetch) { 203 session.traceOperation("RESULT_FETCH_ROW", id); 204 transfer.writeInt(SessionRemote.RESULT_FETCH_ROW).writeInt(id); 205 session.done(transfer); 206 } 207 boolean row = transfer.readBoolean(); 208 if (row) { 209 int len = columns.length; 210 Value[] values = new Value[len]; 211 for (int i = 0; i < len; i++) { 212 values[i] = transfer.readValue(); 213 } 214 return values; 215 } else { 216 sendClose(); 217 return null; 218 } 219 } catch (IOException e) { 220 throw Message.convert(e); 221 } 222 } 223 } 224 225 } 226 | Popular Tags |