Statistics
| Revision:

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

History | View | Annotate | Download (6.25 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.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.IFeatureType;
18
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
19
import org.gvsig.exceptions.BaseException;
20

    
21
public class PostgresqlFeatureCollectionEditing extends AbstractJDBCDataFeatureCollection {
22
        protected DBFeatureType featureType;
23
        protected String totalFilter;
24
        protected PostgresqlStore store;
25
        private int numReg=-1;
26
        private String sql;
27
        private String sqlCount;
28
        private String totalOrder;
29
        private int fetchSize=5000;
30
        private FeatureManager featureManager;
31

    
32

    
33
        PostgresqlFeatureCollectionEditing(FeatureManager fm,PostgresqlStore store,IFeatureType type) {
34
                this.featureManager=fm;
35
                this.store=store;
36
                this.featureType=(DBFeatureType)type;
37

    
38
                this.totalFilter =store.getBaseWhereClause();
39
                this.totalOrder = store.getBaseOrder();
40

    
41
                this.sql = this.store.getSqlSelectPart();
42
                this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID();
43
                if (!isStringEmpty(this.totalFilter)){
44
                        this.sql= this.sql + " Where " + this.totalFilter;
45
                        this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
46
                }
47
                if (!isStringEmpty(this.totalOrder)){
48
                        this.sql= this.sql + " Order by " + this.totalOrder;
49
                }
50

    
51
        }
52

    
53
        private ResultSet getNewResulset(String aSql) throws ReadException{
54
                this.store.open();
55

    
56
                Connection conn = this.store.getCurrentConnection();
57

    
58
                try {
59
                        Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
60
                        return st.executeQuery(aSql);
61

    
62
                } catch (java.sql.SQLException e) {
63
                        throw new SQLException(aSql,this.store.getName(),e);
64
                }
65

    
66
        }
67

    
68
        public int size() {
69
                checkModified();
70
                try {
71
                        if (this.numReg < 0){
72
                                ResultSet r=null;
73
                                r = this.getNewResulset(this.sqlCount);
74
                                try {
75
                                        r.next();
76
                                        numReg = r.getInt(1);
77
                                } catch (java.sql.SQLException e) {
78
                                        throw new ReadException(this.store.getName(),e);
79
                                } finally{
80
                                        try {
81
                                                r.close();
82
                                        } catch (java.sql.SQLException e) {
83
                                                throw new ReadException(this.store.getName(),e);
84
                                        }
85
                                }
86
                                numReg = numReg+featureManager.getNum();
87
                        }
88
                        return numReg;
89
                } catch (BaseException e){
90
                        throw new RuntimeException(e);
91
                }
92

    
93
        }
94

    
95

    
96
        public Iterator iterator() {
97
                checkModified();
98
                try{
99
                        ResultSet r=null;
100
                        r = this.getNewResulset(this.sql);
101
                        PostgresIterator dbIter=new PostgresIterator(this.store,this.featureType,this.sql,this.fetchSize);
102
                        return dbIter;
103
                } catch (BaseException e){
104
                        throw new RuntimeException(e);
105
                }
106
        }
107

    
108
        protected class PostgresIterator implements Iterator{
109
                private ResultSet rs;
110
                private boolean nextChecked=false;
111
                private IFeature feature;
112
                private PostgresqlStore store;
113
                private DBFeatureType featureType;
114
                private String sql;
115
                private int fetchSize;
116
                private int page;
117
                private int index;
118
                private long position;
119
                private boolean rsEOF=false;
120

    
121
                public PostgresIterator(PostgresqlStore store, DBFeatureType featureType,String sql,int fetchSize)  throws ReadException{
122
                        this.sql = sql;
123
                        this.store = store;
124
                        this.featureType = featureType;
125
                        this.fetchSize= fetchSize;
126
                        this.page = 0;
127
                        this.index = 0;
128
                        this.createResulset();
129
                }
130

    
131
                public boolean hasNext(){
132
                        checkModified();
133

    
134
                        IFeature feature=null;
135
                        if (nextChecked){
136
                                return feature != null;
137
                        }
138
                        nextChecked=true;
139
                        while (true){
140
                                if (!rsEOF){
141
                                        try {
142
                                                if (rs.isLast()){
143
                                                        if (this.index == this.fetchSize){
144
                                                                this.index = 0;
145
                                                                this.page++;
146
                                                                this.createResulset();
147
                                                                if (rs.isLast()){
148
                                                                        rsEOF = true;
149
                                                                        rs.close();
150
                                                                        continue;
151
                                                                }
152
                                                        } else{
153
                                                                rsEOF = true;
154
                                                                rs.close();
155
                                                                continue;
156
                                                        }
157
                                                } else {
158
                                                        feature=nextFeature();
159
                                                }
160
                                        } catch (java.sql.SQLException e) {
161
                                                throw new RuntimeException(
162
                                                                new ReadException(this.store.getName(),e)
163
                                                );
164
                                        } catch (ReadException e) {
165
                                                throw new RuntimeException(e);
166
                                        }
167

    
168

    
169
                                }else {
170
                                        if (position<featureManager.getNum()){
171
                                                feature=featureManager.getFeature((int)position);
172
                                        }else{
173
                                                this.feature = null;
174
                                                return false;
175
                                        }
176
                                }
177

    
178

    
179

    
180
                                if(featureManager.isDeleted(feature))
181
                                        continue;
182
                                position++;
183
                                this.feature=feature;
184
                                return true;
185

    
186
                        }
187
                }
188

    
189
                public Object next() {
190
                        checkModified();
191
                        if (!nextChecked){
192
                                hasNext();
193
                        }
194
                        if (feature == null)
195
                                throw new NoSuchElementException();
196
                        nextChecked=false;
197
                        return feature;
198
                }
199

    
200
                private IFeature nextFeature() {
201
                        IFeature feature=null;
202
                        try {
203
                                if(rs.next()){
204
                                        feature=this.store.createFeatureFromResulset(this.rs, featureType);
205
                                }
206
                        } catch (java.sql.SQLException e) {
207
                                throw new RuntimeException(
208
                                                new ReadException(this.store.getName(),e)
209
                                        );
210
                        } catch (ReadException e) {
211
                                throw new RuntimeException(e);
212
                        }
213
                        return feature;
214
                }
215

    
216
                protected void createResulset() throws ReadException{
217

    
218
                        this.store.open();
219

    
220
                        String mSql = PostgresqlStoreUtils.addLimitsToSQL(
221
                                        this.sql,
222
                                        this.fetchSize,
223
                                        this.page);
224
                        Connection conn = ((PostgresqlStore)this.store).getCurrentConnection();
225
                        try {
226
                                if (this.rs != null){
227
                                        this.rs.close();
228
//                                        this.rs.getStatement().close();
229
                                }
230

    
231
                                Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
232
//                                System.out.println(mSql);
233
                                this.rs = st.executeQuery(mSql);
234

    
235
                        } catch (java.sql.SQLException e) {
236
                                throw new SQLException(mSql,this.store.getName(),e);
237
                        }
238

    
239

    
240
                }
241

    
242

    
243
                public void remove() {
244
                        throw new UnsupportedOperationException();
245
                }
246

    
247
        }
248

    
249
        public void dispose() {
250
                this.store.deleteObserver(this);
251
                this.store = null;
252
                this.featureManager = null;
253
                this.featureType = null;
254
        }
255

    
256
}