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

History | View | Annotate | Download (6.52 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.fmap.dal.feature.impl.DefaultFeatureQueryOrder;
43

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

    
53
    public SelectFunction() {
54
        super(DALFunctions.GROUP_DATA_ACCESS,
55
                FUNCTION_SELECT,
56
                Range.is(6),
57
                "Returns a list of features of the table by applying the filter, order and limit indicated.\n"
58
                + "The syntax is:\n\n"
59
                + "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"
60
                + "Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"
61
                + "You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"
62
                + "The SELECT statement must always end with a semicolon.",
63
                "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;",
64
                new String[]{
65
                    "column_names/asterisk - Names of the columns table to retrieve.",
66
                    "table_name - Name of the table",
67
                    "filter - boolean expression to apply as filter",
68
                    "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.",
69
                    "limit - Maximum number of features to return"
70
                },
71
                "List",
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 COLUMNS = 0;
97
    private static final int TABLE = 1;
98
    private static final int WHERE = 2;
99
    private static final int ORDER = 3;
100
    private static final int ORDER_MODE = 4;
101
    private static final int LIMIT = 5;
102

    
103
    @Override
104
    public Object call(Interpreter interpreter, Codes args) throws Exception {
105

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

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

    
145
            // FIXME: add columns to query.addAttributeName() 
146
            query.retrievesAllAttributes();
147
            features = featureStore.getFeatures(query);
148
            return features;
149

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

    
157
    @Override
158
    public Code optimize(Optimizer optimizer, Callable caller) {
159
        return caller; // Don't optimize SELECT
160
    }
161

    
162
}