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 @ 44058
History | View | Annotate | Download (4.39 KB)
1 | 43020 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
||
3 | |||
4 | import java.sql.Connection; |
||
5 | 43093 | jjdelcerro | import java.sql.PreparedStatement; |
6 | 43020 | jjdelcerro | import java.sql.ResultSet; |
7 | import java.sql.SQLException; |
||
8 | 43358 | jjdelcerro | import java.util.ArrayList; |
9 | import java.util.List; |
||
10 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
11 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
13 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
14 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
15 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
16 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
17 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
18 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
19 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
20 | 43093 | jjdelcerro | import org.gvsig.fmap.geom.DataTypes; |
21 | 43020 | jjdelcerro | |
22 | |||
23 | public class FetchFeatureProviderByReferenceOperation extends AbstractConnectionOperation { |
||
24 | private final FeatureType featureType; |
||
25 | private final FeatureReferenceProviderServices reference; |
||
26 | 44058 | jjdelcerro | private final TableReference table; |
27 | 43020 | jjdelcerro | |
28 | public FetchFeatureProviderByReferenceOperation(
|
||
29 | JDBCHelper helper, |
||
30 | FeatureReferenceProviderServices reference, |
||
31 | FeatureType featureType, |
||
32 | 44058 | jjdelcerro | TableReference table |
33 | 43020 | jjdelcerro | ) { |
34 | super(helper);
|
||
35 | this.reference = reference;
|
||
36 | this.table = table;
|
||
37 | this.featureType = featureType;
|
||
38 | } |
||
39 | |||
40 | @Override
|
||
41 | public final Object perform(Connection conn) throws DataException { |
||
42 | FeatureProvider feature = fetchFeatureProviderByReference( |
||
43 | conn, |
||
44 | reference, |
||
45 | featureType, |
||
46 | table |
||
47 | ); |
||
48 | return feature;
|
||
49 | } |
||
50 | |||
51 | public FeatureProvider fetchFeatureProviderByReference(
|
||
52 | Connection conn,
|
||
53 | FeatureReferenceProviderServices reference, |
||
54 | // FeatureType storeType,
|
||
55 | FeatureType featureType, |
||
56 | 44058 | jjdelcerro | TableReference table |
57 | 43020 | jjdelcerro | ) throws DataException {
|
58 | 43358 | jjdelcerro | List<FeatureAttributeDescriptor> columns = new ArrayList<>(); |
59 | 43020 | jjdelcerro | |
60 | // FeatureAttributeDescriptor[] primaryKey = storeType.getPrimaryKey();
|
||
61 | String[] primaryKeys = reference.getKeyNames(); |
||
62 | |||
63 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
64 | |||
65 | for (FeatureAttributeDescriptor attr : featureType) {
|
||
66 | 43093 | jjdelcerro | if( attr.getType()==DataTypes.GEOMETRY ) {
|
67 | sqlbuilder.select().column().name(attr.getName()).as_geometry(); |
||
68 | 43358 | jjdelcerro | columns.add(attr); |
69 | 43093 | jjdelcerro | } else {
|
70 | sqlbuilder.select().column().name(attr.getName()); |
||
71 | 43358 | jjdelcerro | columns.add(attr); |
72 | 43093 | jjdelcerro | } |
73 | 43020 | jjdelcerro | } |
74 | 44058 | jjdelcerro | sqlbuilder.select().from().table() |
75 | .database(this.table.getDatabase())
|
||
76 | .schema(this.table.getSchema())
|
||
77 | .name(this.table.getTable());
|
||
78 | 43020 | jjdelcerro | for (String name : primaryKeys ) { |
79 | if( !sqlbuilder.select().has_column(name) ) {
|
||
80 | sqlbuilder.select().column().name(name); |
||
81 | 43358 | jjdelcerro | columns.add(featureType.getAttributeDescriptor(name)); |
82 | 43020 | jjdelcerro | } |
83 | Object value = reference.getKeyValue(name);
|
||
84 | if( value == null ) { |
||
85 | sqlbuilder.select().where().and( |
||
86 | sqlbuilder.isNull(sqlbuilder.column(name)) |
||
87 | ); |
||
88 | } else {
|
||
89 | sqlbuilder.select().where().and( |
||
90 | sqlbuilder.eq( |
||
91 | sqlbuilder.column(name), |
||
92 | 43093 | jjdelcerro | sqlbuilder.parameter(name).value(value) |
93 | 43020 | jjdelcerro | ) |
94 | ); |
||
95 | } |
||
96 | } |
||
97 | sqlbuilder.select().limit(1);
|
||
98 | 43093 | jjdelcerro | |
99 | 43020 | jjdelcerro | String sql = sqlbuilder.select().toString();
|
100 | |||
101 | 43093 | jjdelcerro | PreparedStatement st = null; |
102 | 43020 | jjdelcerro | ResultSet rs = null; |
103 | try {
|
||
104 | 43093 | jjdelcerro | st = conn.prepareStatement(sql); |
105 | sqlbuilder.setParameters(st); |
||
106 | 43020 | jjdelcerro | rs = JDBCUtils.executeQuery(st, sql); |
107 | if (!rs.next()) {
|
||
108 | return null; |
||
109 | } |
||
110 | FeatureProvider data = this.helper.createFeature(featureType);
|
||
111 | 43358 | jjdelcerro | this.helper.fetchFeature(data, rs, columns.toArray(new FeatureAttributeDescriptor[columns.size()])); |
112 | 43020 | jjdelcerro | return data;
|
113 | |||
114 | } catch (SQLException ex) { |
||
115 | throw new JDBCSQLException(ex); |
||
116 | } finally {
|
||
117 | JDBCUtils.closeQuietly(st); |
||
118 | JDBCUtils.closeQuietly(rs); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | } |