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