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 / FetchFeatureProviderByReferenceOperation.java @ 47787

History | View | Annotate | Download (6.89 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.fmap.dal.store.jdbc2.spi.operations;
25

    
26
import java.sql.PreparedStatement;
27
import java.sql.ResultSet;
28
import java.sql.SQLException;
29
import java.util.ArrayList;
30
import java.util.List;
31
import org.apache.commons.lang3.StringUtils;
32
import org.gvsig.expressionevaluator.ExpressionBuilder;
33
import org.gvsig.fmap.dal.SQLBuilder;
34
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE;
35
import static org.gvsig.fmap.dal.SQLBuilder.PROP_JDBCHELPER;
36
import static org.gvsig.fmap.dal.SQLBuilder.PROP_QUERY;
37
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SYMBOLTABLE;
38
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLE;
39
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLENAME;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
44
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
45
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
46
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
47
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
48
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
49
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
50
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
51
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
52
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
53
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process1_SimpleFields;
54
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process2_ComputedFields;
55
import org.gvsig.fmap.geom.DataTypes;
56

    
57

    
58
public class FetchFeatureProviderByReferenceOperation extends AbstractConnectionOperation {
59
    private final FeatureType featureType;
60
    private final FeatureReferenceProviderServices reference;
61
    private final TableReference table;
62

    
63
    public FetchFeatureProviderByReferenceOperation(
64
            JDBCHelper helper,
65
            FeatureReferenceProviderServices reference,
66
            FeatureType featureType,
67
            TableReference table
68
        ) {
69
        super(helper);
70
        this.reference = reference;
71
        this.table = table;
72
        this.featureType = featureType;
73
    }
74

    
75
    @Override
76
    public final Object perform(JDBCConnection conn) throws DataException {
77
        FeatureProvider feature = fetchFeatureProviderByReference(conn);
78
        return feature;
79
    }
80

    
81
    public String getSQL() {
82
        List<FeatureAttributeDescriptor> columns = new ArrayList<>();        
83
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
84
        String sql = this.getSQL(sqlbuilder,columns);
85
        return sql;
86
    }
87

    
88
    public String getSQL(JDBCSQLBuilderBase sqlbuilder, List<FeatureAttributeDescriptor> columns) {
89
        
90
        List<String> primaryKeys = new ArrayList<>();
91
        for (String primaryKey : reference.getKeyNames()) {
92
            primaryKeys.add(primaryKey);
93
        }
94

    
95
        ExpressionBuilder expbuilder = sqlbuilder.expression();
96

    
97
        expbuilder.setProperty(PROP_FEATURE_TYPE, this.featureType);
98
        expbuilder.setProperty(PROP_TABLE, table);
99
        expbuilder.setProperty(PROP_TABLENAME, table.getTable());
100
        expbuilder.setProperty(PROP_JDBCHELPER, this.helper);
101
        SQLBuilder.SelectBuilder select = sqlbuilder.select();
102

    
103
        select.from().table()
104
                .database(this.table.getDatabase())
105
                .schema(this.table.getSchema())
106
                .name(this.table.getTable());
107

    
108
        process1_SimpleFields(helper, this.featureType, null, sqlbuilder, select, columns, primaryKeys, null);
109
        process2_ComputedFields(helper, this.featureType, null, sqlbuilder, select, null);
110

    
111
        for (String name : primaryKeys )  {
112
            if( !select.has_column(name) ) {
113
                select.column().name(name);
114
                columns.add(featureType.getAttributeDescriptor(name));
115
            }
116
            Object value = reference.getKeyValue(name);
117
            if( value == null ) {
118
                select.where().and(
119
                    expbuilder.is_null(select.column(name))
120
                );                
121
            } else {
122
                select.where().and(
123
                    expbuilder.eq(
124
                        select.column(name), 
125
                        expbuilder.parameter(name).value(value)
126
                    )
127
                );              
128
            }
129
        }
130
        select.limit(1);
131
        
132
        sqlbuilder.setProperties(
133
                null,
134
                PROP_FEATURE_TYPE, this.featureType,
135
                PROP_TABLE, table,
136
                PROP_JDBCHELPER, this.helper
137
        );
138
        this.helper.expandCalculedColumns(sqlbuilder);
139
        this.helper.processSpecialFunctions(sqlbuilder, this.featureType, null, null);
140
        String sql = StringUtils.trim(sqlbuilder.toString());
141
        return sql;
142
    }
143
       
144
    public FeatureProvider fetchFeatureProviderByReference(
145
            JDBCConnection conn
146
        ) throws DataException {
147

    
148
        List<FeatureAttributeDescriptor> columns = new ArrayList<>();        
149
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
150
        String sql = this.getSQL(sqlbuilder,columns);
151
        
152
        PreparedStatement st = null;
153
        ResultSet rs = null;
154
        try {
155
            st = conn.prepareStatement(sql);
156
            sqlbuilder.setParameters(st);
157
            rs = JDBCUtils.executeQuery(st, sql);
158
            if (!rs.next()) {
159
                return null;
160
            }
161
            FeatureProvider data = this.helper.createFeature(featureType);
162
            this.helper.fetchFeature(data, rs, columns.toArray(new FeatureAttributeDescriptor[columns.size()]), null); 
163
            return data;
164

    
165
        } catch (SQLException ex) {
166
            throw new JDBCSQLException(ex,sql);
167
        } finally {
168
            JDBCUtils.closeQuietly(st);
169
            JDBCUtils.closeQuietly(rs);
170
        }        
171
    }
172
    
173
}