Revision 46806 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 | ||
---|---|---|
26 | 26 |
import java.sql.PreparedStatement; |
27 | 27 |
import java.sql.SQLException; |
28 | 28 |
import java.sql.Statement; |
29 |
import java.util.ArrayList; |
|
30 | 29 |
import java.util.Arrays; |
31 | 30 |
import java.util.Iterator; |
32 | 31 |
import java.util.List; |
... | ... | |
39 | 38 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
40 | 39 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
41 | 40 |
import org.gvsig.fmap.dal.DataTypes; |
41 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
42 | 42 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE; |
43 | 43 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLE; |
44 | 44 |
import org.gvsig.fmap.dal.exception.DataException; |
... | ... | |
60 | 60 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
61 | 61 |
import org.gvsig.tools.dispose.Disposable; |
62 | 62 |
import org.gvsig.tools.dispose.DisposeUtils; |
63 |
import org.gvsig.tools.util.Bitmask; |
|
63 | 64 |
import org.gvsig.tools.util.Invocable; |
64 | 65 |
|
65 | 66 |
@SuppressWarnings("UseSpecificCatch") |
... | ... | |
408 | 409 |
.schema(this.table.getSchema()) |
409 | 410 |
.name(this.table.getTable()); |
410 | 411 |
|
411 |
// Primero comprobamos si hay que eliminar la primary-key o algun indice |
|
412 |
for (FeatureAttributeDescriptor attrOrgiginal : currentFeatureTypeDB) { |
|
413 |
if( attrOrgiginal.isComputed() ) { |
|
414 |
continue; |
|
415 |
} |
|
416 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor( |
|
417 |
attrOrgiginal.getName() |
|
418 |
); |
|
419 |
if (attrTarget == null) { |
|
420 |
continue; |
|
421 |
} |
|
422 |
if( attrOrgiginal.isPrimaryKey() && !attrTarget.isPrimaryKey() ) { |
|
423 |
sqlbuilder.alter_table().drop_primary_key(attrTarget.getName()); |
|
424 |
} |
|
425 |
} |
|
412 |
// // Primero comprobamos si hay que eliminar la primary-key o algun indice
|
|
413 |
// for (FeatureAttributeDescriptor attrOrgiginal : currentFeatureTypeDB) {
|
|
414 |
// if( attrOrgiginal.isComputed() ) {
|
|
415 |
// continue;
|
|
416 |
// }
|
|
417 |
// FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor(
|
|
418 |
// attrOrgiginal.getName()
|
|
419 |
// );
|
|
420 |
// if (attrTarget == null) {
|
|
421 |
// continue;
|
|
422 |
// }
|
|
423 |
// if( attrOrgiginal.isPrimaryKey() && !attrTarget.isPrimaryKey() ) {
|
|
424 |
// sqlbuilder.alter_table().drop_primary_key(attrTarget.getName());
|
|
425 |
// }
|
|
426 |
// }
|
|
426 | 427 |
|
427 | 428 |
// Comprobamos si hay que eliminar o modificar alguna columna existente |
428 | 429 |
for (FeatureAttributeDescriptor attrOrgiginal : currentFeatureTypeDB) { |
... | ... | |
434 | 435 |
); |
435 | 436 |
if (attrTarget == null) { |
436 | 437 |
sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
437 |
} else if( !this.areEquals(attrOrgiginal, attrTarget) ) { |
|
438 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) { |
|
439 |
sqlbuilder.alter_table().alter_geometry_column( |
|
440 |
attrTarget.getName(), |
|
441 |
attrTarget.getGeomType().getType(), |
|
442 |
attrTarget.getGeomType().getSubType(), |
|
443 |
attrTarget.getSRS(), |
|
444 |
attrTarget.isIndexed(), |
|
445 |
attrTarget.allowNull() |
|
446 |
); |
|
447 |
} else { |
|
448 |
Object defaultValue = attrTarget.getDefaultValue(); |
|
449 |
if( defaultValue instanceof CharSequence ) { |
|
450 |
if( ExpressionUtils.isDynamicText(defaultValue.toString())) { |
|
451 |
defaultValue = null; |
|
438 |
} else { |
|
439 |
Bitmask changes = this.getAlterTableChanges(attrOrgiginal, attrTarget); |
|
440 |
if( !changes.isEmpty() ) { |
|
441 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) { |
|
442 |
sqlbuilder.alter_table().alter_geometry_column( |
|
443 |
attrTarget.getName(), |
|
444 |
attrTarget.getGeomType().getType(), |
|
445 |
attrTarget.getGeomType().getSubType(), |
|
446 |
attrTarget.getSRS(), |
|
447 |
attrTarget.isIndexed(), |
|
448 |
attrTarget.allowNull() |
|
449 |
); |
|
450 |
} else { |
|
451 |
Object defaultValue = attrTarget.getDefaultValue(); |
|
452 |
if( defaultValue instanceof CharSequence ) { |
|
453 |
if( ExpressionUtils.isDynamicText(defaultValue.toString())) { |
|
454 |
defaultValue = null; |
|
455 |
} |
|
452 | 456 |
} |
453 |
}
|
|
454 |
sqlbuilder.alter_table().alter_column(
|
|
457 |
sqlbuilder.alter_table().alter_column(
|
|
458 |
changes,
|
|
455 | 459 |
attrTarget.getName(), |
456 | 460 |
attrTarget.getType(), |
457 | 461 |
attrTarget.getSize(), |
... | ... | |
462 | 466 |
attrTarget.allowNull(), |
463 | 467 |
attrTarget.isAutomatic(), |
464 | 468 |
defaultValue |
465 |
); |
|
469 |
); |
|
470 |
} |
|
466 | 471 |
} |
467 | 472 |
} |
468 | 473 |
} |
469 | 474 |
|
470 |
// Por ultimi comprobamos si hay que anadir alguna columna nueva.
|
|
475 |
// Por ultimo comprobamos si hay que anadir alguna columna nueva.
|
|
471 | 476 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) { |
472 | 477 |
if( attrTarget.isComputed() ) { |
473 | 478 |
continue; |
... | ... | |
513 | 518 |
PROP_TABLE, table |
514 | 519 |
); |
515 | 520 |
List<String> sqls = sqlbuilder.alter_table().toStrings(); |
516 |
sqls.addAll(buildCreateIndexSQL()); |
|
521 |
// sqls.addAll(buildCreateIndexSQL());
|
|
517 | 522 |
return sqls; |
518 | 523 |
} |
519 | 524 |
|
520 |
protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) {
|
|
521 |
// No interesa si son o no iguales en general, solo si son iguales en lo
|
|
522 |
// que a los atributos usados para crear la columna de la tabla se refiere. |
|
523 |
if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
|
|
524 |
return false;
|
|
525 |
protected Bitmask getAlterTableChanges(FeatureAttributeDescriptor src, FeatureAttributeDescriptor target) {
|
|
526 |
Bitmask changes = Bitmask.createBitmask(0);
|
|
527 |
|
|
528 |
if( src.getType() != target.getType() ) {
|
|
529 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_SET_DATA_TYPE);
|
|
525 | 530 |
} |
526 |
if( attr1.getType() != attr2.getType() ) {
|
|
527 |
return false;
|
|
531 |
if( src.getPrecision() != target.getPrecision() ) {
|
|
532 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_SET_DATA_TYPE);
|
|
528 | 533 |
} |
529 |
if( attr1.getPrecision() != attr2.getPrecision() ) {
|
|
530 |
return false;
|
|
534 |
if( src.getScale() != target.getScale() ) {
|
|
535 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_SET_DATA_TYPE);
|
|
531 | 536 |
} |
532 |
if( attr1.getScale() != attr2.getScale() ) {
|
|
533 |
return false;
|
|
537 |
if( src.getSize() != target.getSize() ) {
|
|
538 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_SET_DATA_TYPE);
|
|
534 | 539 |
} |
535 |
if( attr1.getSize() != attr2.getSize() ) { |
|
536 |
return false; |
|
537 |
} |
|
538 |
if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) { |
|
539 |
return false; |
|
540 |
if( src.isPrimaryKey() != target.isPrimaryKey() ) { |
|
541 |
if( target.isPrimaryKey() ) { |
|
542 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_ADD_PRIMARY_KEY); |
|
543 |
} else { |
|
544 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_DROP_PRIMARY_KEY); |
|
545 |
} |
|
540 | 546 |
} |
541 |
// if( attr1.isIndexed() != attr2.isIndexed() ) { |
|
542 |
// return false; |
|
543 |
// } |
|
544 |
if( attr1.allowNull() != attr2.allowNull() ) { |
|
545 |
return false; |
|
547 |
if( src.isIndexed()!= target.isIndexed()) { |
|
548 |
if( target.isIndexed()) { |
|
549 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_CREATE_INDEX); |
|
550 |
} else { |
|
551 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_DROP_INDEX); |
|
552 |
} |
|
553 |
} |
|
554 |
if( src.isAutomatic() != target.isAutomatic() ) { |
|
555 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_SET_DATA_TYPE); |
|
546 | 556 |
} |
547 |
if( attr1.isAutomatic() != attr2.isAutomatic() ) {
|
|
548 |
return false;
|
|
557 |
if( src.allowNull() != target.allowNull() ) {
|
|
558 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_SET_NULL);
|
|
549 | 559 |
} |
550 |
if( attr1.getDefaultValue() != attr2.getDefaultValue() ) { |
|
551 |
if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) { |
|
552 |
return false; |
|
553 |
} |
|
554 |
if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) { |
|
555 |
return false; |
|
556 |
} |
|
560 |
if( src.getDefaultValue() != target.getDefaultValue() ) { |
|
561 |
changes.setBit(SQLBuilder.AlterTableBuilder.ALTER_COLUMN_SET_DEFAULT); |
|
557 | 562 |
} |
558 |
return true;
|
|
563 |
return changes;
|
|
559 | 564 |
} |
560 | 565 |
|
561 |
protected List<String> buildCreateIndexSQL() { |
|
562 |
ArrayList<String> sqls = new ArrayList<>(); |
|
563 |
|
|
564 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) { |
|
565 |
boolean createIndex; |
|
566 |
boolean dropIndex; |
|
567 |
FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName()); |
|
568 |
if( attrOriginal != null ) { |
|
569 |
createIndex = (attrTarget.isIndexed() && !attrOriginal.isIndexed()); |
|
570 |
dropIndex = (!attrTarget.isIndexed() && attrOriginal.isIndexed()); |
|
571 |
} else { |
|
572 |
createIndex = attrTarget.isIndexed(); |
|
573 |
dropIndex = false; |
|
574 |
} |
|
575 |
if( createIndex ) { |
|
576 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
|
577 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) { |
|
578 |
sqlbuilder.create_index().spatial(); |
|
579 |
} |
|
580 |
sqlbuilder.create_index().if_not_exist(); |
|
581 |
sqlbuilder.create_index().name(table.getTable(), attrTarget.getName()); |
|
582 |
sqlbuilder.create_index().column(attrTarget.getName()); |
|
583 |
sqlbuilder.create_index().table() |
|
584 |
.database(this.table.getDatabase()) |
|
585 |
.schema(this.table.getSchema()) |
|
586 |
.name(this.table.getTable()); |
|
587 |
sqlbuilder.setProperties( |
|
588 |
Variable.class, |
|
589 |
PROP_TABLE, table |
|
590 |
); |
|
591 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
|
592 |
} else if( dropIndex ) { |
|
593 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
|
594 |
sqlbuilder.drop_index().if_not_exist(); |
|
595 |
sqlbuilder.drop_index().name( table.getTable(), attrTarget.getName()); |
|
596 |
} |
|
597 |
} |
|
598 |
return sqls; |
|
599 |
} |
|
566 |
// protected List<String> buildCreateIndexSQL() {
|
|
567 |
// ArrayList<String> sqls = new ArrayList<>();
|
|
568 |
// |
|
569 |
// for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
|
570 |
// boolean createIndex;
|
|
571 |
// boolean dropIndex;
|
|
572 |
// FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName());
|
|
573 |
// if( attrOriginal != null ) {
|
|
574 |
// createIndex = (attrTarget.isIndexed() && !attrOriginal.isIndexed());
|
|
575 |
// dropIndex = (!attrTarget.isIndexed() && attrOriginal.isIndexed());
|
|
576 |
// } else {
|
|
577 |
// createIndex = attrTarget.isIndexed();
|
|
578 |
// dropIndex = false;
|
|
579 |
// }
|
|
580 |
// if( createIndex ) {
|
|
581 |
// JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
|
|
582 |
// if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
|
583 |
// sqlbuilder.create_index().spatial();
|
|
584 |
// }
|
|
585 |
// sqlbuilder.create_index().if_not_exist();
|
|
586 |
// sqlbuilder.create_index().name(table.getTable(), attrTarget.getName());
|
|
587 |
// sqlbuilder.create_index().column(attrTarget.getName());
|
|
588 |
// sqlbuilder.create_index().table()
|
|
589 |
// .database(this.table.getDatabase())
|
|
590 |
// .schema(this.table.getSchema())
|
|
591 |
// .name(this.table.getTable());
|
|
592 |
// sqlbuilder.setProperties(
|
|
593 |
// Variable.class,
|
|
594 |
// PROP_TABLE, table
|
|
595 |
// );
|
|
596 |
// sqls.addAll(sqlbuilder.create_index().toStrings());
|
|
597 |
// } else if( dropIndex ) {
|
|
598 |
// JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
|
|
599 |
// sqlbuilder.drop_index().if_not_exist();
|
|
600 |
// sqlbuilder.drop_index().name( table.getTable(), attrTarget.getName());
|
|
601 |
// }
|
|
602 |
// }
|
|
603 |
// return sqls;
|
|
604 |
// }
|
|
600 | 605 |
|
601 | 606 |
public void performUpdateTable(JDBCConnection conn) throws DataException { |
602 | 607 |
|
Also available in: Unified diff