root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / postgresqlbin / PostgresqlBinFeatureCollection.java @ 23680
History | View | Annotate | Download (6.18 KB)
1 |
package org.gvsig.fmap.data.feature.db.jdbc.postgresqlbin; |
---|---|
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.data.DataStore; |
10 |
import org.gvsig.fmap.data.ReadException; |
11 |
import org.gvsig.fmap.data.feature.Feature; |
12 |
import org.gvsig.fmap.data.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 PostgresqlBinFeatureCollection extends DBDataFeatureCollection { |
20 |
protected DBFeatureType featureType;
|
21 |
protected String filter; |
22 |
protected String totalFilter; |
23 |
protected PostgresqlBinStore 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 |
public PostgresqlBinFeatureCollection(PostgresqlBinStore store,FeatureType type, String filter, String order) { |
32 |
this.store=store;
|
33 |
this.featureType=(DBFeatureType)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 |
|
45 |
this.sqlCount = "Select count(*) From "+ ((PostgresqlBinStoreParameters)this.store.getParameters()).tableID(); |
46 |
if (!isStringEmpty(this.totalFilter)){ |
47 |
this.sql= this.sql + " Where " + this.totalFilter; |
48 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
49 |
} |
50 |
if (!isStringEmpty(this.totalOrder)){ |
51 |
this.sql= this.sql + " Order by " + this.totalOrder; |
52 |
} |
53 |
} |
54 |
} |
55 |
|
56 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
57 |
this.store.open();
|
58 |
|
59 |
Connection conn = this.store.getConnection(); |
60 |
try {
|
61 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
62 |
return st.executeQuery(aSql);
|
63 |
|
64 |
} catch (java.sql.SQLException e) {
|
65 |
throw new SQLException(aSql,this.store.getName(),e); |
66 |
} |
67 |
|
68 |
} |
69 |
|
70 |
|
71 |
private void calculateWhere(){ |
72 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
73 |
this.totalFilter = this.filter; |
74 |
} else {
|
75 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
76 |
} |
77 |
} |
78 |
|
79 |
private void calculateOrder(){ |
80 |
if (isStringEmpty(this.store.getBaseOrder())){ |
81 |
this.totalOrder = this.order; |
82 |
} else {
|
83 |
this.totalOrder = this.store.getBaseOrder() + ", " +this.order; |
84 |
} |
85 |
|
86 |
} |
87 |
|
88 |
public int size() { |
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 |
PostgresqlIterator dbfIter; |
126 |
try {
|
127 |
dbfIter = new PostgresqlIterator(this.store, this.featureType, |
128 |
this.sql, this.fetchSize, index + 1); |
129 |
} catch (ReadException e) {
|
130 |
throw new RuntimeException(e); |
131 |
} |
132 |
|
133 |
return dbfIter;
|
134 |
} |
135 |
|
136 |
protected class PostgresqlIterator extends AbstractJDBCIterator { |
137 |
private String cursorName = null; |
138 |
public PostgresqlIterator(PostgresqlBinStore store,
|
139 |
DBFeatureType featureType, String sql, int fetchSize, |
140 |
int initialPosition)
|
141 |
throws ReadException {
|
142 |
super(store, featureType, sql, fetchSize, null, null, |
143 |
initialPosition); |
144 |
|
145 |
} |
146 |
protected void createResulset() throws ReadException { |
147 |
if (this.cursorName == null) { |
148 |
this.createResulset();
|
149 |
} |
150 |
super.createResulset();
|
151 |
} |
152 |
|
153 |
protected void createCursor() throws ReadException { |
154 |
//Comprobar que no se ha inicializado
|
155 |
((PostgresqlBinStore) this.store).open();
|
156 |
Connection conn = ((PostgresqlBinStore) this.store).getConnection(); |
157 |
String mSql= null; |
158 |
|
159 |
try {
|
160 |
|
161 |
Statement st = conn.createStatement();
|
162 |
|
163 |
this.cursorName = PostgresqlBinStoreUtils.createCursorName();
|
164 |
mSql="BEGIN";
|
165 |
st.execute(mSql); |
166 |
mSql="declare " + this.cursorName + " binary cursor for " + this.sql; |
167 |
st.execute("msql");
|
168 |
// System.out.println(mSql);
|
169 |
|
170 |
|
171 |
} catch (java.sql.SQLException e) {
|
172 |
throw new SQLException(mSql,this.store.getName(),e); |
173 |
} |
174 |
|
175 |
|
176 |
} |
177 |
|
178 |
|
179 |
protected Feature createFeatureFromTheResulset() throws ReadException { |
180 |
return ((PostgresqlBinStore) this.store).createFeatureFromResulset( |
181 |
this.rs, this.featureType); |
182 |
} |
183 |
|
184 |
protected void close() throws java.sql.SQLException { |
185 |
if (rs == null) { |
186 |
return;
|
187 |
} |
188 |
try {
|
189 |
super.close();
|
190 |
} finally{
|
191 |
Statement st;
|
192 |
try {
|
193 |
Connection conn = ((PostgresqlBinStore) this.store) |
194 |
.getConnection(); |
195 |
st = conn.createStatement(); |
196 |
st.execute("Rollback");
|
197 |
} catch (java.sql.SQLException e) {
|
198 |
// TODO Auto-generated catch block
|
199 |
e.printStackTrace(); |
200 |
} catch (ReadException e) {
|
201 |
// TODO Auto-generated catch block
|
202 |
e.printStackTrace(); |
203 |
} |
204 |
} |
205 |
|
206 |
|
207 |
} |
208 |
protected void checkModified() { |
209 |
if (modified) {
|
210 |
throw new ConcurrentModificationException( |
211 |
"FeatureCollection modified");
|
212 |
} |
213 |
} |
214 |
|
215 |
protected String getFinalResulsetSQL() { |
216 |
return "fetch forward " + this.fetchSize + " in " + this.cursorName; |
217 |
} |
218 |
|
219 |
protected Connection getConnection() throws ReadException { |
220 |
((PostgresqlBinStore) this.store).open();
|
221 |
return ((PostgresqlBinStore) this.store).getConnection(); |
222 |
} |
223 |
|
224 |
} |
225 |
|
226 |
public void dispose() { |
227 |
this.store.deleteObserver(this); |
228 |
this.store=null; |
229 |
this.featureType=null; |
230 |
|
231 |
} |
232 |
|
233 |
|
234 |
public boolean isFromStore(DataStore store) { |
235 |
return this.store.equals(store); |
236 |
} |
237 |
|
238 |
public FeatureType getFeatureType() {
|
239 |
return this.featureType; |
240 |
} |
241 |
} |