Statistics
| Revision:

svn-gvsig-desktop / 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 @ 44871

History | View | Annotate | Download (18.8 KB)

1 43020 jjdelcerro
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
2
3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.SQLException;
6
import java.sql.Statement;
7 43687 jjdelcerro
import java.util.ArrayList;
8 43020 jjdelcerro
import java.util.Iterator;
9 43687 jjdelcerro
import java.util.List;
10
import org.apache.commons.collections.CollectionUtils;
11 43650 jjdelcerro
import org.apache.commons.lang3.StringUtils;
12 44198 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
13
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable;
14 44644 jjdelcerro
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
15 43020 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
16
import org.gvsig.fmap.dal.exception.DataException;
17
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
18 43479 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureReference;
19 43020 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
20 44871 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType.FeatureTypeChanged;
21 43020 jjdelcerro
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
22
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
23
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
24
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
25
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
26
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
27
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException;
28
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
29
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
30 44058 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
31 44198 jjdelcerro
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
32
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
33 43629 jjdelcerro
import org.gvsig.tools.dispose.Disposable;
34
import org.gvsig.tools.dispose.DisposeUtils;
35 43020 jjdelcerro
36 44678 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
37 43377 jjdelcerro
public class PerformChangesOperation extends AbstractConnectionWritableOperation {
38 43020 jjdelcerro
39 44058 jjdelcerro
    protected TableReference table;
40 43355 jjdelcerro
    protected FeatureType featureType;
41 44678 jjdelcerro
    protected FeatureType featureTypeSource;
42
    protected FeatureType featureTypeTarget;
43 43355 jjdelcerro
    protected Iterator<FeatureReferenceProviderServices> deleteds;
44
    protected Iterator<FeatureProvider> updateds;
45
    protected Iterator<FeatureProvider> inserteds;
46 43020 jjdelcerro
47 43355 jjdelcerro
    protected boolean typeChanged = false;
48 43020 jjdelcerro
49
    public PerformChangesOperation(JDBCHelper helper) {
50 44058 jjdelcerro
        this(helper, null, null, null, null, null, null);
51 43020 jjdelcerro
    }
52
53
    public PerformChangesOperation(JDBCHelper helper,
54 44058 jjdelcerro
            TableReference table,
55 43020 jjdelcerro
            FeatureType featureType,
56
            Iterator<FeatureReferenceProviderServices> deleteds,
57
            Iterator<FeatureProvider> inserteds,
58
            Iterator<FeatureProvider> updateds,
59 44871 jjdelcerro
            Iterator<FeatureTypeChanged> featureTypesChanged) {
60 43020 jjdelcerro
        super(helper);
61
        this.deleteds = deleteds;
62
        this.inserteds = inserteds;
63
        this.updateds = updateds;
64 44058 jjdelcerro
        this.table = table;
65 43020 jjdelcerro
        this.featureType = featureType;
66 44678 jjdelcerro
        if (featureTypesChanged.hasNext()) {
67 44871 jjdelcerro
            FeatureTypeChanged item = featureTypesChanged.next();
68 44678 jjdelcerro
            this.featureTypeSource = item.getSource();
69
            this.featureTypeTarget = item.getTarget();
70
            typeChanged = true;
71
        } else {
72
            this.featureTypeSource = null;
73
            this.featureTypeTarget = null;
74
            typeChanged = false;
75
        }
76 43020 jjdelcerro
    }
77
78
    public boolean isTypeChanged() {
79
        return typeChanged;
80
    }
81 43377 jjdelcerro
82 43020 jjdelcerro
    @Override
83 43629 jjdelcerro
    public Object perform(Connection conn) throws DataException {
84 44678 jjdelcerro
        if (typeChanged) {
85
            this.performUpdateTable(conn);
86 43020 jjdelcerro
        }
87
        if (deleteds.hasNext()) {
88 44678 jjdelcerro
            performDeletes(conn);
89 43020 jjdelcerro
        }
90
        if (updateds.hasNext()) {
91 44678 jjdelcerro
            performUpdates(conn);
92 43020 jjdelcerro
        }
93
        if (inserteds.hasNext()) {
94 44678 jjdelcerro
            performInserts(conn);
95 43020 jjdelcerro
        }
96
        return true;
97
    }
98
99 44678 jjdelcerro
    public String getDeleteSQL() {
100 43362 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
101 44678 jjdelcerro
        return this.getDeleteSQL(sqlbuilder);
102
    }
103
104
    public String getDeleteSQL(JDBCSQLBuilderBase sqlbuilder) {
105 44198 jjdelcerro
        ExpressionBuilder expbuilder = sqlbuilder.expression();
106
107 44058 jjdelcerro
        sqlbuilder.delete().table()
108
                .database(this.table.getDatabase())
109
                .schema(this.table.getSchema())
110
                .name(this.table.getTable());
111 44678 jjdelcerro
        for (FeatureAttributeDescriptor attr : this.featureType) {
112 43020 jjdelcerro
            if (attr.isPrimaryKey()) {
113
                sqlbuilder.delete().where().and(
114 44198 jjdelcerro
                        expbuilder.eq(
115
                                expbuilder.column(attr.getName()),
116
                                expbuilder.parameter(attr.getName()).as_variable()
117 43020 jjdelcerro
                        )
118
                );
119
            }
120
        }
121
        if (!sqlbuilder.delete().has_where() ) {
122
            throw new RuntimeException("Operation requires missing pk");
123
        }
124 44678 jjdelcerro
        sqlbuilder.setProperties(
125
                Variable.class,
126
                PROP_TABLE, table
127
        );
128
        String sql = sqlbuilder.delete().toString();
129
        return sql;
130 43362 jjdelcerro
    }
131 43020 jjdelcerro
132 44678 jjdelcerro
    public void performDeletes(Connection conn) throws DataException {
133 43362 jjdelcerro
134 44678 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
135
        String sql = getDeleteSQL(sqlbuilder);
136 43362 jjdelcerro
137 43020 jjdelcerro
        PreparedStatement st = null;
138 43629 jjdelcerro
        Disposable paramsDisposer = null;
139 43020 jjdelcerro
        try {
140
            st = conn.prepareStatement(sql);
141
            while (deleteds.hasNext()) {
142 43479 jjdelcerro
                FeatureReference reference = (FeatureReference) deleteds.next();
143 43629 jjdelcerro
                paramsDisposer = sqlbuilder.setParameters(st, reference);
144 43020 jjdelcerro
                int nAffected = JDBCUtils.executeUpdate(st,sql);
145
                if (nAffected == 0) {
146
                    throw new JDBCUpdateWithoutChangesException(
147
                            sqlbuilder.delete().toString(),
148
                            null
149
                    );
150
                }
151
                if (nAffected > 1) {
152 44058 jjdelcerro
                    LOGGER.warn("Remove statement affectst to {} rows ( {} )",
153 43020 jjdelcerro
                            nAffected, sql
154
                    );
155
                }
156
            }
157
        } catch (SQLException e) {
158
            throw new JDBCSQLException(e);
159
        } finally {
160
            JDBCUtils.closeQuietly(st);
161 43629 jjdelcerro
            DisposeUtils.disposeQuietly(paramsDisposer);
162 43020 jjdelcerro
        }
163
    }
164 43362 jjdelcerro
165 44678 jjdelcerro
    public String getInsertSQL() {
166 43020 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
167 44678 jjdelcerro
        return this.getInsertSQL(sqlbuilder);
168
    }
169
170
    public String getInsertSQL(JDBCSQLBuilderBase sqlbuilder) {
171 44644 jjdelcerro
        GeometryExpressionBuilder expbuilder = sqlbuilder.expression();
172 43020 jjdelcerro
173 44058 jjdelcerro
        sqlbuilder.insert().table()
174
                .database(this.table.getDatabase())
175
                .schema(this.table.getSchema())
176
                .name(this.table.getTable());
177 44678 jjdelcerro
        for (FeatureAttributeDescriptor attr : this.featureType) {
178 44348 jjdelcerro
            if( attr.isAutomatic() || attr.isComputed() ) {
179 43362 jjdelcerro
                continue;
180
            }
181 43020 jjdelcerro
            if (attr.getType() == DataTypes.GEOMETRY) {
182
                sqlbuilder.insert().column().name(attr.getName()).with_value(
183 44644 jjdelcerro
                    expbuilder.parameter(attr.getName()).as_variable()
184 44198 jjdelcerro
                        .srs(
185
                            expbuilder.parameter().value(
186
                                attr.getSRS()).as_constant()
187
                        )
188 43020 jjdelcerro
                );
189
            } else {
190
                sqlbuilder.insert().column().name(attr.getName()).with_value(
191 44198 jjdelcerro
                        expbuilder.parameter(attr.getName())
192 43020 jjdelcerro
                );
193
            }
194
        }
195
196 44198 jjdelcerro
        sqlbuilder.setProperties(
197
                Variable.class,
198 44678 jjdelcerro
                PROP_FEATURE_TYPE, featureType,
199 44198 jjdelcerro
                PROP_TABLE, table
200
        );
201 43020 jjdelcerro
        String sql = sqlbuilder.insert().toString();
202 44678 jjdelcerro
        return sql;
203
    }
204
205
    public void performInserts(Connection conn) throws DataException {
206
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
207
        String sql = getInsertSQL(sqlbuilder);
208
209
        PreparedStatement st;
210
        Disposable paramsDisposer;
211 43020 jjdelcerro
        try {
212
            st = conn.prepareStatement(sql);
213
            while (inserteds.hasNext()) {
214
                FeatureProvider feature = inserteds.next();
215 43629 jjdelcerro
                paramsDisposer = sqlbuilder.setParameters(st, feature);
216
                try {
217
                    if (JDBCUtils.executeUpdate(st,sql) == 0) {
218
                        throw new JDBCExecuteSQLException(
219
                                sqlbuilder.insert().toString(),
220
                                null
221
                        );
222
                    }
223
                } finally {
224
                    DisposeUtils.disposeQuietly(paramsDisposer);
225 43020 jjdelcerro
                }
226
            }
227
        } catch (JDBCExecuteSQLException ex) {
228
            throw ex;
229
        } catch (Exception ex) {
230
            throw new JDBCExecuteSQLException(sql,ex);
231
        }
232
    }
233
234 44678 jjdelcerro
    public String getUpdateSQL() {
235 43362 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
236 44678 jjdelcerro
        return this.getUpdateSQL(sqlbuilder);
237
    }
238
239
    public String getUpdateSQL(JDBCSQLBuilderBase sqlbuilder) {
240 44644 jjdelcerro
        GeometryExpressionBuilder expbuilder = sqlbuilder.expression();
241 43020 jjdelcerro
242 44058 jjdelcerro
        sqlbuilder.update().table()
243
                .database(this.table.getDatabase())
244
                .schema(this.table.getSchema())
245
                .name(this.table.getTable());
246 44678 jjdelcerro
        for (FeatureAttributeDescriptor attr : this.featureType) {
247 43020 jjdelcerro
            if (attr.isPrimaryKey()) {
248
                sqlbuilder.update().where().and(
249 44198 jjdelcerro
                        expbuilder.eq(
250
                                expbuilder.column(attr.getName()),
251
                                expbuilder.parameter(attr.getName()).as_variable()
252 43020 jjdelcerro
                        )
253
                );
254 44678 jjdelcerro
                continue;
255 44348 jjdelcerro
            }
256
            if ( attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
257
                continue;
258 43020 jjdelcerro
            }
259 44348 jjdelcerro
            if (attr.getType() == DataTypes.GEOMETRY) {
260
                sqlbuilder.update().column().name(attr.getName()).with_value(
261
                    expbuilder.parameter(attr.getName()).as_geometry_variable().srs(
262
                        expbuilder.parameter().value(attr.getSRS()).as_constant()
263
                    )
264
                );
265
            } else {
266
                sqlbuilder.update().column().name(attr.getName()).with_value(
267
                    expbuilder.parameter(attr.getName()).as_variable()
268
                );
269
            }
270 43020 jjdelcerro
        }
271
        if (!sqlbuilder.update().has_where() ) {
272
            throw new RuntimeException("Operation requires missing pk");
273
        }
274 44678 jjdelcerro
        sqlbuilder.setProperties(
275
                Variable.class,
276
                PROP_FEATURE_TYPE, this.featureType,
277
                PROP_TABLE, table
278
        );
279
        String sql = sqlbuilder.update().toString();
280
        return sql;
281 43362 jjdelcerro
    }
282
283 44678 jjdelcerro
    public void performUpdates(Connection conn) throws DataException {
284 43020 jjdelcerro
285 44678 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
286 44679 jjdelcerro
        String sql = getUpdateSQL(sqlbuilder);
287 43362 jjdelcerro
288 43020 jjdelcerro
        PreparedStatement st = null;
289 43629 jjdelcerro
        Disposable paramsDisposer = null;
290 43020 jjdelcerro
        try {
291
            st = conn.prepareStatement(sql);
292
            while (updateds.hasNext()) {
293
                FeatureProvider featureProvider = (FeatureProvider) updateds.next();
294 43629 jjdelcerro
                paramsDisposer = sqlbuilder.setParameters(st, featureProvider);
295 43020 jjdelcerro
                if (JDBCUtils.executeUpdate(st,sql) == 0) {
296
                    throw new JDBCUpdateWithoutChangesException(sql,null);
297
                }
298
            }
299
        } catch (SQLException e) {
300
            throw new JDBCSQLException(e);
301
        } finally {
302
            JDBCUtils.closeQuietly(st);
303 43629 jjdelcerro
            DisposeUtils.disposeQuietly(paramsDisposer);
304 43020 jjdelcerro
        }
305
    }
306
307 44678 jjdelcerro
    public List<String> getUpdateTableSQLs() {
308 43362 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
309 44058 jjdelcerro
        sqlbuilder.alter_table().table()
310
                .database(this.table.getDatabase())
311
                .schema(this.table.getSchema())
312
                .name(this.table.getTable());
313 43020 jjdelcerro
314 44678 jjdelcerro
        for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
315
            FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor(
316 43020 jjdelcerro
                    attrOrgiginal.getName()
317
            );
318
            if (attrTarget == null) {
319 44190 jjdelcerro
                if( attrOrgiginal.isComputed() ) {
320
                    continue;
321
                }
322 43020 jjdelcerro
                sqlbuilder.alter_table().drop_column(attrOrgiginal.getName());
323 43650 jjdelcerro
            } else if( !this.areEquals(attrOrgiginal, attrTarget) ) {
324 44190 jjdelcerro
                if( attrTarget.isComputed() ) {
325
                    continue;
326
                }
327 43650 jjdelcerro
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
328
                    sqlbuilder.alter_table().alter_geometry_column(
329
                            attrTarget.getName(),
330
                            attrTarget.getGeomType().getType(),
331
                            attrTarget.getGeomType().getSubType(),
332
                            attrTarget.getSRS(),
333
                            attrTarget.isIndexed(),
334
                            attrTarget.allowNull()
335
                    );
336
                } else {
337
                    sqlbuilder.alter_table().alter_column(
338
                            attrTarget.getName(),
339
                            attrTarget.getType(),
340 44669 jjdelcerro
                            attrTarget.getSize(),
341 43650 jjdelcerro
                            attrTarget.getPrecision(),
342 44669 jjdelcerro
                            attrTarget.getScale(),
343 43650 jjdelcerro
                            attrTarget.isPrimaryKey(),
344
                            attrTarget.isIndexed(),
345
                            attrTarget.allowNull(),
346
                            attrTarget.isAutomatic(),
347
                            attrTarget.getDefaultValue()
348
                    );
349
                }
350 43020 jjdelcerro
            }
351
        }
352 44678 jjdelcerro
        for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
353 44190 jjdelcerro
            if( attrTarget.isComputed() ) {
354
                continue;
355
            }
356 44678 jjdelcerro
            if (featureTypeSource.getAttributeDescriptor(attrTarget.getName()) == null) {
357 43650 jjdelcerro
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
358
                    sqlbuilder.alter_table().add_geometry_column(
359
                            attrTarget.getName(),
360
                            attrTarget.getGeomType().getType(),
361
                            attrTarget.getGeomType().getSubType(),
362
                            attrTarget.getSRS(),
363
                            attrTarget.isIndexed(),
364
                            attrTarget.allowNull()
365
                    );
366
                } else {
367
                    sqlbuilder.alter_table().add_column(
368
                            attrTarget.getName(),
369
                            attrTarget.getType(),
370 44669 jjdelcerro
                            attrTarget.getSize(),
371 43650 jjdelcerro
                            attrTarget.getPrecision(),
372 44669 jjdelcerro
                            attrTarget.getScale(),
373 43650 jjdelcerro
                            attrTarget.isPrimaryKey(),
374
                            attrTarget.isIndexed(),
375
                            attrTarget.allowNull(),
376
                            attrTarget.isAutomatic(),
377
                            attrTarget.getDefaultValue()
378
                    );
379
                }
380 43020 jjdelcerro
            }
381
        }
382 44198 jjdelcerro
        sqlbuilder.setProperties(
383
                Variable.class,
384
                PROP_TABLE, table
385
        );
386 44678 jjdelcerro
387
        List<String> sqls = sqlbuilder.alter_table().toStrings();
388
        sqls.addAll(buildCreateIndexSQL());
389
        return sqls;
390 43362 jjdelcerro
    }
391 43650 jjdelcerro
392
    protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) {
393
        // No interesa si son o no iguales en general, solo si son iguales en lo
394
        // que a los atributos usados para crear la columna de la tabla se refiere.
395
        if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
396
            return false;
397
        }
398
        if( attr1.getType() != attr2.getType() ) {
399
            return false;
400
        }
401
        if( attr1.getPrecision() != attr2.getPrecision() ) {
402
            return false;
403
        }
404 44669 jjdelcerro
        if( attr1.getScale() != attr2.getScale() ) {
405
            return false;
406
        }
407 43650 jjdelcerro
        if( attr1.getSize() != attr2.getSize() ) {
408
            return false;
409
        }
410
        if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
411
            return false;
412
        }
413 43687 jjdelcerro
//        if( attr1.isIndexed() != attr2.isIndexed() ) {
414
//            return false;
415
//        }
416 43650 jjdelcerro
        if( attr1.allowNull() != attr2.allowNull() ) {
417
            return false;
418
        }
419
        if( attr1.isAutomatic() != attr2.isAutomatic() ) {
420
            return false;
421
        }
422
        if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
423
            if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) {
424
                return false;
425
            }
426
            if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
427
                return false;
428
            }
429
        }
430
        return true;
431
    }
432 43362 jjdelcerro
433 44678 jjdelcerro
    protected List<String> buildCreateIndexSQL() {
434 43687 jjdelcerro
        ArrayList<String> sqls = new ArrayList<>();
435
436 44678 jjdelcerro
        for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
437 43687 jjdelcerro
            boolean createIndex = false;
438
            if( attrTarget.isIndexed() ) {
439 44678 jjdelcerro
                FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName());
440 43687 jjdelcerro
                if ( attrOriginal == null) {
441
                    createIndex = true;
442
                } else {
443
                    if( attrOriginal.isIndexed() ) {
444
                        createIndex = false;
445
                    } else {
446
                        createIndex = true;
447
                    }
448
                }
449
            }
450
            if( createIndex ) {
451
                JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
452
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
453
                    sqlbuilder.create_index().spatial();
454
                }
455
                sqlbuilder.create_index().if_not_exist();
456
                sqlbuilder.create_index().name("idx_" + table + "_" + attrTarget.getName());
457
                sqlbuilder.create_index().column(attrTarget.getName());
458 44058 jjdelcerro
                sqlbuilder.create_index().table()
459
                    .database(this.table.getDatabase())
460
                    .schema(this.table.getSchema())
461
                    .name(this.table.getTable());
462 44198 jjdelcerro
                sqlbuilder.setProperties(
463
                        Variable.class,
464
                        PROP_TABLE, table
465
                );
466 43687 jjdelcerro
                sqls.addAll(sqlbuilder.create_index().toStrings());
467
            }
468
        }
469
        return sqls;
470
    }
471
472 44678 jjdelcerro
    public void performUpdateTable(Connection conn) throws DataException {
473 43687 jjdelcerro
474 44678 jjdelcerro
        List<String> sqls = this.getUpdateTableSQLs();
475 43687 jjdelcerro
476
        if( !CollectionUtils.isEmpty(sqls) ) {
477
            Statement st = null;
478
            try {
479
                st = conn.createStatement();
480
                for (String sql : sqls) {
481 44190 jjdelcerro
                    if( !StringUtils.isBlank(sql) ) {
482
                        JDBCUtils.execute(st, sql);
483
                    }
484 43687 jjdelcerro
                }
485
            } catch (SQLException e) {
486
                throw new JDBCSQLException(e);
487
            } finally {
488
                JDBCUtils.closeQuietly(st);
489 43020 jjdelcerro
            }
490
        }
491
    }
492
}