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 / PerformChangesOperation.java @ 45065
History | View | Annotate | Download (19.7 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
25 |
|
26 |
import java.sql.Connection; |
27 |
import java.sql.PreparedStatement; |
28 |
import java.sql.SQLException; |
29 |
import java.sql.Statement; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import org.apache.commons.collections.CollectionUtils; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
36 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
37 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
38 |
import org.gvsig.fmap.dal.DataTypes; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
42 |
import org.gvsig.fmap.dal.feature.FeatureType; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType.FeatureTypeChanged; |
44 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
45 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
47 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
48 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
49 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
50 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
51 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
52 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
53 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
54 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
55 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
56 |
import org.gvsig.tools.dispose.Disposable; |
57 |
import org.gvsig.tools.dispose.DisposeUtils; |
58 |
|
59 |
@SuppressWarnings("UseSpecificCatch") |
60 |
public class PerformChangesOperation extends AbstractConnectionWritableOperation { |
61 |
|
62 |
protected TableReference table;
|
63 |
protected FeatureType featureType;
|
64 |
protected FeatureType featureTypeSource;
|
65 |
protected FeatureType featureTypeTarget;
|
66 |
protected Iterator<FeatureReferenceProviderServices> deleteds; |
67 |
protected Iterator<FeatureProvider> updateds; |
68 |
protected Iterator<FeatureProvider> inserteds; |
69 |
|
70 |
protected boolean typeChanged = false; |
71 |
|
72 |
public PerformChangesOperation(JDBCHelper helper) {
|
73 |
this(helper, null, null, null, null, null, null); |
74 |
} |
75 |
|
76 |
public PerformChangesOperation(JDBCHelper helper,
|
77 |
TableReference table, |
78 |
FeatureType featureType, |
79 |
Iterator<FeatureReferenceProviderServices> deleteds,
|
80 |
Iterator<FeatureProvider> inserteds,
|
81 |
Iterator<FeatureProvider> updateds,
|
82 |
Iterator<FeatureTypeChanged> featureTypesChanged) {
|
83 |
super(helper);
|
84 |
this.deleteds = deleteds;
|
85 |
this.inserteds = inserteds;
|
86 |
this.updateds = updateds;
|
87 |
this.table = table;
|
88 |
this.featureType = featureType;
|
89 |
if (featureTypesChanged.hasNext()) {
|
90 |
FeatureTypeChanged item = featureTypesChanged.next(); |
91 |
this.featureTypeSource = item.getSource();
|
92 |
this.featureTypeTarget = item.getTarget();
|
93 |
typeChanged = true;
|
94 |
} else {
|
95 |
this.featureTypeSource = null; |
96 |
this.featureTypeTarget = null; |
97 |
typeChanged = false;
|
98 |
} |
99 |
} |
100 |
|
101 |
public boolean isTypeChanged() { |
102 |
return typeChanged;
|
103 |
} |
104 |
|
105 |
@Override
|
106 |
public Object perform(Connection conn) throws DataException { |
107 |
if (typeChanged) {
|
108 |
this.performUpdateTable(conn);
|
109 |
} |
110 |
if (deleteds.hasNext()) {
|
111 |
performDeletes(conn); |
112 |
} |
113 |
if (updateds.hasNext()) {
|
114 |
performUpdates(conn); |
115 |
} |
116 |
if (inserteds.hasNext()) {
|
117 |
performInserts(conn); |
118 |
} |
119 |
return true; |
120 |
} |
121 |
|
122 |
public String getDeleteSQL() { |
123 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
124 |
return this.getDeleteSQL(sqlbuilder); |
125 |
} |
126 |
|
127 |
public String getDeleteSQL(JDBCSQLBuilderBase sqlbuilder) { |
128 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
129 |
|
130 |
sqlbuilder.delete().table() |
131 |
.database(this.table.getDatabase())
|
132 |
.schema(this.table.getSchema())
|
133 |
.name(this.table.getTable());
|
134 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
135 |
if (attr.isPrimaryKey()) {
|
136 |
sqlbuilder.delete().where().and( |
137 |
expbuilder.eq( |
138 |
expbuilder.column(attr.getName()), |
139 |
expbuilder.parameter(attr.getName()).as_variable() |
140 |
) |
141 |
); |
142 |
} |
143 |
} |
144 |
if (!sqlbuilder.delete().has_where() ) {
|
145 |
throw new RuntimeException("Operation requires missing pk"); |
146 |
} |
147 |
sqlbuilder.setProperties( |
148 |
Variable.class, |
149 |
PROP_TABLE, table |
150 |
); |
151 |
String sql = sqlbuilder.delete().toString();
|
152 |
return sql;
|
153 |
} |
154 |
|
155 |
public void performDeletes(Connection conn) throws DataException { |
156 |
|
157 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
158 |
String sql = getDeleteSQL(sqlbuilder);
|
159 |
|
160 |
PreparedStatement st = null; |
161 |
Disposable paramsDisposer = null;
|
162 |
try {
|
163 |
st = conn.prepareStatement(sql); |
164 |
while (deleteds.hasNext()) {
|
165 |
FeatureReference reference = (FeatureReference) deleteds.next(); |
166 |
paramsDisposer = sqlbuilder.setParameters(st, reference); |
167 |
int nAffected = JDBCUtils.executeUpdate(st,sql);
|
168 |
if (nAffected == 0) { |
169 |
throw new JDBCUpdateWithoutChangesException( |
170 |
sqlbuilder.delete().toString(), |
171 |
null
|
172 |
); |
173 |
} |
174 |
if (nAffected > 1) { |
175 |
LOGGER.warn("Remove statement affectst to {} rows ( {} )",
|
176 |
nAffected, sql |
177 |
); |
178 |
} |
179 |
} |
180 |
} catch (SQLException e) { |
181 |
throw new JDBCSQLException(e); |
182 |
} finally {
|
183 |
JDBCUtils.closeQuietly(st); |
184 |
DisposeUtils.disposeQuietly(paramsDisposer); |
185 |
} |
186 |
} |
187 |
|
188 |
public String getInsertSQL() { |
189 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
190 |
return this.getInsertSQL(sqlbuilder); |
191 |
} |
192 |
|
193 |
public String getInsertSQL(JDBCSQLBuilderBase sqlbuilder) { |
194 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
195 |
|
196 |
sqlbuilder.insert().table() |
197 |
.database(this.table.getDatabase())
|
198 |
.schema(this.table.getSchema())
|
199 |
.name(this.table.getTable());
|
200 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
201 |
if( attr.isAutomatic() || attr.isComputed() ) {
|
202 |
continue;
|
203 |
} |
204 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
205 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
206 |
expbuilder.parameter(attr.getName()).as_variable() |
207 |
.srs( |
208 |
expbuilder.parameter().value( |
209 |
attr.getSRS()).as_constant() |
210 |
) |
211 |
); |
212 |
} else {
|
213 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
214 |
expbuilder.parameter(attr.getName()) |
215 |
); |
216 |
} |
217 |
} |
218 |
|
219 |
sqlbuilder.setProperties( |
220 |
Variable.class, |
221 |
PROP_FEATURE_TYPE, featureType, |
222 |
PROP_TABLE, table |
223 |
); |
224 |
String sql = sqlbuilder.insert().toString();
|
225 |
return sql;
|
226 |
} |
227 |
|
228 |
public void performInserts(Connection conn) throws DataException { |
229 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
230 |
String sql = getInsertSQL(sqlbuilder);
|
231 |
|
232 |
PreparedStatement st;
|
233 |
Disposable paramsDisposer; |
234 |
try {
|
235 |
st = conn.prepareStatement(sql); |
236 |
while (inserteds.hasNext()) {
|
237 |
FeatureProvider feature = inserteds.next(); |
238 |
paramsDisposer = sqlbuilder.setParameters(st, feature); |
239 |
try {
|
240 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
241 |
throw new JDBCExecuteSQLException( |
242 |
sqlbuilder.insert().toString(), |
243 |
null
|
244 |
); |
245 |
} |
246 |
} finally {
|
247 |
DisposeUtils.disposeQuietly(paramsDisposer); |
248 |
} |
249 |
} |
250 |
} catch (JDBCExecuteSQLException ex) {
|
251 |
throw ex;
|
252 |
} catch (Exception ex) { |
253 |
throw new JDBCExecuteSQLException(sql,ex); |
254 |
} |
255 |
} |
256 |
|
257 |
public String getUpdateSQL() { |
258 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
259 |
return this.getUpdateSQL(sqlbuilder); |
260 |
} |
261 |
|
262 |
public String getUpdateSQL(JDBCSQLBuilderBase sqlbuilder) { |
263 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
264 |
|
265 |
sqlbuilder.update().table() |
266 |
.database(this.table.getDatabase())
|
267 |
.schema(this.table.getSchema())
|
268 |
.name(this.table.getTable());
|
269 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
270 |
if (attr.isPrimaryKey()) {
|
271 |
sqlbuilder.update().where().and( |
272 |
expbuilder.eq( |
273 |
expbuilder.column(attr.getName()), |
274 |
expbuilder.parameter(attr.getName()).as_variable() |
275 |
) |
276 |
); |
277 |
continue;
|
278 |
} |
279 |
if ( attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
|
280 |
continue;
|
281 |
} |
282 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
283 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
284 |
expbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
285 |
expbuilder.parameter().value(attr.getSRS()).as_constant() |
286 |
) |
287 |
); |
288 |
} else {
|
289 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
290 |
expbuilder.parameter(attr.getName()).as_variable() |
291 |
); |
292 |
} |
293 |
} |
294 |
if (!sqlbuilder.update().has_where() ) {
|
295 |
throw new RuntimeException("Operation requires missing pk"); |
296 |
} |
297 |
sqlbuilder.setProperties( |
298 |
Variable.class, |
299 |
PROP_FEATURE_TYPE, this.featureType,
|
300 |
PROP_TABLE, table |
301 |
); |
302 |
String sql = sqlbuilder.update().toString();
|
303 |
return sql;
|
304 |
} |
305 |
|
306 |
public void performUpdates(Connection conn) throws DataException { |
307 |
|
308 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
309 |
String sql = getUpdateSQL(sqlbuilder);
|
310 |
|
311 |
PreparedStatement st = null; |
312 |
Disposable paramsDisposer = null;
|
313 |
try {
|
314 |
st = conn.prepareStatement(sql); |
315 |
while (updateds.hasNext()) {
|
316 |
FeatureProvider featureProvider = (FeatureProvider) updateds.next(); |
317 |
paramsDisposer = sqlbuilder.setParameters(st, featureProvider); |
318 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
319 |
throw new JDBCUpdateWithoutChangesException(sql,null); |
320 |
} |
321 |
} |
322 |
} catch (SQLException e) { |
323 |
throw new JDBCSQLException(e); |
324 |
} finally {
|
325 |
JDBCUtils.closeQuietly(st); |
326 |
DisposeUtils.disposeQuietly(paramsDisposer); |
327 |
} |
328 |
} |
329 |
|
330 |
public List<String> getUpdateTableSQLs() { |
331 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
332 |
sqlbuilder.alter_table().table() |
333 |
.database(this.table.getDatabase())
|
334 |
.schema(this.table.getSchema())
|
335 |
.name(this.table.getTable());
|
336 |
|
337 |
for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
|
338 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor( |
339 |
attrOrgiginal.getName() |
340 |
); |
341 |
if (attrTarget == null) { |
342 |
if( attrOrgiginal.isComputed() ) {
|
343 |
continue;
|
344 |
} |
345 |
sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
346 |
} else if( !this.areEquals(attrOrgiginal, attrTarget) ) { |
347 |
if( attrTarget.isComputed() ) {
|
348 |
continue;
|
349 |
} |
350 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
351 |
sqlbuilder.alter_table().alter_geometry_column( |
352 |
attrTarget.getName(), |
353 |
attrTarget.getGeomType().getType(), |
354 |
attrTarget.getGeomType().getSubType(), |
355 |
attrTarget.getSRS(), |
356 |
attrTarget.isIndexed(), |
357 |
attrTarget.allowNull() |
358 |
); |
359 |
} else {
|
360 |
sqlbuilder.alter_table().alter_column( |
361 |
attrTarget.getName(), |
362 |
attrTarget.getType(), |
363 |
attrTarget.getSize(), |
364 |
attrTarget.getPrecision(), |
365 |
attrTarget.getScale(), |
366 |
attrTarget.isPrimaryKey(), |
367 |
attrTarget.isIndexed(), |
368 |
attrTarget.allowNull(), |
369 |
attrTarget.isAutomatic(), |
370 |
attrTarget.getDefaultValue() |
371 |
); |
372 |
} |
373 |
} |
374 |
} |
375 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
376 |
if( attrTarget.isComputed() ) {
|
377 |
continue;
|
378 |
} |
379 |
if (featureTypeSource.getAttributeDescriptor(attrTarget.getName()) == null) { |
380 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
381 |
sqlbuilder.alter_table().add_geometry_column( |
382 |
attrTarget.getName(), |
383 |
attrTarget.getGeomType().getType(), |
384 |
attrTarget.getGeomType().getSubType(), |
385 |
attrTarget.getSRS(), |
386 |
attrTarget.isIndexed(), |
387 |
attrTarget.allowNull() |
388 |
); |
389 |
} else {
|
390 |
sqlbuilder.alter_table().add_column( |
391 |
attrTarget.getName(), |
392 |
attrTarget.getType(), |
393 |
attrTarget.getSize(), |
394 |
attrTarget.getPrecision(), |
395 |
attrTarget.getScale(), |
396 |
attrTarget.isPrimaryKey(), |
397 |
attrTarget.isIndexed(), |
398 |
attrTarget.allowNull(), |
399 |
attrTarget.isAutomatic(), |
400 |
attrTarget.getDefaultValue() |
401 |
); |
402 |
} |
403 |
} |
404 |
} |
405 |
sqlbuilder.setProperties( |
406 |
Variable.class, |
407 |
PROP_TABLE, table |
408 |
); |
409 |
|
410 |
List<String> sqls = sqlbuilder.alter_table().toStrings(); |
411 |
sqls.addAll(buildCreateIndexSQL()); |
412 |
return sqls;
|
413 |
} |
414 |
|
415 |
protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) { |
416 |
// No interesa si son o no iguales en general, solo si son iguales en lo
|
417 |
// que a los atributos usados para crear la columna de la tabla se refiere.
|
418 |
if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
|
419 |
return false; |
420 |
} |
421 |
if( attr1.getType() != attr2.getType() ) {
|
422 |
return false; |
423 |
} |
424 |
if( attr1.getPrecision() != attr2.getPrecision() ) {
|
425 |
return false; |
426 |
} |
427 |
if( attr1.getScale() != attr2.getScale() ) {
|
428 |
return false; |
429 |
} |
430 |
if( attr1.getSize() != attr2.getSize() ) {
|
431 |
return false; |
432 |
} |
433 |
if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
|
434 |
return false; |
435 |
} |
436 |
// if( attr1.isIndexed() != attr2.isIndexed() ) {
|
437 |
// return false;
|
438 |
// }
|
439 |
if( attr1.allowNull() != attr2.allowNull() ) {
|
440 |
return false; |
441 |
} |
442 |
if( attr1.isAutomatic() != attr2.isAutomatic() ) {
|
443 |
return false; |
444 |
} |
445 |
if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
|
446 |
if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) { |
447 |
return false; |
448 |
} |
449 |
if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
|
450 |
return false; |
451 |
} |
452 |
} |
453 |
return true; |
454 |
} |
455 |
|
456 |
protected List<String> buildCreateIndexSQL() { |
457 |
ArrayList<String> sqls = new ArrayList<>(); |
458 |
|
459 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
460 |
boolean createIndex = false; |
461 |
if( attrTarget.isIndexed() ) {
|
462 |
FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName()); |
463 |
if ( attrOriginal == null) { |
464 |
createIndex = true;
|
465 |
} else {
|
466 |
if( attrOriginal.isIndexed() ) {
|
467 |
createIndex = false;
|
468 |
} else {
|
469 |
createIndex = true;
|
470 |
} |
471 |
} |
472 |
} |
473 |
if( createIndex ) {
|
474 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
475 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
476 |
sqlbuilder.create_index().spatial(); |
477 |
} |
478 |
sqlbuilder.create_index().if_not_exist(); |
479 |
sqlbuilder.create_index().name("idx_" + table + "_" + attrTarget.getName()); |
480 |
sqlbuilder.create_index().column(attrTarget.getName()); |
481 |
sqlbuilder.create_index().table() |
482 |
.database(this.table.getDatabase())
|
483 |
.schema(this.table.getSchema())
|
484 |
.name(this.table.getTable());
|
485 |
sqlbuilder.setProperties( |
486 |
Variable.class, |
487 |
PROP_TABLE, table |
488 |
); |
489 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
490 |
} |
491 |
} |
492 |
return sqls;
|
493 |
} |
494 |
|
495 |
public void performUpdateTable(Connection conn) throws DataException { |
496 |
|
497 |
List<String> sqls = this.getUpdateTableSQLs(); |
498 |
|
499 |
if( !CollectionUtils.isEmpty(sqls) ) {
|
500 |
Statement st = null; |
501 |
try {
|
502 |
st = conn.createStatement(); |
503 |
for (String sql : sqls) { |
504 |
if( !StringUtils.isBlank(sql) ) {
|
505 |
JDBCUtils.execute(st, sql); |
506 |
} |
507 |
} |
508 |
} catch (SQLException e) { |
509 |
throw new JDBCSQLException(e); |
510 |
} finally {
|
511 |
JDBCUtils.closeQuietly(st); |
512 |
} |
513 |
} |
514 |
} |
515 |
} |