Statistics
| Revision:

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

History | View | Annotate | Download (7.38 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresqlbin;
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 PostgresqlBinFeatureCollection extends AbstractJDBCDataFeatureCollection {
23
        protected DBFeatureType featureType;
24
        protected String filter;
25
        protected String totalFilter;
26
        protected PostgresqlBinStore 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
        public PostgresqlBinFeatureCollection(PostgresqlBinStore 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 "+ ((PostgresqlBinStoreParameters)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 = 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
                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

    
128
                H2Iterator dbfIter;
129
                try {
130
                        dbfIter=new H2Iterator(this.store,this.featureType,this.sql,this.fetchSize);
131
                } catch (ReadException e) {
132
                        throw new RuntimeException(e);
133
                }
134

    
135
                return dbfIter;
136
        }
137

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

    
148
                public H2Iterator(PostgresqlBinStore 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.index = 0;
154
                        this.createCursor();
155
                        this.createResulset();
156
                }
157

    
158
                protected void createCursor() throws ReadException {
159
                        //Comprobar que no se ha inicializado
160
                        this.store.open();
161
                        Connection conn = this.store.getCurrentConnection();
162
                        String mSql= null;
163

    
164
                        try {
165

    
166
                                Statement st = conn.createStatement();
167

    
168
                                this.cursorName = PostgresqlBinStoreUtils.createCursorName();
169
                                mSql="BEGIN";
170
                                st.execute(mSql);
171
                                mSql="declare " + this.cursorName + " binary cursor for " + this.sql;
172
                                st.execute("msql");
173
//                                System.out.println(mSql);
174

    
175

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

    
180

    
181
                }
182

    
183
                protected void createResulset() throws ReadException{
184

    
185
                        this.store.open();
186

    
187
                        String mSql = "fetch forward " + this.fetchSize + " in "
188
                                        + this.cursorName;
189
                        Connection conn = this.store.getCurrentConnection();
190
                        try {
191
                                if (this.rs != null){
192
                                        this.rs.close();
193
                                        this.rs.getStatement().close();
194
                                }
195

    
196
                                Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
197
//                                System.out.println(mSql);
198
                                this.rs = st.executeQuery(mSql);
199

    
200
                        } catch (java.sql.SQLException e) {
201
                                throw new SQLException(mSql,this.store.getName(),e);
202
                        }
203

    
204

    
205
                }
206

    
207
                public boolean hasNext(){
208
                        checkModified();
209
                        try {
210
                                if (rs.isLast()){
211
                                        if (this.index == this.fetchSize){
212
                                                this.index = 0;
213
                                                this.createResulset();
214
                                                return this.hasNext();
215
                                        } else{
216
                                                this.close();
217
                                                return false;
218
                                        }
219

    
220
                                } else {
221
                                        return true;
222
                                }
223
                        } catch (java.sql.SQLException e) {
224
                                throw new RuntimeException(
225
                                        new ReadException(this.store.getName(),e)
226
                                );
227
                        } catch (ReadException e1){
228
                                throw new RuntimeException(
229
                                                new ReadException(this.store.getName(),e1)
230
                                        );
231
                        }
232
                }
233

    
234
                public Object next() {
235
                        checkModified();
236
                        if (!hasNext())
237
                                throw new NoSuchElementException();
238
                        return nextFeature();
239
                }
240

    
241
                private IFeature nextFeature() {
242
                        try {
243
                                IFeature feature=null;
244
                                try {
245
                                        if(rs.next()){
246
                                                feature = this.store.createFeatureFromResulset(this.rs, this.featureType);
247
                                        } else {
248
                                                this.close();
249
                                                throw new NoSuchElementException();
250
                                        }
251
                                        if (rs.isAfterLast()){
252
                                                this.close();
253
                                        }
254

    
255

    
256
                                } catch (java.sql.SQLException e) {
257
                                        throw new RuntimeException(
258
                                                        new ReadException(this.store.getName(),e)
259
                                                );
260
                                }
261
                                this.index++;
262
                                return feature;
263
                        } catch (BaseException e){
264
                                throw new RuntimeException(
265
                                                new ReadException(this.store.getName(),e)
266
                                        );
267

    
268
                        }
269
                }
270

    
271
                public void remove() {
272
                        throw new UnsupportedOperationException();
273
                }
274

    
275
                private void close(){
276
                        if (rs == null)
277
                                return;
278
                        try {
279
                                rs.close();
280
                                rs.getStatement().close();
281
                        } catch (java.sql.SQLException e) {
282
                                // TODO Auto-generated catch block
283
                                e.printStackTrace();
284
                        } finally{
285
                                Statement st;
286
                                try {
287
                                        Connection conn = this.store.getCurrentConnection();
288
                                        st = conn.createStatement();
289
                                        st.execute("Rollback");
290
                                } catch (java.sql.SQLException e) {
291
                                        // TODO Auto-generated catch block
292
                                        e.printStackTrace();
293
                                }
294

    
295

    
296
                        }
297

    
298

    
299
                }
300

    
301
        }
302

    
303
        public void dispose() {
304
                this.store.deleteObserver(this);
305
                this.store=null;
306
                this.featureType=null;
307

    
308
        }
309

    
310
}