Revision 44058 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 | ||
---|---|---|
23 | 23 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
24 | 24 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
25 | 25 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
26 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
|
26 | 27 |
import org.gvsig.tools.dispose.Disposable; |
27 | 28 |
import org.gvsig.tools.dispose.DisposeUtils; |
28 | 29 |
|
29 | 30 |
public class PerformChangesOperation extends AbstractConnectionWritableOperation { |
30 | 31 |
|
31 |
protected String dbName; |
|
32 |
protected String schemaName; |
|
33 |
protected String tableName; |
|
32 |
protected TableReference table; |
|
34 | 33 |
protected FeatureType featureType; |
35 | 34 |
protected Iterator<FeatureReferenceProviderServices> deleteds; |
36 | 35 |
protected Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged; |
... | ... | |
40 | 39 |
protected boolean typeChanged = false; |
41 | 40 |
|
42 | 41 |
public PerformChangesOperation(JDBCHelper helper) { |
43 |
this(helper, null, null, null, null, null, null, null, null);
|
|
42 |
this(helper, null, null, null, null, null, null); |
|
44 | 43 |
} |
45 | 44 |
|
46 | 45 |
public PerformChangesOperation(JDBCHelper helper, |
47 |
String dbName, |
|
48 |
String schemaName, |
|
49 |
String tableName, |
|
46 |
TableReference table, |
|
50 | 47 |
FeatureType featureType, |
51 | 48 |
Iterator<FeatureReferenceProviderServices> deleteds, |
52 | 49 |
Iterator<FeatureProvider> inserteds, |
53 | 50 |
Iterator<FeatureProvider> updateds, |
54 | 51 |
Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged) { |
55 | 52 |
super(helper); |
56 |
this.dbName = dbName; |
|
57 | 53 |
this.deleteds = deleteds; |
58 | 54 |
this.inserteds = inserteds; |
59 | 55 |
this.updateds = updateds; |
60 |
this.schemaName = schemaName; |
|
61 |
this.tableName = tableName; |
|
56 |
this.table = table; |
|
62 | 57 |
this.featureType = featureType; |
63 | 58 |
this.featureTypesChanged = featureTypesChanged; |
64 | 59 |
} |
... | ... | |
71 | 66 |
public Object perform(Connection conn) throws DataException { |
72 | 67 |
if (featureTypesChanged.hasNext()) { |
73 | 68 |
FeatureStoreProvider.FeatureTypeChanged item = featureTypesChanged.next(); |
74 |
this.performUpdateTable(conn, dbName, schemaName, tableName, item.getSource(), item.getTarget());
|
|
69 |
this.performUpdateTable(conn, table, item.getSource(), item.getTarget());
|
|
75 | 70 |
typeChanged = true; |
76 | 71 |
} else { |
77 | 72 |
typeChanged = false; |
78 | 73 |
} |
79 | 74 |
if (deleteds.hasNext()) { |
80 |
performDeletes(conn, dbName, schemaName, tableName, featureType, deleteds);
|
|
75 |
performDeletes(conn, table, featureType, deleteds);
|
|
81 | 76 |
} |
82 | 77 |
if (updateds.hasNext()) { |
83 |
performUpdates(conn, dbName, schemaName, tableName, featureType, updateds);
|
|
78 |
performUpdates(conn, table, featureType, updateds);
|
|
84 | 79 |
} |
85 | 80 |
if (inserteds.hasNext()) { |
86 |
performInserts(conn, dbName, schemaName, tableName, featureType, inserteds);
|
|
81 |
performInserts(conn, table, featureType, inserteds);
|
|
87 | 82 |
} |
88 | 83 |
return true; |
89 | 84 |
} |
90 | 85 |
|
91 | 86 |
protected JDBCSQLBuilderBase buildDeleteSQL( |
92 |
String database, |
|
93 |
String schema, |
|
94 |
String table, |
|
87 |
TableReference table, |
|
95 | 88 |
FeatureType type |
96 | 89 |
) { |
97 | 90 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
98 |
sqlbuilder.delete().table().database(database).schema(schema).name(table); |
|
91 |
sqlbuilder.delete().table() |
|
92 |
.database(this.table.getDatabase()) |
|
93 |
.schema(this.table.getSchema()) |
|
94 |
.name(this.table.getTable()); |
|
99 | 95 |
for (FeatureAttributeDescriptor attr : type) { |
100 | 96 |
if (attr.isPrimaryKey()) { |
101 | 97 |
sqlbuilder.delete().where().and( |
... | ... | |
113 | 109 |
} |
114 | 110 |
|
115 | 111 |
public void performDeletes(Connection conn, |
116 |
String database, |
|
117 |
String schema, |
|
118 |
String table, |
|
112 |
TableReference table, |
|
119 | 113 |
FeatureType type, |
120 | 114 |
Iterator<FeatureReferenceProviderServices> deleteds) throws DataException { |
121 | 115 |
|
122 |
JDBCSQLBuilderBase sqlbuilder = buildDeleteSQL(database, schema, table, type);
|
|
116 |
JDBCSQLBuilderBase sqlbuilder = buildDeleteSQL(table, type); |
|
123 | 117 |
|
124 | 118 |
PreparedStatement st = null; |
125 | 119 |
Disposable paramsDisposer = null; |
... | ... | |
137 | 131 |
); |
138 | 132 |
} |
139 | 133 |
if (nAffected > 1) { |
140 |
logger.warn("Remove statement affectst to {} rows ( {} )",
|
|
134 |
LOGGER.warn("Remove statement affectst to {} rows ( {} )",
|
|
141 | 135 |
nAffected, sql |
142 | 136 |
); |
143 | 137 |
} |
... | ... | |
151 | 145 |
} |
152 | 146 |
|
153 | 147 |
protected JDBCSQLBuilderBase buildInsertSQL( |
154 |
String database, |
|
155 |
String schema, |
|
156 |
String table, |
|
148 |
TableReference table, |
|
157 | 149 |
FeatureType type |
158 | 150 |
) { |
159 | 151 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
160 | 152 |
|
161 |
sqlbuilder.insert().table().database(database).schema(schema).name(table); |
|
153 |
sqlbuilder.insert().table() |
|
154 |
.database(this.table.getDatabase()) |
|
155 |
.schema(this.table.getSchema()) |
|
156 |
.name(this.table.getTable()); |
|
162 | 157 |
for (FeatureAttributeDescriptor attr : type) { |
163 | 158 |
if( attr.isAutomatic() ) { |
164 | 159 |
continue; |
... | ... | |
179 | 174 |
} |
180 | 175 |
|
181 | 176 |
public void performInserts(Connection conn, |
182 |
String database, |
|
183 |
String schema, |
|
184 |
String table, |
|
177 |
TableReference table, |
|
185 | 178 |
FeatureType type, |
186 | 179 |
Iterator<FeatureProvider> inserteds) throws DataException { |
187 |
JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(database, schema, table, type);
|
|
180 |
JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(table, type); |
|
188 | 181 |
|
189 | 182 |
PreparedStatement st; |
190 | 183 |
Disposable paramsDisposer; |
... | ... | |
213 | 206 |
} |
214 | 207 |
|
215 | 208 |
protected JDBCSQLBuilderBase buildUpdateSQL( |
216 |
String database, |
|
217 |
String schema, |
|
218 |
String table, |
|
209 |
TableReference table, |
|
219 | 210 |
FeatureType type |
220 | 211 |
) { |
221 | 212 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
222 | 213 |
|
223 |
sqlbuilder.update().table().database(database).schema(schema).name(table); |
|
214 |
sqlbuilder.update().table() |
|
215 |
.database(this.table.getDatabase()) |
|
216 |
.schema(this.table.getSchema()) |
|
217 |
.name(this.table.getTable()); |
|
224 | 218 |
for (FeatureAttributeDescriptor attr : type) { |
225 | 219 |
if (attr.isPrimaryKey()) { |
226 | 220 |
sqlbuilder.update().where().and( |
... | ... | |
250 | 244 |
} |
251 | 245 |
|
252 | 246 |
public void performUpdates(Connection conn, |
253 |
String database, |
|
254 |
String schema, |
|
255 |
String table, |
|
247 |
TableReference table, |
|
256 | 248 |
FeatureType type, |
257 | 249 |
Iterator<FeatureProvider> updateds) throws DataException { |
258 | 250 |
|
259 |
JDBCSQLBuilderBase sqlbuilder = buildUpdateSQL(database, schema, table, type);
|
|
251 |
JDBCSQLBuilderBase sqlbuilder = buildUpdateSQL(table, type); |
|
260 | 252 |
|
261 | 253 |
PreparedStatement st = null; |
262 | 254 |
Disposable paramsDisposer = null; |
... | ... | |
279 | 271 |
} |
280 | 272 |
|
281 | 273 |
protected List<String> buildUpdateTableSQL( |
282 |
String database, |
|
283 |
String schema, |
|
284 |
String table, |
|
274 |
TableReference table, |
|
285 | 275 |
FeatureType original, |
286 | 276 |
FeatureType target |
287 | 277 |
) { |
288 | 278 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
289 |
sqlbuilder.alter_table().table().database(database).schema(schema).name(table); |
|
279 |
sqlbuilder.alter_table().table() |
|
280 |
.database(this.table.getDatabase()) |
|
281 |
.schema(this.table.getSchema()) |
|
282 |
.name(this.table.getTable()); |
|
290 | 283 |
|
291 | 284 |
for (FeatureAttributeDescriptor attrOrgiginal : original) { |
292 | 285 |
FeatureAttributeDescriptor attrTarget = target.getAttributeDescriptor( |
... | ... | |
387 | 380 |
} |
388 | 381 |
|
389 | 382 |
protected List<String> buildCreateIndexSQL( |
390 |
String database, |
|
391 |
String schema, |
|
392 |
String table, |
|
383 |
TableReference table, |
|
393 | 384 |
FeatureType original, |
394 | 385 |
FeatureType target |
395 | 386 |
) { |
... | ... | |
417 | 408 |
sqlbuilder.create_index().if_not_exist(); |
418 | 409 |
sqlbuilder.create_index().name("idx_" + table + "_" + attrTarget.getName()); |
419 | 410 |
sqlbuilder.create_index().column(attrTarget.getName()); |
420 |
sqlbuilder.create_index().table().database(database).schema(schema).name(table); |
|
411 |
sqlbuilder.create_index().table() |
|
412 |
.database(this.table.getDatabase()) |
|
413 |
.schema(this.table.getSchema()) |
|
414 |
.name(this.table.getTable()); |
|
421 | 415 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
422 | 416 |
} |
423 | 417 |
} |
... | ... | |
425 | 419 |
} |
426 | 420 |
|
427 | 421 |
public void performUpdateTable(Connection conn, |
428 |
String database, |
|
429 |
String schema, |
|
430 |
String table, |
|
422 |
TableReference table, |
|
431 | 423 |
FeatureType original, |
432 | 424 |
FeatureType target) throws DataException { |
433 | 425 |
|
434 | 426 |
ArrayList<String> sqls = new ArrayList<>(); |
435 | 427 |
|
436 |
sqls.addAll(buildUpdateTableSQL(database, schema, table, original, target));
|
|
437 |
sqls.addAll(buildCreateIndexSQL(database, schema, table, original, target));
|
|
428 |
sqls.addAll(buildUpdateTableSQL(table, original, target)); |
|
429 |
sqls.addAll(buildCreateIndexSQL(table, original, target)); |
|
438 | 430 |
|
439 | 431 |
if( !CollectionUtils.isEmpty(sqls) ) { |
440 | 432 |
Statement st = null; |
Also available in: Unified diff