Revision 43650 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
5 5
import java.sql.SQLException;
6 6
import java.sql.Statement;
7 7
import java.util.Iterator;
8
import org.apache.commons.lang3.StringUtils;
8 9
import org.gvsig.fmap.dal.DataTypes;
9 10
import org.gvsig.fmap.dal.exception.DataException;
10 11
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
......
184 185
        JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(database, schema, table, type);
185 186

  
186 187
        PreparedStatement st;
187
        Disposable paramsDisposer = null;
188
        Disposable paramsDisposer;
188 189
        String sql = sqlbuilder.insert().toString();
189 190
        try {
190 191
            st = conn.prepareStatement(sql);
......
283 284
            FeatureType target
284 285
        ) {
285 286
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
286
        sqlbuilder.update().table().database(database).schema(schema).name(table);
287
        sqlbuilder.alter_table().table().database(database).schema(schema).name(table);
287 288

  
288 289
        for (FeatureAttributeDescriptor attrOrgiginal : original) {
289 290
            FeatureAttributeDescriptor attrTarget = target.getAttributeDescriptor(
......
291 292
            );
292 293
            if (attrTarget == null) {
293 294
                sqlbuilder.alter_table().drop_column(attrOrgiginal.getName());
294
            } else {
295
                sqlbuilder.alter_table().alter_column(
296
                        attrTarget.getName(),
297
                        attrTarget.getType(),
298
                        attrTarget.getPrecision(),
299
                        attrTarget.getSize(),
300
                        attrTarget.isPrimaryKey(),
301
                        attrTarget.isIndexed(),
302
                        attrTarget.allowNull(),
303
                        attrTarget.isAutomatic(),
304
                        attrTarget.getDefaultValue()
305
                );
295
            } else if( !this.areEquals(attrOrgiginal, attrTarget) ) {
296
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
297
                    sqlbuilder.alter_table().alter_geometry_column(
298
                            attrTarget.getName(),
299
                            attrTarget.getGeomType().getType(),
300
                            attrTarget.getGeomType().getSubType(),
301
                            attrTarget.getSRS(),
302
                            attrTarget.isIndexed(),
303
                            attrTarget.allowNull()
304
                    );
305
                } else {
306
                    sqlbuilder.alter_table().alter_column(
307
                            attrTarget.getName(),
308
                            attrTarget.getType(),
309
                            attrTarget.getPrecision(),
310
                            attrTarget.getSize(),
311
                            attrTarget.isPrimaryKey(),
312
                            attrTarget.isIndexed(),
313
                            attrTarget.allowNull(),
314
                            attrTarget.isAutomatic(),
315
                            attrTarget.getDefaultValue()
316
                    );
317
                }
306 318
            }
307 319
        }
308 320
        for (FeatureAttributeDescriptor attrTarget : target) {
309 321
            if (original.getAttributeDescriptor(attrTarget.getName()) == null) {
310
                sqlbuilder.alter_table().add_column(
311
                        attrTarget.getName(),
312
                        attrTarget.getType(),
313
                        attrTarget.getPrecision(),
314
                        attrTarget.getSize(),
315
                        attrTarget.isPrimaryKey(),
316
                        attrTarget.isIndexed(),
317
                        attrTarget.allowNull(),
318
                        attrTarget.isAutomatic(),
319
                        attrTarget.getDefaultValue()
320
                );
322
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
323
                    sqlbuilder.alter_table().add_geometry_column(
324
                            attrTarget.getName(),
325
                            attrTarget.getGeomType().getType(),
326
                            attrTarget.getGeomType().getSubType(),
327
                            attrTarget.getSRS(),
328
                            attrTarget.isIndexed(),
329
                            attrTarget.allowNull()
330
                    );
331
                } else {
332
                    sqlbuilder.alter_table().add_column(
333
                            attrTarget.getName(),
334
                            attrTarget.getType(),
335
                            attrTarget.getPrecision(),
336
                            attrTarget.getSize(),
337
                            attrTarget.isPrimaryKey(),
338
                            attrTarget.isIndexed(),
339
                            attrTarget.allowNull(),
340
                            attrTarget.isAutomatic(),
341
                            attrTarget.getDefaultValue()
342
                    );
343
                }
321 344
            }
322 345
        }
323 346
        return sqlbuilder;
324 347
    }
348
    
349
    protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) {
350
        // No interesa si son o no iguales en general, solo si son iguales en lo 
351
        // que a los atributos usados para crear la columna de la tabla se refiere.
352
        if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
353
            return false;
354
        }
355
        if( attr1.getType() != attr2.getType() ) {
356
            return false;
357
        }
358
        if( attr1.getPrecision() != attr2.getPrecision() ) {
359
            return false;
360
        }
361
        if( attr1.getSize() != attr2.getSize() ) {
362
            return false;
363
        }
364
        if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
365
            return false;
366
        }        
367
        if( attr1.isIndexed() != attr2.isIndexed() ) {
368
            return false;
369
        }
370
        if( attr1.allowNull() != attr2.allowNull() ) {
371
            return false;
372
        }
373
        if( attr1.isAutomatic() != attr2.isAutomatic() ) {
374
            return false;
375
        }
376
        if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
377
            if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) {
378
                return false;
379
            }
380
            if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
381
                return false;
382
            }
383
        }
384
        return true;
385
    }
325 386

  
326 387
    public void performUpdateTable(Connection conn,
327 388
            String database,
......
329 390
            String table,
330 391
            FeatureType original,
331 392
            FeatureType target) throws DataException {
332

  
333 393
        SQLBuilderBase sqlbuilder = buildUpdateTableSQL(null, null, table, original, target);
334 394
        Statement st = null;
335 395
        try {

Also available in: Unified diff