1 10 11 package com.triactive.jdo.store; 12 13 import java.sql.ResultSet ; 14 import java.sql.SQLException ; 15 import java.util.AbstractCollection ; 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 import javax.jdo.JDODataStoreException; 21 import javax.jdo.JDOUserException; 22 23 24 public final class QueryResult extends AbstractCollection 25 { 26 private final Query query; 27 private final Query.ResultObjectFactory rof; 28 private ResultSet rs; 29 private final StoreManager storeMgr; 30 31 private boolean moreResultSetRows; 32 private ArrayList resultObjs = new ArrayList (); 33 34 public QueryResult(Query query, Query.ResultObjectFactory rof, ResultSet rs) throws SQLException 35 { 36 this.query = query; 37 this.rof = rof; 38 this.rs = rs; 39 40 storeMgr = query.getStoreManager(); 41 42 if (!(moreResultSetRows = rs.next())) 43 closeResultSet(); 44 else if (!query.getPersistenceManager().currentTransaction().isActive()) 45 advanceToEndOfResultSet(); 46 } 47 48 private void advanceToEndOfResultSet() 49 { 50 51 while (moreResultSetRows) 52 nextResultSetElement(); 53 } 54 55 private boolean isOpen() 56 { 57 return resultObjs != null; 58 } 59 60 private void assertIsOpen() 61 { 62 if( ! isOpen() ) 63 throw new JDOUserException("Query result has been closed"); 64 } 65 66 private Object nextResultSetElement() 67 { 68 Object nextElement = rof.getObject(rs); 69 70 storeMgr.logSQLWarnings(rs); 71 72 resultObjs.add(nextElement); 73 74 try 75 { 76 if (!(moreResultSetRows = rs.next())) 77 closeResultSet(); 78 } 79 catch (SQLException e) 80 { 81 throw storeMgr.getDatabaseAdapter().newDataStoreException("Failed reading result set", e); 82 } 83 84 return nextElement; 85 } 86 87 public Iterator iterator() 88 { 89 assertIsOpen(); 90 91 return new QueryResultIterator(); 92 } 93 94 private class QueryResultIterator implements Iterator 95 { 96 private int nextRowNum = 0; 97 98 public QueryResultIterator() 99 { 100 } 101 102 public boolean hasNext() 103 { 104 synchronized (QueryResult.this) 105 { 106 if( ! isOpen() ) 107 return false; 108 109 return nextRowNum < resultObjs.size() ? true : moreResultSetRows; 110 } 111 } 112 113 public Object next() 114 { 115 synchronized (QueryResult.this) 116 { 117 if( ! isOpen() ) 118 throw new NoSuchElementException ("This query result has been closed"); 119 120 Object nextElement = null; 121 122 if (nextRowNum < resultObjs.size()) 123 nextElement = resultObjs.get(nextRowNum); 124 else if (moreResultSetRows) 125 nextElement = nextResultSetElement(); 126 else 127 throw new NoSuchElementException ("No more elements in query result"); 128 129 ++nextRowNum; 130 131 return nextElement; 132 } 133 } 134 135 public void remove() 136 { 137 throw new UnsupportedOperationException ("Query result sets are not modifiable"); 138 } 139 } 140 141 public synchronized boolean contains(Object o) 142 { 143 assertIsOpen(); 144 advanceToEndOfResultSet(); 145 146 return resultObjs.contains(o); 147 } 148 149 public synchronized boolean containsAll(Collection c) 150 { 151 assertIsOpen(); 152 advanceToEndOfResultSet(); 153 154 return resultObjs.containsAll(c); 155 } 156 157 public synchronized boolean isEmpty() 158 { 159 assertIsOpen(); 160 161 return resultObjs.isEmpty() && !moreResultSetRows; 162 } 163 164 public synchronized int size() 165 { 166 assertIsOpen(); 167 advanceToEndOfResultSet(); 168 169 return resultObjs.size(); 170 } 171 172 public synchronized Object [] toArray() 173 { 174 assertIsOpen(); 175 advanceToEndOfResultSet(); 176 177 return resultObjs.toArray(); 178 } 179 180 public synchronized Object [] toArray(Object [] a) 181 { 182 assertIsOpen(); 183 advanceToEndOfResultSet(); 184 185 return resultObjs.toArray(a); 186 } 187 188 private void closeResultSet() 189 { 190 if (rs != null) 191 { 192 try 193 { 194 try 195 { 196 rs.getStatement().close(); 197 } 198 catch (SQLException e) 199 { 200 throw storeMgr.getDatabaseAdapter().newDataStoreException("Closing query result failed", e); 201 } 202 } 203 finally 204 { 205 rs = null; 206 } 207 } 208 } 209 210 public synchronized void close() 211 { 212 resultObjs = null; 213 moreResultSetRows = false; 214 215 closeResultSet(); 216 } 217 } 218 | Popular Tags |