Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / arcview / ArcJoinDataSource.java @ 37952

History | View | Annotate | Download (4.93 KB)

1
package com.iver.cit.gvsig.fmap.operations.arcview;
2

    
3
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
4
import com.hardcode.gdbms.engine.data.DataSource;
5
import com.hardcode.gdbms.engine.data.persistence.Memento;
6
import com.hardcode.gdbms.engine.data.persistence.MementoException;
7
import com.hardcode.gdbms.engine.data.persistence.OperationLayerMemento;
8
import com.hardcode.gdbms.engine.strategies.OperationDataSource;
9
import com.hardcode.gdbms.engine.values.Value;
10
import com.hardcode.gdbms.engine.values.ValueFactory;
11

    
12

    
13
/**
14
 * DOCUMENT ME!
15
 *
16
 * @author Fernando Gonz?lez Cort?s
17
 */
18
public class ArcJoinDataSource extends OperationDataSource {
19
        private DataSource source;
20
        private DataSource linked;
21
        private int[] relation;
22
        private int linkFieldindex;
23
        public static String prefix = "j_";
24
        private boolean bIsOpen = false;
25

    
26
        /**
27
         * DOCUMENT ME!
28
         *
29
         * @param result
30
         * @param source
31
         * @param linked DOCUMENT ME!
32
         * @param linkFieldindex DOCUMENT ME!
33
         */
34
        public ArcJoinDataSource(int[] result, DataSource source,
35
                DataSource linked, int linkFieldindex) {
36
                this.relation = result;
37
                this.source = source;
38
                this.linked = linked;
39
                this.linkFieldindex = linkFieldindex;
40
        }
41

    
42
        /**
43
         * @throws ReadDriverException
44
         * @see com.hardcode.gdbms.engine.data.DataSource#start()
45
         */
46
        public void start() throws ReadDriverException {
47
                // Fjp: Tenemos un problema cuando hacemos join con tablas de base de datos, ya que no podemos
48
                // estar abriendo y cerrando continuamente el datasource (muy lento, se cuelga gvSIG).
49
                // Lo correcto ser?a quitar de una vez el AUTOMATIC_DATASOURCE y dejarlo en MANUAL, pero mientras
50
                // tanto habr? que parchear esto, y tener las tablas de un join abiertas.
51
                // Creo que esto funciona porque al pedir valores ya hace cada tabla su start.
52
                // TODO: Comprobar que al hacer el stop no estamos otra vez con el mismo problema. Lo ideal ser?a
53
                // que no se cerrara y abriera el recordset cada vez que llamamos a getFieldValue, o getRecordCount, etc.
54
                if (bIsOpen == false) {
55
                        source.start();
56
                        linked.start();
57
                        bIsOpen = true;
58
                }
59
        }
60

    
61
        /**
62
         * @see com.hardcode.gdbms.engine.data.DataSource#stop()
63
         */
64
        public void stop() throws ReadDriverException {
65
//                source.stop();
66
//                linked.stop();
67
        }
68

    
69
        /**
70
         * @see com.hardcode.gdbms.engine.data.FieldNameAccess#getFieldIndexByName(java.lang.String)
71
         */
72
        public int getFieldIndexByName(String fieldName) throws ReadDriverException {
73
        for (int i = 0; i < getFieldCount(); i++) {
74
            if (getFieldName(i).equals(fieldName)){
75
                return i;
76
            }
77
        }
78

    
79
        return -1;
80
    }
81

    
82
        /**
83
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
84
         */
85
        public Value getFieldValue(long rowIndex, int fieldId)
86
                throws ReadDriverException {
87
                if (fieldId < source.getFieldCount()) {
88
                        return source.getFieldValue(rowIndex, fieldId);
89
                }
90
                fieldId = fieldId - source.getFieldCount();
91

    
92
                if (fieldId >= linkFieldindex) {
93
                        fieldId++;
94
                }
95

    
96
                int index = relation[(int) rowIndex];
97

    
98
                if (index == -1) {
99
                        return ValueFactory.createNullValue();
100
                }
101

    
102
                return linked.getFieldValue(index, fieldId);
103
        }
104

    
105
        /**
106
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
107
         */
108
        public int getFieldCount() throws ReadDriverException {
109
                return (source.getFieldCount() + linked.getFieldCount()) - 1;
110
        }
111

    
112
        /**
113
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
114
         */
115
        public String getFieldName(int fieldId) throws ReadDriverException {
116
                if (fieldId < source.getFieldCount()) {
117
                        return source.getFieldName(fieldId);
118
                }
119
                fieldId = fieldId - source.getFieldCount();
120

    
121
                if (fieldId >= linkFieldindex) {
122
                        fieldId++;
123
                }
124

    
125
                return prefix + linked.getFieldName(fieldId);
126
        }
127

    
128
        /**
129
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
130
         */
131
        public long getRowCount() throws ReadDriverException {
132
                return source.getRowCount();
133
        }
134

    
135
        /**
136
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
137
         */
138
        public int getFieldType(int i) throws ReadDriverException {
139
                if (i < source.getFieldCount()) {
140
                        return source.getFieldType(i);
141
                }
142
                i = i - source.getFieldCount();
143

    
144
                if (i >= linkFieldindex) {
145
                        i++;
146
                }
147

    
148
                return linked.getFieldType(i);
149
        }
150

    
151
        /**
152
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
153
         */
154
        public Memento getMemento() throws MementoException {
155
                return new OperationLayerMemento(getName(),
156
                                new Memento[]{source.getMemento(), linked.getMemento()}, getSQL());
157
        }
158

    
159
        public int getFieldWidth(int i) throws ReadDriverException {
160
                if (i < source.getFieldCount()) {
161
                        return source.getFieldWidth(i);
162
                }
163
                i = i - source.getFieldCount();
164

    
165
                if (i >= linkFieldindex) {
166
                        i++;
167
                }
168

    
169
                return linked.getFieldWidth(i);
170
        }
171

    
172
    public boolean isVirtualField(int fieldId) throws ReadDriverException  {
173
                if (fieldId < source.getFieldCount()) {
174
                        return source.isVirtualField(fieldId);
175
                }
176
                fieldId = fieldId - source.getFieldCount();
177

    
178
                if (fieldId >= linkFieldindex) {
179
                        fieldId++;
180
                }
181

    
182
                return linked.isVirtualField(fieldId);
183
    }
184
}