Revision 44678 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/PerformChangesOperation.java
PerformChangesOperation.java | ||
---|---|---|
32 | 32 |
import org.gvsig.tools.dispose.Disposable; |
33 | 33 |
import org.gvsig.tools.dispose.DisposeUtils; |
34 | 34 |
|
35 |
@SuppressWarnings("UseSpecificCatch") |
|
35 | 36 |
public class PerformChangesOperation extends AbstractConnectionWritableOperation { |
36 | 37 |
|
37 | 38 |
protected TableReference table; |
38 | 39 |
protected FeatureType featureType; |
40 |
protected FeatureType featureTypeSource; |
|
41 |
protected FeatureType featureTypeTarget; |
|
39 | 42 |
protected Iterator<FeatureReferenceProviderServices> deleteds; |
40 |
protected Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged; |
|
41 | 43 |
protected Iterator<FeatureProvider> updateds; |
42 | 44 |
protected Iterator<FeatureProvider> inserteds; |
43 | 45 |
|
... | ... | |
60 | 62 |
this.updateds = updateds; |
61 | 63 |
this.table = table; |
62 | 64 |
this.featureType = featureType; |
63 |
this.featureTypesChanged = featureTypesChanged; |
|
65 |
if (featureTypesChanged.hasNext()) { |
|
66 |
FeatureStoreProvider.FeatureTypeChanged item = featureTypesChanged.next(); |
|
67 |
this.featureTypeSource = item.getSource(); |
|
68 |
this.featureTypeTarget = item.getTarget(); |
|
69 |
typeChanged = true; |
|
70 |
} else { |
|
71 |
this.featureTypeSource = null; |
|
72 |
this.featureTypeTarget = null; |
|
73 |
typeChanged = false; |
|
74 |
} |
|
64 | 75 |
} |
65 | 76 |
|
66 | 77 |
public boolean isTypeChanged() { |
... | ... | |
69 | 80 |
|
70 | 81 |
@Override |
71 | 82 |
public Object perform(Connection conn) throws DataException { |
72 |
if (featureTypesChanged.hasNext()) { |
|
73 |
FeatureStoreProvider.FeatureTypeChanged item = featureTypesChanged.next(); |
|
74 |
this.performUpdateTable(conn, table, item.getSource(), item.getTarget()); |
|
75 |
typeChanged = true; |
|
76 |
} else { |
|
77 |
typeChanged = false; |
|
83 |
if (typeChanged) { |
|
84 |
this.performUpdateTable(conn); |
|
78 | 85 |
} |
79 | 86 |
if (deleteds.hasNext()) { |
80 |
performDeletes(conn, table, featureType, deleteds);
|
|
87 |
performDeletes(conn); |
|
81 | 88 |
} |
82 | 89 |
if (updateds.hasNext()) { |
83 |
performUpdates(conn, table, featureType, updateds);
|
|
90 |
performUpdates(conn); |
|
84 | 91 |
} |
85 | 92 |
if (inserteds.hasNext()) { |
86 |
performInserts(conn, table, featureType, inserteds);
|
|
93 |
performInserts(conn); |
|
87 | 94 |
} |
88 | 95 |
return true; |
89 | 96 |
} |
90 | 97 |
|
91 |
protected JDBCSQLBuilderBase buildDeleteSQL( |
|
92 |
TableReference table, |
|
93 |
FeatureType type |
|
94 |
) { |
|
98 |
public String getDeleteSQL() { |
|
95 | 99 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
100 |
return this.getDeleteSQL(sqlbuilder); |
|
101 |
} |
|
102 |
|
|
103 |
public String getDeleteSQL(JDBCSQLBuilderBase sqlbuilder) { |
|
96 | 104 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
97 | 105 |
|
98 | 106 |
sqlbuilder.delete().table() |
99 | 107 |
.database(this.table.getDatabase()) |
100 | 108 |
.schema(this.table.getSchema()) |
101 | 109 |
.name(this.table.getTable()); |
102 |
for (FeatureAttributeDescriptor attr : type) { |
|
110 |
for (FeatureAttributeDescriptor attr : this.featureType) {
|
|
103 | 111 |
if (attr.isPrimaryKey()) { |
104 | 112 |
sqlbuilder.delete().where().and( |
105 | 113 |
expbuilder.eq( |
... | ... | |
112 | 120 |
if (!sqlbuilder.delete().has_where() ) { |
113 | 121 |
throw new RuntimeException("Operation requires missing pk"); |
114 | 122 |
} |
115 |
return sqlbuilder; |
|
123 |
sqlbuilder.setProperties( |
|
124 |
Variable.class, |
|
125 |
PROP_TABLE, table |
|
126 |
); |
|
127 |
String sql = sqlbuilder.delete().toString(); |
|
128 |
return sql; |
|
116 | 129 |
} |
117 | 130 |
|
118 |
public void performDeletes(Connection conn, |
|
119 |
TableReference table, |
|
120 |
FeatureType type, |
|
121 |
Iterator<FeatureReferenceProviderServices> deleteds) throws DataException { |
|
131 |
public void performDeletes(Connection conn) throws DataException { |
|
122 | 132 |
|
123 |
JDBCSQLBuilderBase sqlbuilder = buildDeleteSQL(table, type); |
|
133 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
|
134 |
String sql = getDeleteSQL(sqlbuilder); |
|
124 | 135 |
|
125 | 136 |
PreparedStatement st = null; |
126 | 137 |
Disposable paramsDisposer = null; |
127 |
sqlbuilder.setProperties( |
|
128 |
Variable.class, |
|
129 |
PROP_TABLE, table |
|
130 |
); |
|
131 |
String sql = sqlbuilder.delete().toString(); |
|
132 | 138 |
try { |
133 | 139 |
st = conn.prepareStatement(sql); |
134 | 140 |
while (deleteds.hasNext()) { |
... | ... | |
155 | 161 |
} |
156 | 162 |
} |
157 | 163 |
|
158 |
protected JDBCSQLBuilderBase buildInsertSQL( |
|
159 |
TableReference table, |
|
160 |
FeatureType type |
|
161 |
) { |
|
164 |
public String getInsertSQL() { |
|
162 | 165 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
166 |
return this.getInsertSQL(sqlbuilder); |
|
167 |
} |
|
168 |
|
|
169 |
public String getInsertSQL(JDBCSQLBuilderBase sqlbuilder) { |
|
163 | 170 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
164 | 171 |
|
165 | 172 |
sqlbuilder.insert().table() |
166 | 173 |
.database(this.table.getDatabase()) |
167 | 174 |
.schema(this.table.getSchema()) |
168 | 175 |
.name(this.table.getTable()); |
169 |
for (FeatureAttributeDescriptor attr : type) { |
|
176 |
for (FeatureAttributeDescriptor attr : this.featureType) {
|
|
170 | 177 |
if( attr.isAutomatic() || attr.isComputed() ) { |
171 | 178 |
continue; |
172 | 179 |
} |
... | ... | |
184 | 191 |
); |
185 | 192 |
} |
186 | 193 |
} |
187 |
return sqlbuilder; |
|
188 |
} |
|
189 | 194 |
|
190 |
public void performInserts(Connection conn, |
|
191 |
TableReference table, |
|
192 |
FeatureType type, |
|
193 |
Iterator<FeatureProvider> inserteds) throws DataException { |
|
194 |
JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(table, type); |
|
195 |
|
|
196 |
PreparedStatement st; |
|
197 |
Disposable paramsDisposer; |
|
198 | 195 |
sqlbuilder.setProperties( |
199 | 196 |
Variable.class, |
200 |
PROP_FEATURE_TYPE, type,
|
|
197 |
PROP_FEATURE_TYPE, featureType,
|
|
201 | 198 |
PROP_TABLE, table |
202 | 199 |
); |
203 | 200 |
String sql = sqlbuilder.insert().toString(); |
201 |
return sql; |
|
202 |
} |
|
203 |
|
|
204 |
public void performInserts(Connection conn) throws DataException { |
|
205 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
|
206 |
String sql = getInsertSQL(sqlbuilder); |
|
207 |
|
|
208 |
PreparedStatement st; |
|
209 |
Disposable paramsDisposer; |
|
204 | 210 |
try { |
205 | 211 |
st = conn.prepareStatement(sql); |
206 | 212 |
while (inserteds.hasNext()) { |
... | ... | |
224 | 230 |
} |
225 | 231 |
} |
226 | 232 |
|
227 |
protected JDBCSQLBuilderBase buildUpdateSQL( |
|
228 |
TableReference table, |
|
229 |
FeatureType type |
|
230 |
) { |
|
233 |
public String getUpdateSQL() { |
|
231 | 234 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
235 |
return this.getUpdateSQL(sqlbuilder); |
|
236 |
} |
|
237 |
|
|
238 |
public String getUpdateSQL(JDBCSQLBuilderBase sqlbuilder) { |
|
232 | 239 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
233 | 240 |
|
234 | 241 |
sqlbuilder.update().table() |
235 | 242 |
.database(this.table.getDatabase()) |
236 | 243 |
.schema(this.table.getSchema()) |
237 | 244 |
.name(this.table.getTable()); |
238 |
for (FeatureAttributeDescriptor attr : type) { |
|
245 |
for (FeatureAttributeDescriptor attr : this.featureType) {
|
|
239 | 246 |
if (attr.isPrimaryKey()) { |
240 | 247 |
sqlbuilder.update().where().and( |
241 | 248 |
expbuilder.eq( |
... | ... | |
243 | 250 |
expbuilder.parameter(attr.getName()).as_variable() |
244 | 251 |
) |
245 | 252 |
); |
253 |
continue; |
|
246 | 254 |
} |
247 | 255 |
if ( attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) { |
248 | 256 |
continue; |
... | ... | |
262 | 270 |
if (!sqlbuilder.update().has_where() ) { |
263 | 271 |
throw new RuntimeException("Operation requires missing pk"); |
264 | 272 |
} |
265 |
return sqlbuilder; |
|
273 |
sqlbuilder.setProperties( |
|
274 |
Variable.class, |
|
275 |
PROP_FEATURE_TYPE, this.featureType, |
|
276 |
PROP_TABLE, table |
|
277 |
); |
|
278 |
String sql = sqlbuilder.update().toString(); |
|
279 |
return sql; |
|
266 | 280 |
} |
267 | 281 |
|
268 |
public void performUpdates(Connection conn, |
|
269 |
TableReference table, |
|
270 |
FeatureType type, |
|
271 |
Iterator<FeatureProvider> updateds) throws DataException { |
|
282 |
public void performUpdates(Connection conn) throws DataException { |
|
272 | 283 |
|
273 |
JDBCSQLBuilderBase sqlbuilder = buildUpdateSQL(table, type); |
|
284 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
|
285 |
String sql = getInsertSQL(sqlbuilder); |
|
274 | 286 |
|
275 | 287 |
PreparedStatement st = null; |
276 | 288 |
Disposable paramsDisposer = null; |
277 |
sqlbuilder.setProperties( |
|
278 |
Variable.class, |
|
279 |
PROP_FEATURE_TYPE, type, |
|
280 |
PROP_TABLE, table |
|
281 |
); |
|
282 |
String sql = sqlbuilder.update().toString(); |
|
283 | 289 |
try { |
284 | 290 |
st = conn.prepareStatement(sql); |
285 | 291 |
while (updateds.hasNext()) { |
... | ... | |
297 | 303 |
} |
298 | 304 |
} |
299 | 305 |
|
300 |
protected List<String> buildUpdateTableSQL( |
|
301 |
TableReference table, |
|
302 |
FeatureType original, |
|
303 |
FeatureType target |
|
304 |
) { |
|
306 |
public List<String> getUpdateTableSQLs() { |
|
305 | 307 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
306 | 308 |
sqlbuilder.alter_table().table() |
307 | 309 |
.database(this.table.getDatabase()) |
308 | 310 |
.schema(this.table.getSchema()) |
309 | 311 |
.name(this.table.getTable()); |
310 | 312 |
|
311 |
for (FeatureAttributeDescriptor attrOrgiginal : original) {
|
|
312 |
FeatureAttributeDescriptor attrTarget = target.getAttributeDescriptor(
|
|
313 |
for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
|
|
314 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor(
|
|
313 | 315 |
attrOrgiginal.getName() |
314 | 316 |
); |
315 | 317 |
if (attrTarget == null) { |
... | ... | |
346 | 348 |
} |
347 | 349 |
} |
348 | 350 |
} |
349 |
for (FeatureAttributeDescriptor attrTarget : target) {
|
|
351 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
|
350 | 352 |
if( attrTarget.isComputed() ) { |
351 | 353 |
continue; |
352 | 354 |
} |
353 |
if (original.getAttributeDescriptor(attrTarget.getName()) == null) {
|
|
355 |
if (featureTypeSource.getAttributeDescriptor(attrTarget.getName()) == null) {
|
|
354 | 356 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) { |
355 | 357 |
sqlbuilder.alter_table().add_geometry_column( |
356 | 358 |
attrTarget.getName(), |
... | ... | |
380 | 382 |
Variable.class, |
381 | 383 |
PROP_TABLE, table |
382 | 384 |
); |
383 |
return sqlbuilder.alter_table().toStrings(); |
|
385 |
|
|
386 |
List<String> sqls = sqlbuilder.alter_table().toStrings(); |
|
387 |
sqls.addAll(buildCreateIndexSQL()); |
|
388 |
return sqls; |
|
384 | 389 |
} |
385 | 390 |
|
386 | 391 |
protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) { |
... | ... | |
424 | 429 |
return true; |
425 | 430 |
} |
426 | 431 |
|
427 |
protected List<String> buildCreateIndexSQL( |
|
428 |
TableReference table, |
|
429 |
FeatureType original, |
|
430 |
FeatureType target |
|
431 |
) { |
|
432 |
protected List<String> buildCreateIndexSQL() { |
|
432 | 433 |
ArrayList<String> sqls = new ArrayList<>(); |
433 | 434 |
|
434 |
for (FeatureAttributeDescriptor attrTarget : target) {
|
|
435 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
|
435 | 436 |
boolean createIndex = false; |
436 | 437 |
if( attrTarget.isIndexed() ) { |
437 |
FeatureAttributeDescriptor attrOriginal = original.getAttributeDescriptor(attrTarget.getName());
|
|
438 |
FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName());
|
|
438 | 439 |
if ( attrOriginal == null) { |
439 | 440 |
createIndex = true; |
440 | 441 |
} else { |
... | ... | |
467 | 468 |
return sqls; |
468 | 469 |
} |
469 | 470 |
|
470 |
public void performUpdateTable(Connection conn, |
|
471 |
TableReference table, |
|
472 |
FeatureType original, |
|
473 |
FeatureType target) throws DataException { |
|
471 |
public void performUpdateTable(Connection conn) throws DataException { |
|
474 | 472 |
|
475 |
ArrayList<String> sqls = new ArrayList<>(); |
|
476 |
|
|
477 |
sqls.addAll(buildUpdateTableSQL(table, original, target)); |
|
478 |
sqls.addAll(buildCreateIndexSQL(table, original, target)); |
|
473 |
List<String> sqls = this.getUpdateTableSQLs(); |
|
479 | 474 |
|
480 | 475 |
if( !CollectionUtils.isEmpty(sqls) ) { |
481 | 476 |
Statement st = null; |
Also available in: Unified diff