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 @ 46315

History | View | Annotate | Download (23 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
25

    
26
import java.sql.PreparedStatement;
27
import java.sql.SQLException;
28
import java.sql.Statement;
29
import java.util.ArrayList;
30
import java.util.Iterator;
31
import java.util.List;
32
import org.apache.commons.collections.CollectionUtils;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.expressionevaluator.ExpressionBuilder;
35
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter;
36
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable;
37
import org.gvsig.expressionevaluator.ExpressionUtils;
38
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
39
import org.gvsig.fmap.dal.DataTypes;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.FeatureReference;
43
import org.gvsig.fmap.dal.feature.FeatureType;
44
import org.gvsig.fmap.dal.feature.FeatureType.FeatureTypeChanged;
45
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
46
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
47
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
48
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
49
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
50
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
51
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
52
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
53
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
54
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
55
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
56
import org.gvsig.tools.dispose.Disposable;
57
import org.gvsig.tools.dispose.DisposeUtils;
58
import org.gvsig.tools.util.Invocable;
59

    
60
@SuppressWarnings("UseSpecificCatch")
61
public class PerformChangesOperation extends AbstractConnectionWritableOperation {
62

    
63
    protected TableReference table;
64
    protected FeatureType featureType;
65
    protected FeatureType featureTypeSource;
66
    protected FeatureType featureTypeTarget;
67
    protected Iterator<FeatureReferenceProviderServices> deleteds;
68
    protected Iterator<FeatureProvider> updateds;
69
    protected Iterator<FeatureProvider> inserteds;
70

    
71
    protected boolean typeChanged = false;
72
    
73
    public PerformChangesOperation(JDBCHelper helper) {
74
        this(helper, null, null, null, null, null, null);
75
    }
76

    
77
    public PerformChangesOperation(JDBCHelper helper,
78
            TableReference table,
79
            FeatureType featureType,
80
            Iterator<FeatureReferenceProviderServices> deleteds,
81
            Iterator<FeatureProvider> inserteds,
82
            Iterator<FeatureProvider> updateds,
83
            Iterator<FeatureTypeChanged> featureTypesChanged) {
84
        super(helper);
85
        this.deleteds = deleteds;
86
        this.inserteds = inserteds;
87
        this.updateds = updateds;
88
        this.table = table;
89
        this.featureType = featureType;
90
        if (featureTypesChanged.hasNext()) {
91
            FeatureTypeChanged item = featureTypesChanged.next();
92
            this.featureTypeSource = item.getSource();
93
            this.featureTypeTarget = item.getTarget();
94
            typeChanged = true;
95
        } else {
96
            this.featureTypeSource = null;
97
            this.featureTypeTarget = null;
98
            typeChanged = false;
99
        }
100
    }
101

    
102
    public boolean isTypeChanged() {
103
        return typeChanged;
104
    }
105

    
106
    @Override
107
    public Object perform(JDBCConnection conn) throws DataException {
108
        if (typeChanged) {
109
            this.performUpdateTable(conn);
110
        }
111
        if (inserteds.hasNext()) {
112
            performInserts(conn);
113
        }
114
        if (updateds.hasNext()) {
115
            performUpdates(conn);
116
        }
117
        if (deleteds.hasNext()) {
118
            performDeletes(conn);
119
        }
120
        return true;
121
    }
122
    
123
    public String getDeleteSQL() {
124
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
125
        return this.getDeleteSQL(sqlbuilder);
126
    }
127
    
128
    public String getDeleteSQL(JDBCSQLBuilderBase sqlbuilder) {
129
        ExpressionBuilder expbuilder = sqlbuilder.expression();
130

    
131
        sqlbuilder.delete().table()
132
                .database(this.table.getDatabase())
133
                .schema(this.table.getSchema())
134
                .name(this.table.getTable());
135
        for (FeatureAttributeDescriptor attr : this.featureType) {
136
            if (attr.isPrimaryKey()) {
137
                sqlbuilder.delete().where().and(
138
                        expbuilder.eq(
139
                                expbuilder.column(attr.getName()),
140
                                expbuilder.parameter(attr.getName()).as_variable()
141
                        )
142
                );
143
            }
144
        }
145
        if (!sqlbuilder.delete().has_where() ) {
146
            throw new RuntimeException("Operation requires missing pk");
147
        }
148
        sqlbuilder.setProperties(
149
                Variable.class, 
150
                PROP_TABLE, table
151
        );
152
        String sql = sqlbuilder.delete().toString();
153
        return sql;
154
    }
155

    
156
    public void performDeletes(JDBCConnection conn) throws DataException {
157
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
158
        String sql = getDeleteSQL(sqlbuilder);
159
        perform_batchmode(conn, this.deleteds, sqlbuilder, sql, new Invocable() {
160
            @Override
161
            public Object call(Object... args) {
162
                JDBCSQLBuilderBase sqlbuilder = (JDBCSQLBuilderBase) args[0];
163
                PreparedStatement preparedStatement = (PreparedStatement) args[1];
164
                FeatureReference reference = (FeatureReference) args[2];
165
                Disposable theParametersDisposable = sqlbuilder.setParameters(preparedStatement, reference);
166
                return theParametersDisposable;
167
            }
168
        });
169
    }
170
    
171
    public String getInsertSQL() {
172
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
173
        return this.getInsertSQL(sqlbuilder);
174
    }
175
    
176
    public String getInsertSQL(JDBCSQLBuilderBase sqlbuilder) {
177
        GeometryExpressionBuilder expbuilder = sqlbuilder.expression();
178

    
179
        sqlbuilder.insert().table()
180
                .database(this.table.getDatabase())
181
                .schema(this.table.getSchema())
182
                .name(this.table.getTable());
183
        for (FeatureAttributeDescriptor attr : this.featureType) {
184
            if( attr.isAutomatic() || attr.isComputed() ) {
185
                continue;
186
            }
187
            if (attr.getType() == DataTypes.GEOMETRY) {
188
                sqlbuilder.insert().column().name(attr.getName()).with_value(
189
                    expbuilder.parameter(attr.getName()).as_variable()
190
                        .srs(
191
                            expbuilder.parameter().value(
192
                                attr.getSRS()).as_constant()
193
                        )
194
                );
195
            } else {
196
                sqlbuilder.insert().column().name(attr.getName()).with_value(
197
                        expbuilder.parameter(attr.getName())
198
                );
199
            }
200
        }
201

    
202
        sqlbuilder.setProperties(
203
                Variable.class, 
204
                PROP_FEATURE_TYPE, featureType,
205
                PROP_TABLE, table
206
        );
207
        sqlbuilder.setProperties(
208
                Parameter.class, 
209
                PROP_FEATURE_TYPE, featureType,
210
                PROP_TABLE, table
211
        );
212
        String sql = sqlbuilder.insert().toString();
213
        return sql;
214
    }
215

    
216
    public void performInserts(JDBCConnection conn) throws DataException {
217
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
218
        String sql = getInsertSQL(sqlbuilder);
219
        perform_batchmode(conn, this.inserteds, sqlbuilder, sql, new Invocable() {
220
            @Override
221
            public Object call(Object... args) {
222
                JDBCSQLBuilderBase sqlbuilder = (JDBCSQLBuilderBase) args[0];
223
                PreparedStatement preparedStatement = (PreparedStatement) args[1];
224
                FeatureProvider feature = (FeatureProvider) args[2];
225
                Disposable theParametersDisposable = sqlbuilder.setParameters(preparedStatement, feature);
226
                return theParametersDisposable;
227
            }
228
        });
229
    }
230
    
231
    public String getUpdateSQL() {
232
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
233
        return this.getUpdateSQL(sqlbuilder);
234
    }
235
    
236
    public String getUpdateSQL(JDBCSQLBuilderBase sqlbuilder) {
237
        GeometryExpressionBuilder expbuilder = sqlbuilder.expression();
238

    
239
        sqlbuilder.update().table()
240
                .database(this.table.getDatabase())
241
                .schema(this.table.getSchema())
242
                .name(this.table.getTable());
243
        for (FeatureAttributeDescriptor attr : this.featureType) {
244
            if (attr.isPrimaryKey()) {
245
                sqlbuilder.update().where().and(
246
                        expbuilder.eq(
247
                                expbuilder.column(attr.getName()),
248
                                expbuilder.parameter(attr.getName()).as_variable()
249
                        )
250
                );
251
                continue;
252
            } 
253
            if ( attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
254
                continue;
255
            }
256
            if (attr.getType() == DataTypes.GEOMETRY) {
257
                if (attr.getSRS()==null) {
258
                    throw new RuntimeException("Geometries with null srs are not valid. SRS is required in "+this.table.toString()+"in the field:"+attr.getName());
259
                }
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
        }
271
        if (!sqlbuilder.update().has_where() ) {
272
            throw new RuntimeException("Operation requires missing pk");
273
        }
274
        sqlbuilder.setProperties(
275
                Variable.class, 
276
                PROP_FEATURE_TYPE, this.featureType,
277
                PROP_TABLE, table
278
        );
279
        sqlbuilder.setProperties(
280
                Parameter.class, 
281
                PROP_FEATURE_TYPE, featureType,
282
                PROP_TABLE, table
283
        );
284
        
285
        String sql = sqlbuilder.update().toString();
286
        return sql;
287
    }
288
    
289
    public void performUpdates(JDBCConnection conn) throws DataException {
290
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
291
        String sql = getUpdateSQL(sqlbuilder);
292
        perform_batchmode(conn, this.updateds, sqlbuilder, sql, new Invocable() {
293
            @Override
294
            public Object call(Object... args) {
295
                JDBCSQLBuilderBase sqlbuilder = (JDBCSQLBuilderBase) args[0];
296
                PreparedStatement preparedStatement = (PreparedStatement) args[1];
297
                FeatureProvider feature = (FeatureProvider) args[2];
298
                Disposable theParametersDisposable = sqlbuilder.setParameters(preparedStatement, feature);
299
                return theParametersDisposable;
300
            }
301
        });
302
    }
303
    
304
       
305
    private void perform_batchmode(JDBCConnection conn, Iterator elements, JDBCSQLBuilderBase sqlbuilder, String sql, Invocable fillPreparedStatement) throws DataException {       
306
        PreparedStatement preparedStatement;
307
        Disposable[] disposableParameters;
308
        try {
309
            preparedStatement = conn.prepareStatement(sql);
310
            int batchSize = this.helper.getConnectionParameters().getBatchSize();
311
            if( batchSize<1 ) {
312
                batchSize = 200;
313
            }
314
            int batchCount = 0;
315
            disposableParameters = new Disposable[batchSize];
316
            
317
            while (elements.hasNext()) {
318
                Object element = elements.next();
319
                
320
                Disposable theParametersDisposable = (Disposable) fillPreparedStatement.call(sqlbuilder,preparedStatement, element );
321
                JDBCUtils.addBatch(preparedStatement,sql);
322
                disposableParameters[batchCount++] = theParametersDisposable;
323

    
324
                if( batchCount >= batchSize || (!elements.hasNext() && batchCount>0) ) {
325
                    int[] status = JDBCUtils.executeBatch(preparedStatement,sql);
326

    
327
                    preparedStatement.clearParameters();
328
                    preparedStatement.clearBatch();
329
                    for (int i = 0; i < batchCount && i < disposableParameters.length; i++) {
330
                        DisposeUtils.dispose(disposableParameters[i]);
331
                        disposableParameters[i] = null;
332
                    }
333
                    batchCount = 0;
334
                    
335
                    for (int n : status) {
336
                        if( n<=Statement.EXECUTE_FAILED ) { //-3
337
                            throw new RuntimeException("Can't process element (n="+n+").");
338
                        }
339
                    }                
340
                }
341
            }
342
            
343
        } catch (Exception ex) {
344
            throw new JDBCExecuteSQLException(sql,ex);
345
        }
346
    }
347

    
348
    public List<String> getUpdateTableSQLs() {
349
        JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
350
        sqlbuilder.alter_table().table()
351
                .database(this.table.getDatabase())
352
                .schema(this.table.getSchema())
353
                .name(this.table.getTable());
354

    
355
        // Primero comprobamos si hay que eliminar la primary-key o algun indice
356
        for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
357
            if( attrOrgiginal.isComputed() ) {
358
                continue;
359
            }
360
            FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor(
361
                    attrOrgiginal.getName()
362
            );
363
            if (attrTarget == null) {
364
                continue;
365
            }
366
            if( attrOrgiginal.isPrimaryKey() && !attrTarget.isPrimaryKey() ) {
367
                sqlbuilder.alter_table().drop_primary_key(attrTarget.getName());
368
            }
369
        }
370

    
371
        // Comprobamos si hay que eliminar o modificar alguna columna existente
372
        for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
373
            if( attrOrgiginal.isComputed() ) {
374
                continue;
375
            }
376
            FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor(
377
                    attrOrgiginal.getName()
378
            );
379
            if (attrTarget == null) {
380
                sqlbuilder.alter_table().drop_column(attrOrgiginal.getName());
381
            } else if( !this.areEquals(attrOrgiginal, attrTarget) ) {
382
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
383
                    sqlbuilder.alter_table().alter_geometry_column(
384
                            attrTarget.getName(),
385
                            attrTarget.getGeomType().getType(),
386
                            attrTarget.getGeomType().getSubType(),
387
                            attrTarget.getSRS(),
388
                            attrTarget.isIndexed(),
389
                            attrTarget.allowNull()
390
                    );
391
                } else {
392
                    Object defaultValue = attrTarget.getDefaultValue();
393
                    if( defaultValue instanceof CharSequence ) {
394
                        if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
395
                            defaultValue = null;
396
                        }
397
                    }
398
                    sqlbuilder.alter_table().alter_column(
399
                            attrTarget.getName(),
400
                            attrTarget.getType(),
401
                            attrTarget.getSize(),
402
                            attrTarget.getPrecision(),
403
                            attrTarget.getScale(),
404
                            attrTarget.isPrimaryKey(),
405
                            attrTarget.isIndexed(),
406
                            attrTarget.allowNull(),
407
                            attrTarget.isAutomatic(),
408
                            defaultValue
409
                    );
410
                }
411
            }
412
        }
413
        
414
        // Por ultimi comprobamos si hay que anadir alguna columna nueva.
415
        for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
416
            if( attrTarget.isComputed() ) {
417
                continue;
418
            }
419
            FeatureAttributeDescriptor attrOrgiginal = featureTypeSource.getAttributeDescriptor(
420
                    attrTarget.getName()
421
            );
422
            if (attrOrgiginal == null) {
423
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
424
                    sqlbuilder.alter_table().add_geometry_column(
425
                            attrTarget.getName(),
426
                            attrTarget.getGeomType().getType(),
427
                            attrTarget.getGeomType().getSubType(),
428
                            attrTarget.getSRS(),
429
                            attrTarget.isIndexed(),
430
                            attrTarget.allowNull()
431
                    );
432
                } else {
433
                    Object defaultValue = attrTarget.getDefaultValue();
434
                    if( defaultValue instanceof CharSequence ) {
435
                        if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
436
                            defaultValue = null;
437
                        }
438
                    }
439
                    sqlbuilder.alter_table().add_column(
440
                            attrTarget.getName(),
441
                            attrTarget.getType(),
442
                            attrTarget.getSize(),
443
                            attrTarget.getPrecision(),
444
                            attrTarget.getScale(),
445
                            attrTarget.isPrimaryKey(),
446
                            attrTarget.isIndexed(),
447
                            attrTarget.allowNull(),
448
                            attrTarget.isAutomatic(),
449
                            defaultValue
450
                    );
451
                }
452
            }
453
        }
454
        
455
        sqlbuilder.setProperties(
456
                Variable.class, 
457
                PROP_TABLE, table
458
        );
459
        List<String> sqls = sqlbuilder.alter_table().toStrings();
460
        sqls.addAll(buildCreateIndexSQL());
461
        return sqls;
462
    }
463
    
464
    protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) {
465
        // No interesa si son o no iguales en general, solo si son iguales en lo 
466
        // que a los atributos usados para crear la columna de la tabla se refiere.
467
        if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
468
            return false;
469
        }
470
        if( attr1.getType() != attr2.getType() ) {
471
            return false;
472
        }
473
        if( attr1.getPrecision() != attr2.getPrecision() ) {
474
            return false;
475
        }
476
        if( attr1.getScale() != attr2.getScale() ) {
477
            return false;
478
        }
479
        if( attr1.getSize() != attr2.getSize() ) {
480
            return false;
481
        }
482
        if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
483
            return false;
484
        }        
485
//        if( attr1.isIndexed() != attr2.isIndexed() ) {
486
//            return false;
487
//        }
488
        if( attr1.allowNull() != attr2.allowNull() ) {
489
            return false;
490
        }
491
        if( attr1.isAutomatic() != attr2.isAutomatic() ) {
492
            return false;
493
        }
494
        if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
495
            if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) {
496
                return false;
497
            }
498
            if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
499
                return false;
500
            }
501
        }
502
        return true;
503
    }
504

    
505
    protected List<String> buildCreateIndexSQL() {
506
        ArrayList<String> sqls = new ArrayList<>();
507
        
508
        for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
509
            boolean createIndex;
510
            boolean dropIndex;
511
            FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName());
512
            if( attrOriginal != null ) {
513
                 createIndex = (attrTarget.isIndexed() && !attrOriginal.isIndexed());
514
                 dropIndex = (!attrTarget.isIndexed() && attrOriginal.isIndexed());
515
            } else {
516
                createIndex = attrTarget.isIndexed();
517
                dropIndex = false;
518
            }
519
            if( createIndex ) {
520
                JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
521
                if( attrTarget.getType()==DataTypes.GEOMETRY ) {
522
                    sqlbuilder.create_index().spatial();
523
                }
524
                sqlbuilder.create_index().if_not_exist();
525
                sqlbuilder.create_index().name(table.getTable(), attrTarget.getName());
526
                sqlbuilder.create_index().column(attrTarget.getName());
527
                sqlbuilder.create_index().table()
528
                    .database(this.table.getDatabase())
529
                    .schema(this.table.getSchema())
530
                    .name(this.table.getTable());
531
                sqlbuilder.setProperties(
532
                        Variable.class, 
533
                        PROP_TABLE, table
534
                );
535
                sqls.addAll(sqlbuilder.create_index().toStrings());
536
            } else if( dropIndex ) {
537
                JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
538
                sqlbuilder.drop_index().if_not_exist();
539
                sqlbuilder.drop_index().name( table.getTable(), attrTarget.getName());
540
            }
541
        }
542
        return sqls;
543
    }
544
    
545
    public void performUpdateTable(JDBCConnection conn) throws DataException {
546
        
547
        List<String> sqls = this.getUpdateTableSQLs();
548

    
549
        if( !CollectionUtils.isEmpty(sqls) ) {
550
            Statement st = null;
551
            String currentsql = null;
552
            try {
553
                st = conn.createStatement();
554
                for (String sql : sqls) {
555
                    currentsql = sql;
556
                    if( !StringUtils.isBlank(sql) ) {
557
                        JDBCUtils.execute(st, sql);
558
                    }
559
                }
560
            } catch (SQLException e) {
561
                throw new JDBCSQLException(e,currentsql);
562
            } finally {
563
                JDBCUtils.closeQuietly(st);
564
            }
565
        }
566
    }
567
}