Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / operations / ResultSetForSetProviderOperation.java @ 43355

History | View | Annotate | Download (6.37 KB)

1 43020 jjdelcerro
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
2
3
import java.sql.Connection;
4
import java.util.ArrayList;
5
import java.util.List;
6
import org.apache.commons.lang3.StringUtils;
7 43355 jjdelcerro
import org.gvsig.fmap.dal.ExpressionBuilder.Config;
8 43020 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
10
import org.gvsig.fmap.dal.feature.FeatureQuery;
11
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
12 43026 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
13 43020 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
15
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler.ResultSetEntry;
16
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
17
import org.gvsig.fmap.geom.DataTypes;
18
import org.gvsig.tools.evaluator.Evaluator;
19
20
public class ResultSetForSetProviderOperation extends AbstractConnectionOperation {
21
    private final String schema;
22
    private final String table;
23
    private final String subquery;
24
    private final String baseFilter;
25
    private final String baseOrder;
26
    private final FeatureType storeType;
27
    private final FeatureType setType;
28
    private final FeatureQuery query;
29
    private final long limit;
30
    private final long offset;
31
    private final int fetchSize;
32
    private final String database;
33
34
    public ResultSetForSetProviderOperation(
35
            JDBCHelper helper,
36
            String database,
37
            String schema,
38
            String table,
39
            String subquery,
40
            String baseFilter,
41
            String baseOrder,
42
            FeatureQuery query,
43
            FeatureType storeType,
44
            FeatureType setType,
45
            long limit,
46
            long offset,
47
            int fetchSize
48
        ) {
49
        super(helper);
50
        this.database = database;
51
        this.schema = schema;
52
        this.table = table;
53
        this.subquery = subquery;
54
        this.baseFilter = baseFilter;
55
        this.baseOrder = baseOrder;
56
        this.storeType = storeType;
57
        this.setType = setType;
58
        this.query = query;
59
        this.limit = limit;
60
        this.offset = offset;
61
        this.fetchSize = fetchSize;
62
    }
63
64
    @Override
65
    public final Object perform(Connection conn) throws DataException {
66
        ResultSetEntry rs = createResultSet(
67
                database, schema, table, subquery,
68
                baseFilter, baseOrder, storeType, setType, query,
69
                limit, offset, fetchSize);
70
        return rs;
71
    }
72
73
    public ResultSetEntry createResultSet(
74
            String database,
75
            String schema,
76
            String table,
77
            String subquery,
78
            String baseFilter,
79
            String baseOrder,
80
            FeatureType storeType,
81
            FeatureType setType,
82
            FeatureQuery query,
83
            long limit,
84
            long offset,
85
            int fetchSize
86
        ) throws DataException {
87
88 43355 jjdelcerro
        double tolerance = -1 ; //query.getScale();
89 43020 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
90
91
        List<String> primaryKeys = new ArrayList<>();
92
        for(FeatureAttributeDescriptor attr : storeType.getPrimaryKey() ) {
93
            primaryKeys.add(attr.getName());
94
        }
95 43093 jjdelcerro
        for(String attrName : primaryKeys ) {
96
            // Ordenamos siempre por las claves primarias para garantizar
97
            // un orden predecible. Ademas se precisa indicar un orden para
98
            // usar OFFSET.
99
            sqlbuilder.select().order_by().column(sqlbuilder.identifier(attrName));
100
        }
101 43020 jjdelcerro
        for(FeatureAttributeDescriptor attr : setType ) {
102
            if( attr.isPrimaryKey() ) {
103
                primaryKeys.remove(attr.getName());
104
            }
105
            if( attr.getType() == DataTypes.GEOMETRY ) {
106 43355 jjdelcerro
                if( tolerance<=0 || !sqlbuilder.getConfig().has_functionality(Config.ST_Simplify)) {
107
                    sqlbuilder.select().column().name(attr.getName()).as_geometry();
108
                } else {
109
                    sqlbuilder.select().column().value(
110
                        sqlbuilder.ST_Simplify(
111
                            sqlbuilder.column(attr.getName()),
112
                            sqlbuilder.constant(tolerance)
113
                        )
114
                    ).as_geometry();
115
                }
116 43020 jjdelcerro
            } else {
117
                sqlbuilder.select().column().name(attr.getName());
118
            }
119
        }
120
        for(String attrName : primaryKeys ) {
121
            sqlbuilder.select().column().name(attrName);
122
        }
123
124
        if( StringUtils.isEmpty(subquery)  ) {
125
            sqlbuilder.select().from().table().database(database).schema(schema).name(table);
126
        } else {
127
            sqlbuilder.select().from().subquery(subquery);
128
        }
129
130
        Evaluator filter = query.getFilter();
131
        if( filter != null ) {
132
            String sqlfilter = filter.getSQL();
133
            if( ! StringUtils.isEmpty(sqlfilter) ) {
134
                sqlbuilder.select().where().set( sqlbuilder.custom(sqlfilter) );
135
            }
136
        }
137
        if( ! StringUtils.isEmpty(baseFilter) ) {
138
            sqlbuilder.select().where().and(sqlbuilder.custom(baseFilter));
139
        }
140
141
        FeatureQueryOrder order = query.getOrder();
142
        if( order != null ) {
143 43026 jjdelcerro
            for( FeatureQueryOrderMember member : order.members() ) {
144 43020 jjdelcerro
                if( member.hasEvaluator() ) {
145
                    String sqlorder = member.getEvaluator().getSQL();
146
                    if( ! StringUtils.isEmpty(sqlorder) ) {
147
                        sqlbuilder.select().order_by()
148
                                .custom(sqlorder);
149
                    }
150
                } else {
151
152
                    sqlbuilder.select().order_by()
153
                            .column(member.getAttributeName())
154
                            .ascending(member.getAscending());
155
                }
156
            }
157
        }
158
        if( !StringUtils.isEmpty(baseOrder) ) {
159
            sqlbuilder.select().order_by().custom(baseOrder);
160
        }
161
162
        if( limit > 0 ) {
163
            sqlbuilder.select().limit(limit);
164
        } else {
165
            sqlbuilder.select().limit(query.getLimit());
166
        }
167
        if( offset>0 ) {
168
            sqlbuilder.select().offset(offset);
169
        }
170
171
        String sql = sqlbuilder.toString();
172
        ResultSetEntry resultSetEntry = this.helper.getResulSetControler().create(
173
                sql, fetchSize
174
        );
175
        return resultSetEntry;
176
    }
177
178
}