svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / arcview / ArcJoin.java @ 1831
History | View | Annotate | Download (4.25 KB)
1 |
package com.iver.cit.gvsig.fmap.operations.arcview; |
---|---|
2 |
|
3 |
import java.util.HashMap; |
4 |
|
5 |
import com.hardcode.gdbms.engine.customQuery.CustomQuery; |
6 |
import com.hardcode.gdbms.engine.customQuery.QueryException; |
7 |
import com.hardcode.gdbms.engine.data.DataSource; |
8 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
9 |
import com.hardcode.gdbms.engine.instruction.Adapter; |
10 |
import com.hardcode.gdbms.engine.instruction.Expression; |
11 |
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException; |
12 |
import com.hardcode.gdbms.engine.instruction.Utilities; |
13 |
import com.hardcode.gdbms.engine.strategies.OperationDataSource; |
14 |
import com.hardcode.gdbms.engine.values.BooleanValue; |
15 |
import com.hardcode.gdbms.engine.values.Value; |
16 |
|
17 |
/**
|
18 |
* @author Fernando Gonz?lez Cort?s
|
19 |
*/
|
20 |
public class ArcJoin implements CustomQuery { |
21 |
|
22 |
/**
|
23 |
* @see com.hardcode.gdbms.engine.customQuery.CustomQuery#evaluate(com.hardcode.gdbms.engine.data.DataSource[], com.hardcode.gdbms.engine.instruction.Expression[])
|
24 |
*/
|
25 |
public OperationDataSource evaluate(DataSource[] tables, Expression[] values) throws QueryException { |
26 |
if (tables.length != 2) throw new QueryException("ArcJoin s?lo opera con 2 tablas"); |
27 |
if (values.length != 2) throw new QueryException("Se esperan dos expresiones de campo"); |
28 |
|
29 |
//Se establece el origen de datos para las expresiones
|
30 |
((Adapter) values[0]).getInstructionContext().setDs(tables[0]); |
31 |
((Adapter) values[0]).getInstructionContext().setFromTable(tables[0]); |
32 |
((Adapter) values[1]).getInstructionContext().setDs(tables[0]); |
33 |
((Adapter) values[1]).getInstructionContext().setFromTable(tables[0]); |
34 |
|
35 |
String fieldName0 = values[0].getFieldName(); |
36 |
if (fieldName0 == null) throw new QueryException("El valor debe ser una referencia a columna:" + Utilities.getText(((Adapter)values[0]).getEntity())); |
37 |
String fieldName1 = values[1].getFieldName(); |
38 |
if (fieldName1 == null) throw new QueryException("El valor debe ser una referencia a columna:" + Utilities.getText(((Adapter)values[1]).getEntity())); |
39 |
|
40 |
try {
|
41 |
tables[0].start();
|
42 |
tables[1].start();
|
43 |
|
44 |
int[] result = new int[(int) tables[0].getRowCount()]; |
45 |
|
46 |
int index0 = tables[0].getFieldIndexByName(fieldName0); |
47 |
if (index0 == -1) throw new QueryException("No existe el campo: " + fieldName0); |
48 |
int index1 = tables[1].getFieldIndexByName(fieldName1); |
49 |
if (index1 == -1) throw new QueryException("No existe el campo: " + fieldName1); |
50 |
|
51 |
//Construimos el ?ndice
|
52 |
HashMap idx = new HashMap(((int) tables[1].getRowCount())*2); |
53 |
for (int i = 0; i < tables[1].getRowCount(); i++) { |
54 |
Value v = tables[1].getFieldValue(i, index1);
|
55 |
if (idx.get(v) == null) |
56 |
idx.put(v, new Integer(i)); |
57 |
} |
58 |
|
59 |
/* Index idx = new DiskIndex(((int) tables[1].getRowCount())*2);
|
60 |
idx.start();
|
61 |
for (int i = 0; i < tables[1].getRowCount(); i++) {
|
62 |
idx.add(tables[1].getFieldValue(i, index1), i);
|
63 |
}
|
64 |
*/
|
65 |
//Hacemos la query
|
66 |
for (int i = 0; i < tables[0].getRowCount(); i++) { |
67 |
Value v = tables[0].getFieldValue(i, index0);
|
68 |
Integer pi = (Integer) idx.get(v); |
69 |
if (pi == null){ |
70 |
result[i] = -1;
|
71 |
} else {
|
72 |
try {
|
73 |
if (((BooleanValue)v.equals(tables[1].getFieldValue(pi.intValue(), index1))).getValue()){ |
74 |
result[i] = pi.intValue(); |
75 |
} |
76 |
} catch (IncompatibleTypesException e1) {
|
77 |
throw new QueryException("Los tipos de datos son incompatibles: " + tables[0].getFieldType(index0) + " - " + tables[1].getFieldType(index1), e1); |
78 |
} |
79 |
} |
80 |
} |
81 |
/*
|
82 |
for (int i = 0; i < tables[0].getRowCount(); i++) {
|
83 |
Value v = tables[0].getFieldValue(i, index0);
|
84 |
PositionIterator pi = idx.getPositions(v);
|
85 |
boolean any = false;
|
86 |
while (pi.hasNext()){
|
87 |
int pos = pi.next();
|
88 |
try {
|
89 |
if (((BooleanValue)v.equals(tables[1].getFieldValue(pos, index1))).getValue()){
|
90 |
result[i] = pos;
|
91 |
any = true;
|
92 |
}
|
93 |
} catch (IncompatibleTypesException e1) {
|
94 |
throw new QueryException("Los tipos de datos son incompatibles: " + tables[0].getFieldType(index0) + " - " + tables[1].getFieldType(index1), e1);
|
95 |
}
|
96 |
}
|
97 |
if (!any) result[i] = -1;
|
98 |
}
|
99 |
*/
|
100 |
tables[0].stop();
|
101 |
tables[1].stop();
|
102 |
// idx.stop();
|
103 |
|
104 |
return new ArcJoinDataSource(result, tables[0], tables[1], index1); |
105 |
} catch (DriverException e) {
|
106 |
throw new QueryException("Error accediendo a los datos", e); |
107 |
} |
108 |
} |
109 |
|
110 |
} |