1 19 20 21 package org.apache.cayenne.access; 22 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.LinkedHashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.apache.cayenne.CayenneRuntimeException; 31 import org.apache.cayenne.access.util.DefaultOperationObserver; 32 import org.apache.cayenne.query.Query; 33 import org.apache.cayenne.util.Util; 34 35 42 public class QueryResult extends DefaultOperationObserver { 43 44 protected Map queries = new LinkedHashMap (); 46 47 50 public void clear() { 51 queries.clear(); 52 } 53 54 57 public Iterator getQueries() { 58 return queries.keySet().iterator(); 59 } 60 61 66 public List getResults(Query query) { 67 List list = (List ) queries.get(query); 68 return (list != null) ? list : Collections.EMPTY_LIST; 69 } 70 71 76 public int getFirstUpdateCount(Query query) { 77 List allResults = getResults(query); 78 int size = allResults.size(); 79 if (size > 0) { 80 Iterator it = allResults.iterator(); 81 while (it.hasNext()) { 82 Object object = it.next(); 83 84 if (object instanceof Number ) { 86 return ((Number ) object).intValue(); 87 } 88 else if (object instanceof int[]) { 90 int[] counts = (int[]) object; 91 return counts.length > 0 ? counts[0] : -1; 92 } 93 } 94 } 95 return -1; 96 } 97 98 104 public int[] getFirstUpdateCounts(Query query) { 105 List allResults = getResults(query); 106 int size = allResults.size(); 107 108 if (size > 0) { 109 Iterator it = allResults.iterator(); 110 while (it.hasNext()) { 111 Object object = it.next(); 112 113 if (object instanceof Number ) { 115 return new int[] { 116 ((Number ) object).intValue() 117 }; 118 } 119 else if (object instanceof int[]) { 121 return (int[]) object; 122 } 123 } 124 } 125 126 return new int[0]; 127 } 128 129 133 public List getFirstRows(Query query) { 134 List allResults = getResults(query); 135 int size = allResults.size(); 136 if (size == 0) { 137 return Collections.EMPTY_LIST; 138 } 139 else { 140 Iterator it = allResults.iterator(); 141 while (it.hasNext()) { 142 Object obj = it.next(); 143 if (obj instanceof List ) { 144 return (List ) obj; 145 } 146 } 147 } 148 149 return Collections.EMPTY_LIST; 150 } 151 152 157 public List getRows(Query query) { 158 List allResults = getResults(query); 159 int size = allResults.size(); 160 if (size == 0) { 161 return Collections.EMPTY_LIST; 162 } 163 164 List list = new ArrayList (size); 165 Iterator it = allResults.iterator(); 166 while (it.hasNext()) { 167 Object obj = it.next(); 168 if (obj instanceof List ) { 169 list.add(obj); 170 } 171 } 172 173 return list; 174 } 175 176 181 public List getUpdates(Query query) { 182 List allResults = getResults(query); 183 int size = allResults.size(); 184 if (size == 0) { 185 return Collections.EMPTY_LIST; 186 } 187 188 List list = new ArrayList (size); 189 Iterator it = allResults.iterator(); 190 while (it.hasNext()) { 191 Object object = it.next(); 192 if (object instanceof Number ) { 193 list.add(object); 194 } 195 else if (object instanceof int[]) { 196 int[] ints = (int[]) object; 197 for (int i = 0; i < ints.length; i++) { 198 list.add(new Integer (ints[i])); 199 } 200 } 201 } 202 203 return list; 204 } 205 206 209 public void nextQueryException(Query query, Exception ex) { 210 super.nextQueryException(query, ex); 211 throw new CayenneRuntimeException("Query exception.", Util.unwindException(ex)); 212 } 213 214 217 public void nextGlobalException(Exception ex) { 218 super.nextGlobalException(ex); 219 throw new CayenneRuntimeException("Global exception.", Util.unwindException(ex)); 220 } 221 222 225 public boolean isIteratedResult() { 226 return false; 227 } 228 229 public void nextBatchCount(Query query, int[] resultCount) { 230 List list = (List ) queries.get(query); 231 if (list == null) { 232 list = new ArrayList (5); 233 queries.put(query, list); 234 } 235 236 list.add(resultCount); 237 } 238 239 public void nextCount(Query query, int resultCount) { 240 super.nextCount(query, resultCount); 241 242 List list = (List ) queries.get(query); 243 if (list == null) { 244 list = new ArrayList (5); 245 queries.put(query, list); 246 } 247 248 list.add(new Integer (resultCount)); 249 } 250 251 public void nextDataRows(Query query, List dataRows) { 252 super.nextDataRows(query, dataRows); 253 254 List list = (List ) queries.get(query); 255 if (list == null) { 256 list = new ArrayList (5); 257 queries.put(query, list); 258 } 259 260 list.add(dataRows); 261 } 262 263 public void nextDataRows(Query q, ResultIterator it) { 264 throw new CayenneRuntimeException("Iterated results are not supported by " 265 + this.getClass().getName()); 266 } 267 268 } 269 | Popular Tags |