Revision 44191 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/spi/JDBCHelperBase.java

View differences:

JDBCHelperBase.java
5 5
import java.sql.Connection;
6 6
import java.sql.ResultSet;
7 7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.lang3.mutable.MutableBoolean;
9
import org.gvsig.expressionevaluator.Code;
8 10
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType;
9 11
import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.EWKB;
10 12
import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.NATIVE;
11 13
import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.WKB;
12 14
import static org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType.WKT;
15
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
16
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
17
import org.gvsig.expressionevaluator.Function;
13 18
import org.gvsig.fmap.dal.DataTypes;
14 19
import org.gvsig.fmap.dal.exception.DataException;
15 20
import org.gvsig.fmap.dal.exception.InitializeException;
16 21
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
22
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
17 23
import org.gvsig.fmap.dal.feature.FeatureType;
18 24

  
19 25
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
......
41 47
import org.gvsig.fmap.geom.GeometryLocator;
42 48
import org.gvsig.fmap.geom.GeometryManager;
43 49
import org.gvsig.tools.dispose.impl.AbstractDisposable;
50
import org.gvsig.tools.evaluator.Evaluator;
44 51
import org.gvsig.tools.exception.BaseException;
45 52
import org.gvsig.tools.exception.NotYetImplemented;
53
import org.gvsig.tools.visitor.VisitCanceledException;
54
import org.gvsig.tools.visitor.Visitor;
46 55
import org.slf4j.Logger;
47 56
import org.slf4j.LoggerFactory;
48 57

  
58
@SuppressWarnings("UseSpecificCatch")
49 59
public class JDBCHelperBase extends AbstractDisposable implements ResourceConsumer, JDBCHelper {
50 60

  
51 61
    private static final boolean ALLOW_AUTOMATIC_VALUES = true;
52 62
    private static final String QUOTE_FOR_USE_IN_IDENTIFIERS = "\"";
53 63
    private static final String QUOTE_FOR_USE_IN_STRINGS = "'";
54 64

  
55
    private static final Logger logger = LoggerFactory.getLogger(JDBCHelperBase.class);
65
    private static final Logger LOGGER = LoggerFactory.getLogger(JDBCHelperBase.class);
56 66

  
57 67
    private ResulSetControler resulSetControler = null;
58 68

  
......
112 122
    }
113 123

  
114 124
    @Override
125
    public boolean allowNestedOperations() {
126
        return false;
127
    }
128
    
129
    @Override
115 130
    public boolean canWriteGeometry(int geometryType, int geometrySubtype) {
116 131
        // Como va a guardar las geometrias en WKT, puede escribirlas todas.
117 132
        return true;
......
142 157
    public String getQuoteForStrings() {
143 158
        return QUOTE_FOR_USE_IN_STRINGS;
144 159
    }
160

  
161
    @Override
162
    public boolean supportFilter(Evaluator filter) {
163
        // No podemos filtrar cuando:
164
        // - Hay una subquery especificada en los parametros 
165
        // - Si hay un filtro y el getSQL devuelbe null.
166
        // - Si se esta usando alguna funcion no-sql en el getSQL
167
        // - Si se estan usando funciones OGC y no soportamos funciones espaciales
168
        // 
169
        // Un proveedor especifico podria sobreescribir el metodo,
170
        // para hilar mas fino al comprobar si soporta el filtro o no.
171
        //
172
        
173
        if (this.useSubquery()) {
174
            // Se esta usando una subquery en los parametros de acceso a la
175
            // BBDD, asi que no podemos filtrar.
176
            return false;
177
        }
178
        if (filter == null) {
179
            // No hay que filtrar nada, asi que se p?ede.
180
            return true;
181
        }
182
        String sql = filter.getSQL();
183
        if (StringUtils.isEmpty(sql)) {
184
            // Hay un filtro, pero la SQL es null, con lo que no podemos
185
            // filtrar por el.
186
            return false;
187
        }
188

  
189
        // Ahora vamos a comprobar que las funciones que se usan son 
190
        // compatibles sql, y que no se usan funciones OGC si el 
191
        // proveedor dice que no soporta funciones espaciales.
192
        final MutableBoolean isCompatible = new MutableBoolean(true);
193
        final boolean supportSpatialFunctions = this.hasSpatialFunctions();
194
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
195
        Code code = manager.compile(sql);
196
        try {
197
            code.accept(new Visitor() {
198
                @Override
199
                public void visit(Object code_obj) throws VisitCanceledException, BaseException {
200
                    Code code = (Code) code_obj;
201
                    if ( code.code()==Code.CALLER ) {
202
                        Code.Caller caller = (Code.Caller) code;
203
                        Function function = caller.function();
204
                        if( function==null ) {
205
                            isCompatible.setValue(false);
206
                            throw new VisitCanceledException();
207
                        }
208
                        if( !function.isSQLCompatible() ) {
209
                            isCompatible.setValue(false);
210
                            throw new VisitCanceledException();
211
                        }
212
                        if( !supportSpatialFunctions ) {
213
                            if( StringUtils.equalsIgnoreCase(function.group(),Function.GROUP_OGC) ) {
214
                                isCompatible.setValue(false);
215
                                throw new VisitCanceledException();
216
                            }
217
                        }
218
                    }
219
                }
220
            });
221

  
222
        } catch (VisitCanceledException ex) {
223
            // Do nothing
224
        } catch (Exception ex) {
225
            LOGGER.warn("Can't calculate if is SQL compatible.", ex);
226
        }
227
        
228
        return isCompatible.booleanValue();
229
    }
145 230
    
146 231
    @Override
232
    public boolean supportOrder(FeatureQueryOrder order) {
233
        if (this.useSubquery()) {
234
            return false;
235
        }
236
        if (order == null) {
237
            return true;
238
        }
239
        for( FeatureQueryOrder.FeatureQueryOrderMember member : order.members() ) {
240
            if (member.hasEvaluator()) {
241
                if( !this.supportFilter(member.getEvaluator()) ) {
242
                    return false;
243
                }
244
            }            
245
        }
246
        return true;
247
    }
248
    
249
    @Override
147 250
    public OperationsFactory getOperations() {
148 251
        if (this.operationsFactory== null) {
149 252
            this.operationsFactory = new OperationsFactoryBase(this);
......
212 315
    @Override
213 316
    public void closeConnection(Connection connection) {
214 317
        if( connection != null ) {
215
            logger.debug("Clossing connection "+connection.hashCode());
318
            LOGGER.debug("Clossing connection "+connection.hashCode());
216 319
            try {
217 320
                connection.close();
218 321
            } catch(Exception ex) {
219
                logger.warn("Can't close connection.", ex);
322
                LOGGER.warn("Can't close connection.", ex);
220 323
            }
221 324
       }
222 325
    }
326

  
327
    public void closeConnectionQuietly(Connection connection) {
328
        if( connection != null ) {
329
            LOGGER.debug("Clossing connection quietly "+connection.hashCode());
330
            try {
331
                connection.close();
332
            } catch(Exception ex) {
333
                LOGGER.warn("Can't close connection.", ex);
334
            }
335
       }
336
    }
223 337
    
224 338
    @Override
225 339
    protected void doDispose() throws BaseException {
......
348 462
            DataStoreProviderServices providerServices
349 463
        ) throws InitializeException {
350 464
        
351
        JDBCStoreProviderBase store = new JDBCStoreProviderBase(
465
        JDBCStoreProviderBase theStore = new JDBCStoreProviderBase(
352 466
                parameters, 
353 467
                providerServices, 
354 468
                DBHelper.newMetadataContainer(JDBCLibrary.NAME),
355 469
                this
356 470
        );
357
        this.initialize(store, parameters, store);
358
        return store;
471
        this.initialize(theStore, parameters, theStore);
472
        return theStore;
359 473
    }
360 474

  
361 475
    @Override

Also available in: Unified diff