Revision 43650 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.spi/src/main/java/org/gvsig/fmap/dal/feature/spi/SQLBuilderBase.java

View differences:

SQLBuilderBase.java
1198 1198
        protected List<String> drops;
1199 1199
        protected List<ColumnDescriptorBuilderBase> adds;
1200 1200
        protected List<ColumnDescriptorBuilderBase> alters;
1201
        protected List<Pair> renames;
1201
        protected List<Pair<String,String>> renames;
1202 1202

  
1203 1203
        public AlterTableBuilderBase() {
1204 1204
            this.drops = new ArrayList<>();
......
1241 1241
        }
1242 1242

  
1243 1243
        @Override
1244
        public AlterTableBuilder add_geometry_column(String columnName, int type, int subtype, IProjection proj, boolean isIndexed, boolean allowNulls) {
1245
            if( StringUtils.isEmpty(columnName) ) {
1246
                throw new IllegalArgumentException("Argument 'columnName' can't be empty.");
1247
            }
1248
            this.adds.add(new ColumnDescriptorBuilderBase(columnName, type, subtype, proj, isIndexed, allowNulls));
1249
            return this;
1250
        }
1251

  
1252
        @Override
1244 1253
        public AlterTableBuilder alter_column(String columnName, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) {
1245 1254
            if (isPk || isAutomatic) {
1246 1255
                allowNulls = false;
......
1250 1259
        }
1251 1260

  
1252 1261
        @Override
1262
        public AlterTableBuilder alter_geometry_column(String columnName, int type, int subtype, IProjection proj, boolean isIndexed, boolean allowNulls) {
1263
            if( StringUtils.isEmpty(columnName) ) {
1264
                throw new IllegalArgumentException("Argument 'columnName' can't be empty.");
1265
            }
1266
            this.alters.add(new ColumnDescriptorBuilderBase(columnName, type, subtype, proj, isIndexed, allowNulls));
1267
            return this;
1268
        }
1269

  
1270
        @Override
1253 1271
        public AlterTableBuilder rename_column(String source, String target) {
1254 1272
            this.renames.add(new ImmutablePair(source, target));
1255 1273
            return this;
......
1330 1348
             [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
1331 1349

  
1332 1350
             */
1333
            StringBuilder builder = new StringBuilder();
1334 1351

  
1335
            builder.append("ALTER TABLE");
1336
            builder.append(this.table.toString());
1337
            builder.append(" ");
1338
            boolean first = true;
1339 1352
            for (String column : drops) {
1340
                if (first) {
1341
                    first = false;
1342
                } else {
1343
                    builder.append(", ");
1344
                }
1345
                builder.append("DROP COLUMN IF EXISTS ");
1346
                builder.append(column);
1353
                StringBuilder builder = new StringBuilder();
1354
                builder.append("ALTER TABLE ");
1355
                builder.append(this.table.toString());
1356
                builder.append(" DROP COLUMN IF EXISTS ");
1357
                builder.append(identifier(column)); 
1358
                sqls.add(builder.toString());
1347 1359
            }
1348
            first = drops.isEmpty();
1349 1360
            for (ColumnDescriptorBuilderBase column : adds) {
1350
                if (first) {
1351
                    first = false;
1352
                } else {
1353
                    builder.append(", ");
1354
                }
1355
                builder.append("ADD COLUMN ");
1356
                builder.append(column.getName());
1361
                StringBuilder builder = new StringBuilder();
1362
                builder.append("ALTER TABLE ");
1363
                builder.append(this.table.toString());
1364
                builder.append(" ADD COLUMN ");
1365
                builder.append(identifier(column.getName())); 
1357 1366
                builder.append(" ");
1358 1367
                if( column.getType() == DataTypes.INT && column.isAutomatic() ) {
1359 1368
                    builder.append(" SERIAL");
......
1377 1386
                if (column.isPrimaryKey()) {
1378 1387
                    builder.append(" PRIMARY KEY");
1379 1388
                }
1389
                sqls.add(builder.toString());
1390
                if( column.isIndexed() ) {
1391
                    String sql;
1392
                    String name = "idx_" + this.table().getName() + "_" + column.getName();
1393
                    if( column.isGeometry() ) {
1394
                        sql = MessageFormat.format(
1395
                            config.getString(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column),
1396
                            name,
1397
                            this.table().toString(),
1398
                            column.getName()
1399
                        );
1400
                    } else {
1401
                        sql = MessageFormat.format(
1402
                            config.getString(SQLConfig.CREATE_INDEX_name_ON_table_column),
1403
                            name,
1404
                            this.table().toString(),
1405
                            column.getName()
1406
                        );
1407
                    }
1408
                    sqls.add(sql);
1409
                }
1380 1410
            }
1381
            first = drops.isEmpty() && adds.isEmpty();
1382 1411
            for (ColumnDescriptorBuilderBase column : alters) {
1383
                if (first) {
1384
                    first = false;
1385
                } else {
1386
                    builder.append(", ");
1387
                }
1388
                builder.append("ALTER COLUMN ");
1389
                builder.append(column.getName());
1390
                builder.append("SET DATA TYPE ");
1412
                StringBuilder builder = new StringBuilder();
1413
                builder.append("ALTER TABLE ");
1414
                builder.append(this.table.toString());
1415
                builder.append(" ALTER COLUMN ");
1416
                builder.append(identifier(column.getName())); 
1417
                builder.append(" SET DATA TYPE ");
1391 1418
                if( column.getType() == DataTypes.INT && column.isAutomatic() ) {
1392 1419
                    builder.append(" SERIAL");
1393 1420
                } else {
1394 1421
                    builder.append(sqltype(column.getType(), column.getPrecision(), column.getSize()));
1395 1422
                }
1396
                builder.append(", ");
1397 1423
                if (column.getDefaultValue() == null) {
1398 1424
                    if (column.allowNulls()) {
1399
                        builder.append("ALTER COLUMN ");
1400
                        builder.append(column.getName());
1401
                        builder.append(" SET DEFAULT NULL");
1425
                        builder.append(" DEFAULT NULL");
1402 1426
                    } else {
1403
                        builder.append("ALTER COLUMN ");
1404
                        builder.append(column.getName());
1405 1427
                        builder.append(" DROP DEFAULT");
1406 1428
                    }
1407 1429
                } else {
1408
                    builder.append("ALTER COLUMN ");
1409
                    builder.append(column.getName());
1410
                    builder.append(" SET DEFAULT '");
1430
                    builder.append(" DEFAULT '");
1411 1431
                    builder.append(column.getDefaultValue().toString());
1412 1432
                    builder.append("'");
1413 1433
                }
1434
                sqls.add(builder.toString());
1435
                if( column.isIndexed() ) {
1436
                    String sql;
1437
                    String name = "idx_" + this.table().getName() + "_" + column.getName();
1438
                    if( column.isGeometry() ) {
1439
                        sql = MessageFormat.format(
1440
                            config.getString(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column),
1441
                            name,
1442
                            this.table().toString(),
1443
                            column.getName()
1444
                        );
1445
                    } else {
1446
                        sql = MessageFormat.format(
1447
                            config.getString(SQLConfig.CREATE_INDEX_name_ON_table_column),
1448
                            name,
1449
                            this.table().toString(),
1450
                            column.getName()
1451
                        );
1452
                    }
1453
                    sqls.add(sql);
1454
                }
1414 1455
            }
1415
            first = drops.isEmpty() && adds.isEmpty() && alters.isEmpty();
1416
            for (Pair pair : renames) {
1417
                if (first) {
1418
                    first = false;
1419
                } else {
1420
                    builder.append(", ");
1421
                }
1422
                builder.append("RENAME COLUMN ");
1423
                builder.append(pair.getLeft());
1456
            for (Pair<String,String> pair : renames) {
1457
                StringBuilder builder = new StringBuilder();
1458
                builder.append("ALTER TABLE ");
1459
                builder.append(this.table.toString());
1460
                builder.append(" RENAME COLUMN ");
1461
                builder.append(identifier(pair.getLeft())); 
1424 1462
                builder.append(" TO ");
1425
                builder.append(pair.getRight());
1463
                builder.append(identifier(pair.getRight())); 
1464
                sqls.add(builder.toString());
1426 1465
            }
1427
            sqls.add(builder.toString());
1428

  
1429 1466
            return sqls;
1430 1467
        }
1431 1468

  
......
1845 1882
    }
1846 1883
    
1847 1884
    @Override
1885
    @Deprecated
1848 1886
    public String sqltype(int type, int p, int s) {
1887
        return this.sqltype(type, p, s, Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.UNKNOWN);
1888
    }
1889

  
1890
    @Override
1891
    public String sqltype(int type, int p, int s, int geomType, int geomSubtype) {
1849 1892
        switch (type) {
1850 1893
            case DataTypes.BOOLEAN:
1851 1894
                return config.getString(SQLConfig.type_boolean);

Also available in: Unified diff