Statistics
| Revision:

svn-gvsig-desktop / 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 @ 45366

History | View | Annotate | Download (5.09 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.expressionevaluator.impl.function.dataaccess;
25

    
26
import org.apache.commons.lang3.Range;
27
import org.gvsig.expressionevaluator.Code;
28
import org.gvsig.expressionevaluator.Codes;
29
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
30
import org.gvsig.expressionevaluator.Interpreter;
31
import org.gvsig.expressionevaluator.impl.DALFunctions;
32
import static org.gvsig.expressionevaluator.impl.function.dataaccess.SelectFunction.removeOuterTablesReferences;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataManager;
35
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT;
36
import org.gvsig.fmap.dal.DataStore;
37
import org.gvsig.expressionevaluator.ExpressionEvaluator;
38
import org.gvsig.fmap.dal.feature.FeatureQuery;
39
import org.gvsig.fmap.dal.feature.FeatureSet;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
42
import org.gvsig.tools.dispose.DisposeUtils;
43

    
44
/**
45
 *
46
 * @author jjdelcerro
47
 */
48
@SuppressWarnings("UseSpecificCatch")
49
public class SelectCountFunction extends AbstractFeatureFunction {
50

    
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
  }
69

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

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

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

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

    
90
  @Override
91
  public Object call(Interpreter interpreter, Codes args) throws Exception {
92

    
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;
98
        }
99
    }
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

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

    
140
}