1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.Collections ; 47 import java.util.Comparator ; 48 import java.util.Vector ; 49 50 import java.sql.SQLException ; 51 52 import com.quadcap.sql.types.Op; 53 import com.quadcap.sql.types.Value; 54 import com.quadcap.sql.types.ValueException; 55 56 import com.quadcap.util.Debug; 57 58 64 public class StaticCursor extends CursorImpl implements Externalizable { 65 Tuple tuple = null; 66 Vector v; 67 Row currentRow = null; 68 int pos = 0; 69 70 73 public StaticCursor(Session session, Tuple tuple, Row row) { 74 super(session, ""); 75 this.tuple = tuple; 76 this.v = new Vector (); 77 v.addElement(row); 78 } 79 80 83 public StaticCursor(Session session, Vector v) { 84 super(session, ""); 85 this.v = v; 86 } 87 88 91 public void addRow(Row row) { 92 v.addElement(row); 93 } 94 95 98 public Row getRow() { 99 return currentRow; 100 } 101 102 105 public void updateRow(Row row) throws SQLException { 106 throw new SQLException ("Not implemented for static cursor", "0A000"); 107 } 108 109 112 public void deleteRow() throws SQLException { 113 throw new SQLException ("Not implemented for static cursor", "0A000"); 114 } 115 116 123 public Column getColumn(int col) throws SQLException { 124 if (tuple == null) { 125 return super.getColumn(col); 126 } 127 return tuple.getColumn(col); 128 } 129 130 137 public Column getColumn(String columnName) throws SQLException { 138 if (tuple == null) { 139 return super.getColumn(columnName); 140 } 141 return tuple.getColumn(columnName); 142 } 143 144 147 public Value get(String columnName) throws SQLException { 148 Column col = getColumn(columnName); 149 if (col != null) { 150 return currentRow.item(col.getColumn()); 151 } 152 return null; 153 } 154 155 159 public void put(String columnName, Value v) throws SQLException { 160 Column col = getColumn(columnName); 161 if (col != null) { 162 currentRow.set(col.getColumn(), v); 163 } 164 } 165 166 169 public int getColumnCount() throws SQLException { 170 if (tuple != null) return tuple.getColumnCount(); 171 int ret = super.getColumnCount(); 172 if (ret > 0) return ret; 173 if (v.size() > 0) { 174 Row row = (Row)v.elementAt(0); 175 return row.size(); 176 } 177 throw new SQLException ("no columns", "Q0017"); 178 } 179 180 183 public boolean isWritable(int column) { return false; } 184 185 188 public long size() throws SQLException { return v.size(); } 189 190 194 public boolean absolute(int row) throws SQLException { 195 currentRow = null; 196 if (row == 0) { 197 throw new SQLException ("Attempt to position cursor on row zero", 198 "42000"); 199 } 200 if (row < 0) { 201 row += v.size(); 202 } 203 if (row <= 0 || row > v.size()) return false; 204 currentRow = (Row)v.elementAt(row-1); 205 return true; 206 } 207 208 211 public void beforeFirst() throws SQLException { 212 pos = 0; 213 } 214 215 218 public void afterLast() throws SQLException { 219 pos = v.size(); 220 } 221 222 226 public boolean next() { 227 currentRow = null; 228 if (pos < v.size()) { 229 currentRow = (Row)v.elementAt(pos++); 230 } 231 return currentRow != null; 232 } 233 234 238 public boolean prev() { 239 currentRow = null; 240 if (pos > 0 && pos <= v.size()) { 241 currentRow = (Row)v.elementAt(--pos); 242 } 243 return currentRow != null; 244 } 245 246 249 public void readExternal(ObjectInput in) 250 throws IOException , ClassNotFoundException 251 { 252 this.pos = in.readInt(); 253 this.v = (Vector )in.readObject(); 254 } 255 256 259 public void writeExternal(ObjectOutput out) throws IOException { 260 out.writeInt(pos); 261 out.writeObject(v); 262 } 263 264 267 public void close() { 268 tuple = null; 269 currentRow = null; 270 v = null; 271 } 272 273 public int[] getSortColumns() { 274 return new int[0]; 275 } 276 277 public Comparator getComparator() { 278 return new Comparator () { 279 public int compare(Object o1, Object o2) { 280 try { 281 Row r1 = (Row)o1; 282 Row r2 = (Row)o2; 283 int[] keys = getSortColumns(); 284 for (int i = 0; i < keys.length; i++) { 285 int col = keys[i]; 286 Value v1 = r1.item(col); 287 Value v2 = r2.item(col); 288 if (Value.boolOp(Op.LT, v1, v2)) return -1; 289 if (Value.boolOp(Op.GT, v1, v2)) return 1; 290 } 291 } catch (ValueException e) { 292 Debug.print(e); 293 throw new DbRuntimeException(e); 294 } catch (SQLException e) { 295 Debug.print(e); 296 throw new DbRuntimeException(e); 297 } catch (Throwable e) { 298 Debug.print(e); 299 throw new DbRuntimeException(e); 300 } 301 return 0; 302 } 303 }; 304 } 305 306 public void sort() throws ValueException, SQLException { 307 try { 308 Collections.sort(v, getComparator()); 309 } catch (DbRuntimeException e) { 310 Throwable ex = e.getException(); 311 if (ex instanceof ValueException) { 312 throw (ValueException)ex; 313 } else if (ex instanceof SQLException ) { 314 throw (SQLException )ex; 315 } else { 316 throw e; 317 } 318 } 319 } 320 321 public void updateRow(int pos, Row row) { 322 v.setElementAt(row, pos); 323 } 324 325 public void reset() { 326 pos = 0; 327 } 328 } 329 | Popular Tags |