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

History | View | Annotate | Download (4.7 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 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.expressionevaluator.ExpressionEvaluator;
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.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
41
import org.gvsig.tools.dispose.DisposeUtils;
42

    
43
/**
44
 *
45
 * @author jjdelcerro
46
 */
47
@SuppressWarnings("UseSpecificCatch")
48
public class SelectCountFunction extends AbstractSelectFunction {
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 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
    }
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
    private static final int TABLE = 0;
89
    private static final int WHERE = 1;
90

    
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);
123
        }
124
    }
125

    
126
}