1 2 12 package com.versant.core.jdbc.fetch; 13 14 import com.versant.core.common.BindingSupportImpl; 15 import com.versant.core.common.Debug; 16 17 import java.sql.ResultSet ; 18 import java.sql.PreparedStatement ; 19 import java.sql.SQLException ; 20 21 27 public class FetchResultImp implements FetchResult { 28 29 private FetchSpec spec; 30 private String sql; 31 private PreparedStatement ps; 32 private ResultSet rs; 33 private boolean scrollable; 34 private boolean haveRow; 35 private Object [] opData; 36 37 public FetchResultImp(FetchSpec spec, PreparedStatement ps, String sql, 38 boolean scrollable) { 39 this.spec = spec; 40 this.ps = ps; 41 this.sql = sql; 42 this.scrollable = scrollable; 43 opData = new Object [spec.getFetchOpCount()]; 44 } 45 46 public void execute() { 47 if (rs != null) { 48 throw BindingSupportImpl.getInstance().internal( 49 "query has already been executed"); 50 } 51 checkClosed(); 52 try { 53 rs = ps.executeQuery(); 54 } catch (Exception e) { 55 throw spec.getSqlDriver().mapException(e, 56 "Error executing query: " + e + "\nSQL:\n" + sql, 57 true); 58 } 59 } 60 61 public void cancel() { 62 if (rs != null) { 63 try { 64 ps.cancel(); 65 } catch (SQLException e) { 66 throw spec.getSqlDriver().mapException(e, e + "\nSQL:\n" + sql, 67 true); 68 } 69 } 70 } 71 72 public void close() { 73 if (isClosed()) { 74 return; 75 } 76 if (rs != null) { 77 try { 78 rs.close(); 79 } catch (Exception e) { 80 } 82 rs = null; 83 } 84 try { 85 ps.close(); 86 } catch (SQLException e) { 87 } 89 ps = null; 90 spec.fetchResultClosed(this); 91 opData = null; 92 } 93 94 public boolean isClosed() { 95 return ps == null; 96 } 97 98 public boolean hasNext() { 99 if (ps == null) { 100 return false; 101 } 102 if (rs == null) { 103 execute(); 104 } 105 try { 106 if (haveRow || (haveRow = rs.next())) { 107 return true; 108 } else { 109 close(); 110 return false; 111 } 112 } catch (Exception e) { 113 throw spec.getSqlDriver().mapException(e, e.toString(), true); 114 } 115 } 116 117 public Object next() { 118 if (haveRow || hasNext()) { 119 Object row = spec.createRow(this); 120 haveRow = false; 121 return row; 122 } else { 123 throw BindingSupportImpl.getInstance().internal("no more rows: " + 124 this); 125 } 126 } 127 128 public void remove() { 129 throw new UnsupportedOperationException (); 130 } 131 132 public boolean isScrollable() { 133 return scrollable; 134 } 135 136 private void checkClosed() { 137 if (ps == null) { 138 throw BindingSupportImpl.getInstance().internal( 139 "FetchResult has been closed"); 140 } 141 } 142 143 public int size() { 144 if (Debug.DEBUG) { 145 checkScrollable(); 146 } 147 try { 148 if (rs == null) { 149 execute(); 150 } 151 if (rs.last()) { 152 return rs.getRow(); 153 } else { 154 return 0; 155 } 156 } catch (Exception e) { 157 throw spec.getSqlDriver().mapException(e, e.toString(), true); 158 } 159 } 160 161 public Object get(int index) { 162 if (Debug.DEBUG) { 163 checkScrollable(); 164 } 165 try { 166 if (rs == null) { 167 execute(); 168 } 169 if (rs.absolute(index + 1)) { 170 return spec.createRow(this); 171 } else { 172 throw BindingSupportImpl.getInstance().internal( 173 "invalud index " + index + ": " + this); 174 } 175 } catch (Exception e) { 176 throw spec.getSqlDriver().mapException(e, e.toString(), true); 177 } 178 } 179 180 private void checkScrollable() { 181 if (!scrollable) { 182 throw BindingSupportImpl.getInstance().internal("not scrollable: " + 183 this); 184 } 185 } 186 187 190 public Object getData(FetchOp op) { 191 if (Debug.DEBUG) { 192 checkOp(op); 193 } 194 return opData[op.getIndex()]; 195 } 196 197 200 public void setData(FetchOp op, Object data) { 201 if (Debug.DEBUG) { 202 checkOp(op); 203 } 204 opData[op.getIndex()] = data; 205 } 206 207 private void checkOp(FetchOp op) { 208 if (op == null) { 209 throw BindingSupportImpl.getInstance().internal( 210 "op is null: " + this); 211 } 212 if (op.getSpec() != spec) { 213 throw BindingSupportImpl.getInstance().internal( 214 "spec mismatch: " + this + ", op.getSpec() = " + 215 op.getSpec() + ", op " + op); 216 } 217 } 218 219 public String toString() { 220 return "FetchResult 0x" + 221 Integer.toHexString(System.identityHashCode(this)) + 222 (isClosed() ? " CLOSED" : "") + 223 " spec " + spec; 224 } 225 226 public ResultSet getResultSet() { 227 return rs; 228 } 229 230 public FetchSpec getFetchSpec() { 231 return spec; 232 } 233 234 public PreparedStatement getPreparedStatement() { 235 return ps; 236 } 237 238 } 239 240 | Popular Tags |