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

History | View | Annotate | Download (4.84 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.fmap.dal.DataTypes;
7
import org.gvsig.fmap.dal.exception.DataException;
8
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
9
import org.gvsig.fmap.dal.feature.FeatureType;
10
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
11
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
12
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
15
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
16
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.AppendOperation;
17
import org.gvsig.tools.dispose.Disposable;
18
import org.gvsig.tools.dispose.DisposeUtils;
19

    
20

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

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

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

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

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

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