Revision 46163

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.api/src/main/java/org/gvsig/fmap/dal/feature/FeatureQuery.java
285 285
   * @param pageSize the load page size
286 286
   */
287 287
  void setPageSize(long pageSize);
288
  
289
  /**
290
   * Returns true if a subquery should be used. It will only take effect 
291
   * when the sql perameter of the connection has a value. 
292
   * @return 
293
   */
294
  public boolean isUseSubquery();
295
  
296
  /**
297
   * Indicates whether to use a subquery or not in case the connection has 
298
   * an sql indicated. This flag will have no effect if the "sql" parameter 
299
   * in the connection has not been specified.
300
   * By default it will be true and whenever an sql has been indicated in the 
301
   * connection, a subquery will be used to access the data. If false is assigned, 
302
   * the sql should return, in the same order, the rows required by the 
303
   * FeatureType of the store.
304
   * 
305
   * @param useSubquery 
306
   */
307
  public void setUseSubquery(boolean useSubquery);
288 308

  
289 309
  /**
290 310
   * Return the names of attributes for group the features.
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DefaultFeatureQuery.java
115 115
    private FeatureExtraColumns extraColumn = new DefaultFeatureExtraColumns();
116 116
    
117 117
    private MutableSymbolTable symbolTable;
118
	private String storeName;
119 118
    
119
    private String storeName;
120
    
121
    private boolean useSubquery;
122
    
120 123
    /**
121 124
     * Creates a FeatureQuery which will load all available Features of a type.
122 125
     *
123 126
     */
124 127
    public DefaultFeatureQuery() {
125
		super();
126
	}
128
        super();
129
        this.useSubquery = true; // true for backwards compatibility. 
130
    }
127 131
	
128 132
    public DefaultFeatureQuery(String storeName) {
129 133
        this();
130
		this.storeName = storeName;
134
	this.storeName = storeName;
131 135
		
132 136
    }
133 137

  
......
138 142
     *            the type of Features of the query
139 143
     */
140 144
    public DefaultFeatureQuery(FeatureType featureType) {
141
        super();
145
        this();
142 146
        this.setFeatureType(featureType);
143 147
    }
144 148

  
......
152 156
     *            based on the properties of the Features
153 157
     */
154 158
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter) {
155
        super();
159
        this();
156 160
        this.setFeatureType(featureType);
157 161
        this.filter = filter;
158 162
    }
......
170 174
     */
171 175
    public DefaultFeatureQuery(FeatureType featureType, Evaluator filter,
172 176
        double scale) {
177
        this();
173 178
        this.setFeatureType(featureType);
174 179
        this.filter = filter;
175 180
        this.setScale(scale);
......
183 188
     *            the list of attribute names to load
184 189
     */
185 190
    public DefaultFeatureQuery(String[] attributeNames) {
186
        super();
191
        this();
187 192
        setAttributeNames(attributeNames);
188 193
    }
189 194

  
......
197 202
     *            based on the properties of the Features
198 203
     */
199 204
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter) {
200
        super();
205
        this();
201 206
        setAttributeNames(attributeNames);
202 207
        this.filter = filter;
203 208
    }
......
215 220
     */
216 221
    public DefaultFeatureQuery(String[] attributeNames, Evaluator filter,
217 222
        double scale) {
223
        this();
218 224
        setAttributeNames(attributeNames);
219 225
        this.filter = filter;
220 226
        this.setScale(scale);
......
608 614
        state.set("filter", filterList);
609 615
        state.set("limit", this.limit);
610 616
        state.set("pageSize", this.pageSize);
617
        state.set("useSubquery", this.useSubquery);
611 618
        
612 619
        state.set("order", this.order);
613 620
        state.set("groupByColumns", this.groupByColumns);
......
662 669
        }
663 670
        this.limit = state.getLong("limit");
664 671
        this.pageSize = state.getLong("pageSize");
665
		this.storeName = state.getString("storeName");
672
        this.useSubquery = state.getBoolean("useSubquery",true);
673
        this.storeName = state.getString("storeName");
666 674
        
667 675
        
668 676
        this.order = (FeatureQueryOrder) state.get("order");
......
718 726

  
719 727
        definition.addDynFieldLong("pageSize").setMandatory(false);
720 728
        
729
        definition.addDynFieldBoolean("useSubquery").setMandatory(false);
721 730
       
722 731
        definition.addDynFieldList("groupByColumns")
723 732
                .setClassOfItems(String.class);
......
873 882
    this.limit = other.limit;
874 883

  
875 884
    this.pageSize = other.pageSize;
885
    this.useSubquery = other.useSubquery;
876 886

  
877 887
    if( this.groupByColumns!=null && other.groupByColumns!=null ) {
878 888
      this.groupByColumns.clear();
......
926 936
        this.symbolTable.setVar(name, value);
927 937
    }
928 938

  
939
    @Override
940
    public boolean isUseSubquery() {
941
        return useSubquery;
942
    }
943

  
944
    @Override
945
    public void setUseSubquery(boolean useSubquery) {
946
        this.useSubquery = useSubquery;
947
    }
929 948
    
930 949
}
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/operations/ResultSetForSetProviderOperation.java
28 28
import java.util.HashMap;
29 29
import java.util.List;
30 30
import java.util.Map;
31
import static javax.management.Query.attr;
32 31
import org.apache.commons.lang3.ArrayUtils;
33 32
import org.apache.commons.lang3.StringUtils;
34 33
import org.gvsig.expressionevaluator.Code;
......
36 35
import org.gvsig.expressionevaluator.ExpressionBuilder;
37 36
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_LET;
38 37
import org.gvsig.expressionevaluator.ExpressionUtils;
39
import org.gvsig.expressionevaluator.SymbolTable;
40 38
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder;
41 39
import org.gvsig.fmap.dal.exception.DataException;
42 40
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
......
57 55
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY;
58 56
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE;
59 57
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
60
import org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters.ComputedAttribute;
61 58
import org.gvsig.fmap.geom.DataTypes;
62 59
import org.gvsig.tools.dynobject.DynField;
63 60
import org.gvsig.tools.evaluator.Evaluator;
64 61
import org.gvsig.tools.lang.CloneableUtils;
65
import org.gvsig.tools.util.ContainerUtils;
66 62

  
67 63
public class ResultSetForSetProviderOperation extends AbstractConnectionOperation {
68 64

  
......
126 122
        double tolerance = -1; //query.getScale(); 
127 123
        ExpressionBuilder expbuilder = sqlbuilder.expression();
128 124
        SelectBuilder select = sqlbuilder.select();
125
        ArrayList<ExpressionBuilder.Value> valuesToRemoveFeatureType = new ArrayList<>();
126
        
127
        if( shouldUseACustomSelect() ) {
128
            String sql = table.getSubquery();
129
            if( StringUtils.containsAny(sql, "${where_filter}", "${and_filter}") ) {
130
                Evaluator filter = query.getFilter();
131
                if (filter != null) {
132
                    String sqlfilter = filter.getSQL();
133
                    if (!StringUtils.isEmpty(sqlfilter)) {
134
                        if (this.helper.supportFilter(this.storeType, filter)) {
135
                            select.where().set(expbuilder.toValue(sqlfilter));
136
                        }
137
                    }
138
                }
139
                if (!StringUtils.isEmpty(baseFilter)) {
140
                    select.where().and(expbuilder.toValue(baseFilter));
141
                }
142
            }
143
            if( StringUtils.containsAny(sql, "${order_by_orderspec}", "${comma_orderspec}") ) {
144
                FeatureQueryOrder order = query.getOrder();
145
                if (order != null) {
146
                    for (FeatureQueryOrderMember member : order.members()) {
147
                        String attrName = member.getAttributeName();
148
                        ExpressionBuilder.Variable col = expbuilder.column(attrName);
149
                        select.order_by().value(col).ascending(member.getAscending());
150
                    }
151
                }
152
            }
153
            if( select.has_where() || select.has_order_by() ) {
154
                sqlbuilder.setProperties(
155
                        null,
156
                        PROP_FEATURE_TYPE, this.storeType,
157
                        PROP_TABLE, table,
158
                        PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(),
159
                        PROP_JDBCHELPER, this.helper,
160
                        PROP_QUERY, this.query
161
                );
162
                for (ExpressionBuilder.Value value : valuesToRemoveFeatureType) {
163
                    value.setProperty(PROP_FEATURE_TYPE, null);
164
                }
165
                if( select.has_where()) {
166
                    String s = select.where().toString();
167
                    sql = StringUtils.replace(sql, "${where_filter}", "WHERE " + s);
168
                    sql = StringUtils.replace(sql, "${and_filter}", "AND (" + s + ")");
169
                }
170
                if( select.has_order_by() ) {
171
                    String s = select.order_by().toString();
172
                    sql = StringUtils.replace(sql, "${order_by_orderspec}", "ORDER BY " + s);
173
                    sql = StringUtils.replace(sql, "${comma_orderspec}", ", "+ s);
174
                }
175
            }
176
            for (FeatureAttributeDescriptor attr : storeType) {
177
                columns.add(attr);
178
            }
179
            return sql;
180
        }
181
        
129 182

  
130 183
        Map<String, EditableFeatureAttributeDescriptor> allExtraColumns = new HashMap<>();
131 184
        for (EditableFeatureAttributeDescriptor column : this.setType.getExtraColumns().getColumns()) {
......
147 200
        if (query != null && query.hasConstantsAttributeNames()) {
148 201
            constantsAttributeNames = query.getConstantsAttributeNames();
149 202
        }
150
        ArrayList<ExpressionBuilder.Value> valuesToRemoveFeatureType = new ArrayList<>();
151 203
        for (FeatureAttributeDescriptor attr : setType) {
152 204
            if (attr.isComputed()) {
153 205
//              if(StringUtils.isNotBlank(System.getenv("ENABLE_COMPUTED_SQL_ATTR"))) { 
......
387 439
                            extraColumnNames.add(attrName);
388 440
                        }
389 441
                    }
390
					ExpressionBuilder.Variable col = expbuilder.column(attrName);
442
                    ExpressionBuilder.Variable col = expbuilder.column(attrName);
391 443
					
392
					// En el groupBy no queremos que se sustituya el nombre del campo calculado
393
					// por su expresion. Se encarga el formater y lo evitamos quitandole el ftype
394
					// al value.
395
					valuesToRemoveFeatureType.add(col);
396
					select.order_by().value(col).ascending(member.getAscending());
444
                    // En el groupBy no queremos que se sustituya el nombre del campo calculado
445
                    // por su expresion. Se encarga el formater y lo evitamos quitandole el ftype
446
                    // al value.
447
                    valuesToRemoveFeatureType.add(col);
448
                    select.order_by().value(col).ascending(member.getAscending());
397 449
//                    select.order_by()
398 450
//                            .column(member.getAttributeName())
399 451
//                            .ascending(member.getAscending());
......
483 535
        return resultSetEntry;
484 536
    }
485 537

  
538
    private boolean shouldUseACustomSelect() {
539
        if( !table.hasSubquery() ) {
540
            return false;
541
        }
542
        if( this.query == null ) {
543
            return false;
544
        }
545
        if( this.query.isUseSubquery() ) {
546
            return false;
547
        }
548
        if( this.query.hasGroupByColumns() ) {
549
            return false;
550
        }
551
        if( this.query.hasAggregateFunctions() ) {
552
            return false;
553
        }        
554
        if( !this.query.hasFilter() && !this.query.hasOrder() ) {
555
            return true;
556
        }
557
//        si el filtro es incompatible con la BBDD
558
//            return false
559
//        si el orden es incompatible con la BBDD
560
//            return false
561
//        si filtro u orden usan la funcion foreing_value 
562
//            return false;
563
        return true;
564
    }
486 565
}

Also available in: Unified diff