Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostgresqlFeatureCollection.java @ 20058

History | View | Annotate | Download (6.67 KB)

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

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

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

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

    
22
public class PostgresqlFeatureCollection extends AbstractJDBCDataFeatureCollection {
23
        protected DBFeatureType featureType;
24
        protected String filter;
25
        protected String totalFilter;
26
        protected JDBCStore store;
27
        private String order;
28
        private int numReg=-1;
29
        private String sql;
30
        private String sqlCount;
31
        private String totalOrder;
32
        private int fetchSize=5000;
33

    
34
        PostgresqlFeatureCollection(JDBCStore store,IFeatureType type, String filter, String order) {
35
                this.store=store;
36
                this.featureType=(DBFeatureType)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

    
48
                        this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID();
49
                        if (!isStringEmpty(this.totalFilter)){
50
                                this.sql= this.sql + " Where " + this.totalFilter;
51
                                this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
52
                        }
53
                        if (!isStringEmpty(this.totalOrder)){
54
                                this.sql= this.sql + " Order by " + this.totalOrder;
55
                        }
56
                }
57
        }
58

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

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

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

    
71
        }
72

    
73

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

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

    
89
        }
90

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

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

    
125
        }
126

    
127
        public Iterator iterator() {
128
                checkModified();
129
                PostgresqlIterator dbIter;
130
                try {
131
                        dbIter=new PostgresqlIterator((PostgresqlStore)this.store,this.featureType,this.sql,this.fetchSize);
132
                } catch (ReadException e) {
133
                        throw new RuntimeException(e);
134
                }
135

    
136
                return dbIter;
137
        }
138

    
139
        protected class PostgresqlIterator implements Iterator{
140
                private ResultSet rs;
141
                private PostgresqlStore store;
142
                private DBFeatureType featureType;
143
                private String sql;
144
                private int fetchSize;
145
                private int page;
146
                private int index;
147

    
148
                public PostgresqlIterator(PostgresqlStore store,DBFeatureType featureType ,String sql,int fetchSize) throws ReadException{
149
                        this.sql = sql;
150
                        this.store = store;
151
                        this.featureType = featureType;
152
                        this.fetchSize= fetchSize;
153
                        this.page = 0;
154
                        this.index = 0;
155
                        this.createResulset();
156
                }
157

    
158
                protected void createResulset() throws ReadException{
159

    
160
                        this.store.open();
161

    
162
                        String mSql = PostgresqlStoreUtils.addLimitsToSQL(
163
                                        this.sql,
164
                                        this.fetchSize,
165
                                        this.page);
166
                        Connection conn = this.store.getCurrentConnection();
167
                        try {
168
                                if (this.rs != null){
169
                                        this.rs.close();
170
//                                        this.rs.getStatement().close();
171
                                }
172

    
173
                                Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
174
//                                System.out.println(mSql);
175
                                this.rs = st.executeQuery(mSql);
176

    
177
                        } catch (java.sql.SQLException e) {
178
                                throw new SQLException(mSql,this.store.getName(),e);
179
                        }
180

    
181

    
182
                }
183

    
184
                public boolean hasNext(){
185
                        checkModified();
186
                        try {
187
                                if (rs.isLast()){
188
                                        if (this.index == this.fetchSize){
189
                                                this.index = 0;
190
                                                this.page++;
191
                                                this.createResulset();
192
                                                return this.hasNext();
193
                                        } else{
194
                                                return false;
195
                                        }
196

    
197
                                } else {
198
                                        return true;
199
                                }
200
                        } catch (java.sql.SQLException e) {
201
                                throw new RuntimeException(
202
                                        new ReadException(this.store.getName(),e)
203
                                );
204
                        } catch (ReadException e1){
205
                                throw new RuntimeException(
206
                                                new ReadException(this.store.getName(),e1)
207
                                        );
208
                        }
209
                }
210

    
211
                public Object next() {
212
                        checkModified();
213
                        if (!hasNext())
214
                                throw new NoSuchElementException();
215
                        return nextFeature();
216
                }
217

    
218
                private IFeature nextFeature() {
219
                        try {
220
                                IFeature feature=null;
221
                                try {
222
                                        if(rs.next()){
223
                                                feature = this.store.createFeatureFromResulset(this.rs, this.featureType);
224
                                        } else {
225
                                                rs.close();
226
//                                                rs.getStatement().close();
227
                                                throw new NoSuchElementException();
228
                                        }
229
                                        if (rs.isAfterLast()){
230
                                                rs.close();
231
//                                                rs.getStatement().close();
232
                                        }
233

    
234

    
235
                                } catch (java.sql.SQLException e) {
236
                                        throw new RuntimeException(
237
                                                        new ReadException(this.store.getName(),e)
238
                                                );
239
                                }
240
                                this.index++;
241
                                return feature;
242
                        } catch (BaseException e){
243
                                throw new RuntimeException(
244
                                                new ReadException(this.store.getName(),e)
245
                                        );
246

    
247
                        }
248
                }
249

    
250
                public void remove() {
251
                        throw new UnsupportedOperationException();
252
                }
253

    
254
        }
255

    
256
        public void dispose() {
257
                this.store.deleteObserver(this);
258
                this.store=null;
259
                this.featureType=null;
260

    
261
        }
262

    
263
}