1 package com.quadcap.sql; 2 3 40 41 import java.sql.SQLException ; 42 43 import com.quadcap.util.Debug; 44 45 50 abstract public class CursorImpl extends TupleImpl implements Cursor { 51 protected Session session = null; 52 protected Cursor outer; 53 54 57 private CursorImpl() {} 58 59 62 public CursorImpl(Session session, String name) { 63 super(name); 64 this.session = session; 65 } 66 67 70 public CursorImpl(Session session, String name, Cursor outer) { 71 super(name); 72 this.session = session; 73 this.outer = outer; 74 } 75 76 79 public Session getSession() { return session; } 80 81 85 abstract public Row getRow() throws SQLException ; 86 87 90 public void insertRow(Row row) throws SQLException { 91 throw new SQLException ("Insert not allowed for this cursor type"); 92 } 93 abstract public void updateRow(Row row) throws SQLException ; 94 abstract public void deleteRow() throws SQLException ; 95 96 abstract public void beforeFirst() throws SQLException ; 97 abstract public void afterLast() throws SQLException ; 98 abstract public boolean next() throws SQLException ; 99 100 public boolean prev() throws SQLException { 101 throw new SQLException ("prev() not supported for this cursor type"); 102 } 103 104 abstract public boolean isWritable(int column) throws SQLException ; 105 abstract public void close() throws SQLException ; 106 107 public void finalize() throws Throwable { 108 try { 109 close(); 110 } catch (Throwable t) { 111 } 112 super.finalize(); 113 } 114 115 public Column getColumn(String columnName) throws SQLException { 116 Column c = super.getColumn(columnName); 117 if (c == null && outer != null) { 118 c = outer.getColumn(columnName); 119 } 120 return c; 121 } 122 123 public void setOuterCursor(Cursor c) { 124 outer = c; 125 } 126 127 public Cursor getOuterCursor() { 128 return outer; 129 } 130 131 135 public boolean absolute(int row) throws SQLException { 136 if (row > 0) { 137 beforeFirst(); 138 while (row-- > 0) if (!next()) return false; 139 } else { 140 afterLast(); 141 while (row++ < 0) if (!prev()) return false; 142 } 143 return true; 144 } 145 146 public void reset(Expression expr, Cursor outer) throws SQLException {} 147 148 public Table getTable() { return null; } 149 public long getRowId() { return 0; } 150 151 public String toString() { 153 try { 154 StringBuffer sb = 155 new StringBuffer (Table.strip(getClass().getName())); 156 sb.append(": "); 157 sb.append(getName()); 158 if (outer != null) { 159 sb.append(" (outer "); 160 sb.append(outer.toString()); sb.append(")"); 162 } 163 sb.append(" {"); 164 for (int i = 1; i <= getColumnCount(); i++) { 165 Column c = getColumn(i); 166 if (i > 1) sb.append(','); 167 sb.append(c.getName()); 168 } 169 sb.append('}'); 170 return sb.toString(); 171 } catch (Exception e) { 172 Debug.print(e); 173 return this.getClass().getName(); 174 } 175 } 176 177 static void dumpCursor(String label, Cursor c) throws SQLException { 178 if (label.length() > 0) label = label + " "; 179 int count = 0; 180 c.beforeFirst(); 181 while (c.next()) { 182 Debug.println(label + "Row " + (++count) + ": " + c.getRow()); 183 } 184 c.beforeFirst(); 185 } 186 } 188 | Popular Tags |