Revision 47211 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
99 99
        private int scale;
100 100
        private boolean isPk;
101 101
        private boolean _allowNulls;
102
        private boolean _allowIndexDuplicateds;
102 103
        private boolean _isAutomatic;
103 104
        private Object defaultValue;
104 105
        private int geom_type;
......
123 124
            this.geom_srsdbcode = null;
124 125
            this.tablebbox = null;
125 126
            this._isIndexed = false;
127
            this._allowIndexDuplicateds = true;
126 128
        }
127 129

  
128 130
        public ColumnDescriptorBase(String name, int type, int size, int precision, int scale, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) {
131
            this(name, type, size, precision, scale, isPk, isIndexed, allowNulls, isAutomatic, defaultValue, true);
132
        }
133
        
134
        public ColumnDescriptorBase(String name, int type, int size, int precision, int scale, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue, boolean allowIndexDuplicateds) {
129 135
            this.name = name;
130 136
            this.type = type;
131 137
            this.size = size;
......
140 146
            this.geom_srsdbcode = null;
141 147
            this.tablebbox = null;
142 148
            this._isIndexed = isIndexed;
149
            this._allowIndexDuplicateds = allowIndexDuplicateds;
143 150
        }
144 151

  
145 152
        public ColumnDescriptorBase(String name, int geom_type, int geom_subtype, IProjection proj, boolean isIndexed, boolean allowNulls) {
......
157 164
            this.geom_srsdbcode = srs_id(proj);
158 165
            this.tablebbox = null;
159 166
            this._isIndexed = isIndexed;
167
            this._allowIndexDuplicateds = true;
160 168
        }
161 169

  
162 170
        public ColumnDescriptorBase(String name, int geom_type, int geom_subtype, Object srsdbcode, boolean isIndexed, boolean allowNulls) {
......
174 182
            this.geom_srsdbcode = srsdbcode;
175 183
            this.tablebbox = null;
176 184
            this._isIndexed = isIndexed;
185
            this._allowIndexDuplicateds = true;
177 186
        }
178 187

  
179 188
        private ColumnDescriptorBase(FeatureAttributeDescriptor fad) {
......
185 194
            this._allowNulls = fad.allowNull();
186 195
            this._isAutomatic = fad.isAutomatic();
187 196
            this._isIndexed = fad.isIndexed();
197
            this._allowIndexDuplicateds = fad.allowIndexDuplicateds();
188 198

  
189 199
            if (fad.getType() == org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
190 200
                this.geom_type = fad.getGeomType().getType();
......
353 363
        public void setTableBBox(Envelope bbox) {
354 364
            this.tablebbox = bbox;
355 365
        }
366

  
367
        @Override
368
        public boolean allowIndexDuplicateds() {
369
            return this._allowIndexDuplicateds;
370
        }
371

  
372
        @Override
373
        public void setAllowIndexDuplicateds(boolean allowIndexDuplicateds) {
374
            this._allowIndexDuplicateds = allowIndexDuplicateds;
375
        }
376
        
356 377
    }
357 378

  
358 379
    public class ColumnBase extends AbstractValue implements Column {
......
2237 2258
            extends AbstractStatement
2238 2259
            implements DropIndexBuilder {
2239 2260

  
2240
        protected boolean ifNotExist = false;
2261
        protected boolean ifExist = false;
2241 2262
        protected String indexName;
2242 2263

  
2243 2264
        public DropIndexBuilderBase() {
2244 2265
        }
2245 2266

  
2246 2267
        @Override
2247
        public DropIndexBuilder if_not_exist() {
2248
            this.ifNotExist = true;
2268
        public DropIndexBuilder if_exist() {
2269
            this.ifExist = true;
2249 2270
            return this;
2250 2271
        }
2251 2272

  
......
2296 2317
        public List<String> toStrings(Formatter formatter) {
2297 2318
            StringBuilder builder = new StringBuilder();
2298 2319
            builder.append("DROP INDEX ");
2299
            if (this.ifNotExist) {
2300
                builder.append("IF NOT EXISTS ");
2320
            if (this.ifExist) {
2321
                builder.append("IF EXISTS ");
2301 2322
            }
2302 2323
            builder.append(as_identifier(this.indexName));
2303 2324
            List<String> sqls = new ArrayList<>();
......
2322 2343
        
2323 2344
        protected List<Pair<String, String>> renames;
2324 2345
        protected String drop_primary_key_column;
2346
        private final SQLBuilderBase sqlbuilder;
2325 2347

  
2326
        public AlterTableBuilderBase() {
2348
        public AlterTableBuilderBase(SQLBuilderBase sqlbuilder) {
2349
            this.sqlbuilder = sqlbuilder;
2327 2350
            this.drops = new ArrayList<>();
2328 2351
            this.adds = new ArrayList<>();
2329 2352
            this.alters = new ArrayList<>();
......
2386 2409
        }
2387 2410

  
2388 2411
        @Override
2389
        public AlterTableBuilder add_column(String columnName, int type, int size, int precision, int scale, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) {
2412
        public AlterTableBuilder add_column(String columnName, int type, int size, int precision, int scale, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue, boolean allowIndexDuplicateds) {
2390 2413
            if (isPk || isAutomatic) {
2391 2414
                allowNulls = false;
2392 2415
            }
2393
            this.adds.add(new ColumnDescriptorBase(columnName, type, size, precision, scale, isPk, isIndexed, allowNulls, isAutomatic, defaultValue));
2416
            this.adds.add(new ColumnDescriptorBase(columnName, type, size, precision, scale, isPk, isIndexed, allowNulls, isAutomatic, defaultValue, allowIndexDuplicateds));
2394 2417
            return this;
2395 2418
        }
2396 2419

  
......
2436 2459
            if( operation==null ) {
2437 2460
                operation = Bitmask.createBitmask(0);
2438 2461
            }
2439
            operation.setBit(ALTER_COLUMN_ALL);
2462
            if( operation.isEmpty() ) {
2463
                operation.setBit(ALTER_COLUMN_ALL);
2464
            }
2440 2465
            this.operations.add(new ImmutablePair<>(operation,column));
2441 2466
            return this;
2442 2467
        }
2443 2468

  
2444 2469
        @Override
2445
        public AlterTableBuilder alter_column(Bitmask operation, String columnName, int type, int size, int precision, int scale, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) {
2470
        public AlterTableBuilder alter_column(Bitmask operation, String columnName, int type, int size, int precision, int scale, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue, boolean allowIndexDuplicateds) {
2446 2471
            if ( (isPk || isAutomatic) && allowNulls) {
2447 2472
                allowNulls = false;
2448 2473
                operation.setBit(ALTER_COLUMN_SET_NULL);
2449 2474
            }
2450
            ColumnDescriptorBase column = new ColumnDescriptorBase(columnName, type, size, precision, scale, isPk, isIndexed, allowNulls, isAutomatic, defaultValue);
2475
            ColumnDescriptorBase column = new ColumnDescriptorBase(columnName, type, size, precision, scale, isPk, isIndexed, allowNulls, isAutomatic, defaultValue, allowIndexDuplicateds);
2451 2476
            update_or_add_alters(column);
2452 2477
            if( operation == null ) {
2453 2478
                operation = Bitmask.createBitmask(0);
2454 2479
            }
2455
            operation.setBit(ALTER_COLUMN_ALL);
2480
            if( operation.isEmpty() ) {
2481
                operation.setBit(ALTER_COLUMN_ALL);
2482
            }
2456 2483
            this.operations.add(new ImmutablePair<>(operation,column));
2457 2484
            return this;
2458 2485
        }
......
2479 2506
            if( operation == null ) {
2480 2507
                operation = Bitmask.createBitmask(0);
2481 2508
            }
2509
            if( operation.isEmpty() ) {
2510
                operation.setBit(ALTER_COLUMN_ALL);
2511
            }
2482 2512
            operation.setBit(ALTER_COLUMN_GEOMETRY);
2483 2513
            this.operations.add(new ImmutablePair<>(operation,column));
2484 2514
            return this;
......
2539 2569
        }
2540 2570
        
2541 2571
        protected List<String> create_index_sqls(Formatter<Value> formatter, ColumnDescriptor column) {
2542
            // CREATE INDEX IF NOT EXISTS idx_name ON table_name ( column_name )",
2543
            StringBuilder builder = new StringBuilder();
2544
            builder.append("CREATE INDEX ");
2545
            builder.append("IF NOT EXISTS ");
2546
            builder.append(as_identifier(getConstrainName("IDX",column.getName())));
2547
            builder.append(" ON ");
2548
            builder.append(this.table.toString(formatter));
2549
            builder.append(" ( ");
2550
            builder.append(as_identifier(column.getName()));
2551
            builder.append(" )");
2552
            return Collections.singletonList(builder.toString());
2572
            CreateIndexBuilder createIndex = this.sqlbuilder.createCreateIndexBuilder();
2573
            if( column.isGeometry() ) {
2574
                createIndex.spatial();
2575
            }
2576
            createIndex.if_not_exist();
2577
            createIndex.name(as_identifier(getConstrainName("IDX",column.getName())));
2578
            createIndex.column(column.getName());
2579
            createIndex.table()
2580
                    .database(this.table.getDatabase())
2581
                    .schema(this.table.getSchema())
2582
                    .name(this.table.getName()
2583
            );
2584
            if(!column.allowIndexDuplicateds()){
2585
                createIndex.unique();
2586
            }
2587
            return createIndex.toStrings();
2553 2588
        }
2554 2589

  
2555 2590
        protected List<String> drop_index_sqls(Formatter<Value> formatter, ColumnDescriptor column) {
2556
            // DROP INDEX IF EXISTS idx_name",
2557
            StringBuilder builder = new StringBuilder();
2558
            builder.append("DROP INDEX ");
2559
            builder.append("IF EXISTS ");
2560
            builder.append(as_identifier(getConstrainName("IDX",column.getName())));
2561
            return Collections.singletonList(builder.toString());
2591
            DropIndexBuilder dropIndex = this.sqlbuilder.createDropIndexBuilder();
2592
            dropIndex.if_exist();
2593
            dropIndex.name(as_identifier(getConstrainName("IDX",column.getName())));
2594
            return dropIndex.toStrings();
2562 2595
        }
2563 2596

  
2564 2597
        protected List<String> alter_table_alter_column_set_data_type_sqls(Formatter<Value> formatter, ColumnDescriptor column) {
......
3638 3671
    }
3639 3672

  
3640 3673
    protected AlterTableBuilder createAlterTableBuilder() {
3641
        return new AlterTableBuilderBase();
3674
        return new AlterTableBuilderBase(this);
3642 3675
    }
3643 3676

  
3644 3677
    protected InsertBuilder createInsertBuilder() {

Also available in: Unified diff