Revision 43687 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

View differences:

PerformChangesOperation.java
4 4
import java.sql.PreparedStatement;
5 5
import java.sql.SQLException;
6 6
import java.sql.Statement;
7
import java.util.ArrayList;
7 8
import java.util.Iterator;
9
import java.util.List;
10
import org.apache.commons.collections.CollectionUtils;
8 11
import org.apache.commons.lang3.StringUtils;
9 12
import org.gvsig.fmap.dal.DataTypes;
10 13
import org.gvsig.fmap.dal.exception.DataException;
......
14 17
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
15 18
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
16 19
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
17
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase;
18 20
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
19 21
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
20 22
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
......
276 278
        }
277 279
    }
278 280

  
279
    protected JDBCSQLBuilderBase buildUpdateTableSQL(
281
    protected List<String> buildUpdateTableSQL(
280 282
            String database,
281 283
            String schema,
282 284
            String table,
......
343 345
                }
344 346
            }
345 347
        }
346
        return sqlbuilder;
348
        return sqlbuilder.alter_table().toStrings();
347 349
    }
348 350
    
349 351
    protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) {
......
364 366
        if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
365 367
            return false;
366 368
        }        
367
        if( attr1.isIndexed() != attr2.isIndexed() ) {
368
            return false;
369
        }
369
//        if( attr1.isIndexed() != attr2.isIndexed() ) {
370
//            return false;
371
//        }
370 372
        if( attr1.allowNull() != attr2.allowNull() ) {
371 373
            return false;
372 374
        }
......
384 386
        return true;
385 387
    }
386 388

  
389
    protected List<String> buildCreateIndexSQL(
390
            String database,
391
            String schema,
392
            String table,
393
            FeatureType original,
394
            FeatureType target
395
        ) {
396
        ArrayList<String> sqls = new ArrayList<>();
397
        
398
        for (FeatureAttributeDescriptor attrTarget : target) {
399
            boolean createIndex = false;
400
            if( attrTarget.isIndexed() ) {
401
                FeatureAttributeDescriptor attrOriginal = original.getAttributeDescriptor(attrTarget.getName());
402
                if ( attrOriginal == null) {
403
                    createIndex = true;
404
                } else {
405
                    if( attrOriginal.isIndexed() ) {
406
                        createIndex = false;
407
                    } else {
408
                        createIndex = true;
409
                    }
410
                }
411
            }
412
            if( createIndex ) {
413
                JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
414
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
415
                    sqlbuilder.create_index().spatial();
416
                }
417
                sqlbuilder.create_index().if_not_exist();
418
                sqlbuilder.create_index().name("idx_" + table + "_" + attrTarget.getName());
419
                sqlbuilder.create_index().column(attrTarget.getName());
420
                sqlbuilder.create_index().table().database(database).schema(schema).name(table);
421
                sqls.addAll(sqlbuilder.create_index().toStrings());
422
            }
423
        }
424
        return sqls;
425
    }
426
    
387 427
    public void performUpdateTable(Connection conn,
388 428
            String database,
389 429
            String schema,
390 430
            String table,
391 431
            FeatureType original,
392 432
            FeatureType target) throws DataException {
393
        SQLBuilderBase sqlbuilder = buildUpdateTableSQL(database, schema, table, original, target);
394
        Statement st = null;
395
        try {
396
            st = conn.createStatement();
397
            for (String sql : sqlbuilder.alter_table().toStrings()) {
398
                JDBCUtils.execute(st, sql);
433
        
434
        ArrayList<String> sqls = new ArrayList<>();
435
        
436
        sqls.addAll(buildUpdateTableSQL(database, schema, table, original, target));
437
        sqls.addAll(buildCreateIndexSQL(database, schema, table, original, target));
438

  
439
        if( !CollectionUtils.isEmpty(sqls) ) {
440
            Statement st = null;
441
            try {
442
                st = conn.createStatement();
443
                for (String sql : sqls) {
444
                    JDBCUtils.execute(st, sql);
445
                }
446
            } catch (SQLException e) {
447
                throw new JDBCSQLException(e);
448
            } finally {
449
                JDBCUtils.closeQuietly(st);
399 450
            }
400
        } catch (SQLException e) {
401
            throw new JDBCSQLException(e);
402
        } finally {
403
            JDBCUtils.closeQuietly(st);
404 451
        }
405 452
    }
406

  
407 453
}

Also available in: Unified diff