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

History | View | Annotate | Download (7.12 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.apache.commons.lang3.StringUtils;
28
import org.gvsig.expressionevaluator.Code;
29
import org.gvsig.expressionevaluator.Codes;
30
import org.gvsig.expressionevaluator.ExpressionBuilder;
31
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_COUNT;
32
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
33
import org.gvsig.expressionevaluator.Interpreter;
34
import org.gvsig.expressionevaluator.impl.DALFunctions;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT;
38
import org.gvsig.fmap.dal.DataStore;
39
import org.gvsig.expressionevaluator.ExpressionEvaluator;
40
import org.gvsig.fmap.dal.SQLBuilder;
41
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE;
42
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER;
43
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLE;
44
import org.gvsig.fmap.dal.feature.FeatureQuery;
45
import org.gvsig.fmap.dal.feature.FeatureSet;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.FeatureType;
48
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
49
import org.gvsig.tools.dispose.DisposeUtils;
50

    
51
/**
52
 *
53
 * @author jjdelcerro
54
 */
55
@SuppressWarnings("UseSpecificCatch")
56
public class SelectCountFunction extends AbstractSelectFunction {
57

    
58
    public SelectCountFunction() {
59
        super(DALFunctions.GROUP_DATA_ACCESS,
60
                FUNCTION_SELECT_COUNT,
61
                Range.between(1, 2),
62
                "Returns the number of features of the table by applying the filter indicated.\n"
63
                + "The syntax is:\n\n"
64
                + "SELECT COUNT(*) FROM table WHERE boolean_expression;\n\n"
65
                + "Indicate a filter expression with WHERE is optional.\n"
66
                + "The SELECT statement must always end with a semicolon.",
67
                "SELECT COUNT(*) FROM {{table}} WHERE filter ;",
68
                new String[]{
69
                    "table - Name of the table",
70
                    "filter - boolean expression with the filter to apply",},
71
                "Long",
72
                true
73
        );
74
    }
75

    
76
    @Override
77
    public boolean isHidden() {
78
        return false;
79
    }
80

    
81
    @Override
82
    public boolean allowConstantFolding() {
83
        return false;
84
    }
85

    
86
    @Override
87
    public boolean useArgumentsInsteadObjects() {
88
        return true;
89
    }
90

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

    
96
    private static final int TABLE = 0;
97
    private static final int WHERE = 1;
98

    
99
    @Override
100
    public Object call(Interpreter interpreter, Codes args) throws Exception {
101

    
102
        String storeName = this.getIdentifier(args, TABLE);
103
        Code where = this.getWhereCode(args, WHERE);
104

    
105
        FeatureStore featureStore = null;
106
        FeatureSet set = null;
107
        try {
108
            featureStore = this.getFeatureStore(storeName);
109
            if (featureStore == null) {
110
                throw new ExpressionRuntimeException("Cant locate the feature store '" + storeName + "' in function '" + this.name() + "'.");
111
            }
112
            if (where == null) {
113
                set = featureStore.getFeatureSet();
114
            } else {
115
                FeatureQuery query = featureStore.createFeatureQuery();
116
                Code where2 = removeOuterTablesReferences(interpreter, where);
117
                ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
118
                filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable());
119
                query.addFilter(filter);
120
                query.retrievesAllAttributes();
121
                set = featureStore.getFeatureSet(query);
122
            }
123
            return set.getSize();
124
        } catch (ExpressionRuntimeException ex) {
125
            throw ex;
126
        } catch (Exception ex) {
127
            throw new ExpressionRuntimeException("Problems calling '" + this.name() + "' function", ex);
128
        } finally {
129
            DisposeUtils.disposeQuietly(set);
130
            DisposeUtils.disposeQuietly(featureStore);
131
        }
132
    }
133

    
134
    @Override
135
    public ExpressionBuilder.Value toValue(ExpressionBuilder builder, Codes args) {
136
        try {
137
            SQLBuilder sqlBuilder = (SQLBuilder) builder.getProperty(PROP_SQLBUILDER);
138
            if(sqlBuilder == null){
139
                return super.toValue(builder, args);
140
            }
141
            FeatureType featureType = null;
142
            SQLBuilder.SelectBuilder select = sqlBuilder.createSelectBuilder();
143
            String builderTableName = (String) builder.getProperty(SQLBuilder.PROP_TABLENAME);
144

    
145
            String storeName = this.getIdentifier(args, TABLE);
146
            Code where = this.getWhereCode(args, WHERE);
147

    
148
            if (storeName != null) {
149
                select.from().table().name(storeName);
150
            }
151

    
152
            SQLBuilder.TableNameBuilder table = select.from().table();
153
            String tableName = table.getName();
154
            
155
            select.column().value(builder.function(FUNCTION_COUNT, builder.custom("*")));
156

    
157
            if (where != null) {
158
                ExpressionBuilder.Value value = where.toValue(builder);
159
                select.where().value(value);
160
                sqlBuilder.setProperties(value, null, SQLBuilder.PROP_ADD_TABLE_NAME_TO_COLUMNS, true);
161
            }
162

    
163
            if (featureType == null) {
164
                if (StringUtils.equalsIgnoreCase(builderTableName, tableName)) {
165
                    featureType = (FeatureType) builder.getProperty(SQLBuilder.PROP_FEATURE_TYPE);
166
                } else {
167
                    DataManager dataManager = DALLocator.getDataManager();
168
                    featureType = dataManager.getStoresRepository().getFeatureType(tableName);
169
                }
170
            }
171

    
172
            sqlBuilder.setProperties(
173
                    select,
174
                    null,
175
                    PROP_FEATURE_TYPE, featureType,
176
                    PROP_TABLE, table
177
            );
178

    
179
            return builder.group(select);
180
        } catch (Exception ex) {
181
            return super.toValue(builder, args);
182
        }
183
    }
184
    
185
}