svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostgresqlFeatureCollectionEditingFiltered.java @ 20973
History | View | Annotate | Download (8.21 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresql; |
---|---|
2 |
|
3 |
|
4 |
import java.sql.Connection; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.Statement; |
7 |
import java.util.Iterator; |
8 |
import java.util.NoSuchElementException; |
9 |
|
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.data.vectorial.expressionevaluator.FeatureFilter; |
18 |
import org.gvsig.exceptions.BaseException; |
19 |
|
20 |
public class PostgresqlFeatureCollectionEditingFiltered extends DBDataFeatureCollection { |
21 |
protected DBFeatureType featureType;
|
22 |
protected String totalFilter; |
23 |
protected String filter; |
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 |
private FeatureFilter parser;
|
32 |
|
33 |
|
34 |
PostgresqlFeatureCollectionEditingFiltered(FeatureManager fm,PostgresqlStore store,IFeatureType type,String filter) {
|
35 |
this.featureManager=fm;
|
36 |
this.store=store;
|
37 |
this.featureType=(DBFeatureType)type;
|
38 |
|
39 |
this.filter = filter;
|
40 |
|
41 |
this.calculateWhere();
|
42 |
this.totalOrder = store.getBaseOrder();
|
43 |
|
44 |
this.sql = this.store.getSqlSelectPart(); |
45 |
this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID(); |
46 |
if (!isStringEmpty(this.totalFilter)){ |
47 |
this.sql= this.sql + " Where " + this.totalFilter; |
48 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
49 |
} |
50 |
if (!isStringEmpty(this.totalOrder)){ |
51 |
this.sql= this.sql + " Order by " + this.totalOrder; |
52 |
} |
53 |
|
54 |
if (this.filter!=null) |
55 |
parser = new FeatureFilter(filter,this.featureType); |
56 |
|
57 |
} |
58 |
|
59 |
private void calculateWhere(){ |
60 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
61 |
this.totalFilter = this.filter; |
62 |
} else {
|
63 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
64 |
} |
65 |
} |
66 |
|
67 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
68 |
this.store.open();
|
69 |
|
70 |
Connection con = this.store.getConnection(); |
71 |
|
72 |
try {
|
73 |
Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
74 |
return st.executeQuery(aSql);
|
75 |
|
76 |
} catch (java.sql.SQLException e) {
|
77 |
throw new SQLException(aSql,this.store.getName(),e); |
78 |
} |
79 |
|
80 |
} |
81 |
|
82 |
public int size() { |
83 |
checkModified(); |
84 |
if (this.numReg < 0){ |
85 |
this.numReg =0; |
86 |
try{
|
87 |
Iterator iter = this.iterator(); |
88 |
while (true){ |
89 |
iter.next(); |
90 |
this.numReg++;
|
91 |
} |
92 |
} catch (NoSuchElementException e){ |
93 |
//Normal condition exit
|
94 |
} |
95 |
} |
96 |
return this.numReg; |
97 |
|
98 |
} |
99 |
|
100 |
|
101 |
public Iterator iterator() { |
102 |
checkModified(); |
103 |
try{
|
104 |
PostgresIterator dbIter=new PostgresIterator(this.store,this.featureType,this.sql,this.fetchSize,this.featureManager); |
105 |
return dbIter;
|
106 |
} catch (BaseException e){
|
107 |
throw new RuntimeException(e); |
108 |
} |
109 |
} |
110 |
|
111 |
protected class PostgresIterator implements Iterator{ |
112 |
private Iterator dbIter; |
113 |
private Iterator mIter; |
114 |
public PostgresIterator(PostgresqlStore store,DBFeatureType featureType,String sql, int fetchSize,FeatureManager featureManager) throws ReadException{ |
115 |
this.dbIter = new IteratorDB(store,featureType,sql,fetchSize,featureManager); |
116 |
this.mIter = new IteratorMemory(featureType,featureManager); |
117 |
} |
118 |
public boolean hasNext() { |
119 |
return dbIter.hasNext() || mIter.hasNext();
|
120 |
} |
121 |
public Object next() { |
122 |
if (dbIter.hasNext()){
|
123 |
return dbIter.next();
|
124 |
} |
125 |
return mIter.next();
|
126 |
} |
127 |
public void remove() { |
128 |
throw new UnsupportedOperationException(); |
129 |
} |
130 |
|
131 |
|
132 |
|
133 |
} |
134 |
|
135 |
protected class IteratorDB implements Iterator{ |
136 |
private boolean nextChecked=false; |
137 |
private IFeature feature;
|
138 |
private ResultSet rs; |
139 |
private PostgresqlStore store;
|
140 |
private DBFeatureType featureType;
|
141 |
private boolean rsEOF=false; |
142 |
private FeatureManager featureManager;
|
143 |
private int fetchSize; |
144 |
private String sql; |
145 |
private int page; |
146 |
private int index; |
147 |
|
148 |
|
149 |
public IteratorDB(PostgresqlStore store, DBFeatureType featureType,String sql,int fetchSize, FeatureManager featureManager) throws ReadException{ |
150 |
this.store = store;
|
151 |
this.featureType = featureType;
|
152 |
this.featureManager= featureManager;
|
153 |
this.fetchSize = fetchSize;
|
154 |
this.sql = sql;
|
155 |
this.page = 0; |
156 |
this.index = 0; |
157 |
this.createResulset();
|
158 |
|
159 |
} |
160 |
|
161 |
public boolean hasNext(){ |
162 |
checkModified(); |
163 |
|
164 |
|
165 |
if (nextChecked){
|
166 |
return this.feature != null; |
167 |
} |
168 |
IFeature feature=null;
|
169 |
nextChecked=true;
|
170 |
while (true){ |
171 |
if (!rsEOF){
|
172 |
try {
|
173 |
if (rs.isLast()){
|
174 |
if (this.index == this.fetchSize){ |
175 |
this.index = 0; |
176 |
this.page++;
|
177 |
this.createResulset();
|
178 |
if (rs.isLast()){
|
179 |
rsEOF = true;
|
180 |
rs.close(); |
181 |
continue;
|
182 |
} |
183 |
} else{
|
184 |
rsEOF = true;
|
185 |
rs.close(); |
186 |
continue;
|
187 |
} |
188 |
} else {
|
189 |
feature=nextFeature(); |
190 |
if(featureManager.isDeleted(feature))
|
191 |
continue;
|
192 |
this.feature=feature;
|
193 |
return true; |
194 |
|
195 |
} |
196 |
} catch (java.sql.SQLException e) {
|
197 |
throw new RuntimeException( |
198 |
new ReadException(this.store.getName(),e) |
199 |
); |
200 |
} catch (ReadException e) {
|
201 |
throw new RuntimeException(e); |
202 |
} |
203 |
|
204 |
}else {
|
205 |
return false; |
206 |
|
207 |
} |
208 |
|
209 |
|
210 |
} |
211 |
} |
212 |
|
213 |
public Object next() { |
214 |
checkModified(); |
215 |
if (!nextChecked){
|
216 |
hasNext(); |
217 |
} |
218 |
if (this.feature == null) |
219 |
throw new NoSuchElementException(); |
220 |
nextChecked=false;
|
221 |
IFeature feature = this.feature;
|
222 |
this.feature = null; |
223 |
return feature;
|
224 |
} |
225 |
|
226 |
private IFeature nextFeature() {
|
227 |
IFeature feature=null;
|
228 |
try {
|
229 |
if(rs.next()){
|
230 |
feature=this.store.createFeatureFromResulset(this.rs, featureType); |
231 |
} |
232 |
} catch (java.sql.SQLException e) {
|
233 |
throw new RuntimeException( |
234 |
new ReadException(this.store.getName(),e) |
235 |
); |
236 |
} catch (ReadException e) {
|
237 |
throw new RuntimeException(e); |
238 |
} |
239 |
return feature;
|
240 |
} |
241 |
|
242 |
public void remove() { |
243 |
throw new UnsupportedOperationException(); |
244 |
} |
245 |
|
246 |
|
247 |
protected void createResulset() throws ReadException{ |
248 |
|
249 |
this.store.open();
|
250 |
|
251 |
String mSql = PostgresqlStoreUtils.addLimitsToSQL(
|
252 |
this.sql,
|
253 |
this.fetchSize,
|
254 |
this.page);
|
255 |
Connection conn = ((PostgresqlStore)this.store).getConnection(); |
256 |
try {
|
257 |
if (this.rs != null){ |
258 |
this.rs.close();
|
259 |
} |
260 |
|
261 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
262 |
this.rs = st.executeQuery(mSql);
|
263 |
|
264 |
} catch (java.sql.SQLException e) {
|
265 |
throw new SQLException(mSql,this.store.getName(),e); |
266 |
} |
267 |
|
268 |
|
269 |
} |
270 |
|
271 |
} |
272 |
|
273 |
|
274 |
protected class IteratorMemory implements Iterator{ |
275 |
protected long position=0; |
276 |
private boolean nextChecked=false; |
277 |
private IFeature feature;
|
278 |
private FeatureManager featureManager;
|
279 |
|
280 |
public IteratorMemory(DBFeatureType featureType,FeatureManager featureManager){
|
281 |
this.featureManager= featureManager;
|
282 |
position=0;
|
283 |
} |
284 |
|
285 |
public boolean hasNext(){ |
286 |
checkModified(); |
287 |
|
288 |
|
289 |
if (nextChecked){
|
290 |
return this.feature != null; |
291 |
} |
292 |
IFeature feature=null;
|
293 |
nextChecked=true;
|
294 |
while (true){ |
295 |
if (position<featureManager.getNum()){
|
296 |
try {
|
297 |
feature=featureManager.getFeature((int)position,store);
|
298 |
} catch (ReadException e) {
|
299 |
throw new RuntimeException(e); |
300 |
} |
301 |
position++; |
302 |
|
303 |
}else{
|
304 |
this.feature = null; |
305 |
return false; |
306 |
} |
307 |
|
308 |
if(featureManager.isDeleted(feature))
|
309 |
continue;
|
310 |
|
311 |
if (filter == null) { |
312 |
this.feature=feature;
|
313 |
return true; |
314 |
|
315 |
} else {
|
316 |
try {
|
317 |
if (parser.match(feature)){
|
318 |
this.feature=feature;
|
319 |
return true; |
320 |
}else{
|
321 |
continue;
|
322 |
} |
323 |
} catch (Exception e) { |
324 |
throw new RuntimeException(e); |
325 |
} |
326 |
} |
327 |
} |
328 |
} |
329 |
|
330 |
public Object next() { |
331 |
checkModified(); |
332 |
if (!nextChecked){
|
333 |
hasNext(); |
334 |
} |
335 |
if (this.feature == null) |
336 |
throw new NoSuchElementException(); |
337 |
nextChecked=false;
|
338 |
IFeature feature = this.feature;
|
339 |
this.feature = null; |
340 |
return feature;
|
341 |
} |
342 |
|
343 |
public void remove() { |
344 |
throw new UnsupportedOperationException(); |
345 |
} |
346 |
} |
347 |
|
348 |
|
349 |
public void dispose() { |
350 |
this.store.deleteObserver(this); |
351 |
this.store = null; |
352 |
this.featureManager = null; |
353 |
this.featureType = null; |
354 |
this.parser= null; |
355 |
|
356 |
} |
357 |
|
358 |
} |