root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / data / feature / db / jdbc / postgresql / PostgresqlFeatureCollection.java @ 24491
History | View | Annotate | Download (5.6 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.Feature; |
12 |
import org.gvsig.fmap.dal.feature.FeatureType; |
13 |
import org.gvsig.fmap.dal.feature.IsNotFeatureSettingException; |
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.DBStore; |
17 |
import org.gvsig.fmap.data.feature.db.jdbc.JDBCStore; |
18 |
import org.gvsig.fmap.data.feature.db.jdbc.SQLException; |
19 |
import org.gvsig.tools.exception.BaseException; |
20 |
|
21 |
public class PostgresqlFeatureCollection extends DBDataFeatureCollection { |
22 |
protected DBFeatureType featureType;
|
23 |
protected String filter; |
24 |
protected String totalFilter; |
25 |
protected JDBCStore store;
|
26 |
private String order; |
27 |
private int numReg=-1; |
28 |
private String sql; |
29 |
private String sqlCount; |
30 |
private String totalOrder; |
31 |
private int fetchSize=5000; |
32 |
|
33 |
PostgresqlFeatureCollection(DBStore store,FeatureType type, String filter, String order) { |
34 |
this.store=(JDBCStore)store;
|
35 |
this.featureType=(DBFeatureType)type;
|
36 |
this.filter=filter;
|
37 |
this.order=order;
|
38 |
this.calculateWhere();
|
39 |
this.calculateOrder();
|
40 |
|
41 |
if (this.store.isUseSqlSource()){ |
42 |
this.sql = this.store.getSqlSource(); |
43 |
this.sqlCount = null; |
44 |
} else {
|
45 |
this.sql = this.store.getSqlSelectPart(); |
46 |
|
47 |
this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID(); |
48 |
if (!isStringEmpty(this.totalFilter)){ |
49 |
this.sql= this.sql + " Where " + this.totalFilter; |
50 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
51 |
} |
52 |
if (!isStringEmpty(this.totalOrder)){ |
53 |
this.sql= this.sql + " Order by " + this.totalOrder; |
54 |
} |
55 |
} |
56 |
} |
57 |
|
58 |
private ResultSet getNewResulset(String aSql) throws ReadException, IsNotFeatureSettingException{ |
59 |
this.store.open();
|
60 |
|
61 |
Connection conn = ((PostgresqlStore)this.store).getConnection(); |
62 |
try {
|
63 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
64 |
return st.executeQuery(aSql);
|
65 |
|
66 |
} catch (java.sql.SQLException e) {
|
67 |
throw new SQLException(aSql,this.store.getName(),e); |
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
|
73 |
private void calculateWhere(){ |
74 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
75 |
this.totalFilter = this.filter; |
76 |
} else {
|
77 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
78 |
} |
79 |
} |
80 |
|
81 |
private void calculateOrder(){ |
82 |
if (isStringEmpty(this.store.getBaseOrder())){ |
83 |
this.totalOrder = this.order; |
84 |
} else {
|
85 |
this.totalOrder = this.store.getBaseOrder() + ", " +this.order; |
86 |
} |
87 |
|
88 |
} |
89 |
|
90 |
public int size() { |
91 |
checkModified(); |
92 |
try {
|
93 |
if (this.numReg < 0){ |
94 |
ResultSet r=null; |
95 |
try {
|
96 |
if (this.sqlCount != null){ |
97 |
r = this.getNewResulset(this.sqlCount); |
98 |
r.next(); |
99 |
numReg = r.getInt(1);
|
100 |
} else{
|
101 |
this.numReg = 0; |
102 |
r = this.getNewResulset(this.sql); |
103 |
while (r.next()){
|
104 |
this.numReg++;
|
105 |
} |
106 |
} |
107 |
} catch (java.sql.SQLException e) {
|
108 |
throw new ReadException(this.store.getName(),e); |
109 |
|
110 |
} finally{
|
111 |
try {
|
112 |
if (r != null) { |
113 |
r.close(); |
114 |
} |
115 |
} catch (java.sql.SQLException e) {
|
116 |
throw new ReadException(this.store.getName(),e); |
117 |
} |
118 |
} |
119 |
} |
120 |
return numReg;
|
121 |
} catch (BaseException e){
|
122 |
throw new RuntimeException(e); |
123 |
} |
124 |
|
125 |
} |
126 |
|
127 |
protected Iterator internalIterator(int index) { |
128 |
checkModified(); |
129 |
PostgresqlIterator dbIter; |
130 |
try {
|
131 |
dbIter = new PostgresqlIterator((PostgresqlStore) this.store, |
132 |
this.featureType, this.sql, this.fetchSize, index + 1); |
133 |
} catch (ReadException e) {
|
134 |
throw new RuntimeException(e); |
135 |
} |
136 |
|
137 |
return dbIter;
|
138 |
} |
139 |
|
140 |
protected class PostgresqlIterator extends AbstractPostgresqlDBIterator { |
141 |
public PostgresqlIterator(PostgresqlStore store,
|
142 |
DBFeatureType featureType, String sql, int fetchSize, |
143 |
int initialPosition) throws ReadException { |
144 |
super(store, featureType, sql, fetchSize, null, null, |
145 |
initialPosition); |
146 |
} |
147 |
|
148 |
protected void createResulset() throws ReadException{ |
149 |
|
150 |
((PostgresqlStore) this.store).open();
|
151 |
|
152 |
String mSql = PostgresqlStoreUtils.addLimitsToSQL(
|
153 |
this.sql,
|
154 |
this.fetchSize,
|
155 |
this.page);
|
156 |
Connection conn = ((PostgresqlStore) this.store).getConnection(); |
157 |
try {
|
158 |
if (this.rs != null){ |
159 |
this.rs.close();
|
160 |
// this.rs.getStatement().close();
|
161 |
} |
162 |
|
163 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
164 |
// System.out.println(mSql);
|
165 |
this.rs = st.executeQuery(mSql);
|
166 |
|
167 |
} catch (java.sql.SQLException e) {
|
168 |
throw new SQLException(mSql,this.store.getName(),e); |
169 |
} |
170 |
|
171 |
|
172 |
} |
173 |
|
174 |
protected Feature createFeatureFromTheResulset() throws ReadException { |
175 |
return ((PostgresqlStore) this.store).createFeatureFromResulset( |
176 |
this.rs, this.featureType); |
177 |
} |
178 |
|
179 |
protected void checkModified() { |
180 |
if (modified) {
|
181 |
throw new ConcurrentModificationException( |
182 |
"FeatureCollection modified");
|
183 |
} |
184 |
} |
185 |
} |
186 |
|
187 |
public void dispose() { |
188 |
this.store.deleteObserver(this); |
189 |
this.store=null; |
190 |
this.featureType=null; |
191 |
|
192 |
} |
193 |
|
194 |
|
195 |
public boolean isFromStore(DataStore store) { |
196 |
return this.store.equals(store); |
197 |
} |
198 |
|
199 |
public FeatureType getFeatureType() {
|
200 |
return this.featureType; |
201 |
} |
202 |
} |