svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostgresqlFeatureCollection.java @ 20973
History | View | Annotate | Download (6.61 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.ReadException; |
10 |
import org.gvsig.data.datastores.vectorial.db.DBDataFeatureCollection; |
11 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
12 |
import org.gvsig.data.datastores.vectorial.db.DBStore; |
13 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore; |
14 |
import org.gvsig.data.datastores.vectorial.db.jdbc.SQLException; |
15 |
import org.gvsig.data.vectorial.IFeature; |
16 |
import org.gvsig.data.vectorial.IFeatureType; |
17 |
import org.gvsig.data.vectorial.IsNotFeatureSettingException; |
18 |
import org.gvsig.exceptions.BaseException; |
19 |
|
20 |
public class PostgresqlFeatureCollection extends DBDataFeatureCollection { |
21 |
protected DBFeatureType featureType;
|
22 |
protected String filter; |
23 |
protected String totalFilter; |
24 |
protected JDBCStore store;
|
25 |
private String order; |
26 |
private int numReg=-1; |
27 |
private String sql; |
28 |
private String sqlCount; |
29 |
private String totalOrder; |
30 |
private int fetchSize=5000; |
31 |
|
32 |
PostgresqlFeatureCollection(DBStore store,IFeatureType type, String filter, String order) { |
33 |
this.store=(JDBCStore)store;
|
34 |
this.featureType=(DBFeatureType)type;
|
35 |
this.filter=filter;
|
36 |
this.order=order;
|
37 |
this.calculateWhere();
|
38 |
this.calculateOrder();
|
39 |
|
40 |
if (this.store.isUseSqlSource()){ |
41 |
this.sql = this.store.getSqlSource(); |
42 |
this.sqlCount = null; |
43 |
} else {
|
44 |
this.sql = this.store.getSqlSelectPart(); |
45 |
|
46 |
this.sqlCount = "Select count(*) From "+ ((PostgresqlStoreParameters)this.store.getParameters()).tableID(); |
47 |
if (!isStringEmpty(this.totalFilter)){ |
48 |
this.sql= this.sql + " Where " + this.totalFilter; |
49 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
50 |
} |
51 |
if (!isStringEmpty(this.totalOrder)){ |
52 |
this.sql= this.sql + " Order by " + this.totalOrder; |
53 |
} |
54 |
} |
55 |
} |
56 |
|
57 |
private ResultSet getNewResulset(String aSql) throws ReadException, IsNotFeatureSettingException{ |
58 |
this.store.open();
|
59 |
|
60 |
Connection conn = ((PostgresqlStore)this.store).getConnection(); |
61 |
try {
|
62 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
63 |
return st.executeQuery(aSql);
|
64 |
|
65 |
} catch (java.sql.SQLException e) {
|
66 |
throw new SQLException(aSql,this.store.getName(),e); |
67 |
} |
68 |
|
69 |
} |
70 |
|
71 |
|
72 |
private void calculateWhere(){ |
73 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
74 |
this.totalFilter = this.filter; |
75 |
} else {
|
76 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
77 |
} |
78 |
} |
79 |
|
80 |
private void calculateOrder(){ |
81 |
if (isStringEmpty(this.store.getBaseOrder())){ |
82 |
this.totalOrder = this.order; |
83 |
} else {
|
84 |
this.totalOrder = this.store.getBaseOrder() + ", " +this.order; |
85 |
} |
86 |
|
87 |
} |
88 |
|
89 |
public int size() { |
90 |
checkModified(); |
91 |
try {
|
92 |
if (this.numReg < 0){ |
93 |
ResultSet r=null; |
94 |
try {
|
95 |
if (this.sqlCount != null){ |
96 |
r = this.getNewResulset(this.sqlCount); |
97 |
r.next(); |
98 |
numReg = r.getInt(1);
|
99 |
} else{
|
100 |
this.numReg = 0; |
101 |
r = this.getNewResulset(this.sql); |
102 |
while (r.next()){
|
103 |
this.numReg++;
|
104 |
} |
105 |
} |
106 |
} catch (java.sql.SQLException e) {
|
107 |
throw new ReadException(this.store.getName(),e); |
108 |
|
109 |
} finally{
|
110 |
try {
|
111 |
if (r != null) |
112 |
r.close(); |
113 |
} catch (java.sql.SQLException e) {
|
114 |
throw new ReadException(this.store.getName(),e); |
115 |
} |
116 |
} |
117 |
} |
118 |
return numReg;
|
119 |
} catch (BaseException e){
|
120 |
throw new RuntimeException(e); |
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
public Iterator iterator() { |
126 |
checkModified(); |
127 |
PostgresqlIterator dbIter; |
128 |
try {
|
129 |
dbIter=new PostgresqlIterator((PostgresqlStore)this.store,this.featureType,this.sql,this.fetchSize); |
130 |
} catch (ReadException e) {
|
131 |
throw new RuntimeException(e); |
132 |
} |
133 |
|
134 |
return dbIter;
|
135 |
} |
136 |
|
137 |
protected class PostgresqlIterator implements Iterator{ |
138 |
private ResultSet rs; |
139 |
private PostgresqlStore store;
|
140 |
private DBFeatureType featureType;
|
141 |
private String sql; |
142 |
private int fetchSize; |
143 |
private int page; |
144 |
private int index; |
145 |
|
146 |
public PostgresqlIterator(PostgresqlStore store,DBFeatureType featureType ,String sql,int fetchSize) throws ReadException{ |
147 |
this.sql = sql;
|
148 |
this.store = store;
|
149 |
this.featureType = featureType;
|
150 |
this.fetchSize= fetchSize;
|
151 |
this.page = 0; |
152 |
this.index = 0; |
153 |
this.createResulset();
|
154 |
} |
155 |
|
156 |
protected void createResulset() throws ReadException{ |
157 |
|
158 |
this.store.open();
|
159 |
|
160 |
String mSql = PostgresqlStoreUtils.addLimitsToSQL(
|
161 |
this.sql,
|
162 |
this.fetchSize,
|
163 |
this.page);
|
164 |
Connection conn = this.store.getConnection(); |
165 |
try {
|
166 |
if (this.rs != null){ |
167 |
this.rs.close();
|
168 |
// this.rs.getStatement().close();
|
169 |
} |
170 |
|
171 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
172 |
// System.out.println(mSql);
|
173 |
this.rs = st.executeQuery(mSql);
|
174 |
|
175 |
} catch (java.sql.SQLException e) {
|
176 |
throw new SQLException(mSql,this.store.getName(),e); |
177 |
} |
178 |
|
179 |
|
180 |
} |
181 |
|
182 |
public boolean hasNext(){ |
183 |
checkModified(); |
184 |
try {
|
185 |
if (rs.isLast()){
|
186 |
if (this.index == this.fetchSize){ |
187 |
this.index = 0; |
188 |
this.page++;
|
189 |
this.createResulset();
|
190 |
return this.hasNext(); |
191 |
} else{
|
192 |
return false; |
193 |
} |
194 |
|
195 |
} else {
|
196 |
return true; |
197 |
} |
198 |
} catch (java.sql.SQLException e) {
|
199 |
throw new RuntimeException( |
200 |
new ReadException(this.store.getName(),e) |
201 |
); |
202 |
} catch (ReadException e1){
|
203 |
throw new RuntimeException( |
204 |
new ReadException(this.store.getName(),e1) |
205 |
); |
206 |
} |
207 |
} |
208 |
|
209 |
public Object next() { |
210 |
checkModified(); |
211 |
if (!hasNext())
|
212 |
throw new NoSuchElementException(); |
213 |
return nextFeature();
|
214 |
} |
215 |
|
216 |
private IFeature nextFeature() {
|
217 |
try {
|
218 |
IFeature feature=null;
|
219 |
try {
|
220 |
if(rs.next()){
|
221 |
feature = this.store.createFeatureFromResulset(this.rs, this.featureType); |
222 |
} else {
|
223 |
rs.close(); |
224 |
// rs.getStatement().close();
|
225 |
throw new NoSuchElementException(); |
226 |
} |
227 |
if (rs.isAfterLast()){
|
228 |
rs.close(); |
229 |
// rs.getStatement().close();
|
230 |
} |
231 |
|
232 |
|
233 |
} catch (java.sql.SQLException e) {
|
234 |
throw new RuntimeException( |
235 |
new ReadException(this.store.getName(),e) |
236 |
); |
237 |
} |
238 |
this.index++;
|
239 |
return feature;
|
240 |
} catch (BaseException e){
|
241 |
throw new RuntimeException( |
242 |
new ReadException(this.store.getName(),e) |
243 |
); |
244 |
|
245 |
} |
246 |
} |
247 |
|
248 |
public void remove() { |
249 |
throw new UnsupportedOperationException(); |
250 |
} |
251 |
|
252 |
} |
253 |
|
254 |
public void dispose() { |
255 |
this.store.deleteObserver(this); |
256 |
this.store=null; |
257 |
this.featureType=null; |
258 |
|
259 |
} |
260 |
|
261 |
} |