Revision 10627 trunk/libraries/libGDBMS/src/test/java/com/hardcode/gdbms/engine/strategies/SumQuery.java

View differences:

SumQuery.java
1 1
package com.hardcode.gdbms.engine.strategies;
2 2

  
3
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
3 4
import com.hardcode.gdbms.engine.customQuery.CustomQuery;
4 5
import com.hardcode.gdbms.engine.customQuery.QueryException;
5 6
import com.hardcode.gdbms.engine.data.DataSource;
6
import com.hardcode.gdbms.engine.data.driver.DriverException;
7 7
import com.hardcode.gdbms.engine.instruction.Adapter;
8 8
import com.hardcode.gdbms.engine.instruction.Expression;
9 9
import com.hardcode.gdbms.engine.instruction.Utilities;
10
import com.hardcode.gdbms.engine.strategies.OperationDataSource;
11 10
import com.hardcode.gdbms.engine.values.NumericValue;
12 11
import com.hardcode.gdbms.engine.values.Value;
13 12

  
......
23 22
	public OperationDataSource evaluate(DataSource[] tables, Expression[] values) throws QueryException {
24 23
		if (tables.length != 1) throw new QueryException("SUM only operates on one table");
25 24
		if (values.length != 1) throw new QueryException("SUM only operates with one value");
26
		
25

  
27 26
		((Adapter) values[0]).getInstructionContext().setFromTables(new DataSource[]{tables[0]});
28 27
		((Adapter) values[0]).getInstructionContext().setDs(tables[0]);
29 28

  
30 29
		String fieldName = values[0].getFieldName();
31 30
		if (fieldName == null) throw new QueryException("field not found " + Utilities.getText(((Adapter)values[0]).getEntity()));
32
		
31

  
33 32
		double res = 0;
34 33
		try {
35 34

  
36 35
			tables[0].start();
37
			
36

  
38 37
			int fieldIndex = tables[0].getFieldIndexByName(fieldName);
39 38
			if (fieldIndex == -1) throw new RuntimeException("we found the field name of the expression but could not find the field index?");
40
		
39

  
41 40
			for (int i = 0; i < tables[0].getRowCount(); i++) {
42 41
				Value v = tables[0].getFieldValue(i, fieldIndex);
43 42
				if (v instanceof NumericValue){
......
46 45
					throw new QueryException("SUM only operates with numeric fields");
47 46
				}
48 47
			}
49
			
48

  
50 49
			tables[0].stop();
51
		} catch (DriverException e) {
50
		} catch (ReadDriverException e) {
52 51
			throw new QueryException("Error reading data", e);
53 52
		}
54
		
53

  
55 54
		return new SumDataSource(res);
56 55
	}
57 56

  

Also available in: Unified diff