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 @ 44748

History | View | Annotate | Download (4.65 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 java.util.Objects;
27
import org.apache.commons.lang3.Range;
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 org.gvsig.expressionevaluator.spi.AbstractFunction;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataManager;
35
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT;
36
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT;
37
import org.gvsig.fmap.dal.DataStore;
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.tools.dispose.DisposeUtils;
42

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

    
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 * 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
            },
64
            "Long",
65
            false
66
    );
67
  }
68

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

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

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

    
84
  @Override
85
  public boolean useArgumentsInsteadObjects() {
86
    return true;
87
  }
88

    
89
  @Override
90
  public Object call(Interpreter interpreter, Object[] args) throws Exception {
91
    throw new UnsupportedOperationException();
92
  }
93

    
94
  @Override
95
  public Object call(Interpreter interpreter, Codes args) throws Exception {
96

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

    
130
  protected DataStore getStore(String storeName) {
131
    DataManager dataManager = DALLocator.getDataManager();
132
    DataStore store = dataManager.getStoresRepository().getStore(storeName);
133
    return store;
134
  }
135

    
136
}