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 / AppendOperation.java @ 45008

History | View | Annotate | Download (5.74 KB)

1

    
2
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
3

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

    
24

    
25
public class AppendOperation {
26

    
27
    protected Connection connection = null;
28
    protected final JDBCHelper helper;
29
    protected final TableReference table;
30
    protected final FeatureType type;
31
    
32
    protected JDBCSQLBuilderBase sqlbuilder = null;
33
    protected GeometryExpressionBuilder expbuilder;
34

    
35
    protected PreparedStatement preparedStatement;
36
    protected String insertSQL;
37
    
38
    public AppendOperation(
39
            JDBCHelper helper, 
40
            TableReference table, 
41
            FeatureType type
42
        ) {
43
        this.helper = helper;
44
        this.table = table;
45
        this.type = type;
46
    }
47
    
48
    public void begin() throws DataException {
49
        if (this.sqlbuilder != null) {
50
            throw new AlreadyEditingException(this.helper.getSourceId());
51
        }
52

    
53
        try {
54
            this.connection = this.helper.getConnectionWritable();
55
            
56
            this.sqlbuilder = this.helper.createSQLBuilder();
57
            this.expbuilder = this.sqlbuilder.expression();
58

    
59
            this.sqlbuilder.insert().table()
60
                    .database(this.table.getDatabase())
61
                    .schema(this.table.getSchema())
62
                    .name(this.table.getTable()
63
            );
64
            for (FeatureAttributeDescriptor attr : type) {
65
                if( attr.isAutomatic() || attr.isComputed() ) {
66
                    continue;
67
                }
68
                if (attr.getType() == DataTypes.GEOMETRY) {
69
                        this.sqlbuilder.insert().column().name(attr.getName()).with_value( 
70
                            expbuilder.parameter(attr.getName()).as_geometry_variable().srs( 
71
                                    expbuilder.parameter().value(attr.getSRS()) 
72
                            ) 
73
                        );
74
                    } else {
75
                        this.sqlbuilder.insert().column().name(attr.getName()).with_value(
76
                        expbuilder.parameter(attr.getName()).as_variable()
77
                    );
78
                }
79
            }
80

    
81
            this.insertSQL = this.sqlbuilder.insert().toString();
82
            if( this.connection != null ) { // Not in test mode ???
83
              this.preparedStatement = this.connection.prepareStatement(insertSQL);
84
              this.connection.setAutoCommit(false);
85
              for (String sql : this.getPreviousSQLs()) {
86
                JDBCUtils.execute(this.connection, sql);
87
              }
88
            }
89
            
90
        } catch (SQLException ex) {
91
            throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex);
92
        }
93

    
94
    }
95
    
96
    protected void clean() {
97
        JDBCUtils.closeQuietly(this.preparedStatement);
98
        this.helper.closeConnection(this.connection);
99
        this.connection = null;
100
        this.preparedStatement = null;
101
        this.sqlbuilder = null;
102
        this.insertSQL = null;        
103
    }
104
    
105
    public void end() {
106
        try {
107
            if( this.connection == null ) {
108
              return; // In test mode ???
109
            }
110
            this.connection.commit();
111
            for (String sql : this.getPostSQLs()) {
112
              JDBCUtils.execute(this.connection, sql);
113
            }
114
        } catch (SQLException ex) {
115
            try {
116
                this.connection.rollback();
117
            } catch (SQLException ex1) {
118
            }
119
            throw new RuntimeException("Can't commit transaction", ex);
120
        } finally {
121
            clean();
122
        }
123
    }
124
    
125
    public void abort() {        
126
        try {
127
            if( this.connection == null ) {
128
              return; // In test mode ???
129
            }
130
            this.connection.rollback();
131
            for (String sql : this.getPostSQLs()) {
132
              JDBCUtils.execute(this.connection, sql);
133
            }
134
        } catch (SQLException ex) {
135
        } finally {
136
          clean();
137
        }
138
    }
139

    
140
    public String getSQL() { // For test
141
      return this.insertSQL;
142
    }
143
    
144
    public List<String> getPreviousSQLs() {
145
      return Collections.EMPTY_LIST;
146
    }
147
    
148
    public List<String> getPostSQLs() {
149
      return Collections.EMPTY_LIST;
150
    }    
151
    
152
    public List<Object> getSQLParameters(FeatureProvider feature) {
153
      return this.sqlbuilder.getParameters(feature);
154
    }
155
    
156
    public void append(FeatureProvider feature) throws DataException {
157
        int n;
158
        Disposable paramsDisposer = null;
159
        try {
160
            paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature);
161
            n = JDBCUtils.executeUpdate(this.preparedStatement,this.insertSQL);
162
        } catch(Exception ex) {
163
            throw new RuntimeException("Can't insert feature.", ex);
164
        } finally {
165
            DisposeUtils.disposeQuietly(paramsDisposer);
166
        }
167
        if( n<1 ) {
168
            throw new RuntimeException("Can't insert feature (n="+n+").");
169
        }
170
    }
171
}