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/SelectCountFunction.java

View differences:

SelectCountFunction.java
29 29
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
30 30
import org.gvsig.expressionevaluator.Interpreter;
31 31
import org.gvsig.expressionevaluator.impl.DALFunctions;
32
import static org.gvsig.expressionevaluator.impl.function.dataaccess.SelectFunction.removeOuterTablesReferences;
33 32
import org.gvsig.fmap.dal.DALLocator;
34 33
import org.gvsig.fmap.dal.DataManager;
35 34
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT;
......
46 45
 * @author jjdelcerro
47 46
 */
48 47
@SuppressWarnings("UseSpecificCatch")
49
public class SelectCountFunction extends AbstractFeatureFunction {
48
public class SelectCountFunction extends AbstractSelectFunction {
50 49

  
51
  public SelectCountFunction() {
52
    super(DALFunctions.GROUP_DATA_ACCESS,
53
            FUNCTION_SELECT_COUNT,
54
            Range.between(1,2),
55
            "Returns the number of features of the table by applying the filter indicated.\n" +
56
                    "The syntax is:\n\n"+
57
                    "SELECT COUNT(*) FROM table WHERE boolean_expression;\n\n"+
58
                    "Indicate a filter expression with WHERE is optional.\n"+
59
                    "The SELECT statement must always end with a semicolon.",
60
            "SELECT COUNT(*) FROM {{table}} WHERE filter ;",
61
            new String[]{
62
              "table - Name of the table",
63
              "filter - boolean expression with the filter to apply",
64
            },
65
            "Long",
66
            false
67
    );
68
  }
50
    public SelectCountFunction() {
51
        super(DALFunctions.GROUP_DATA_ACCESS,
52
                FUNCTION_SELECT_COUNT,
53
                Range.between(1, 2),
54
                "Returns the number of features of the table by applying the filter indicated.\n"
55
                + "The syntax is:\n\n"
56
                + "SELECT COUNT(*) FROM table WHERE boolean_expression;\n\n"
57
                + "Indicate a filter expression with WHERE is optional.\n"
58
                + "The SELECT statement must always end with a semicolon.",
59
                "SELECT COUNT(*) FROM {{table}} WHERE filter ;",
60
                new String[]{
61
                    "table - Name of the table",
62
                    "filter - boolean expression with the filter to apply",},
63
                "Long",
64
                false
65
        );
66
    }
69 67

  
70
  @Override
71
  public boolean isHidden() {
72
    return false;
73
  }
68
    @Override
69
    public boolean isHidden() {
70
        return false;
71
    }
74 72

  
75
  @Override
76
  public boolean allowConstantFolding() {
77
    return false;
78
  }
73
    @Override
74
    public boolean allowConstantFolding() {
75
        return false;
76
    }
79 77

  
80
  @Override
81
  public boolean useArgumentsInsteadObjects() {
82
    return true;
83
  }
78
    @Override
79
    public boolean useArgumentsInsteadObjects() {
80
        return true;
81
    }
84 82

  
85
  @Override
86
  public Object call(Interpreter interpreter, Object[] args) throws Exception {
87
    throw new UnsupportedOperationException();
88
  }
83
    @Override
84
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
85
        throw new UnsupportedOperationException();
86
    }
89 87

  
90
  @Override
91
  public Object call(Interpreter interpreter, Codes args) throws Exception {
88
    private static final int TABLE = 0;
89
    private static final int WHERE = 1;
92 90

  
93
    String storeName =  (String) getObject(interpreter, args, 0);
94
    Code where = args.get(1);
95
    if( where.code()==Code.CONSTANT ) {
96
        if( ((Code.Constant)where).value()==null ) {
97
            where = null;
91
    @Override
92
    public Object call(Interpreter interpreter, Codes args) throws Exception {
93

  
94
        String storeName = this.getTableName(args, TABLE);
95
        Code where = this.getWhereCode(args, WHERE);
96

  
97
        FeatureStore featureStore = null;
98
        FeatureSet set = null;
99
        try {
100
            featureStore = this.getFeatureStore(storeName);
101
            if (featureStore == null) {
102
                throw new ExpressionRuntimeException("Cant locate the feature store '" + storeName + "' in function '" + this.name() + "'.");
103
            }
104
            if (where == null) {
105
                set = featureStore.getFeatureSet();
106
            } else {
107
                FeatureQuery query = featureStore.createFeatureQuery();
108
                Code where2 = removeOuterTablesReferences(interpreter, where);
109
                ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
110
                filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable());
111
                query.addFilter(filter);
112
                query.retrievesAllAttributes();
113
                set = featureStore.getFeatureSet(query);
114
            }
115
            return set.getSize();
116
        } catch (ExpressionRuntimeException ex) {
117
            throw ex;
118
        } catch (Exception ex) {
119
            throw new ExpressionRuntimeException("Problems calling '" + this.name() + "' function", ex);
120
        } finally {
121
            DisposeUtils.disposeQuietly(set);
122
            DisposeUtils.disposeQuietly(featureStore);
98 123
        }
99 124
    }
100
    
101
    FeatureStore featureStore = null;
102
    FeatureSet set = null;
103
    try {
104
      DataStore store = this.getStore(storeName);
105
      if (store == null ) {
106
        throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_SELECT_COUNT + "'.");
107
      }
108
      if (!(store instanceof FeatureStore)) {
109
        throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_SELECT_COUNT + "', a FeatureStore is required.");
110
      }
111
      featureStore = (FeatureStore) store;
112
      if (where == null ) {
113
        set = featureStore.getFeatureSet();
114
      } else {
115
          FeatureQuery query = featureStore.createFeatureQuery();
116
          Code where2 = removeOuterTablesReferences(interpreter, where);
117
          ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
118
          filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable());
119
          query.addFilter(filter);
120
          query.retrievesAllAttributes();
121
          set = featureStore.getFeatureSet(query);
122
      }
123
      return set.getSize();
124
    } catch (ExpressionRuntimeException ex) {
125
      throw ex;
126
    } catch (Exception ex) {
127
      throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT_COUNT + "' function", ex);
128
    } finally {
129
      DisposeUtils.disposeQuietly(set);
130
      DisposeUtils.disposeQuietly(featureStore);
131
    }
132
  }
133 125

  
134
  protected DataStore getStore(String storeName) {
135
    DataManager dataManager = DALLocator.getDataManager();
136
    DataStore store = dataManager.getStoresRepository().getStore(storeName);
137
    return store;
138
  }
139

  
140 126
}

Also available in: Unified diff