Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / data / feature / db / jdbc / postgresql / PostgresqlFeatureCollectionEditing.java @ 24491

History | View | Annotate | Download (3.86 KB)

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

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

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

    
29

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

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

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

    
48
        }
49

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

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

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

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

    
63
        }
64

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

    
90
        }
91

    
92
    protected Iterator internalIterator(int index) {
93
        checkModified();
94
        try {
95
            ResultSet r = null;
96
            r = this.getNewResulset(this.sql);
97
            PostgresIterator dbIter = new PostgresIterator(this.store,
98
                    this.featureType, this.sql, this.fetchSize,
99
                    this.featureManager, index + 1);
100
            return dbIter;
101
        } catch (BaseException e) {
102
            throw new RuntimeException(e);
103
        }
104
    }
105
        
106
        protected class PostgresIterator extends AbstractPostgresqlDBIterator {
107

    
108
                public PostgresIterator(PostgresqlStore store,
109
                                DBFeatureType featureType, String sql, int fetchSize,
110
                                FeatureManager featureManager, int initialPosition)
111
                throws ReadException {
112
            super(store, featureType, sql, fetchSize, featureManager, null,
113
                    initialPosition);
114
                }
115

    
116
                protected void checkModified() {
117
                        if (modified) {
118
                                throw new ConcurrentModificationException(
119
                                                "FeatureCollection modified");
120
                        }
121
                }
122
        }
123

    
124
        public void dispose() {
125
                this.store.deleteObserver(this);
126
                this.store = null;
127
                this.featureManager = null;
128
                this.featureType = null;
129
        }
130

    
131

    
132
        public boolean isFromStore(DataStore store) {
133
                return this.store.equals(store);
134
        }
135

    
136
        public FeatureType getFeatureType() {
137
                return this.featureType;
138
        }
139
}