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 @ 45534
History | View | Annotate | Download (6.03 KB)
1 | 45065 | jjdelcerro | /**
|
---|---|---|---|
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 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
25 | |||
26 | import java.sql.Connection; |
||
27 | 43093 | jjdelcerro | import java.sql.PreparedStatement; |
28 | 43020 | jjdelcerro | import java.sql.ResultSet; |
29 | import java.sql.SQLException; |
||
30 | 43358 | jjdelcerro | import java.util.ArrayList; |
31 | import java.util.List; |
||
32 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
33 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
34 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
35 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
36 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
37 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
38 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
39 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
40 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
41 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
42 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
43 | 44198 | jjdelcerro | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
44 | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
||
45 | 43093 | jjdelcerro | import org.gvsig.fmap.geom.DataTypes; |
46 | 43020 | jjdelcerro | |
47 | |||
48 | public class FetchFeatureProviderByReferenceOperation extends AbstractConnectionOperation { |
||
49 | private final FeatureType featureType; |
||
50 | private final FeatureReferenceProviderServices reference; |
||
51 | 44058 | jjdelcerro | private final TableReference table; |
52 | 43020 | jjdelcerro | |
53 | public FetchFeatureProviderByReferenceOperation(
|
||
54 | JDBCHelper helper, |
||
55 | FeatureReferenceProviderServices reference, |
||
56 | FeatureType featureType, |
||
57 | 44058 | jjdelcerro | TableReference table |
58 | 43020 | jjdelcerro | ) { |
59 | super(helper);
|
||
60 | this.reference = reference;
|
||
61 | this.table = table;
|
||
62 | this.featureType = featureType;
|
||
63 | } |
||
64 | |||
65 | @Override
|
||
66 | public final Object perform(Connection conn) throws DataException { |
||
67 | 44678 | jjdelcerro | FeatureProvider feature = fetchFeatureProviderByReference(conn); |
68 | 43020 | jjdelcerro | return feature;
|
69 | } |
||
70 | 44678 | jjdelcerro | |
71 | public String getSQL() { |
||
72 | 43358 | jjdelcerro | List<FeatureAttributeDescriptor> columns = new ArrayList<>(); |
73 | 44678 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
74 | String sql = this.getSQL(sqlbuilder,columns); |
||
75 | return sql;
|
||
76 | } |
||
77 | |||
78 | public String getSQL(JDBCSQLBuilderBase sqlbuilder, List<FeatureAttributeDescriptor> columns) { |
||
79 | 43020 | jjdelcerro | |
80 | // FeatureAttributeDescriptor[] primaryKey = storeType.getPrimaryKey();
|
||
81 | String[] primaryKeys = reference.getKeyNames(); |
||
82 | |||
83 | 44198 | jjdelcerro | ExpressionBuilder expbuilder = sqlbuilder.expression(); |
84 | |||
85 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : featureType) {
|
86 | 44355 | jjdelcerro | if( attr.isComputed() ) {
|
87 | continue;
|
||
88 | } |
||
89 | 43093 | jjdelcerro | if( attr.getType()==DataTypes.GEOMETRY ) {
|
90 | sqlbuilder.select().column().name(attr.getName()).as_geometry(); |
||
91 | 43358 | jjdelcerro | columns.add(attr); |
92 | 43093 | jjdelcerro | } else {
|
93 | sqlbuilder.select().column().name(attr.getName()); |
||
94 | 43358 | jjdelcerro | columns.add(attr); |
95 | 43093 | jjdelcerro | } |
96 | 43020 | jjdelcerro | } |
97 | 44058 | jjdelcerro | sqlbuilder.select().from().table() |
98 | .database(this.table.getDatabase())
|
||
99 | .schema(this.table.getSchema())
|
||
100 | .name(this.table.getTable());
|
||
101 | 43020 | jjdelcerro | for (String name : primaryKeys ) { |
102 | if( !sqlbuilder.select().has_column(name) ) {
|
||
103 | sqlbuilder.select().column().name(name); |
||
104 | 43358 | jjdelcerro | columns.add(featureType.getAttributeDescriptor(name)); |
105 | 43020 | jjdelcerro | } |
106 | Object value = reference.getKeyValue(name);
|
||
107 | if( value == null ) { |
||
108 | sqlbuilder.select().where().and( |
||
109 | 44198 | jjdelcerro | expbuilder.is_null(expbuilder.column(name)) |
110 | 43020 | jjdelcerro | ); |
111 | } else {
|
||
112 | sqlbuilder.select().where().and( |
||
113 | 44198 | jjdelcerro | expbuilder.eq( |
114 | expbuilder.column(name), |
||
115 | expbuilder.parameter(name).value(value) |
||
116 | 43020 | jjdelcerro | ) |
117 | ); |
||
118 | } |
||
119 | } |
||
120 | sqlbuilder.select().limit(1);
|
||
121 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
122 | ExpressionBuilder.Variable.class, |
||
123 | PROP_FEATURE_TYPE, featureType, |
||
124 | PROP_TABLE, table |
||
125 | ); |
||
126 | 43020 | jjdelcerro | String sql = sqlbuilder.select().toString();
|
127 | 44678 | jjdelcerro | return sql;
|
128 | } |
||
129 | |||
130 | public FeatureProvider fetchFeatureProviderByReference(
|
||
131 | Connection conn
|
||
132 | ) throws DataException {
|
||
133 | 43020 | jjdelcerro | |
134 | 44678 | jjdelcerro | List<FeatureAttributeDescriptor> columns = new ArrayList<>(); |
135 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
136 | String sql = this.getSQL(sqlbuilder,columns); |
||
137 | |||
138 | 43093 | jjdelcerro | PreparedStatement st = null; |
139 | 43020 | jjdelcerro | ResultSet rs = null; |
140 | try {
|
||
141 | 43093 | jjdelcerro | st = conn.prepareStatement(sql); |
142 | sqlbuilder.setParameters(st); |
||
143 | 43020 | jjdelcerro | rs = JDBCUtils.executeQuery(st, sql); |
144 | if (!rs.next()) {
|
||
145 | return null; |
||
146 | } |
||
147 | FeatureProvider data = this.helper.createFeature(featureType);
|
||
148 | 44376 | jjdelcerro | this.helper.fetchFeature(data, rs, columns.toArray(new FeatureAttributeDescriptor[columns.size()]), null); |
149 | 43020 | jjdelcerro | return data;
|
150 | |||
151 | } catch (SQLException ex) { |
||
152 | throw new JDBCSQLException(ex); |
||
153 | } finally {
|
||
154 | JDBCUtils.closeQuietly(st); |
||
155 | JDBCUtils.closeQuietly(rs); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | } |