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