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