root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / data / feature / db / jdbc / h2 / H2FeatureCollection.java @ 24491
History | View | Annotate | Download (4.66 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.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.data.feature.db.DBDataFeatureCollection; |
14 |
import org.gvsig.fmap.data.feature.db.DBFeatureType; |
15 |
import org.gvsig.fmap.data.feature.db.jdbc.AbstractJDBCIterator; |
16 |
import org.gvsig.fmap.data.feature.db.jdbc.SQLException; |
17 |
import org.gvsig.tools.exception.BaseException; |
18 |
|
19 |
public class H2FeatureCollection extends DBDataFeatureCollection { |
20 |
protected DBFeatureType featureType;
|
21 |
protected String filter; |
22 |
protected String totalFilter; |
23 |
protected H2Store store;
|
24 |
private String order; |
25 |
private int numReg=-1; |
26 |
private String sql; |
27 |
private String sqlCount; |
28 |
private String totalOrder; |
29 |
private int fetchSize = 5000; |
30 |
|
31 |
H2FeatureCollection(H2Store store,DBFeatureType type, String filter, String order) { |
32 |
this.store=store;
|
33 |
this.featureType=type;
|
34 |
this.filter=filter;
|
35 |
this.order=order;
|
36 |
this.calculateWhere();
|
37 |
this.calculateOrder();
|
38 |
|
39 |
if (store.isUseSqlSource()){
|
40 |
this.sql = store.getSqlSource();
|
41 |
this.sqlCount = null; |
42 |
} else {
|
43 |
this.sql = this.store.getSqlSelectPart(); |
44 |
this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID(); |
45 |
if (!isStringEmpty(this.totalFilter)){ |
46 |
this.sql= this.sql + " Where " + this.totalFilter; |
47 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
48 |
} |
49 |
if (!isStringEmpty(this.totalOrder)){ |
50 |
this.sql= this.sql + " Order by " + this.totalOrder; |
51 |
} |
52 |
} |
53 |
} |
54 |
|
55 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
56 |
this.store.open();
|
57 |
|
58 |
Connection conn = this.store.getConnection(); |
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 |
|
70 |
private void calculateWhere(){ |
71 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
72 |
this.totalFilter = this.filter; |
73 |
} else {
|
74 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
75 |
} |
76 |
} |
77 |
|
78 |
private void calculateOrder(){ |
79 |
if (isStringEmpty(this.store.getBaseOrder())){ |
80 |
this.totalOrder = this.order; |
81 |
} else {
|
82 |
this.totalOrder = this.store.getBaseOrder() + ", " +this.order; |
83 |
} |
84 |
|
85 |
} |
86 |
|
87 |
public int size() { |
88 |
checkModified(); |
89 |
try {
|
90 |
if (this.numReg < 0){ |
91 |
ResultSet r=null; |
92 |
try {
|
93 |
if (this.sqlCount != null){ |
94 |
r = this.getNewResulset(this.sqlCount); |
95 |
r.next(); |
96 |
numReg = r.getInt(1);
|
97 |
} else{
|
98 |
this.numReg = 0; |
99 |
r = this.getNewResulset(this.sql); |
100 |
while (r.next()){
|
101 |
this.numReg++;
|
102 |
} |
103 |
} |
104 |
} catch (java.sql.SQLException e) {
|
105 |
throw new ReadException(this.store.getName(),e); |
106 |
|
107 |
} finally{
|
108 |
try {
|
109 |
if (r != null) { |
110 |
r.close(); |
111 |
} |
112 |
} catch (java.sql.SQLException e) {
|
113 |
throw new ReadException(this.store.getName(),e); |
114 |
} |
115 |
} |
116 |
} |
117 |
return numReg;
|
118 |
} catch (BaseException e){
|
119 |
throw new RuntimeException(e); |
120 |
} |
121 |
|
122 |
} |
123 |
|
124 |
protected Iterator internalIterator(int index) { |
125 |
checkModified(); |
126 |
AbstractJDBCIterator dbfIter = null;
|
127 |
try {
|
128 |
dbfIter = new H2Iterator(this.store, this.featureType, this.sql, |
129 |
this.fetchSize, index + 1); |
130 |
} catch (ReadException e) {
|
131 |
throw new RuntimeException(e); |
132 |
} |
133 |
return dbfIter;
|
134 |
} |
135 |
|
136 |
public class H2Iterator extends AbstractH2DBIterator { |
137 |
|
138 |
public H2Iterator(H2Store store, DBFeatureType featureType, String sql, |
139 |
int fetchSize, int initialPosition) throws ReadException { |
140 |
super(store, featureType, sql, fetchSize, null, null, |
141 |
initialPosition); |
142 |
} |
143 |
|
144 |
protected void checkModified() { |
145 |
if (modified) {
|
146 |
throw new ConcurrentModificationException( |
147 |
"FeatureCollection modified");
|
148 |
} |
149 |
} |
150 |
|
151 |
protected Feature createFeatureFromTheResulset() throws ReadException { |
152 |
return ((H2Store) this.store).createFeatureFromResulset(this.rs, |
153 |
this.featureType);
|
154 |
} |
155 |
|
156 |
} |
157 |
|
158 |
public void dispose() { |
159 |
this.store.deleteObserver(this); |
160 |
this.store=null; |
161 |
this.featureType=null; |
162 |
|
163 |
} |
164 |
|
165 |
public boolean isFromStore(DataStore store) { |
166 |
return this.store.equals(store); |
167 |
} |
168 |
|
169 |
public FeatureType getFeatureType() {
|
170 |
return this.featureType; |
171 |
} |
172 |
} |