Revision 46088 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/dataaccess/SelectFunction.java

View differences:

SelectFunction.java
43 43
import org.gvsig.expressionevaluator.Optimizer;
44 44
import org.gvsig.expressionevaluator.SymbolTable;
45 45
import org.gvsig.expressionevaluator.impl.DALFunctions;
46
import org.gvsig.fmap.dal.DALLocator;
47
import org.gvsig.fmap.dal.DataManager;
48 46
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT;
49 47
import org.gvsig.fmap.dal.DataStore;
50 48
import org.gvsig.expressionevaluator.ExpressionEvaluator;
......
56 54
import org.gvsig.fmap.dal.feature.FeatureStore;
57 55
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
58 56
import org.gvsig.expressionevaluator.Code.Callable;
59
import org.gvsig.expressionevaluator.spi.AbstractFunction;
60 57
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQueryOrder;
61 58

  
62 59
/**
......
64 61
 * @author jjdelcerro
65 62
 */
66 63
@SuppressWarnings("UseSpecificCatch")
67
public class SelectFunction 
68
        extends AbstractFunction 
69
        implements Optimizer.FunctionOptimizer
70
  {
64
public class SelectFunction
65
        extends AbstractSelectFunction
66
        implements Optimizer.FunctionOptimizer {
71 67

  
72
  public SelectFunction() {
73
    super(DALFunctions.GROUP_DATA_ACCESS,
74
            FUNCTION_SELECT,
75
            Range.is(6),
76
            "Returns a list of features of the table by applying the filter, order and limit indicated.\n"+
77
                "The syntax is:\n\n"+
78
                "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"+
79
                "Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"+
80
                "You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"+
81
                "The SELECT statement must always end with a semicolon.",
82
            "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;",
83
            new String[]{
84
              "column_names/asterisk - Names of the columns table to retrieve.",
85
              "table_name - Name of the table",
86
              "filter - boolean expression to apply as filter",
87
              "order_column - the order used to retrieve the features. It is a list of column names separated by a comma. The column name can optionally be followed by ASC or DESC to indicate whether the order should be ascending or descending.",
88
              "limit - Maximum number of features to return"
89
            },
90
            "List",
91
            true
92
    );
93
  }
68
    public SelectFunction() {
69
        super(DALFunctions.GROUP_DATA_ACCESS,
70
                FUNCTION_SELECT,
71
                Range.is(6),
72
                "Returns a list of features of the table by applying the filter, order and limit indicated.\n"
73
                + "The syntax is:\n\n"
74
                + "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"
75
                + "Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"
76
                + "You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"
77
                + "The SELECT statement must always end with a semicolon.",
78
                "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;",
79
                new String[]{
80
                    "column_names/asterisk - Names of the columns table to retrieve.",
81
                    "table_name - Name of the table",
82
                    "filter - boolean expression to apply as filter",
83
                    "order_column - the order used to retrieve the features. It is a list of column names separated by a comma. The column name can optionally be followed by ASC or DESC to indicate whether the order should be ascending or descending.",
84
                    "limit - Maximum number of features to return"
85
                },
86
                "List",
87
                true
88
        );
89
    }
94 90

  
95
  @Override
96
  public boolean isHidden() {
97
    return false;
98
  }
91
    @Override
92
    public boolean isHidden() {
93
        return false;
94
    }
99 95

  
100
  @Override
101
  public boolean allowConstantFolding() {
102
    return false;
103
  }
96
    @Override
97
    public boolean allowConstantFolding() {
98
        return false;
99
    }
104 100

  
105
  @Override
106
  public boolean useArgumentsInsteadObjects() {
107
    return true;
108
  }
109
  
110
  @Override
111
  public Object call(Interpreter interpreter, Object[] args) throws Exception {
112
    throw new UnsupportedOperationException();
113
  }
114

  
115
  private static final int COLUMNS = 0;
116
  private static final int TABLE = 1;
117
  private static final int WHERE = 2;
118
  private static final int ORDER = 3;
119
  private static final int ORDER_MODE = 4;
120
  private static final int LIMIT = 5;
121
  
122
  
123
  private Callable getTupleOrNull(Codes args, int argn) {
124
    Code code = args.get(argn);
125
    if( code.code()==Code.CONSTANT ) {
126
      if( ((Code.Constant)code).value()!=null ) {
127
        throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'.");
128
      }
129
      return null;
101
    @Override
102
    public boolean useArgumentsInsteadObjects() {
103
        return true;
130 104
    }
131
    if( code.code()!=Code.CALLABLE ) {
132
      throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'.");
133
    }
134
    Callable caller = (Callable) code;
135
    if( !StringUtils.equalsIgnoreCase(FUNCTION_TUPLE, caller.name()) ) {
136
      throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'.");
137
    }
138
    return caller;
139
  }
140
  
141
  @Override
142
  public Object call(Interpreter interpreter, Codes args) throws Exception {
143 105

  
144
    Code.Identifier storeName =  (Code.Identifier) args.get(TABLE);
145
    Code columns = getTupleOrNull(args, COLUMNS);
146
    Code where = args.get(WHERE);
147
    if( where.code()==Code.CONSTANT ) {
148
        if( ((Code.Constant)where).value()==null ) {
149
            where = null;
150
        }
106
    @Override
107
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
108
        throw new UnsupportedOperationException();
151 109
    }
152
    Number limit = (Number) getObject(interpreter, args, LIMIT);
153
    Callable order = getTupleOrNull(args, ORDER);
154
    Callable order_mode = getTupleOrNull(args, ORDER_MODE);
155
    
156
    FeatureQueryOrder queryOrder = null;
157
    if( order!=null || order_mode!=null ) {
158
      for( int n=0 ; n<order.parameters().size(); n++) {
159
        String member = (String) interpreter.run(order.parameters().get(n));
160
        Boolean mode = (Boolean) interpreter.run(order_mode.parameters().get(n));
161
        if( queryOrder == null ) {
162
          queryOrder = new DefaultFeatureQueryOrder();
163
        }
164
        queryOrder.add(member, mode);
165
      }
166
    }
167
    // FIXME: add columns to query.addAttributeName() 
168
    try {
169
      DataStore store = this.getStore(storeName.name());
170
      if (store == null ) {
171
        throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_SELECT + "'.");
172
      }
173
      if (!(store instanceof FeatureStore)) {
174
        throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_SELECT + "', a FeatureStore is required.");
175
      }
176
      FeatureStore featureStore = (FeatureStore) store;
177
      List<Feature> features;
178
      if (where == null && queryOrder == null && limit==null ) {
179
        features = featureStore.getFeatures();
180
      } else {
181
        FeatureQuery query = featureStore.createFeatureQuery();
182
        if (where != null) {
183
          Code where2 = removeOuterTablesReferences(interpreter, where);
184
          ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
185
          filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable());
186
          query.addFilter(filter);
187
        }
188
        if (queryOrder != null) {
189
          query.getOrder().copyFrom(queryOrder);
190
        }
191
        if( limit!=null ) {
192
          query.setLimit(limit.longValue());
193
        }
194
        query.retrievesAllAttributes();
195
        features = featureStore.getFeatures(query);
196
      }
197
      return features;
198
    } catch (ExpressionRuntimeException ex) {
199
      throw ex;
200
    } catch (Exception ex) {
201
      throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT + "' function", ex);
202
    }
203
  }
204 110

  
205
  protected DataStore getStore(String storeName) {
206
    DataManager dataManager = DALLocator.getDataManager();
207
    DataStore store = dataManager.getStoresRepository().getStore(storeName);
208
    return store;
209
  }
111
    private static final int COLUMNS = 0;
112
    private static final int TABLE = 1;
113
    private static final int WHERE = 2;
114
    private static final int ORDER = 3;
115
    private static final int ORDER_MODE = 4;
116
    private static final int LIMIT = 5;
210 117

  
211
  public static Code removeOuterTablesReferences(Interpreter interpreter, Code where) {
212
    try {
213
      SymbolTable symbolTable = interpreter.getSymbolTable();
214
      TableAttributeHandler table = (TableAttributeHandler) symbolTable.value(SYMBOL_CURRENT_TABLE);
215
      List<Pair<Code,Code>>replaces = new ArrayList<>();
216
      CodeBuilder codeBuilder = ExpressionUtils.createCodeBuilder();
217
      final Set<Code> replacesToASkip = new HashSet<>();
218
      Code where2 = where.clone();
219
      where2.accept((Object o) -> {
220
        if( o == null ) {
221
            return;
222
        }
223
        Code code = (Code) o;
224
        switch(code.code()) {
225
            case Code.CALLABLE:
226
                Code.Callable caller = (Code.Callable) code;
227
                if( StringUtils.equalsIgnoreCase(caller.name(),FUNCTION_GETATTR) ) {
228
                  Codes args = caller.parameters();
229
                  Code arg0 = args.get(0);
230
                  Code arg1 = args.get(1);
231
                  replacesToASkip.add(arg1);
232
                  if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant ) {
233
                    Object tt = symbolTable.value(((Code.Identifier)arg0).name());
234
                    if( tt instanceof TableAttributeHandler && 
235
                        StringUtils.equalsIgnoreCase(((TableAttributeHandler)tt).getName(), table.getName()) ) {
236
                      String columnName = Objects.toString(((Code.Constant)arg1).value(), null);
237
                      if( columnName!=null ) {
238
                        Object value = table.get(columnName);
239
                        replaces.add(
240
                                new ImmutablePair<>(
241
                                        caller,
242
                                        codeBuilder.constant(value)
243
                                )
244
                        );
245
                      }
246
                    }
247
                  }
118
    @Override
119
    public Object call(Interpreter interpreter, Codes args) throws Exception {
120

  
121
        String storeName = this.getTableName(args, TABLE);
122
        Code columns = getTupleOrNull(args, COLUMNS);
123
        Code where = this.getWhereCode(args, WHERE);
124
        Number limit = (Number) getObject(interpreter, args, LIMIT);
125

  
126
        Callable order = getTupleOrNull(args, ORDER);
127
        Callable order_mode = getTupleOrNull(args, ORDER_MODE);
128
        FeatureQueryOrder queryOrder = null;
129
        if (order != null || order_mode != null) {
130
            for (int n = 0; n < order.parameters().size(); n++) {
131
                String member = (String) interpreter.run(order.parameters().get(n));
132
                Boolean mode = (Boolean) interpreter.run(order_mode.parameters().get(n));
133
                if (queryOrder == null) {
134
                    queryOrder = new DefaultFeatureQueryOrder();
248 135
                }
249
                break;
136
                queryOrder.add(member, mode);
137
            }
250 138
        }
251
      });
252
      where2.accept((Object o) -> {
253
        if( o == null ) {
254
            return;
139
        FeatureStore featureStore = null;
140
        try {
141
            featureStore = this.getFeatureStore(storeName);
142
            if (featureStore == null) {
143
                throw new ExpressionRuntimeException("Cant locate the feature store '" + storeName + "' in function '" + this.name() + "'.");
144
            }
145
            List<Feature> features;
146
            FeatureQuery query = featureStore.createFeatureQuery();
147
            if (where != null) {
148
                Code where2 = removeOuterTablesReferences(interpreter, where);
149
                ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
150
                filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable());
151
                query.addFilter(filter);
152
            }
153
            if (queryOrder != null) {
154
                query.getOrder().copyFrom(queryOrder);
155
            }
156
            if (limit != null) {
157
                query.setLimit(limit.longValue());
158
            }
159

  
160
            // FIXME: add columns to query.addAttributeName() 
161
            query.retrievesAllAttributes();
162
            features = featureStore.getFeatures(query);
163
            return features;
164

  
165
        } catch (ExpressionRuntimeException ex) {
166
            throw ex;
167
        } catch (Exception ex) {
168
            throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT + "' function", ex);
255 169
        }
256
        Code code = (Code) o;
257
        if( replacesToASkip.contains(code) ) {
258
            return;
259
        }
260
        switch(code.code()) {
261
            case Code.IDENTIFIER:
262
                Code.Identifier id = (Code.Identifier)code;
263
                if( symbolTable.exists(id.name()) ) {
264
                    Object value = symbolTable.value(id.name());
265
                    replaces.add(
266
                            new ImmutablePair<>(
267
                                    id,
268
                                    codeBuilder.constant(value)
269
                            )
270
                    );
271
                }
272
                break;
273
        }
274
      });
275
      if( replaces.isEmpty() ) {
276
          return where;
277
      }
278
      for (Pair<Code, Code> replace : replaces) {
279
        if( replace!=null ) {
280
          where2.replace(replace.getLeft(), replace.getRight());
281
        }
282
      }
283
      return where2;
284
    } catch (Exception ex) {
285
      throw new ExpressionRuntimeException("Can't remove references to outer tables.", ex);
286 170
    }
287
  }
288 171

  
289
  @Override
290
  public Code optimize(Optimizer optimizer, Callable caller) {
291
    return caller; // Don't optimize SELECT
292
  }
172
    @Override
173
    public Code optimize(Optimizer optimizer, Callable caller) {
174
        return caller; // Don't optimize SELECT
175
    }
293 176

  
294 177
}

Also available in: Unified diff