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 / jdbc2 / impl / JDBCSetProvider.java @ 44191
History | View | Annotate | Download (9.76 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 modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.store.jdbc2.impl; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.List; |
27 |
import java.util.NoSuchElementException; |
28 |
import org.apache.commons.collections.CollectionUtils; |
29 |
import org.apache.commons.lang3.BooleanUtils; |
30 |
import org.gvsig.fmap.dal.exception.DataException; |
31 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
32 |
import org.gvsig.fmap.dal.feature.FeatureType; |
33 |
|
34 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider; |
35 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
36 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
37 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
38 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider; |
39 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
40 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
41 |
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler; |
42 |
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler.ResultSetEntry; |
43 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.TableIsEmptyOperation; |
44 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation; |
45 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CountOperation; |
46 |
import org.gvsig.tools.evaluator.Evaluator; |
47 |
import org.gvsig.tools.exception.BaseException; |
48 |
import org.slf4j.Logger; |
49 |
import org.slf4j.LoggerFactory; |
50 |
|
51 |
public class JDBCSetProvider extends AbstractFeatureSetProvider { |
52 |
|
53 |
final static protected Logger LOGGER = LoggerFactory.getLogger(JDBCSetProvider.class); |
54 |
|
55 |
private class EmptyJDBCIterator extends JDBCIterator { |
56 |
|
57 |
protected EmptyJDBCIterator(JDBCStoreProvider store) throws DataException { |
58 |
super(store, null, null, null); |
59 |
} |
60 |
|
61 |
@Override
|
62 |
protected boolean internalHasNext() { |
63 |
return false; |
64 |
} |
65 |
|
66 |
@Override
|
67 |
protected Object internalNext() { |
68 |
throw new NoSuchElementException(); |
69 |
} |
70 |
|
71 |
@Override
|
72 |
protected void doDispose() throws BaseException { |
73 |
// nothing to do
|
74 |
} |
75 |
|
76 |
} |
77 |
|
78 |
|
79 |
protected Long size = null; |
80 |
protected Boolean isEmpty = null; |
81 |
protected List<ResultSetEntry> resultSets; |
82 |
private int defaultFetchSize = 1000; |
83 |
|
84 |
public JDBCSetProvider(
|
85 |
AbstractFeatureStoreProvider store, |
86 |
JDBCHelper helper, |
87 |
FeatureQuery query, |
88 |
FeatureType featureType |
89 |
) throws DataException {
|
90 |
super(store, query, featureType);
|
91 |
this.resultSets = new ArrayList<>(); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* @return the defaultFetchSize
|
96 |
*/
|
97 |
public int getDefaultFetchSize() { |
98 |
return defaultFetchSize;
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* @param defaultFetchSize the defaultFetchSize to set
|
103 |
*/
|
104 |
public void setDefaultFetchSize(int defaultFetchSize) { |
105 |
this.defaultFetchSize = defaultFetchSize;
|
106 |
} |
107 |
|
108 |
// @Override
|
109 |
// protected JDBCStoreProvider getStore() {
|
110 |
// return (JDBCStoreProvider) super.getStore();
|
111 |
// }
|
112 |
|
113 |
protected JDBCStoreProvider getJDBCStore() {
|
114 |
// Corregido el problema en AbstractFeatureSetProvider, de que getStore
|
115 |
// debe ser un FeatureStoreProvider y no un AbstractFeatureStoreProvider
|
116 |
// este metodo debe desaparecer y quedar el getStore de arriba que esta
|
117 |
// comentarizado
|
118 |
return (JDBCStoreProvider) super.getStore(); |
119 |
} |
120 |
|
121 |
protected JDBCHelper getHelper() {
|
122 |
return this.getJDBCStore().getHelper(); |
123 |
} |
124 |
|
125 |
protected OperationsFactory getOperations() {
|
126 |
return this.getHelper().getOperations(); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public boolean canIterateFromIndex() { |
131 |
return this.getHelper().supportOffsetInSelect(); |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public boolean canFilter() { |
136 |
return this.getHelper().supportFilter(getQuery().getFilter()); |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public boolean canOrder() { |
141 |
return this.getHelper().supportOrder(getQuery().getOrder()); |
142 |
} |
143 |
|
144 |
public ResulSetControler getResulSetControler() {
|
145 |
return this.getHelper().getResulSetControler(); |
146 |
} |
147 |
|
148 |
@Override
|
149 |
protected void doDispose() throws BaseException { |
150 |
if( ! CollectionUtils.isEmpty(this.resultSets) ) { |
151 |
for( ResultSetEntry resulset : this.resultSets ) { |
152 |
JDBCUtils.closeQuietly(resulset); |
153 |
} |
154 |
} |
155 |
size = null;
|
156 |
isEmpty = null;
|
157 |
} |
158 |
|
159 |
protected int getFetchSize() { |
160 |
long pageSize = -1; |
161 |
if (getQuery() != null) { |
162 |
pageSize = getQuery().getPageSize(); |
163 |
pageSize = pageSize > Integer.MAX_VALUE ? Integer.MAX_VALUE : pageSize; |
164 |
} |
165 |
return (pageSize > 0 ? (int) pageSize : defaultFetchSize); |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public long getSize() throws DataException { |
170 |
if (size == null) { |
171 |
JDBCStoreParameters params = this.getJDBCStore().getParameters();
|
172 |
String filtersql = null; |
173 |
Evaluator filter = this.getQuery().getFilter();
|
174 |
if( filter != null ) { |
175 |
filtersql = filter.getSQL(); |
176 |
} |
177 |
CountOperation selectCount = this.getOperations().createCount(
|
178 |
this.getOperations().createTableReference(params),
|
179 |
params.getBaseFilter(), |
180 |
filtersql |
181 |
); |
182 |
size = (Long) selectCount.perform();
|
183 |
} |
184 |
return size;
|
185 |
} |
186 |
|
187 |
@Override
|
188 |
public boolean isEmpty() throws DataException { |
189 |
if (this.isEmpty == null) { |
190 |
if (this.size == null) { |
191 |
JDBCStoreParameters params = this.getJDBCStore().getParameters();
|
192 |
String filtersql = null; |
193 |
Evaluator filter = this.getQuery().getFilter();
|
194 |
if( filter != null ) { |
195 |
filtersql = filter.getSQL(); |
196 |
} |
197 |
TableIsEmptyOperation isEmpty_ = this.getOperations().createTableIsEmpty(
|
198 |
this.getOperations().createTableReference(params),
|
199 |
params.getBaseFilter(), |
200 |
filtersql |
201 |
); |
202 |
this.isEmpty = (Boolean) isEmpty_.perform(); |
203 |
} else {
|
204 |
this.isEmpty = (this.size < 1); |
205 |
} |
206 |
} |
207 |
return isEmpty;
|
208 |
} |
209 |
|
210 |
|
211 |
protected JDBCIterator createFastIterator(long index) throws DataException { |
212 |
return createFastIterator(index, 0); |
213 |
} |
214 |
|
215 |
@Override
|
216 |
protected JDBCIterator createFastIterator(long index, long elements) throws DataException { |
217 |
if( BooleanUtils.isTrue(isEmpty) ) {
|
218 |
return new EmptyJDBCIterator(this.getJDBCStore()); |
219 |
} |
220 |
|
221 |
JDBCStoreParameters params = this.getJDBCStore().getParameters();
|
222 |
FeatureType storeType = this.getStore()
|
223 |
.getStoreServices() |
224 |
.getProviderFeatureType(this.getFeatureType().getId());
|
225 |
ResultSetForSetProviderOperation createResultSet |
226 |
= getOperations().createResultSetForSetProvider( |
227 |
this.getOperations().createTableReference(params),
|
228 |
params.getBaseFilter(), |
229 |
params.getBaseOrder(), |
230 |
this.getQuery(),
|
231 |
storeType, |
232 |
this.getFeatureType(),
|
233 |
elements, |
234 |
index, |
235 |
this.getDefaultFetchSize()
|
236 |
); |
237 |
|
238 |
ResultSetEntry resultSetEntry = (ResultSetEntry) createResultSet.perform(); |
239 |
|
240 |
this.resultSets.add(resultSetEntry);
|
241 |
return new JDBCFastIterator( |
242 |
this.getJDBCStore(),
|
243 |
this,
|
244 |
this.getFeatureType(),
|
245 |
resultSetEntry |
246 |
); |
247 |
} |
248 |
|
249 |
@Override
|
250 |
protected JDBCIterator createIterator(long index) throws DataException { |
251 |
return createIterator(index, -1); |
252 |
} |
253 |
|
254 |
@Override
|
255 |
protected JDBCIterator createIterator(long index, long elements) throws DataException { |
256 |
if( BooleanUtils.isTrue(isEmpty) ) {
|
257 |
return new EmptyJDBCIterator(this.getJDBCStore()); |
258 |
} |
259 |
JDBCStoreParameters params = this.getJDBCStore().getParameters();
|
260 |
FeatureType storeType = this.getStore()
|
261 |
.getStoreServices() |
262 |
.getProviderFeatureType(this.getFeatureType().getId());
|
263 |
ResultSetForSetProviderOperation createResultSet |
264 |
= getOperations().createResultSetForSetProvider( |
265 |
this.getOperations().createTableReference(params),
|
266 |
params.getBaseFilter(), |
267 |
params.getBaseOrder(), |
268 |
this.getQuery(),
|
269 |
storeType, |
270 |
this.getFeatureType(),
|
271 |
elements, |
272 |
index, |
273 |
this.getDefaultFetchSize()
|
274 |
); |
275 |
|
276 |
ResultSetEntry resultSetEntry = (ResultSetEntry) createResultSet.perform(); |
277 |
this.resultSets.add(resultSetEntry);
|
278 |
return new JDBCIterator( |
279 |
this.getJDBCStore(),
|
280 |
this,
|
281 |
this.getFeatureType(),
|
282 |
resultSetEntry |
283 |
); |
284 |
} |
285 |
|
286 |
} |