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

History | View | Annotate | Download (4.47 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.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.DataManager;
34
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT;
35
import org.gvsig.fmap.dal.DataStore;
36
import org.gvsig.fmap.dal.feature.FeatureQuery;
37
import org.gvsig.fmap.dal.feature.FeatureSet;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.tools.dispose.DisposeUtils;
40

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

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

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

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

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

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

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

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

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

    
129
}