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

History | View | Annotate | Download (4.51 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_COUNT;
36
import org.gvsig.fmap.dal.DataStore;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
import org.gvsig.fmap.dal.feature.FeatureSet;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.tools.dispose.DisposeUtils;
41

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

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

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

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

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

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

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

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

    
124
  protected DataStore getStore(String storeName) {
125
    DataManager dataManager = DALLocator.getDataManager();
126
    DataStore store = dataManager.getStoresRepository().getStore(storeName);
127
    return store;
128
  }
129

    
130
}