Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / h2 / H2FeatureCollectionEditing.java @ 23680

History | View | Annotate | Download (3.99 KB)

1
package org.gvsig.fmap.data.feature.db.jdbc.h2;
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

    
9
import org.gvsig.fmap.data.DataStore;
10
import org.gvsig.fmap.data.ReadException;
11
import org.gvsig.fmap.data.feature.Feature;
12
import org.gvsig.fmap.data.feature.FeatureManager;
13
import org.gvsig.fmap.data.feature.FeatureType;
14
import org.gvsig.fmap.data.feature.db.DBDataFeatureCollection;
15
import org.gvsig.fmap.data.feature.db.DBFeatureType;
16
import org.gvsig.fmap.data.feature.db.jdbc.SQLException;
17
import org.gvsig.tools.exception.BaseException;
18

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

    
30

    
31
        H2FeatureCollectionEditing(FeatureManager fm,H2Store store,FeatureType 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 "+ ((H2StoreParameters)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
//                if (this.filter!=null)
50
//                        parser = new FeatureFilterParser(filter,this.featureType);
51

    
52
        }
53

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

    
57
                Connection conn = this.store.getConnection();
58

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

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

    
67
        }
68

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

    
94
        }
95

    
96

    
97
        protected Iterator internalIterator(int index) {
98
        checkModified();
99
        try {
100
            H2Iterator dbfIter = new H2Iterator(this.store, this.featureType,
101
                    this.sql, this.fetchSize, this.featureManager, index + 1);
102
            return dbfIter;
103
        } catch (BaseException e) {
104
            throw new RuntimeException(e);
105
        }
106
        }
107
        
108
        protected class H2Iterator extends AbstractH2DBIterator {
109

    
110
                public H2Iterator(H2Store store, DBFeatureType featureType, String sql,
111
                int fetchSize, FeatureManager featureManager,
112
                int initialPosition) throws ReadException {
113
            super(store, featureType, sql, fetchSize, featureManager, null,
114
                    initialPosition);
115
        }
116

    
117
                protected Feature createFeatureFromTheResulset() throws ReadException {
118
                        return ((H2Store) this.store).createFeatureFromResulset(this.rs,
119
                                        this.featureType);
120
                }
121

    
122
                protected void checkModified() {
123
                        if (modified) {
124
                                throw new ConcurrentModificationException(
125
                                                "FeatureCollection modified");
126
                        }
127
                }
128
        }
129

    
130
        public void dispose() {
131
                this.store.deleteObserver(this);
132
                this.store = null;
133
                this.featureManager = null;
134
                this.featureType = null;
135
        }
136

    
137
        public boolean isFromStore(DataStore store) {
138
                return this.store.equals(store);
139
        }
140

    
141
        public FeatureType getFeatureType() {
142
                return this.featureType;
143
        }
144
}