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

History | View | Annotate | Download (8.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 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.expressionevaluator.ExpressionEvaluatorLocator;
41
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
42
import org.gvsig.expressionevaluator.Formatter;
43
import static org.gvsig.expressionevaluator.impl.function.dataaccess.SelectFunction.MODE_FROM_SELECTION;
44
import static org.gvsig.expressionevaluator.impl.function.dataaccess.SelectFunction.MODE_FROM_SELECTION_IF_NOT_EMPTY;
45
import static org.gvsig.expressionevaluator.impl.function.dataaccess.SelectFunction.MODE_FROM_STORE;
46
import org.gvsig.fmap.dal.SQLBuilder;
47
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE;
48
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER;
49
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLE;
50
import org.gvsig.fmap.dal.feature.FeatureQuery;
51
import org.gvsig.fmap.dal.feature.FeatureSet;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.dal.feature.FeatureType;
54
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase;
55
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
56
import org.gvsig.tools.dispose.DisposeUtils;
57

    
58
/**
59
 *
60
 * @author jjdelcerro
61
 */
62
@SuppressWarnings("UseSpecificCatch")
63
public class SelectCountFunction extends AbstractSelectFunction {
64

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

    
83
    @Override
84
    public boolean isHidden() {
85
        return false;
86
    }
87

    
88
    @Override
89
    public boolean allowConstantFolding() {
90
        return false;
91
    }
92

    
93
    @Override
94
    public boolean useArgumentsInsteadObjects() {
95
        return true;
96
    }
97

    
98
    @Override
99
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
100
        throw new UnsupportedOperationException();
101
    }
102

    
103
    private static final int TABLE = 0;
104
    private static final int WHERE = 1;
105

    
106
    @Override
107
    public Object call(Interpreter interpreter, Codes args) throws Exception {
108

    
109
        String storeName = this.getIdentifier(args, TABLE);
110
        Code where = this.getWhereCode(args, WHERE);
111

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

    
141
    @Override
142
    public ExpressionBuilder.Value toValue(ExpressionBuilder builder, Codes args) {
143
        try {
144
            SQLBuilder sqlBuilder = (SQLBuilder) builder.getProperty(PROP_SQLBUILDER);
145
            if(sqlBuilder == null){
146
                return super.toValue(builder, args);
147
            }
148
            FeatureType featureType = null;
149
            SQLBuilder.SelectBuilder select = sqlBuilder.createSelectBuilder();
150
            String builderTableName = (String) builder.getProperty(SQLBuilder.PROP_TABLENAME);
151

    
152
            Code tableCode = args.get(TABLE);
153
            Code where = this.getWhereCode(args, WHERE);
154

    
155
            if (tableCode != null) {
156
                if( isHostExpression(tableCode) ) {
157
                    select.from().custom(tableCode.toString());    
158
                } else {
159
                    String tableName = this.getIdentifier(args, TABLE);
160
                    select.from().table().name(tableName);
161
                }
162
            }
163

    
164
            SQLBuilder.TableNameBuilder table = select.from().table();
165
            String tableName = table.getName();
166
            
167
            select.column().value(builder.function(FUNCTION_COUNT, builder.custom("*")));
168

    
169
            if (where != null) {
170
                ExpressionBuilder.Value value = where.toValue(builder);
171
                select.where().value(value);
172
                sqlBuilder.setProperties(value, null, SQLBuilder.PROP_ADD_TABLE_NAME_TO_COLUMNS, true);
173
            }
174

    
175
            if (featureType == null) {
176
                if (StringUtils.equalsIgnoreCase(builderTableName, tableName)) {
177
                    featureType = (FeatureType) builder.getProperty(SQLBuilder.PROP_FEATURE_TYPE);
178
                } else {
179
                    DataManager dataManager = DALLocator.getDataManager();
180
                    featureType = dataManager.getStoresRepository().getFeatureType(tableName);
181
                }
182
            }
183

    
184
            sqlBuilder.setProperties(
185
                    select,
186
                    null,
187
                    PROP_FEATURE_TYPE, featureType,
188
                    PROP_TABLE, table
189
            );
190

    
191
            return builder.group(select);
192
        } catch (Exception ex) {
193
            return super.toValue(builder, args);
194
        }
195
    }
196

    
197
    @Override
198
    public String toString(Codes args, Formatter<Code> formatter) {
199
        SQLBuilderBase sqlbuilder = new SQLBuilderBase();
200
        ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getExpressionEvaluatorManager();
201
        ExpressionBuilder expressionBuilder = expressionManager.createExpressionBuilder();
202
        expressionBuilder.setProperty(PROP_SQLBUILDER, sqlbuilder);
203
        
204
        ExpressionBuilder.Value values = this.toValue(expressionBuilder, args);
205
                
206
        return values.toString();
207
    }
208
        
209
}