svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2FeatureCollection.java @ 20058
History | View | Annotate | Download (5.56 KB)
1 | 19845 | vcaballero | package org.gvsig.data.datastores.vectorial.db.jdbc.h2; |
---|---|---|---|
2 | 19541 | vcaballero | |
3 | 19610 | jmvivo | import java.sql.Connection; |
4 | 19541 | vcaballero | import java.sql.ResultSet; |
5 | import java.sql.Statement; |
||
6 | 19906 | jmvivo | import java.util.ConcurrentModificationException; |
7 | 19541 | vcaballero | import java.util.Iterator; |
8 | import java.util.NoSuchElementException; |
||
9 | |||
10 | 19893 | jmvivo | import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
11 | import org.gvsig.data.datastores.vectorial.db.jdbc.AbstractJDBCDataFeatureCollection; |
||
12 | import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore; |
||
13 | 19845 | vcaballero | import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException; |
14 | 19610 | jmvivo | import org.gvsig.data.exception.ReadException; |
15 | 19541 | vcaballero | import org.gvsig.data.vectorial.AbstractFeatureCollection; |
16 | import org.gvsig.data.vectorial.IFeature; |
||
17 | import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
||
18 | import org.gvsig.data.vectorial.IFeatureType; |
||
19 | 19610 | jmvivo | import org.gvsig.exceptions.BaseException; |
20 | 19541 | vcaballero | |
21 | import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
||
22 | |||
23 | 19893 | jmvivo | public class H2FeatureCollection extends AbstractJDBCDataFeatureCollection { |
24 | 20058 | jmvivo | protected DBFeatureType featureType;
|
25 | 19541 | vcaballero | protected String filter; |
26 | 19610 | jmvivo | protected String totalFilter; |
27 | 19738 | vcaballero | protected H2Store store;
|
28 | 19610 | jmvivo | private String order; |
29 | private int numReg=-1; |
||
30 | private String sql; |
||
31 | private String sqlCount; |
||
32 | private String totalOrder; |
||
33 | 19541 | vcaballero | |
34 | 20058 | jmvivo | H2FeatureCollection(H2Store store,DBFeatureType type, String filter, String order) { |
35 | 19738 | vcaballero | this.store=store;
|
36 | 19965 | jmvivo | this.featureType=type;
|
37 | 19541 | vcaballero | this.filter=filter;
|
38 | 19610 | jmvivo | this.order=order;
|
39 | this.calculateWhere();
|
||
40 | this.calculateOrder();
|
||
41 | 19541 | vcaballero | |
42 | 19833 | jmvivo | if (store.isUseSqlSource()){
|
43 | this.sql = store.getSqlSource();
|
||
44 | this.sqlCount = null; |
||
45 | } else {
|
||
46 | this.sql = this.store.getSqlSelectPart(); |
||
47 | this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)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 | 19610 | jmvivo | } |
56 | 19541 | vcaballero | } |
57 | |||
58 | 19610 | jmvivo | private ResultSet getNewResulset(String aSql) throws ReadException{ |
59 | 19738 | vcaballero | this.store.open();
|
60 | 19610 | jmvivo | |
61 | 19893 | jmvivo | Connection conn = this.store.getCurrentConnection(); |
62 | 19610 | jmvivo | try {
|
63 | 19893 | jmvivo | Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
64 | 19975 | jmvivo | return st.executeQuery(aSql);
|
65 | 19610 | jmvivo | |
66 | } catch (java.sql.SQLException e) {
|
||
67 | 19906 | jmvivo | throw new SQLException(aSql,this.store.getName(),e); |
68 | 19610 | jmvivo | } |
69 | |||
70 | } |
||
71 | |||
72 | |||
73 | private void calculateWhere(){ |
||
74 | 19738 | vcaballero | if (isStringEmpty(this.store.getBaseWhereClause())){ |
75 | 19610 | jmvivo | this.totalFilter = this.filter; |
76 | } else {
|
||
77 | 19738 | vcaballero | this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
78 | 19610 | jmvivo | } |
79 | } |
||
80 | |||
81 | private void calculateOrder(){ |
||
82 | 19738 | vcaballero | if (isStringEmpty(this.store.getBaseOrder())){ |
83 | 19610 | jmvivo | this.totalOrder = this.order; |
84 | } else {
|
||
85 | 19738 | vcaballero | this.totalOrder = this.store.getBaseOrder() + ", " +this.order; |
86 | 19610 | jmvivo | } |
87 | |||
88 | } |
||
89 | |||
90 | 19541 | vcaballero | public int size() { |
91 | 19893 | jmvivo | checkModified(); |
92 | 19610 | jmvivo | try {
|
93 | 19663 | jmvivo | if (this.numReg < 0){ |
94 | ResultSet r=null; |
||
95 | 19610 | jmvivo | try {
|
96 | 19833 | jmvivo | 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 | 19610 | jmvivo | } catch (java.sql.SQLException e) {
|
108 | 19906 | jmvivo | throw new ReadException(this.store.getName(),e); |
109 | 19833 | jmvivo | |
110 | 19663 | jmvivo | } finally{
|
111 | try {
|
||
112 | 19833 | jmvivo | if (r != null) |
113 | r.close(); |
||
114 | 19663 | jmvivo | } catch (java.sql.SQLException e) {
|
115 | 19906 | jmvivo | throw new ReadException(this.store.getName(),e); |
116 | 19663 | jmvivo | } |
117 | 19610 | jmvivo | } |
118 | } |
||
119 | 19663 | jmvivo | return numReg;
|
120 | 19610 | jmvivo | } catch (BaseException e){
|
121 | throw new RuntimeException(e); |
||
122 | } |
||
123 | |||
124 | 19541 | vcaballero | } |
125 | |||
126 | public Iterator iterator() { |
||
127 | 19893 | jmvivo | checkModified(); |
128 | 19610 | jmvivo | ResultSet r;
|
129 | try {
|
||
130 | 19617 | jmvivo | r = this.getNewResulset(this.sql); |
131 | 19610 | jmvivo | } catch (ReadException e) {
|
132 | throw new RuntimeException(e); |
||
133 | } |
||
134 | 19738 | vcaballero | H2Iterator dbfIter=new H2Iterator(this.store,this.featureType,r); |
135 | 19541 | vcaballero | return dbfIter;
|
136 | } |
||
137 | |||
138 | protected class H2Iterator implements Iterator{ |
||
139 | 19610 | jmvivo | private ResultSet rs; |
140 | 20058 | jmvivo | private H2Store store;
|
141 | private DBFeatureType featureType;
|
||
142 | 19541 | vcaballero | |
143 | 20058 | jmvivo | public H2Iterator(H2Store store,DBFeatureType featureType ,ResultSet rs){ |
144 | 19610 | jmvivo | this.rs = rs;
|
145 | 19738 | vcaballero | this.store = store;
|
146 | 19610 | jmvivo | this.featureType = featureType;
|
147 | 19541 | vcaballero | } |
148 | |||
149 | 19906 | jmvivo | protected void checkModified(){ |
150 | if (modified)
|
||
151 | throw new ConcurrentModificationException("FeatureCollection modified"); |
||
152 | } |
||
153 | |||
154 | 19541 | vcaballero | public boolean hasNext(){ |
155 | 19893 | jmvivo | checkModified(); |
156 | 19610 | jmvivo | try {
|
157 | if (rs.isLast()){
|
||
158 | return false; |
||
159 | } else {
|
||
160 | return true; |
||
161 | 19541 | vcaballero | } |
162 | 19610 | jmvivo | } catch (java.sql.SQLException e) {
|
163 | throw new RuntimeException( |
||
164 | 19906 | jmvivo | new ReadException(this.store.getName(),e) |
165 | 19610 | jmvivo | ); |
166 | 19541 | vcaballero | } |
167 | } |
||
168 | |||
169 | public Object next() { |
||
170 | 19893 | jmvivo | checkModified(); |
171 | 19610 | jmvivo | if (!hasNext())
|
172 | 19541 | vcaballero | throw new NoSuchElementException(); |
173 | 19610 | jmvivo | return nextFeature();
|
174 | 19541 | vcaballero | } |
175 | |||
176 | private IFeature nextFeature() {
|
||
177 | try {
|
||
178 | 19610 | jmvivo | IFeature feature=null;
|
179 | try {
|
||
180 | if(rs.next()){
|
||
181 | 20058 | jmvivo | feature = this.store.createFeatureFromResulset(this.rs, this.featureType); |
182 | 19610 | jmvivo | } else {
|
183 | 19663 | jmvivo | rs.close(); |
184 | rs.getStatement().close(); |
||
185 | 19610 | jmvivo | throw new NoSuchElementException(); |
186 | 19541 | vcaballero | } |
187 | 19610 | jmvivo | if (rs.isAfterLast()){
|
188 | rs.close(); |
||
189 | 19663 | jmvivo | rs.getStatement().close(); |
190 | 19610 | jmvivo | } |
191 | |||
192 | |||
193 | } catch (java.sql.SQLException e) {
|
||
194 | throw new RuntimeException( |
||
195 | 19906 | jmvivo | new ReadException(this.store.getName(),e) |
196 | 19610 | jmvivo | ); |
197 | 19541 | vcaballero | } |
198 | 19610 | jmvivo | return feature;
|
199 | } catch (BaseException e){
|
||
200 | throw new RuntimeException( |
||
201 | 19906 | jmvivo | new ReadException(this.store.getName(),e) |
202 | 19610 | jmvivo | ); |
203 | |||
204 | 19541 | vcaballero | } |
205 | } |
||
206 | |||
207 | public void remove() { |
||
208 | throw new UnsupportedOperationException(); |
||
209 | } |
||
210 | |||
211 | } |
||
212 | |||
213 | 19975 | jmvivo | public void dispose() { |
214 | this.store.deleteObserver(this); |
||
215 | this.store=null; |
||
216 | this.featureType=null; |
||
217 | |||
218 | } |
||
219 | |||
220 | 19541 | vcaballero | } |