svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc / JDBCSetProvider.java @ 44871
History | View | Annotate | Download (10.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.jdbc; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Arrays; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
import java.util.NoSuchElementException; |
31 |
|
32 |
import org.gvsig.fmap.dal.DataTypes; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
35 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
36 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
37 |
import org.gvsig.fmap.dal.feature.FeatureType; |
38 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember; |
39 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider; |
40 |
import org.gvsig.tools.evaluator.Evaluator; |
41 |
import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
42 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
43 |
import org.gvsig.tools.exception.BaseException; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
/**
|
48 |
* @author jmvivo
|
49 |
*
|
50 |
*/
|
51 |
public class JDBCSetProvider extends AbstractFeatureSetProvider { |
52 |
|
53 |
final static private Logger logger = LoggerFactory |
54 |
.getLogger(JDBCSetProvider.class); |
55 |
|
56 |
protected String filter; |
57 |
protected String order; |
58 |
protected Long size = null; |
59 |
protected Boolean isEmpty = null; |
60 |
|
61 |
protected List resultSetIDReferenced; |
62 |
|
63 |
private JDBCHelper helper = null; |
64 |
|
65 |
private int defaultFetchSize = 1000; |
66 |
|
67 |
private long limit = -1l; |
68 |
|
69 |
private boolean directSQLMode; |
70 |
|
71 |
public JDBCSetProvider(JDBCStoreProvider store, FeatureQuery query,
|
72 |
FeatureType featureType) throws DataException {
|
73 |
super(store, query, featureType);
|
74 |
this.helper = store.getHelper();
|
75 |
this.resultSetIDReferenced = new ArrayList(); |
76 |
this.directSQLMode = store.isDirectSQLMode();
|
77 |
|
78 |
if (query.hasFilter() && this.canFilter()) { |
79 |
setFilter(query.getFilter()); |
80 |
} else {
|
81 |
setFilter(null);
|
82 |
} |
83 |
|
84 |
if (query.hasOrder() && canOrder()) {
|
85 |
setOrder(query.getOrder()); |
86 |
} else {
|
87 |
setOrder(null);
|
88 |
} |
89 |
|
90 |
if (query != null) { |
91 |
limit = query.getLimit(); |
92 |
} |
93 |
} |
94 |
|
95 |
/**
|
96 |
* @return the defaultFetchSize
|
97 |
*/
|
98 |
public int getDefaultFetchSize() { |
99 |
return defaultFetchSize;
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* @param defaultFetchSize
|
104 |
* the defaultFetchSize to set
|
105 |
*/
|
106 |
public void setDefaultFetchSize(int defaultFetchSize) { |
107 |
this.defaultFetchSize = defaultFetchSize;
|
108 |
} |
109 |
|
110 |
protected String getSqlForEvaluator(Evaluator filter) { |
111 |
if (filter == null) { |
112 |
return null; |
113 |
} |
114 |
EvaluatorFieldsInfo info = filter.getFieldsInfo(); |
115 |
String filterString = filter.getSQL();
|
116 |
if (info == null) { |
117 |
return filterString;
|
118 |
} |
119 |
String[] filterNames = info.getFieldNames(); |
120 |
String[] finalNames = new String[filterNames.length]; |
121 |
EvaluatorFieldValue[] fValues;
|
122 |
|
123 |
List values = new ArrayList(); |
124 |
|
125 |
FeatureAttributeDescriptor attr; |
126 |
for (int i = 0; i < filterNames.length; i++) { |
127 |
attr = getFeatureType().getAttributeDescriptor(filterNames[i]); |
128 |
if (attr == null) { |
129 |
finalNames[i] = filterNames[i]; |
130 |
continue;
|
131 |
} |
132 |
finalNames[i] = getEscapedFieldName(attr.getName()); |
133 |
|
134 |
} |
135 |
|
136 |
for (int i = 0; i < filterNames.length; i++) { |
137 |
if (!filterNames[i].equals(finalNames[i])) {
|
138 |
filterString = filterString.replaceAll( |
139 |
"\\b" + filterNames[i] + "\\b", |
140 |
finalNames[i]); |
141 |
} |
142 |
} |
143 |
|
144 |
return filterString;
|
145 |
} |
146 |
|
147 |
|
148 |
protected String getEscapedFieldName(String fieldName) { |
149 |
if (helper == null) { |
150 |
helper = getJDBCStoreProvider().getHelper(); |
151 |
} |
152 |
return helper.escapeFieldName(fieldName);
|
153 |
} |
154 |
|
155 |
|
156 |
protected void setOrder(FeatureQueryOrder order) { |
157 |
if (order == null || order.size() == 0) { |
158 |
this.order = null; |
159 |
return;
|
160 |
} |
161 |
|
162 |
StringBuilder buffer = new StringBuilder(); |
163 |
Iterator iter = order.iterator();
|
164 |
FeatureQueryOrderMember menber; |
165 |
while (true) { |
166 |
menber = (FeatureQueryOrderMember) iter.next(); |
167 |
if (menber.hasEvaluator()) {
|
168 |
buffer.append(getSqlForEvaluator(menber.getEvaluator())); |
169 |
} else {
|
170 |
buffer.append(getEscapedFieldName(menber.getAttributeName())); |
171 |
} |
172 |
if (menber.getAscending()) {
|
173 |
buffer.append(" ASC");
|
174 |
} else {
|
175 |
buffer.append(" DESC");
|
176 |
} |
177 |
if (iter.hasNext()) {
|
178 |
buffer.append(", ");
|
179 |
} else {
|
180 |
buffer.append(' ');
|
181 |
break;
|
182 |
} |
183 |
} |
184 |
|
185 |
this.order = buffer.toString();
|
186 |
} |
187 |
|
188 |
protected void setFilter(Evaluator filter) { |
189 |
this.filter = getSqlForEvaluator(filter);
|
190 |
} |
191 |
|
192 |
|
193 |
|
194 |
/* (non-Javadoc)
|
195 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canFilter()
|
196 |
*/
|
197 |
public boolean canFilter() { |
198 |
Evaluator filter = getQuery().getFilter(); |
199 |
if (directSQLMode){
|
200 |
return false; |
201 |
} |
202 |
if (filter != null) { |
203 |
if (filter.getSQL() == null || filter.getSQL().length() == 0) { |
204 |
return false; |
205 |
} else {
|
206 |
// TODO Check Geom fields if
|
207 |
EvaluatorFieldsInfo fInfo = filter.getFieldsInfo(); |
208 |
if (fInfo == null || fInfo.getFieldNames() == null) { |
209 |
return true; |
210 |
} |
211 |
Iterator names = Arrays.asList(fInfo.getFieldNames()) |
212 |
.iterator(); |
213 |
String name;
|
214 |
int type;
|
215 |
while (names.hasNext()) {
|
216 |
name = (String) names.next();
|
217 |
type = |
218 |
this.getFeatureType()
|
219 |
.getAttributeDescriptor(name) |
220 |
.getType(); |
221 |
if (type == DataTypes.GEOMETRY
|
222 |
&& !this.helper.supportsGeometry()) {
|
223 |
return false; |
224 |
} |
225 |
|
226 |
|
227 |
|
228 |
} |
229 |
|
230 |
return true; |
231 |
} |
232 |
|
233 |
} else{
|
234 |
return false; |
235 |
} |
236 |
} |
237 |
|
238 |
/* (non-Javadoc)
|
239 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canIterateFromIndex()
|
240 |
*/
|
241 |
public boolean canIterateFromIndex() { |
242 |
return helper.supportOffset();
|
243 |
} |
244 |
|
245 |
/* (non-Javadoc)
|
246 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canOrder()
|
247 |
*/
|
248 |
public boolean canOrder() { |
249 |
if (directSQLMode){
|
250 |
return false; |
251 |
} |
252 |
// TODO Check Geom fields if postgis not are available
|
253 |
FeatureQuery query = getQuery(); |
254 |
if (query.hasOrder()) {
|
255 |
Iterator iter = query.getOrder().iterator();
|
256 |
FeatureQueryOrderMember menber; |
257 |
String sql;
|
258 |
while (iter.hasNext()){
|
259 |
menber = (FeatureQueryOrderMember) iter.next(); |
260 |
if (menber.hasEvaluator()){
|
261 |
sql = menber.getEvaluator().getSQL(); |
262 |
if (sql == null || sql.length() == 0) { |
263 |
return false; |
264 |
} |
265 |
} |
266 |
} |
267 |
} |
268 |
return true; |
269 |
} |
270 |
|
271 |
private JDBCStoreProvider getJDBCStoreProvider() {
|
272 |
return (JDBCStoreProvider) getStore();
|
273 |
} |
274 |
|
275 |
@Override
|
276 |
protected void doDispose() throws BaseException { |
277 |
if (resultSetIDReferenced != null) { |
278 |
Iterator iter = resultSetIDReferenced.iterator();
|
279 |
Integer resID;
|
280 |
while (iter.hasNext()) {
|
281 |
resID = (Integer) iter.next();
|
282 |
if (resID != null) { |
283 |
logger.warn( |
284 |
"ResultSet (ID {}) not closed on dispose, will close",
|
285 |
resID); |
286 |
try {
|
287 |
getJDBCStoreProvider().closeResulset(resID.intValue()); |
288 |
} catch (DataException e) {
|
289 |
logger.error("Close resulset Exception", e);
|
290 |
} |
291 |
} |
292 |
iter.remove(); |
293 |
} |
294 |
} |
295 |
resultSetIDReferenced = null;
|
296 |
filter = null;
|
297 |
order = null;
|
298 |
size = null;
|
299 |
isEmpty = null;
|
300 |
} |
301 |
|
302 |
protected String getSQL(long fromIndex) throws DataException { |
303 |
return getJDBCStoreProvider().compoundSelect(getFeatureType(), filter,
|
304 |
order, limit, fromIndex); |
305 |
|
306 |
} |
307 |
|
308 |
/* (non-Javadoc)
|
309 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#getSize()
|
310 |
*/
|
311 |
public long getSize() throws DataException { |
312 |
if (size == null) { |
313 |
size = new Long(getJDBCStoreProvider().getCount(filter)); |
314 |
} |
315 |
return size.longValue();
|
316 |
} |
317 |
|
318 |
/* (non-Javadoc)
|
319 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#isEmpty()
|
320 |
*/
|
321 |
public boolean isEmpty() throws DataException { |
322 |
JDBCStoreProvider store = getJDBCStoreProvider(); |
323 |
if (isEmpty == null) { |
324 |
if (size == null) { |
325 |
String sql =
|
326 |
store.compoundSelect(getFeatureType(), filter, null, 1, |
327 |
0);
|
328 |
int rsID = store.createResultSet(sql, getFetchSize());
|
329 |
isEmpty = new Boolean(!store.resulsetNext(rsID)); |
330 |
store.closeResulset(rsID); |
331 |
} else {
|
332 |
isEmpty = new Boolean(size.longValue() < 1); |
333 |
} |
334 |
} |
335 |
return isEmpty.booleanValue();
|
336 |
} |
337 |
|
338 |
protected int getFetchSize() { |
339 |
long pageSize = -1; |
340 |
if (getQuery() != null) { |
341 |
pageSize = getQuery().getPageSize(); |
342 |
pageSize = |
343 |
pageSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : pageSize; |
344 |
} |
345 |
return (pageSize > 0 ? (int) pageSize : defaultFetchSize); |
346 |
} |
347 |
|
348 |
protected JDBCIterator createFastIterator(long index) throws DataException { |
349 |
if (isEmpty != null && isEmpty.booleanValue()) { |
350 |
return new EmptyJDBCIterator(getJDBCStoreProvider()); |
351 |
} |
352 |
int rsID =
|
353 |
getJDBCStoreProvider().createResultSet(getSQL(index), |
354 |
getFetchSize()); |
355 |
return createDefaultFastIterator(rsID);
|
356 |
} |
357 |
|
358 |
protected JDBCIterator createDefaultFastIterator(int resultSetID) |
359 |
throws DataException {
|
360 |
return new JDBCFastIterator(getJDBCStoreProvider(), this, |
361 |
getFeatureType(), resultSetID); |
362 |
} |
363 |
|
364 |
protected JDBCIterator createIterator(long index) throws DataException { |
365 |
if (isEmpty != null && isEmpty.booleanValue()) { |
366 |
return new EmptyJDBCIterator(getJDBCStoreProvider()); |
367 |
} |
368 |
int rsID =
|
369 |
getJDBCStoreProvider().createResultSet(getSQL(index), |
370 |
getFetchSize()); |
371 |
return createDefaultIterator(rsID);
|
372 |
} |
373 |
|
374 |
protected JDBCIterator createDefaultIterator(int resultSetID) |
375 |
throws DataException {
|
376 |
return new JDBCIterator(getJDBCStoreProvider(), this, getFeatureType(), |
377 |
resultSetID); |
378 |
} |
379 |
|
380 |
public void addResulsetReference(int resulsetID) { |
381 |
this.resultSetIDReferenced.add(new Integer(resulsetID)); |
382 |
} |
383 |
|
384 |
public void removeResulsetReference(int resulsetID) { |
385 |
this.resultSetIDReferenced.remove(new Integer(resulsetID)); |
386 |
} |
387 |
|
388 |
private class EmptyJDBCIterator extends JDBCIterator { |
389 |
|
390 |
protected EmptyJDBCIterator(JDBCStoreProvider store) throws DataException { |
391 |
super(store, null, null, -1); |
392 |
} |
393 |
|
394 |
@Override
|
395 |
protected boolean internalHasNext() { |
396 |
return false; |
397 |
} |
398 |
|
399 |
@Override
|
400 |
protected Object internalNext() { |
401 |
throw new NoSuchElementException(); |
402 |
} |
403 |
|
404 |
@Override
|
405 |
protected void doDispose() throws BaseException { |
406 |
// nothing to do
|
407 |
} |
408 |
|
409 |
} |
410 |
|
411 |
} |