Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / strategies / FilteredDataSource.java @ 4851

History | View | Annotate | Download (5.48 KB)

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

    
3
import java.io.IOException;
4

    
5
import com.hardcode.gdbms.engine.data.DataSource;
6
import com.hardcode.gdbms.engine.data.driver.DriverException;
7
import com.hardcode.gdbms.engine.data.indexes.IndexFactory;
8
import com.hardcode.gdbms.engine.data.indexes.VariableIndexSet;
9
import com.hardcode.gdbms.engine.data.persistence.Memento;
10
import com.hardcode.gdbms.engine.data.persistence.MementoException;
11
import com.hardcode.gdbms.engine.data.persistence.OperationLayerMemento;
12
import com.hardcode.gdbms.engine.instruction.EvaluationException;
13
import com.hardcode.gdbms.engine.instruction.Expression;
14
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
15
import com.hardcode.gdbms.engine.instruction.SemanticException;
16
import com.hardcode.gdbms.engine.values.BooleanValue;
17
import com.hardcode.gdbms.engine.values.Value;
18

    
19

    
20
/**
21
 * Representa una fuente de datos que contiene una cl?usula where mediante la
22
 * cual se filtran los campos
23
 *
24
 * @author Fernando Gonz?lez Cort?s
25
 */
26
public class FilteredDataSource extends OperationDataSource {
27
        private DataSource source;
28
        private Expression whereExpression;
29
        private VariableIndexSet indexes;
30

    
31
        /**
32
         * Creates a new FilteredDataSource object.
33
         *
34
         * @param source DataSource que se va a filtrar
35
         * @param whereExpression Expresi?n de la cl?usula where
36
         */
37
        public FilteredDataSource(DataSource source, Expression whereExpression) {
38
                this.source = source;
39
                this.whereExpression = whereExpression;
40
        }
41

    
42
    public Value[] aggregatedFilter(Expression[] fields) throws IncompatibleTypesException, DriverException, EvaluationException, IOException {
43
        Value[] aggregatedValues = new Value[fields.length];
44
                indexes = IndexFactory.createVariableIndex();
45
                indexes.open();
46

    
47
                for (long i = 0; i < source.getRowCount(); i++) {
48
                        try {
49
                                if (((BooleanValue) whereExpression.evaluateExpression(i)).getValue()) {
50
                                        indexes.addIndex(i);
51
                                        for (int j = 0; j < aggregatedValues.length; j++) {
52
                        aggregatedValues[j] = fields[j].evaluate(i);
53
                    }
54
                                }
55
                        } catch (ClassCastException e) {
56
                                throw new IncompatibleTypesException("where expression is not boolean",
57
                                        e);
58
                        }
59
                }
60

    
61
                indexes.indexSetComplete();
62
                
63
                return aggregatedValues;
64
    }
65

    
66
    /**
67
         * M?todo que construye el array de ?ndices de las posiciones que las filas
68
         * filtradas ocupan en el DataSource origen
69
         *
70
         * @throws DriverException Si se produce un fallo en el driver al acceder a
71
         *                    los datos
72
         * @throws IOException Si se produce un error usando las estructuras de
73
         *                    datos internas
74
         * @throws SemanticException Si se produce alg?n error sem?ntico al evaluar
75
         *                    la expresi?n
76
         * @throws IncompatibleTypesException Si la expresi?n where no evalua a
77
         *                    booleano
78
         * @throws EvaluationException If the expression evaluation fails
79
         */
80
        public void filtrar()
81
                throws DriverException, IOException, SemanticException, EvaluationException {
82
                indexes = IndexFactory.createVariableIndex();
83
                indexes.open();
84

    
85
                for (long i = 0; i < source.getRowCount(); i++) {
86
                        try {
87
                                if (((BooleanValue) whereExpression.evaluateExpression(i)).getValue()) {
88
                                        indexes.addIndex(i);
89
                                }
90
                        } catch (ClassCastException e) {
91
                                throw new IncompatibleTypesException("where expression is not boolean",
92
                                        e);
93
                        }
94
                }
95

    
96
                indexes.indexSetComplete();
97
        }
98

    
99
        /**
100
         * @see com.hardcode.gdbms.engine.data.DataSource#open()
101
         */
102
        public void start() throws DriverException {
103
                source.start();
104
        }
105

    
106
        /**
107
         * @see com.hardcode.gdbms.engine.data.DataSource#close()
108
         */
109
        public void stop() throws DriverException {
110
                source.stop();
111

    
112
                try {
113
                        indexes.close();
114
                } catch (IOException e) {
115
                        throw new DriverException(e);
116
                }
117
        }
118

    
119
        /**
120
         * @see com.hardcode.gdbms.engine.data.FieldNameAccess#getFieldIndexByName(java.lang.String)
121
         */
122
        public int getFieldIndexByName(String fieldName) throws DriverException {
123
                return source.getFieldIndexByName(fieldName);
124
        }
125

    
126
        /**
127
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
128
         *                 int)
129
         */
130
        public Value getFieldValue(long rowIndex, int fieldId)
131
                throws DriverException {
132
                try {
133
                        return source.getFieldValue(indexes.getIndex(rowIndex), fieldId);
134
                } catch (IOException e) {
135
                        throw new DriverException(e);
136
                }
137
        }
138

    
139
        /**
140
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
141
         */
142
        public int getFieldCount() throws DriverException {
143
                return source.getFieldCount();
144
        }
145

    
146
        /**
147
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
148
         */
149
        public String getFieldName(int fieldId) throws DriverException {
150
                return source.getFieldName(fieldId);
151
        }
152

    
153
        /**
154
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
155
         */
156
        public long getRowCount() throws DriverException {
157
                return indexes.getIndexCount();
158
        }
159

    
160
        /**
161
         * DOCUMENT ME!
162
         *
163
         * @return DOCUMENT ME!
164
         *
165
         * @throws IOException
166
         *
167
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
168
         */
169
        public long[] getWhereFilter() throws IOException {
170
                return indexes.getIndexes();
171
        }
172

    
173
        /**
174
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
175
         */
176
        public int getFieldType(int i) throws DriverException {
177
                return source.getFieldType(i);
178
        }
179

    
180
        /**
181
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
182
         */
183
        public Memento getMemento() throws MementoException {
184
                return new OperationLayerMemento(getName(),
185
                        new Memento[] { source.getMemento() }, getSQL());
186
        }
187

    
188
        public int getFieldWidth(int i) throws DriverException {        
189
                return source.getFieldWidth(i);
190
        }
191
}