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 @ 45614
History | View | Annotate | Download (20.3 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.Parameter; |
37 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
38 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
39 |
import org.gvsig.fmap.dal.DataTypes; |
40 |
import org.gvsig.fmap.dal.exception.DataException; |
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType.FeatureTypeChanged; |
45 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
47 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
48 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
49 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
50 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
51 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
52 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
53 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
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 (inserteds.hasNext()) {
|
111 |
performInserts(conn); |
112 |
} |
113 |
if (updateds.hasNext()) {
|
114 |
performUpdates(conn); |
115 |
} |
116 |
if (deleteds.hasNext()) {
|
117 |
performDeletes(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 |
sqlbuilder.setProperties( |
225 |
Parameter.class, |
226 |
PROP_FEATURE_TYPE, featureType, |
227 |
PROP_TABLE, table |
228 |
); |
229 |
String sql = sqlbuilder.insert().toString();
|
230 |
return sql;
|
231 |
} |
232 |
|
233 |
public void performInserts(Connection conn) throws DataException { |
234 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
235 |
String sql = getInsertSQL(sqlbuilder);
|
236 |
|
237 |
PreparedStatement st;
|
238 |
Disposable paramsDisposer; |
239 |
try {
|
240 |
st = conn.prepareStatement(sql); |
241 |
while (inserteds.hasNext()) {
|
242 |
FeatureProvider feature = inserteds.next(); |
243 |
paramsDisposer = sqlbuilder.setParameters(st, feature); |
244 |
try {
|
245 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
246 |
throw new JDBCExecuteSQLException( |
247 |
sqlbuilder.insert().toString(), |
248 |
null
|
249 |
); |
250 |
} |
251 |
} finally {
|
252 |
DisposeUtils.disposeQuietly(paramsDisposer); |
253 |
} |
254 |
} |
255 |
} catch (JDBCExecuteSQLException ex) {
|
256 |
throw ex;
|
257 |
} catch (Exception ex) { |
258 |
throw new JDBCExecuteSQLException(sql,ex); |
259 |
} |
260 |
} |
261 |
|
262 |
public String getUpdateSQL() { |
263 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
264 |
return this.getUpdateSQL(sqlbuilder); |
265 |
} |
266 |
|
267 |
public String getUpdateSQL(JDBCSQLBuilderBase sqlbuilder) { |
268 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
269 |
|
270 |
sqlbuilder.update().table() |
271 |
.database(this.table.getDatabase())
|
272 |
.schema(this.table.getSchema())
|
273 |
.name(this.table.getTable());
|
274 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
275 |
if (attr.isPrimaryKey()) {
|
276 |
sqlbuilder.update().where().and( |
277 |
expbuilder.eq( |
278 |
expbuilder.column(attr.getName()), |
279 |
expbuilder.parameter(attr.getName()).as_variable() |
280 |
) |
281 |
); |
282 |
continue;
|
283 |
} |
284 |
if ( attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
|
285 |
continue;
|
286 |
} |
287 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
288 |
if (attr.getSRS()==null) { |
289 |
throw new RuntimeException("Geometries with null srs are not valid. SRS is required in "+this.table.toString()+"in the field:"+attr.getName()); |
290 |
} |
291 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
292 |
expbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
293 |
expbuilder.parameter().value(attr.getSRS()).as_constant() |
294 |
) |
295 |
); |
296 |
} else {
|
297 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
298 |
expbuilder.parameter(attr.getName()).as_variable() |
299 |
); |
300 |
} |
301 |
} |
302 |
if (!sqlbuilder.update().has_where() ) {
|
303 |
throw new RuntimeException("Operation requires missing pk"); |
304 |
} |
305 |
sqlbuilder.setProperties( |
306 |
Variable.class, |
307 |
PROP_FEATURE_TYPE, this.featureType,
|
308 |
PROP_TABLE, table |
309 |
); |
310 |
sqlbuilder.setProperties( |
311 |
Parameter.class, |
312 |
PROP_FEATURE_TYPE, featureType, |
313 |
PROP_TABLE, table |
314 |
); |
315 |
|
316 |
String sql = sqlbuilder.update().toString();
|
317 |
return sql;
|
318 |
} |
319 |
|
320 |
public void performUpdates(Connection conn) throws DataException { |
321 |
|
322 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
323 |
String sql = getUpdateSQL(sqlbuilder);
|
324 |
|
325 |
PreparedStatement st = null; |
326 |
Disposable paramsDisposer = null;
|
327 |
try {
|
328 |
st = conn.prepareStatement(sql); |
329 |
while (updateds.hasNext()) {
|
330 |
FeatureProvider featureProvider = (FeatureProvider) updateds.next(); |
331 |
paramsDisposer = sqlbuilder.setParameters(st, featureProvider); |
332 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
333 |
throw new JDBCUpdateWithoutChangesException(sql,null); |
334 |
} |
335 |
} |
336 |
} catch (SQLException e) { |
337 |
throw new JDBCSQLException(e); |
338 |
} finally {
|
339 |
JDBCUtils.closeQuietly(st); |
340 |
DisposeUtils.disposeQuietly(paramsDisposer); |
341 |
} |
342 |
} |
343 |
|
344 |
public List<String> getUpdateTableSQLs() { |
345 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
346 |
sqlbuilder.alter_table().table() |
347 |
.database(this.table.getDatabase())
|
348 |
.schema(this.table.getSchema())
|
349 |
.name(this.table.getTable());
|
350 |
|
351 |
for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
|
352 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor( |
353 |
attrOrgiginal.getName() |
354 |
); |
355 |
if (attrTarget == null) { |
356 |
if( attrOrgiginal.isComputed() ) {
|
357 |
continue;
|
358 |
} |
359 |
sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
360 |
} else if( !this.areEquals(attrOrgiginal, attrTarget) ) { |
361 |
if( attrTarget.isComputed() ) {
|
362 |
continue;
|
363 |
} |
364 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
365 |
sqlbuilder.alter_table().alter_geometry_column( |
366 |
attrTarget.getName(), |
367 |
attrTarget.getGeomType().getType(), |
368 |
attrTarget.getGeomType().getSubType(), |
369 |
attrTarget.getSRS(), |
370 |
attrTarget.isIndexed(), |
371 |
attrTarget.allowNull() |
372 |
); |
373 |
} else {
|
374 |
sqlbuilder.alter_table().alter_column( |
375 |
attrTarget.getName(), |
376 |
attrTarget.getType(), |
377 |
attrTarget.getSize(), |
378 |
attrTarget.getPrecision(), |
379 |
attrTarget.getScale(), |
380 |
attrTarget.isPrimaryKey(), |
381 |
attrTarget.isIndexed(), |
382 |
attrTarget.allowNull(), |
383 |
attrTarget.isAutomatic(), |
384 |
attrTarget.getDefaultValue() |
385 |
); |
386 |
} |
387 |
} |
388 |
} |
389 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
390 |
if( attrTarget.isComputed() ) {
|
391 |
continue;
|
392 |
} |
393 |
if (featureTypeSource.getAttributeDescriptor(attrTarget.getName()) == null) { |
394 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
395 |
sqlbuilder.alter_table().add_geometry_column( |
396 |
attrTarget.getName(), |
397 |
attrTarget.getGeomType().getType(), |
398 |
attrTarget.getGeomType().getSubType(), |
399 |
attrTarget.getSRS(), |
400 |
attrTarget.isIndexed(), |
401 |
attrTarget.allowNull() |
402 |
); |
403 |
} else {
|
404 |
sqlbuilder.alter_table().add_column( |
405 |
attrTarget.getName(), |
406 |
attrTarget.getType(), |
407 |
attrTarget.getSize(), |
408 |
attrTarget.getPrecision(), |
409 |
attrTarget.getScale(), |
410 |
attrTarget.isPrimaryKey(), |
411 |
attrTarget.isIndexed(), |
412 |
attrTarget.allowNull(), |
413 |
attrTarget.isAutomatic(), |
414 |
attrTarget.getDefaultValue() |
415 |
); |
416 |
} |
417 |
} |
418 |
} |
419 |
sqlbuilder.setProperties( |
420 |
Variable.class, |
421 |
PROP_TABLE, table |
422 |
); |
423 |
List<String> sqls = sqlbuilder.alter_table().toStrings(); |
424 |
sqls.addAll(buildCreateIndexSQL()); |
425 |
return sqls;
|
426 |
} |
427 |
|
428 |
protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) { |
429 |
// No interesa si son o no iguales en general, solo si son iguales en lo
|
430 |
// que a los atributos usados para crear la columna de la tabla se refiere.
|
431 |
if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
|
432 |
return false; |
433 |
} |
434 |
if( attr1.getType() != attr2.getType() ) {
|
435 |
return false; |
436 |
} |
437 |
if( attr1.getPrecision() != attr2.getPrecision() ) {
|
438 |
return false; |
439 |
} |
440 |
if( attr1.getScale() != attr2.getScale() ) {
|
441 |
return false; |
442 |
} |
443 |
if( attr1.getSize() != attr2.getSize() ) {
|
444 |
return false; |
445 |
} |
446 |
if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
|
447 |
return false; |
448 |
} |
449 |
// if( attr1.isIndexed() != attr2.isIndexed() ) {
|
450 |
// return false;
|
451 |
// }
|
452 |
if( attr1.allowNull() != attr2.allowNull() ) {
|
453 |
return false; |
454 |
} |
455 |
if( attr1.isAutomatic() != attr2.isAutomatic() ) {
|
456 |
return false; |
457 |
} |
458 |
if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
|
459 |
if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) { |
460 |
return false; |
461 |
} |
462 |
if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
|
463 |
return false; |
464 |
} |
465 |
} |
466 |
return true; |
467 |
} |
468 |
|
469 |
protected List<String> buildCreateIndexSQL() { |
470 |
ArrayList<String> sqls = new ArrayList<>(); |
471 |
|
472 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
473 |
boolean createIndex = false; |
474 |
if( attrTarget.isIndexed() ) {
|
475 |
FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName()); |
476 |
if ( attrOriginal == null) { |
477 |
createIndex = true;
|
478 |
} else {
|
479 |
if( attrOriginal.isIndexed() ) {
|
480 |
createIndex = false;
|
481 |
} else {
|
482 |
createIndex = true;
|
483 |
} |
484 |
} |
485 |
} |
486 |
if( createIndex ) {
|
487 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
488 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
489 |
sqlbuilder.create_index().spatial(); |
490 |
} |
491 |
sqlbuilder.create_index().if_not_exist(); |
492 |
sqlbuilder.create_index().name("idx_" + table + "_" + attrTarget.getName()); |
493 |
sqlbuilder.create_index().column(attrTarget.getName()); |
494 |
sqlbuilder.create_index().table() |
495 |
.database(this.table.getDatabase())
|
496 |
.schema(this.table.getSchema())
|
497 |
.name(this.table.getTable());
|
498 |
sqlbuilder.setProperties( |
499 |
Variable.class, |
500 |
PROP_TABLE, table |
501 |
); |
502 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
503 |
} |
504 |
} |
505 |
return sqls;
|
506 |
} |
507 |
|
508 |
public void performUpdateTable(Connection conn) throws DataException { |
509 |
|
510 |
List<String> sqls = this.getUpdateTableSQLs(); |
511 |
|
512 |
if( !CollectionUtils.isEmpty(sqls) ) {
|
513 |
Statement st = null; |
514 |
try {
|
515 |
st = conn.createStatement(); |
516 |
for (String sql : sqls) { |
517 |
if( !StringUtils.isBlank(sql) ) {
|
518 |
JDBCUtils.execute(st, sql); |
519 |
} |
520 |
} |
521 |
} catch (SQLException e) { |
522 |
throw new JDBCSQLException(e); |
523 |
} finally {
|
524 |
JDBCUtils.closeQuietly(st); |
525 |
} |
526 |
} |
527 |
} |
528 |
} |