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 / SelectCountFromSelectionFunction.java @ 46100

History | View | Annotate | Download (5.34 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.function.Predicate;
27
import org.apache.commons.lang3.Range;
28
import org.gvsig.expressionevaluator.Code;
29
import org.gvsig.expressionevaluator.Codes;
30
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
31
import org.gvsig.expressionevaluator.Interpreter;
32
import org.gvsig.expressionevaluator.impl.DALFunctions;
33
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT_FROM_SELECTION;
34
import org.gvsig.fmap.dal.feature.FeatureSet;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
37
import org.gvsig.tools.evaluator.EvaluatorData;
38
import org.gvsig.tools.evaluator.EvaluatorException;
39
import org.gvsig.tools.util.ContainerUtils;
40
import org.gvsig.tools.util.FilteredIterator;
41

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

    
49
    public SelectCountFromSelectionFunction() {
50
        super(DALFunctions.GROUP_DATA_ACCESS,
51
                FUNCTION_SELECT_COUNT_FROM_SELECTION,
52
                Range.is(3),
53
                "Returns the number of features of the table by applying the filter indicated.\n"
54
                + "The syntax is:\n\n"
55
                + "SELECT COUNT(*) FROM SELECTION [IF NOT EMPTY] OF 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
                "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
    private static final int TABLE = 0;
88
    private static final int WHERE = 1;
89
    private static final int SELECCTION_IF_NOT_EMPTY = 2;
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
        Object selection_if_not_empty = getObject(interpreter, args, SELECCTION_IF_NOT_EMPTY);
97
        
98
        FeatureStore featureStore;
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
            Iterable features = featureStore.getFeatureSelection();
105
            if( ((FeatureSet)features).isEmpty() ) {
106
                if( selection_if_not_empty != null ) {
107
                    features = featureStore.getFeatureSet();
108
                    // En este caso habria que optimizar el where, pero de 
109
                    // momento asi va, aunque lento.
110
                }                
111
            }
112
            if (where != null) {
113
                final Iterable baseSelection = features;
114
                final DefaultFeatureExpressionEvaluator evaluator = new DefaultFeatureExpressionEvaluator(where.toString());
115
                evaluator.toSymbolTable().addSymbolTable(interpreter.getSymbolTable());
116
                final Predicate filter = (Predicate) (Object t) -> {
117
                    try {
118
                        return (boolean) evaluator.evaluate((EvaluatorData) t);
119
                    } catch (EvaluatorException ex1) {
120
                        throw new ExpressionRuntimeException("Can't evaluate expression for row.", ex1);
121
                    }
122
                };
123
                features = (Iterable) () -> new FilteredIterator(baseSelection.iterator(), filter);
124
            }
125
            return ContainerUtils.size64(features);
126

    
127
        } catch (ExpressionRuntimeException ex) {
128
            throw ex;
129
        } catch (Exception ex) {
130
            throw new ExpressionRuntimeException("Problems calling '" + this.name() + "' function", ex);
131
        }
132
    }
133

    
134
}