1 package com.daffodilwoods.daffodildb.client; 2 3 import java.sql.SQLException ; 4 import com.daffodilwoods.database.resource.*; 5 public class RecordSetBufferIterator implements _RecordSetBufferIterator { 6 7 private _RecordSetBuffer recordSetBuffer; 8 Object currentKey; 9 private _Record record; 10 public RecordSetBufferIterator() { 11 } 12 13 public void setRecordSetBuffer( _RecordSetBuffer recordSetBuffer) { 14 this.recordSetBuffer = recordSetBuffer; 15 if(recordSetBuffer != null){ 16 record = recordSetBuffer.getRecordInstance(); 17 record.setBuffer(recordSetBuffer); 18 } 19 currentKey = null; 20 } 21 22 public _RecordSetBuffer getRecordSetBuffer() { 23 return recordSetBuffer; 24 } 25 26 public int getRowCount() throws SQLException { 27 return recordSetBuffer.getRowCount(); 28 } 29 30 public boolean next() throws SQLException { 31 ensure(); 32 if(currentKey == null ) 33 return false; 34 Object nextKey = recordSetBuffer.getNextKey(currentKey); 35 if(nextKey != null){ 36 currentKey = nextKey; 37 recordSetBuffer.loadRecordForKey(record,currentKey); 38 return true; 39 } 40 return false; 41 } 42 43 public boolean previous() throws SQLException { 44 ensure(); 45 if(currentKey == null ) 46 return false; 47 Object previousKey = recordSetBuffer.getPreviousKey(currentKey); 48 if(previousKey != null){ 49 currentKey = previousKey; 50 recordSetBuffer.loadRecordForKey(record,currentKey); 51 return true; 52 } 53 return false; 54 } 55 56 public boolean top() throws SQLException { 57 currentKey = recordSetBuffer.getTopKey(); 58 if(currentKey != null){ 59 recordSetBuffer.loadRecordForKey(record,currentKey); 60 return true; 61 } 62 return false; 63 } 64 65 public boolean bottom() throws SQLException { 66 currentKey = recordSetBuffer.getBottomKey(); 67 if(currentKey != null){ 68 recordSetBuffer.loadRecordForKey(record,currentKey); 69 return true; 70 } 71 return false; 72 } 73 74 75 public void insertInitiate(int[] columnIndexes,Object [] values) throws DException,SQLException { 76 _Record rec = recordSetBuffer.getInsertedRecord(); 77 if(columnIndexes != null) 78 for (int i = 0; i < columnIndexes.length; i++) { 79 rec.updateInitiate(columnIndexes[i],values[i]); 80 } 81 try { 82 recordSetBuffer.insertRow();; 83 } 84 catch (DException ex) { 85 throw ex.getSqlException(null); 86 } 87 } 88 89 public void updateInitiate(int[] columnIndexes,Object [] values) throws SQLException { 90 _Record rec = getRecord(); 91 for (int i = 0; i < columnIndexes.length; i++) { 92 rec.updateInitiate(columnIndexes[i],values[i]); 93 } 94 try { 95 recordSetBuffer.updateRow(rec); 96 validatePosition(); 97 } 98 catch (DException ex) { 99 throw ex.getSqlException(null); 100 } 101 102 } 103 104 public void deleteInitiate() throws DException,SQLException { 105 _Record rec = getRecord(); 106 try { 107 recordSetBuffer.deleteRow(rec); 108 validatePosition(); 109 } 110 catch (DException ex) { 111 throw ex.getSqlException(null); 112 } 113 114 } 115 116 117 public _Record getRecord() throws SQLException { 118 ensure(); 119 if(currentKey == null) 120 return null; 121 if(record.getRecordSetBuffer()==null){ 122 throw new SQLException (new DException("DSE1104", new Object []{recordSetBuffer,record.getRecordSetBuffer()}).getMessage()); 123 } 124 _Record currentRecord = recordSetBuffer.getRecordInstance(); 125 record.loadRecord(currentRecord); 126 return currentRecord; 127 } 128 129 public Object getColumnValue(String columnName) throws SQLException { 130 ensure(); 131 return currentKey == null ? null : record.getColumnValue(columnName); 132 } 133 134 public Object getColumnValue(int columnIndex) throws SQLException { 135 ensure(); 136 return currentKey == null ? null : record.getColumnValue(columnIndex); 137 } 138 139 public Object getCurrent() throws SQLException { 140 ensure(); 141 if(record.getRecordSetBuffer() == null) 142 throw new SQLException (new DException("DSE884", new Object []{recordSetBuffer,record}).getMessage()); 143 return getRecord(); 144 } 145 146 147 public Object getKey() throws SQLException { 148 ensure(); 149 return currentKey; 150 } 151 152 public void moveToKey(Object key) throws SQLException { 153 if(key == null) 154 throw new DException("DSE550",null).getSqlException(null); 155 currentKey = key; 156 recordSetBuffer.loadRecordForKey(record,currentKey); 157 } 158 159 public boolean isTop() throws SQLException { 160 ensure(); 161 return currentKey != null && recordSetBuffer.isTop(currentKey) ; 162 } 163 164 public boolean isBottom() throws SQLException { 165 ensure(); 166 return currentKey != null && recordSetBuffer.isBottom(currentKey) ; 167 } 168 169 public boolean hasRecord() throws SQLException { 170 ensure(); 171 return currentKey != null; 172 } 173 174 public boolean seek(String clause) throws SQLException { 175 throw new UnsupportedOperationException (" this method is to be removed "); 176 } 177 178 public void validatePosition() throws SQLException { 179 if(currentKey != null){ 180 currentKey = recordSetBuffer.locateNearestKey(currentKey); 181 if(currentKey != null) 182 recordSetBuffer.loadRecordForKey(record,currentKey); 183 } 184 } 185 186 private void ensure() throws SQLException { 187 if(currentKey == null){ 188 currentKey = recordSetBuffer.getTopKey(); 189 if(currentKey != null) 190 recordSetBuffer.loadRecordForKey(record,currentKey); 191 } 192 } 193 } 194 | Popular Tags |