svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2FeatureCollection.java @ 20973
History | View | Annotate | Download (5.23 KB)
1 |
package org.gvsig.data.datastores.vectorial.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 |
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.IFeature; |
15 |
import org.gvsig.exceptions.BaseException; |
16 |
|
17 |
public class H2FeatureCollection extends DBDataFeatureCollection { |
18 |
protected DBFeatureType featureType;
|
19 |
protected String filter; |
20 |
protected String totalFilter; |
21 |
protected H2Store store;
|
22 |
private String order; |
23 |
private int numReg=-1; |
24 |
private String sql; |
25 |
private String sqlCount; |
26 |
private String totalOrder; |
27 |
|
28 |
H2FeatureCollection(H2Store store,DBFeatureType type, String filter, String order) { |
29 |
this.store=store;
|
30 |
this.featureType=type;
|
31 |
this.filter=filter;
|
32 |
this.order=order;
|
33 |
this.calculateWhere();
|
34 |
this.calculateOrder();
|
35 |
|
36 |
if (store.isUseSqlSource()){
|
37 |
this.sql = store.getSqlSource();
|
38 |
this.sqlCount = null; |
39 |
} else {
|
40 |
this.sql = this.store.getSqlSelectPart(); |
41 |
this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID(); |
42 |
if (!isStringEmpty(this.totalFilter)){ |
43 |
this.sql= this.sql + " Where " + this.totalFilter; |
44 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
45 |
} |
46 |
if (!isStringEmpty(this.totalOrder)){ |
47 |
this.sql= this.sql + " Order by " + this.totalOrder; |
48 |
} |
49 |
} |
50 |
} |
51 |
|
52 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
53 |
this.store.open();
|
54 |
|
55 |
Connection conn = this.store.getConnection(); |
56 |
try {
|
57 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
58 |
return st.executeQuery(aSql);
|
59 |
|
60 |
} catch (java.sql.SQLException e) {
|
61 |
throw new SQLException(aSql,this.store.getName(),e); |
62 |
} |
63 |
|
64 |
} |
65 |
|
66 |
|
67 |
private void calculateWhere(){ |
68 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
69 |
this.totalFilter = this.filter; |
70 |
} else {
|
71 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
72 |
} |
73 |
} |
74 |
|
75 |
private void calculateOrder(){ |
76 |
if (isStringEmpty(this.store.getBaseOrder())){ |
77 |
this.totalOrder = this.order; |
78 |
} else {
|
79 |
this.totalOrder = this.store.getBaseOrder() + ", " +this.order; |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
public int size() { |
85 |
checkModified(); |
86 |
try {
|
87 |
if (this.numReg < 0){ |
88 |
ResultSet r=null; |
89 |
try {
|
90 |
if (this.sqlCount != null){ |
91 |
r = this.getNewResulset(this.sqlCount); |
92 |
r.next(); |
93 |
numReg = r.getInt(1);
|
94 |
} else{
|
95 |
this.numReg = 0; |
96 |
r = this.getNewResulset(this.sql); |
97 |
while (r.next()){
|
98 |
this.numReg++;
|
99 |
} |
100 |
} |
101 |
} catch (java.sql.SQLException e) {
|
102 |
throw new ReadException(this.store.getName(),e); |
103 |
|
104 |
} finally{
|
105 |
try {
|
106 |
if (r != null) |
107 |
r.close(); |
108 |
} catch (java.sql.SQLException e) {
|
109 |
throw new ReadException(this.store.getName(),e); |
110 |
} |
111 |
} |
112 |
} |
113 |
return numReg;
|
114 |
} catch (BaseException e){
|
115 |
throw new RuntimeException(e); |
116 |
} |
117 |
|
118 |
} |
119 |
|
120 |
public Iterator iterator() { |
121 |
checkModified(); |
122 |
ResultSet r;
|
123 |
try {
|
124 |
r = this.getNewResulset(this.sql); |
125 |
} catch (ReadException e) {
|
126 |
throw new RuntimeException(e); |
127 |
} |
128 |
H2Iterator dbfIter=new H2Iterator(this.store,this.featureType,r); |
129 |
return dbfIter;
|
130 |
} |
131 |
|
132 |
protected class H2Iterator implements Iterator{ |
133 |
private ResultSet rs; |
134 |
private H2Store store;
|
135 |
private DBFeatureType featureType;
|
136 |
|
137 |
public H2Iterator(H2Store store,DBFeatureType featureType ,ResultSet rs){ |
138 |
this.rs = rs;
|
139 |
this.store = store;
|
140 |
this.featureType = featureType;
|
141 |
} |
142 |
|
143 |
protected void checkModified(){ |
144 |
if (modified)
|
145 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
146 |
} |
147 |
|
148 |
public boolean hasNext(){ |
149 |
checkModified(); |
150 |
try {
|
151 |
if (rs.isLast()){
|
152 |
return false; |
153 |
} else {
|
154 |
return true; |
155 |
} |
156 |
} catch (java.sql.SQLException e) {
|
157 |
throw new RuntimeException( |
158 |
new ReadException(this.store.getName(),e) |
159 |
); |
160 |
} |
161 |
} |
162 |
|
163 |
public Object next() { |
164 |
checkModified(); |
165 |
if (!hasNext())
|
166 |
throw new NoSuchElementException(); |
167 |
return nextFeature();
|
168 |
} |
169 |
|
170 |
private IFeature nextFeature() {
|
171 |
try {
|
172 |
IFeature feature=null;
|
173 |
try {
|
174 |
if(rs.next()){
|
175 |
feature = this.store.createFeatureFromResulset(this.rs, this.featureType); |
176 |
} else {
|
177 |
rs.close(); |
178 |
rs.getStatement().close(); |
179 |
throw new NoSuchElementException(); |
180 |
} |
181 |
if (rs.isAfterLast()){
|
182 |
rs.close(); |
183 |
rs.getStatement().close(); |
184 |
} |
185 |
|
186 |
|
187 |
} catch (java.sql.SQLException e) {
|
188 |
throw new RuntimeException( |
189 |
new ReadException(this.store.getName(),e) |
190 |
); |
191 |
} |
192 |
return feature;
|
193 |
} catch (BaseException e){
|
194 |
throw new RuntimeException( |
195 |
new ReadException(this.store.getName(),e) |
196 |
); |
197 |
|
198 |
} |
199 |
} |
200 |
|
201 |
public void remove() { |
202 |
throw new UnsupportedOperationException(); |
203 |
} |
204 |
|
205 |
} |
206 |
|
207 |
public void dispose() { |
208 |
this.store.deleteObserver(this); |
209 |
this.store=null; |
210 |
this.featureType=null; |
211 |
|
212 |
} |
213 |
|
214 |
} |