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 / SelectFunction.java @ 46505

History | View | Annotate | Download (8.66 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.List;
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.Optimizer;
33
import org.gvsig.expressionevaluator.impl.DALFunctions;
34
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT;
35
import org.gvsig.expressionevaluator.ExpressionEvaluator;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator;
41
import org.gvsig.expressionevaluator.Code.Callable;
42
import org.gvsig.expressionevaluator.ExpressionBuilder;
43
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
44
import org.gvsig.fmap.dal.SQLBuilder;
45
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQueryOrder;
46

    
47
/**
48
 *
49
 * @author jjdelcerro
50
 */
51
@SuppressWarnings("UseSpecificCatch")
52
public class SelectFunction
53
        extends AbstractSelectFunction
54
        implements Optimizer.FunctionOptimizer {
55

    
56
    public SelectFunction() {
57
        super(DALFunctions.GROUP_DATA_ACCESS,
58
                FUNCTION_SELECT,
59
                Range.is(6),
60
                "Returns a list of features of the table by applying the filter, order and limit indicated.\n"
61
                + "The syntax is:\n\n"
62
                + "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"
63
                + "Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"
64
                + "You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"
65
                + "The SELECT statement must always end with a semicolon.",
66
                "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;",
67
                new String[]{
68
                    "column_names/asterisk - Names of the columns table to retrieve.",
69
                    "table_name - Name of the table",
70
                    "filter - boolean expression to apply as filter",
71
                    "order_column - the order used to retrieve the features. It is a list of column names separated by a comma. The column name can optionally be followed by ASC or DESC to indicate whether the order should be ascending or descending.",
72
                    "limit - Maximum number of features to return"
73
                },
74
                "List",
75
                true
76
        );
77
    }
78

    
79
    @Override
80
    public boolean isHidden() {
81
        return false;
82
    }
83

    
84
    @Override
85
    public boolean allowConstantFolding() {
86
        return false;
87
    }
88

    
89
    @Override
90
    public boolean useArgumentsInsteadObjects() {
91
        return true;
92
    }
93

    
94
    @Override
95
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
96
        throw new UnsupportedOperationException();
97
    }
98

    
99
    private static final int COLUMNS = 0;
100
    private static final int TABLE = 1;
101
    private static final int WHERE = 2;
102
    private static final int ORDER = 3;
103
    private static final int ORDER_MODE = 4;
104
    private static final int LIMIT = 5;
105

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

    
109
        String storeName = this.getTableName(args, TABLE);
110
        Code columns = getTupleOrNull(args, COLUMNS);
111
        Code where = this.getWhereCode(args, WHERE);
112
        Number limit = (Number) getObject(interpreter, args, LIMIT);
113

    
114
        Callable order = getTupleOrNull(args, ORDER);
115
        Callable order_mode = getTupleOrNull(args, ORDER_MODE);
116
        FeatureQueryOrder queryOrder = null;
117
        if (order != null || order_mode != null) {
118
            for (int n = 0; n < order.parameters().size(); n++) {
119
                String member = (String) interpreter.run(order.parameters().get(n));
120
                Boolean mode = (Boolean) interpreter.run(order_mode.parameters().get(n));
121
                if (queryOrder == null) {
122
                    queryOrder = new DefaultFeatureQueryOrder();
123
                }
124
                queryOrder.add(member, mode);
125
            }
126
        }
127
        FeatureStore featureStore;
128
        try {
129
            featureStore = this.getFeatureStore(storeName);
130
            if (featureStore == null) {
131
                throw new ExpressionRuntimeException("Cant locate the feature store '" + storeName + "' in function '" + this.name() + "'.");
132
            }
133
            List<Feature> features;
134
            FeatureQuery query = featureStore.createFeatureQuery();
135
            if (where != null) {
136
                Code where2 = removeOuterTablesReferences(interpreter, where);
137
                ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
138
                filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable());
139
                query.addFilter(filter);
140
            }
141
            if (queryOrder != null) {
142
                query.getOrder().copyFrom(queryOrder);
143
            }
144
            if (limit != null) {
145
                query.setLimit(limit.longValue());
146
            }
147

    
148
            // FIXME: add columns to query.addAttributeName() 
149
            query.retrievesAllAttributes();
150
            features = featureStore.getFeatures(query);
151
            return features;
152

    
153
        } catch (ExpressionRuntimeException ex) {
154
            throw ex;
155
        } catch (Exception ex) {
156
            throw new ExpressionRuntimeException("Problems calling '" + this.name() + "' function", ex);
157
        }
158
    }
159

    
160
    @Override
161
    public Code optimize(Optimizer optimizer, Callable caller) {
162
        return caller; // Don't optimize SELECT
163
    }
164

    
165
    @Override
166
    public ExpressionBuilder.Value toValue(ExpressionBuilder builder, Codes args) {
167
        try {
168
            SQLBuilder sqlBuilder = (SQLBuilder) builder.getProperty("SQLBUILDER");
169
            if(sqlBuilder == null){
170
                return super.toValue(builder, args);
171
            }
172
            SQLBuilder.SelectBuilder select = sqlBuilder.createSelectBuilder();
173

    
174
            String storeName = this.getTableName(args, TABLE);
175
            Callable columns = getTupleOrNull(args, COLUMNS);
176
            Code where = this.getWhereCode(args, WHERE);
177
            Callable order = getTupleOrNull(args, ORDER);
178
            Callable order_mode = getTupleOrNull(args, ORDER_MODE);
179
            Code limit = args.get(LIMIT);
180

    
181
            if (storeName != null) {
182
                select.from().table().name(storeName);
183
            }
184

    
185
            if (columns != null) {
186
                for (Code column : columns.parameters()) {
187
                    if (column instanceof Code.Identifier) {
188
                        select.column().name(
189
                                select.from().table(),
190
                                ((Code.Identifier) column).name()
191
                        );
192
                    }
193
                }
194
            }
195

    
196
            if (where != null) {
197
                select.where().value(where.toValue(builder));
198
            }
199

    
200
            if (limit != null) {
201
                select.limit(((Number) ((Code.Constant) limit).value()).longValue());
202
            }
203

    
204
            if (order != null || order_mode != null) {
205
                for (int n = 0; n < order.parameters().size(); n++) {
206
                    Code member = order.parameters().get(n);
207
                    Code.Constant mode = (Code.Constant) order_mode.parameters().get(n);
208
                    select.order_by().value(member.toValue(builder)).ascending((boolean) mode.value());
209
                }
210
            }
211
            return select;
212
        } catch (Exception ex) {
213
            return super.toValue(builder, args);
214
        }
215
    }
216
    
217
    
218

    
219
}