Revision 44678 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

View differences:

H2SpatialAppendOperation.java
3 3

  
4 4
import java.sql.PreparedStatement;
5 5
import java.sql.SQLException;
6
import java.util.ArrayList;
7
import java.util.List;
6 8
import org.gvsig.fmap.dal.DataTypes;
7 9
import org.gvsig.fmap.dal.exception.DataException;
8 10
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
......
30 32
    }
31 33
    
32 34
    @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

  
35
    public List<String> getPreviousSQLs() {
36
      List<String> sqls = new ArrayList<>();
37
      sqls.add("SET LOG 1");
38
      sqls.add("SET LOCK_MODE 1");
39
      sqls.add("SET UNDO_LOG 0");
40
      return sqls;
77 41
    }
78 42
    
79 43
    @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;        
44
    public List<String> getPostSQLs() {
45
      List<String> sqls = new ArrayList<>();
46
      sqls.add("SET LOG 2");
47
      sqls.add("SET LOCK_MODE 3");
48
      sqls.add("SET UNDO_LOG 1");
49
      return sqls;
87 50
    }
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
    }
51
       
135 52
}

Also available in: Unified diff