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.h2 / src / main / java / org / gvsig / fmap / dal / store / h2 / operations / H2SpatialAppendOperation.java @ 44198

History | View | Annotate | Download (4.87 KB)

1

    
2
package org.gvsig.fmap.dal.store.h2.operations;
3

    
4
import java.sql.PreparedStatement;
5
import java.sql.SQLException;
6
import org.gvsig.expressionevaluator.ExpressionBuilder;
7
import org.gvsig.fmap.dal.DataTypes;
8
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
10
import org.gvsig.fmap.dal.feature.FeatureType;
11
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
12
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
13
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
15
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
16
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
17
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.AppendOperation;
18
import org.gvsig.tools.dispose.Disposable;
19
import org.gvsig.tools.dispose.DisposeUtils;
20

    
21

    
22
@SuppressWarnings("UseSpecificCatch")
23
public class H2SpatialAppendOperation extends AppendOperation {
24

    
25
    public H2SpatialAppendOperation(
26
            JDBCHelper helper, 
27
            TableReference table, 
28
            FeatureType type
29
        ) {
30
        super(helper, table, type);
31
    }
32
    
33
    @Override
34
    public void begin() throws DataException {
35
        if (this.sqlbuilder != null) {
36
            throw new AlreadyEditingException(this.helper.getSourceId());
37
        }
38

    
39
        try {
40
            this.connection = this.helper.getConnectionWritable();
41
            
42
            this.sqlbuilder = this.helper.createSQLBuilder();
43
            this.expbuilder = this.sqlbuilder.expression();
44

    
45
            this.sqlbuilder.insert().table()
46
                .database(this.table.getDatabase())
47
                .schema(this.table.getSchema())
48
                .name(this.table.getTable());
49
            for (FeatureAttributeDescriptor attr : type) {
50
                if( attr.isAutomatic() ) {
51
                    continue;
52
                }
53
                if (attr.getType() == DataTypes.GEOMETRY) {
54
                    this.sqlbuilder.insert().column().name(attr.getName()).with_value( 
55
                        this.expbuilder.parameter(attr.getName()).as_geometry_variable().srs( 
56
                                this.expbuilder.parameter().value(attr.getSRS()) 
57
                        ) 
58
                    );
59
                } else {
60
                    this.sqlbuilder.insert().column().name(attr.getName()).with_value(
61
                        this.expbuilder.parameter(attr.getName()).as_variable()
62
                    );
63
                }
64
            }
65

    
66
            PreparedStatement st;
67
            this.sql = this.sqlbuilder.insert().toString();
68
            this.preparedStatement = this.connection.prepareStatement(sql);
69
            this.connection.setAutoCommit(false);
70
            JDBCUtils.execute(this.connection, "SET LOG 1");
71
            JDBCUtils.execute(this.connection, "SET LOCK_MODE 1");
72
            JDBCUtils.execute(this.connection, "SET UNDO_LOG 0");
73
            
74
        } catch (SQLException ex) {
75
            throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex);
76
        }
77

    
78
    }
79
    
80
    @Override
81
    protected void clean() {
82
        JDBCUtils.closeQuietly(this.preparedStatement);
83
        this.helper.closeConnection(this.connection);
84
        this.connection = null;
85
        this.preparedStatement = null;
86
        this.sqlbuilder = null;
87
        this.sql = null;        
88
    }
89
    
90
    @Override
91
    public void end() {
92
        try {
93
            this.connection.commit();
94
            JDBCUtils.execute(this.connection, "SET LOG 2");
95
            JDBCUtils.execute(this.connection, "SET LOCK_MODE 3");
96
            JDBCUtils.execute(this.connection, "SET UNDO_LOG 1");
97
        } catch (SQLException ex) {
98
            try {
99
                this.connection.rollback();
100
            } catch (SQLException ex1) {
101
            }
102
            throw new RuntimeException("Can't commit transaction", ex);
103
        } finally {
104
            clean();
105
        }
106
    }
107
    
108
    @Override
109
    public void abort() {        
110
        try {
111
            this.connection.rollback();
112
            JDBCUtils.execute(this.connection, "SET LOG 2");
113
            JDBCUtils.execute(this.connection, "SET LOCK_MODE 3");
114
            JDBCUtils.execute(this.connection, "SET UNDO_LOG 1");
115
        } catch (SQLException ex) {
116
        }
117
        clean();
118
    }
119
    
120
    @Override
121
    public void append(FeatureProvider feature) throws DataException {
122
        int n;
123
        Disposable paramsDisposer = null;
124
        try {
125
            paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature);
126
            n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql);
127
        } catch(Exception ex) {
128
            throw new RuntimeException("Can't insert feature.", ex);
129
        } finally {
130
            DisposeUtils.disposeQuietly(paramsDisposer);
131
        }
132
        if( n<1 ) {
133
            throw new RuntimeException("Can't insert feature (n="+n+").");
134
        }
135
    }
136
}