Revision 46102 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.h2spatial/org.gvsig.h2spatial.h2gis132/org.gvsig.h2spatial.h2gis132.provider/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialSQLBuilder.java

View differences:

H2SpatialSQLBuilder.java
7 7
import java.text.MessageFormat;
8 8
import java.util.ArrayList;
9 9
import java.util.Date;
10
import java.util.HashSet;
10 11
import java.util.List;
11 12
import java.util.Objects;
13
import java.util.Set;
14
import org.apache.commons.lang3.StringUtils;
12 15
import org.apache.commons.lang3.tuple.Pair;
13 16
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter;
14 17
import org.gvsig.expressionevaluator.ExpressionUtils;
......
132 135
        }
133 136

  
134 137
    }
135
        
138
    
136 139
    protected class H2SpatialAlterTableBuilderBase extends AlterTableBuilderBase {
140

  
137 141
        @Override
138 142
        public List<String> toStrings(Formatter formatter) {
139 143
            List<String> sqls = new ArrayList<>();
140 144
            if( this.isEmpty() ) {
141 145
                return sqls;
142 146
            }
147
            
148
            String local_drop_primary_key_column = this.drop_primary_key_column;
143 149
            for (String column : drops) {
144 150
                StringBuilder builder = new StringBuilder();
145 151
                builder.append("ALTER TABLE ");
......
147 153
                builder.append(" DROP COLUMN IF EXISTS ");
148 154
                builder.append(as_identifier(column)); 
149 155
                sqls.add(builder.toString());
156
                if( StringUtils.equals(local_drop_primary_key_column, column) ) {
157
                    // Si hemos eliminado la columna sobre la que estaba la pk, ya no hay que
158
                    // eliminar la pk
159
                    local_drop_primary_key_column = null; 
160
                }
150 161
            }
151 162
            for (ColumnDescriptor column : adds) {
152 163
                StringBuilder builder = new StringBuilder();
......
183 194
                } else {
184 195
                    builder.append(" NOT NULL");
185 196
                }
197
                sqls.add(builder.toString());
198
                
186 199
                if (column.isPrimaryKey()) {
187
                    builder.append(" PRIMARY KEY");
200
                    String sql;
201
                    sql = MessageFormat.format(
202
                        "ALTER TABLE \"{0}\".\"{1}\" ADD CONSTRAINT \"{2}\" PRIMARY KEY ( \"{3}\" ) INDEX \"{4}\"",
203
                        this.table().getSchema(),
204
                        this.table().getName(),
205
                        this.getConstrainName("PK", column.getName()),
206
                        column.getName(),
207
                        this.getConstrainName("IDX", column.getName())
208
                    );
209
                    sqls.add(sql);
210
                } else if( column.isIndexed() ) {
211
                    String sql;
212
                    sql = MessageFormat.format(
213
                        "CREATE INDEX IF NOT EXISTS \"{0}\" ON \"{1}\".\"{2}\" ( \"{3}\" )",
214
                        this.getConstrainName("IDX", column.getName()),
215
                        this.table().getSchema(),
216
                        this.table().getName(),
217
                        column.getName()
218
                    );
219
                    sqls.add(sql);
188 220
                }
189
                sqls.add(builder.toString());
190 221
                
191 222
                if( column.isGeometry() ) {
192
                    String constraint_name = "constraint_" + this.table().getName() + "_" + column.getName() + "_geom";
223
                    String constraint_name = this.getConstrainName("GEOM", column.getName());
193 224
                    String sql;
194 225
                    if (column.getGeometrySRSId() == null) {
195 226
                        if ((int) sqlgeometrytype(column.getGeometryType(), column.getGeometrySubtype()) == 0) {
......
238 269
                    }
239 270
                    sqls.add(sql);
240 271
                }
272
                if( StringUtils.equals(local_drop_primary_key_column, column.getName()) ) {
273
                    // Si la columna de la que tenemos que quitar la pk la acabamos de a?adir
274
                    // ya se habra a?adido como toca, y no tendremos que quitar luego la pk.
275
                    local_drop_primary_key_column = null; 
276
                }
241 277
            }
278
            
279
            if( StringUtils.isNotBlank(local_drop_primary_key_column) ) {
280
                String sql;
281
                sql = MessageFormat.format(
282
                    "ALTER TABLE \"{0}\".\"{1}\" DROP CONSTRAINT IF EXISTS \"{2}\"",
283
                    this.table().getSchema(),
284
                    this.table().getName(),
285
                    this.getConstrainName("PK", local_drop_primary_key_column)
286
                );
287
                sqls.add(sql);
288
                sql = MessageFormat.format(
289
                    "DROP INDEX IF EXISTS \"{0}\"",
290
                    this.getConstrainName("IDX", local_drop_primary_key_column)
291
                );
292
                sqls.add(sql);
293
            }
294
            
242 295
            for (ColumnDescriptor column : alters) {
243 296
                StringBuilder builder = new StringBuilder();
244 297
                builder.append("ALTER TABLE ");
......
268 321
                if( column.isAutomatic() ) {
269 322
                    builder.append(" AUTO_INCREMENT");
270 323
                }
324
                if (column.allowNulls()) {
325
                    builder.append(" NULL");
326
                } else {
327
                    builder.append(" NOT NULL");
328
                }
271 329
                sqls.add(builder.toString());
330
                if (column.isPrimaryKey()) {
331
                    String sql;
332
                    sql = MessageFormat.format(
333
                        "ALTER TABLE \"{0}\".\"{1}\" ADD CONSTRAINT \"{2}\" PRIMARY KEY ( \"{3}\" ) INDEX \"{4}\"",
334
                        this.table().getSchema(),
335
                        this.table().getName(),
336
                        this.getConstrainName("PK", column.getName()),
337
                        column.getName(),
338
                        this.getConstrainName("IDX", column.getName())
339
                    );
340
                    sqls.add(sql);
341
                }
272 342
                if( column.isGeometry() ) {
273 343
                    String sql;
274
                    String constraint_name = "constraint_" + this.table().getName() + "_" + column.getName()+"_dim";
275 344
                    sql = MessageFormat.format(
276 345
                        "ALTER TABLE \"{0}\".\"{1}\" ADD CONSTRAINT IF NOT EXISTS \"{2}\" CHECK ST_CoordDim(\"{3}\") = {4}",
277 346
                        this.table().getSchema(),
278 347
                        this.table().getName(),
279
                        constraint_name,
348
                        this.getConstrainName("DIM", column.getName()),
280 349
                        column.getName(),
281 350
                        sqlgeometrynumdimension(column.getGeometryType(),column.getGeometrySubtype())
282 351
                    );

Also available in: Unified diff