1 30 package com.genimen.djeneric.repository.rdbms; 31 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.util.Stack ; 35 36 import com.genimen.djeneric.repository.DjCursor; 37 import com.genimen.djeneric.repository.DjExtent; 38 import com.genimen.djeneric.repository.DjList; 39 import com.genimen.djeneric.repository.DjObject; 40 import com.genimen.djeneric.repository.DjObjectMatcher; 41 import com.genimen.djeneric.repository.DjSession; 42 import com.genimen.djeneric.repository.exceptions.DjenericException; 43 import com.genimen.djeneric.util.DjLogger; 44 45 51 public class RdbmsCursor extends DjCursor 52 { 53 SqlStatement _stmt; 54 ResultSet _rs = null; 55 Stack _subCursors = new Stack (); 56 DjObjectMatcher _matcher = null; 57 58 69 protected RdbmsCursor(DjSession session, DjExtent extent, SqlStatement stmt, DjObjectMatcher qbe, 70 DjList sessionObjects, boolean forceReloadFromDB) throws DjenericException 71 { 72 super(session, extent, sessionObjects, forceReloadFromDB); 73 _stmt = stmt; 74 _matcher = qbe; 75 } 76 77 85 public void chain(DjCursor subCursor) 86 { 87 _subCursors.push(subCursor); 88 } 89 90 96 public DjObject getNext() throws DjenericException 97 { 98 DjObject result = null; 99 100 while (result == null && !_subCursors.isEmpty()) 101 { 102 RdbmsCursor firstTryThisOne = (RdbmsCursor) _subCursors.peek(); 103 result = firstTryThisOne.getNext(); 104 if (result == null) _subCursors.pop(); 107 } 108 if (result != null) return result; 109 110 if (getSessionObjects().size() > 0) 111 { 112 result = getSessionObjects().getDjenericObjectAt(0); 113 getSessionObjects().remove(0); 114 return result; 115 } 116 117 try 118 { 119 if (_rs == null) _rs = _stmt.executeQuery(); 120 121 while (true) 125 { 126 if (!_rs.next()) 127 { 128 close(); 129 return null; 130 } 131 132 RdbmsSession ses = (RdbmsSession) getSession(); 133 RdbmsDjenericObject po = (RdbmsDjenericObject) ses.getFromSession(_rs.getLong(getIdColumnName())); 135 if (po == null) 136 { 137 po = (RdbmsDjenericObject) getSession().createObject(getExtent()); 138 po.setFromRecord(_rs, true); 139 return po; 140 } 141 else 142 { 143 147 boolean skip = po.isMarkedForDelete(); 148 if (!skip && isForceReloadFromDB()) po.reload(); 149 if (!skip && _matcher != null) skip = !_matcher.match(po); 151 if (!skip) return po; 152 } 153 } 154 } 155 catch (SQLException x) 156 { 157 DjLogger.log(_stmt.getExternalSql()); 158 DjLogger.log(_stmt.getInternalSql()); 159 throw new DjenericException(x); 160 } 161 } 162 163 168 public void close() throws DjenericException 169 { 170 try 171 { 172 if (_rs != null) _rs.close(); 173 _rs = null; 174 _stmt.close(); 175 } 176 catch (SQLException x) 177 { 178 throw new DjenericException(x); 179 } 180 } 181 182 } | Popular Tags |