1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import com.daffodilwoods.daffodildb.server.sql99.common.*; 4 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 5 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 6 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 7 import com.daffodilwoods.daffodildb.utils.*; 8 import com.daffodilwoods.daffodildb.utils.field.*; 9 import com.daffodilwoods.database.resource.*; 10 import com.daffodilwoods.database.utility.*; 11 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression; 12 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 13 14 31 32 public class AggregateGroupByIterator extends GroupByIterator { 33 34 41 public AggregateGroupByIterator(_Iterator iterator0, ColumnDetails[] aggregateColumnDetails0,_ServerSession serverSession) throws DException { 42 super(iterator0, null, aggregateColumnDetails0,null,serverSession); 43 initializingExecutables(); 44 } 45 46 51 private void initializingExecutables() throws DException { 52 int len = mapping.length; 53 for (int i = 0; i < len; i++) { 54 ( (_Aggregate) mapping[i][1]).initialize(); 55 } 56 } 57 58 61 62 69 public boolean first() throws DException { if (iterator.first()) { 71 state = FORWARD; boolean b = (status = CheckForSameGroup() ? VALIDSTATE : AFTERLAST) == VALIDSTATE; 73 return b; 74 }else{ 75 int len = mapping.length; 76 for (int i = 0; i < len; i++) { 77 ( (_Aggregate) mapping[i][1]).initialize(); 78 } 79 } 80 status = VALIDSTATE; 81 return true; 82 } 83 84 91 public boolean last() throws DException { 92 if (iterator.last()) { 93 state = BACKWARD; return (status = CheckForSameGroupPrevious() ? VALIDSTATE : BEFOREFIRST) == VALIDSTATE; 95 }else{ 96 int len = mapping.length; 97 for (int i = 0; i < len; i++) { 98 ( (_Aggregate) mapping[i][1]).initialize(); 99 } 100 } 101 status = VALIDSTATE; 102 return true; 103 } 104 105 112 public boolean previous() throws com.daffodilwoods.database.resource.DException { 113 if (status == AFTERLAST) { 114 return last(); 115 } 116 status = BEFOREFIRST; 117 return false; 118 } 119 120 127 public boolean next() throws com.daffodilwoods.database.resource.DException { 128 if (status == BEFOREFIRST) { 129 return first(); 130 } 131 status = AFTERLAST; 132 return false; 133 } 134 135 145 protected boolean CheckForSameGroup() throws DException { 146 int len = mapping.length; 147 for (int i = 0; i < len; i++) { 148 ( (_Aggregate) mapping[i][1]).initialize(); 149 } 150 do { 151 evaluateAggregates(); 152 } while (iterator.next()); 153 return true; 154 } 155 156 166 protected boolean CheckForSameGroupPrevious() throws DException { 167 int len = mapping.length; 168 for (int i = 0; i < len; i++) { 169 ( (_Aggregate) mapping[i][1]).initialize(); 170 } 171 do { 172 evaluateAggregates(); 173 } while (iterator.previous()); 174 return true; 175 } 176 177 185 186 public Object getKey() throws DException { 187 if (keyColumnInformation == null) 188 return null; 189 int length = keyColumnInformation.length; 190 Object [] key = new Object [length]; 191 for (int i = 0; i < length; i++) { 192 key[i] = FieldUtility.getField(Datatypes.LONG, new Long (0)); } 194 return key; 195 } 196 197 198 199 public byte[] getByteKey() throws DException { 200 if (keyColumnInformation == null) 201 return null; 202 int length = keyColumnInformation.length; 203 byte[] key = new byte[length]; 204 for (int i = 0; i < length; i++) { 205 key[i] = 0; } 207 return key; 208 209 } 210 211 212 218 public void move(Object keys) throws DException { 219 switch (state) { 220 case FORWARD: 221 last(); 222 break; 223 case BACKWARD: 224 first(); 225 break; 226 } 227 status = VALIDSTATE; 228 } 229 230 231 public void moveByteKey(byte[] key) throws DException { 232 move(key); 233 } 234 235 241 242 250 public Object getColumnValues(_Reference[] references) throws DException { 251 int len = references.length; 252 Object [] result = new Object [len]; 253 for (int i = 0; i < len; i++) { 254 result[i] = getColumnValues(references[i]); 255 } 256 return result; 257 } 258 259 267 public Object getColumnValues(_Reference references) throws DException { 268 Object result = null; 269 int length = mapping.length; 270 for (int i = 0; i < length; i++) { 271 if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references.getColumn().trim())) { 272 result = ( (_Aggregate) mapping[i][1]).getResult(); 273 return result; 274 } 275 } 276 result = FieldUtility.getField(Datatypes.LONG, new Long (0)); return result; 278 } 279 280 288 289 public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException { 290 FieldBase result = null; 291 int length = mapping.length; 292 for (int i = 0; i < length; i++) { 293 if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references.getColumn().trim())) { 294 result = (FieldBase) ( (_Aggregate) mapping[i][1]).getResult(); 295 return result; 296 } 297 } 298 result = FieldUtility.getField(Datatypes.LONG, new Long (0)); 299 return result; 300 } 301 302 310 public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException { 311 int len = references.length; 312 FieldBase[] result = new FieldBase[len]; 313 for (int i = 0; i < len; i++) { 314 result[i] = field(references[i]); 315 } 316 return result; 317 } 318 319 public String toString() { 320 String str = "AggregateGroupByIterator"; 321 try { 322 if (iterator != null) { 323 str += "[" + iterator.toString() + "]"; 324 } 325 } catch (Exception e) { 326 e.printStackTrace(); 327 } 328 return str; 329 } 330 } 331 | Popular Tags |