Revision 46518 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
FetchFeatureProviderByReferenceOperation.java | ||
---|---|---|
28 | 28 |
import java.sql.SQLException; |
29 | 29 |
import java.util.ArrayList; |
30 | 30 |
import java.util.List; |
31 |
import org.apache.commons.lang3.StringUtils; |
|
31 | 32 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
33 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
34 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE; |
|
35 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_JDBCHELPER; |
|
36 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_QUERY; |
|
37 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SYMBOLTABLE; |
|
38 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLE; |
|
39 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLENAME; |
|
32 | 40 |
import org.gvsig.fmap.dal.exception.DataException; |
33 | 41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
34 | 42 |
import org.gvsig.fmap.dal.feature.FeatureType; |
... | ... | |
42 | 50 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
43 | 51 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
44 | 52 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
53 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process1_SimpleFields; |
|
54 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process2_ComputedFields; |
|
45 | 55 |
import org.gvsig.fmap.geom.DataTypes; |
46 | 56 |
|
47 | 57 |
|
... | ... | |
77 | 87 |
|
78 | 88 |
public String getSQL(JDBCSQLBuilderBase sqlbuilder, List<FeatureAttributeDescriptor> columns) { |
79 | 89 |
|
80 |
// FeatureAttributeDescriptor[] primaryKey = storeType.getPrimaryKey(); |
|
81 |
String[] primaryKeys = reference.getKeyNames(); |
|
82 |
|
|
90 |
List<String> primaryKeys = new ArrayList<>(); |
|
91 |
for (String primaryKey : reference.getKeyNames()) { |
|
92 |
primaryKeys.add(primaryKey); |
|
93 |
} |
|
94 |
|
|
83 | 95 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
84 | 96 |
|
85 |
for (FeatureAttributeDescriptor attr : featureType) { |
|
86 |
if( attr.isComputed() ) { |
|
87 |
continue; |
|
88 |
} |
|
89 |
if( attr.getType()==DataTypes.GEOMETRY ) { |
|
90 |
sqlbuilder.select().column().name(attr.getName()).as_geometry(); |
|
91 |
columns.add(attr); |
|
92 |
} else { |
|
93 |
sqlbuilder.select().column().name(attr.getName()); |
|
94 |
columns.add(attr); |
|
95 |
} |
|
96 |
} |
|
97 |
sqlbuilder.select().from().table() |
|
97 |
expbuilder.setProperty(PROP_FEATURE_TYPE, this.featureType); |
|
98 |
expbuilder.setProperty(PROP_TABLE, table); |
|
99 |
expbuilder.setProperty(PROP_TABLENAME, table.getTable()); |
|
100 |
expbuilder.setProperty(PROP_JDBCHELPER, this.helper); |
|
101 |
SQLBuilder.SelectBuilder select = sqlbuilder.select(); |
|
102 |
|
|
103 |
select.from().table() |
|
98 | 104 |
.database(this.table.getDatabase()) |
99 | 105 |
.schema(this.table.getSchema()) |
100 | 106 |
.name(this.table.getTable()); |
107 |
|
|
108 |
process1_SimpleFields(helper, this.featureType, null, sqlbuilder, select, columns, primaryKeys, null); |
|
109 |
process2_ComputedFields(helper, this.featureType, null, sqlbuilder, select, null); |
|
110 |
|
|
101 | 111 |
for (String name : primaryKeys ) { |
102 |
if( !sqlbuilder.select().has_column(name) ) {
|
|
103 |
sqlbuilder.select().column().name(name);
|
|
112 |
if( !select.has_column(name) ) {
|
|
113 |
select.column().name(name);
|
|
104 | 114 |
columns.add(featureType.getAttributeDescriptor(name)); |
105 | 115 |
} |
106 | 116 |
Object value = reference.getKeyValue(name); |
107 | 117 |
if( value == null ) { |
108 |
sqlbuilder.select().where().and(
|
|
109 |
expbuilder.is_null(expbuilder.column(name))
|
|
118 |
select.where().and(
|
|
119 |
expbuilder.is_null(select.column(name))
|
|
110 | 120 |
); |
111 | 121 |
} else { |
112 |
sqlbuilder.select().where().and(
|
|
122 |
select.where().and(
|
|
113 | 123 |
expbuilder.eq( |
114 |
expbuilder.column(name),
|
|
124 |
select.column(name),
|
|
115 | 125 |
expbuilder.parameter(name).value(value) |
116 | 126 |
) |
117 | 127 |
); |
118 | 128 |
} |
119 | 129 |
} |
120 |
sqlbuilder.select().limit(1); |
|
130 |
select.limit(1); |
|
131 |
|
|
121 | 132 |
sqlbuilder.setProperties( |
122 |
ExpressionBuilder.Variable.class, |
|
123 |
PROP_FEATURE_TYPE, featureType, |
|
124 |
PROP_TABLE, table |
|
133 |
null, |
|
134 |
PROP_FEATURE_TYPE, this.featureType, |
|
135 |
PROP_TABLE, table, |
|
136 |
PROP_JDBCHELPER, this.helper |
|
125 | 137 |
); |
126 |
String sql = sqlbuilder.select().toString(); |
|
138 |
this.helper.expandCalculedColumns(sqlbuilder); |
|
139 |
this.helper.processSpecialFunctions(sqlbuilder, this.featureType, null); |
|
140 |
String sql = StringUtils.trim(sqlbuilder.toString()); |
|
127 | 141 |
return sql; |
128 | 142 |
} |
129 | 143 |
|
Also available in: Unified diff