Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostgresqlFeatureCollectionEditing.java @ 20973

History | View | Annotate | Download (6.36 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.InitializeException;
10
import org.gvsig.data.ReadException;
11
import org.gvsig.data.datastores.vectorial.db.DBDataFeatureCollection;
12
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
13
import org.gvsig.data.datastores.vectorial.db.jdbc.SQLException;
14
import org.gvsig.data.vectorial.FeatureManager;
15
import org.gvsig.data.vectorial.IFeature;
16
import org.gvsig.data.vectorial.IFeatureType;
17
import org.gvsig.exceptions.BaseException;
18

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

    
30

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

    
36
                this.totalFilter =store.getBaseWhereClause();
37
                this.totalOrder = store.getBaseOrder();
38

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

    
49
        }
50

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

    
54
                Connection conn = this.store.getConnection();
55

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

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

    
64
        }
65

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

    
91
        }
92

    
93

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

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

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

    
129
                public boolean hasNext(){
130
                        checkModified();
131

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

    
166

    
167
                                }else {
168
                                        if (position<featureManager.getNum()){
169
                                                try {
170
                                                        feature=featureManager.getFeature((int)position,store);
171
                                                } catch (ReadException e) {
172
                                                        throw new RuntimeException(e);
173
                                                }
174
                                        }else{
175
                                                this.feature = null;
176
                                                return false;
177
                                        }
178
                                }
179

    
180

    
181
                                IFeature tmp;
182
                                try {
183
                                        tmp = featureManager.getFeature(feature.getID(), store);
184
                                } catch (InitializeException e) {
185
                                        throw new RuntimeException(e);
186
                                }
187
                                if (tmp != null){
188
                                        feature = tmp;
189
                                }else if(featureManager.isDeleted(feature))
190
                                        continue;
191
                                position++;
192
                                this.feature=feature;
193
                                return true;
194

    
195
                        }
196
                }
197

    
198
                public Object next() {
199
                        checkModified();
200
                        if (!nextChecked){
201
                                hasNext();
202
                        }
203
                        if (feature == null)
204
                                throw new NoSuchElementException();
205
                        nextChecked=false;
206
                        return feature;
207
                }
208

    
209
                private IFeature nextFeature() {
210
                        IFeature feature=null;
211
                        try {
212
                                if(rs.next()){
213
                                        feature=this.store.createFeatureFromResulset(this.rs, featureType);
214
                                }
215
                        } catch (java.sql.SQLException e) {
216
                                throw new RuntimeException(
217
                                                new ReadException(this.store.getName(),e)
218
                                        );
219
                        } catch (ReadException e) {
220
                                throw new RuntimeException(e);
221
                        }
222
                        return feature;
223
                }
224

    
225
                protected void createResulset() throws ReadException{
226

    
227
                        this.store.open();
228

    
229
                        String mSql = PostgresqlStoreUtils.addLimitsToSQL(
230
                                        this.sql,
231
                                        this.fetchSize,
232
                                        this.page);
233
                        Connection conn = ((PostgresqlStore)this.store).getConnection();
234
                        try {
235
                                if (this.rs != null){
236
                                        this.rs.close();
237
//                                        this.rs.getStatement().close();
238
                                }
239

    
240
                                Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
241
//                                System.out.println(mSql);
242
                                this.rs = st.executeQuery(mSql);
243

    
244
                        } catch (java.sql.SQLException e) {
245
                                throw new SQLException(mSql,this.store.getName(),e);
246
                        }
247

    
248

    
249
                }
250

    
251

    
252
                public void remove() {
253
                        throw new UnsupportedOperationException();
254
                }
255

    
256
        }
257

    
258
        public void dispose() {
259
                this.store.deleteObserver(this);
260
                this.store = null;
261
                this.featureManager = null;
262
                this.featureType = null;
263
        }
264

    
265
}