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

History | View | Annotate | Download (5.78 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.Collection;
8
import java.util.Collections;
9
import java.util.List;
10
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
11
import org.gvsig.fmap.dal.DataTypes;
12
import org.gvsig.fmap.dal.exception.DataException;
13
import org.gvsig.fmap.dal.feature.Feature;
14
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.dal.feature.FeatureType;
16
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
17
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
18
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException;
19
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
20
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
21
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
22
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
23
import org.gvsig.tools.dispose.Disposable;
24
import org.gvsig.tools.dispose.DisposeUtils;
25

    
26

    
27
public class AppendOperation {
28

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

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

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

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

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

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

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