Statistics
| Revision:

root / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2FeatureCollection.java @ 19975

History | View | Annotate | Download (5.55 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
2

    
3
import java.sql.Connection;
4
import java.sql.ResultSet;
5
import java.sql.Statement;
6
import java.util.ConcurrentModificationException;
7
import java.util.Iterator;
8
import java.util.NoSuchElementException;
9

    
10
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
11
import org.gvsig.data.datastores.vectorial.db.jdbc.AbstractJDBCDataFeatureCollection;
12
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
13
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException;
14
import org.gvsig.data.exception.ReadException;
15
import org.gvsig.data.vectorial.AbstractFeatureCollection;
16
import org.gvsig.data.vectorial.IFeature;
17
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
18
import org.gvsig.data.vectorial.IFeatureType;
19
import org.gvsig.exceptions.BaseException;
20

    
21
import com.iver.cit.gvsig.fmap.drivers.WKBParser2;
22

    
23
public class H2FeatureCollection extends AbstractJDBCDataFeatureCollection {
24
        protected IFeatureType featureType;
25
        protected String filter;
26
        protected String totalFilter;
27
        protected H2Store store;
28
        private String order;
29
        private int numReg=-1;
30
        private String sql;
31
        private String sqlCount;
32
        private String totalOrder;
33

    
34
        H2FeatureCollection(H2Store store,IFeatureType type, String filter, String order) {
35
                this.store=store;
36
                this.featureType=type;
37
                this.filter=filter;
38
                this.order=order;
39
                this.calculateWhere();
40
                this.calculateOrder();
41

    
42
                if (store.isUseSqlSource()){
43
                        this.sql = store.getSqlSource();
44
                        this.sqlCount = null;
45
                } else {
46
                        this.sql = this.store.getSqlSelectPart();
47
                        this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID();
48
                        if (!isStringEmpty(this.totalFilter)){
49
                                this.sql= this.sql + " Where " + this.totalFilter;
50
                                this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
51
                        }
52
                        if (!isStringEmpty(this.totalOrder)){
53
                                this.sql= this.sql + " Order by " + this.totalOrder;
54
                        }
55
                }
56
        }
57

    
58
        private ResultSet getNewResulset(String aSql) throws ReadException{
59
                this.store.open();
60

    
61
                Connection conn = this.store.getCurrentConnection();
62
                try {
63
                        Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
64
                        return st.executeQuery(aSql);
65

    
66
                } catch (java.sql.SQLException e) {
67
                        throw new SQLException(aSql,this.store.getName(),e);
68
                }
69

    
70
        }
71

    
72

    
73
        private void calculateWhere(){
74
                if (isStringEmpty(this.store.getBaseWhereClause())){
75
                        this.totalFilter = this.filter;
76
                } else {
77
                        this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter;
78
                }
79
        }
80

    
81
        private void calculateOrder(){
82
                if (isStringEmpty(this.store.getBaseOrder())){
83
                        this.totalOrder = this.order;
84
                } else {
85
                        this.totalOrder = this.store.getBaseOrder() + ", " +this.order;
86
                }
87

    
88
        }
89

    
90
        public int size() {
91
                checkModified();
92
                try {
93
                        if (this.numReg < 0){
94
                                ResultSet r=null;
95
                                try {
96
                                        if (this.sqlCount != null){
97
                                                r = this.getNewResulset(this.sqlCount);
98
                                                r.next();
99
                                                numReg = r.getInt(1);
100
                                        } else{
101
                                                this.numReg = 0;
102
                                                r = this.getNewResulset(this.sql);
103
                                                while (r.next()){
104
                                                        this.numReg++;
105
                                                }
106
                                        }
107
                                } catch (java.sql.SQLException e) {
108
                                        throw new ReadException(this.store.getName(),e);
109

    
110
                                } finally{
111
                                        try {
112
                                                if (r != null)
113
                                                        r.close();
114
                                        } catch (java.sql.SQLException e) {
115
                                                throw new ReadException(this.store.getName(),e);
116
                                        }
117
                                }
118
                        }
119
                        return numReg;
120
                } catch (BaseException e){
121
                        throw new RuntimeException(e);
122
                }
123

    
124
        }
125

    
126
        public Iterator iterator() {
127
                checkModified();
128
                ResultSet r;
129
                try {
130
                        r = this.getNewResulset(this.sql);
131
                } catch (ReadException e) {
132
                        throw new RuntimeException(e);
133
                }
134
                H2Iterator dbfIter=new H2Iterator(this.store,this.featureType,r);
135
                return dbfIter;
136
        }
137

    
138
        protected class H2Iterator implements Iterator{
139
                private ResultSet rs;
140
                private JDBCStore store;
141
                private IFeatureType featureType;
142

    
143
                public H2Iterator(JDBCStore store,IFeatureType featureType ,ResultSet rs){
144
                        this.rs = rs;
145
                        this.store = store;
146
                        this.featureType = featureType;
147
                }
148

    
149
                protected void checkModified(){
150
                        if (modified)
151
                                throw new ConcurrentModificationException("FeatureCollection modified");
152
                }
153

    
154
                public boolean hasNext(){
155
                        checkModified();
156
                        try {
157
                                if (rs.isLast()){
158
                                        return false;
159
                                } else {
160
                                        return true;
161
                                }
162
                        } catch (java.sql.SQLException e) {
163
                                throw new RuntimeException(
164
                                        new ReadException(this.store.getName(),e)
165
                                );
166
                        }
167
                }
168

    
169
                public Object next() {
170
                        checkModified();
171
                        if (!hasNext())
172
                                throw new NoSuchElementException();
173
                        return nextFeature();
174
                }
175

    
176
                private IFeature nextFeature() {
177
                        try {
178
                                IFeature feature=null;
179
                                try {
180
                                        if(rs.next()){
181
                                                feature = H2Utils.createFeature(this.store, this.rs, this.featureType);
182
                                        } else {
183
                                                rs.close();
184
                                                rs.getStatement().close();
185
                                                throw new NoSuchElementException();
186
                                        }
187
                                        if (rs.isAfterLast()){
188
                                                rs.close();
189
                                                rs.getStatement().close();
190
                                        }
191

    
192

    
193
                                } catch (java.sql.SQLException e) {
194
                                        throw new RuntimeException(
195
                                                        new ReadException(this.store.getName(),e)
196
                                                );
197
                                }
198
                                return feature;
199
                        } catch (BaseException e){
200
                                throw new RuntimeException(
201
                                                new ReadException(this.store.getName(),e)
202
                                        );
203

    
204
                        }
205
                }
206

    
207
                public void remove() {
208
                        throw new UnsupportedOperationException();
209
                }
210

    
211
        }
212

    
213
        public void dispose() {
214
                this.store.deleteObserver(this);
215
                this.store=null;
216
                this.featureType=null;
217

    
218
        }
219

    
220
}