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

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

    
22

    
23
public class AppendOperation {
24

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

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

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

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

    
79
            this.sql = this.sqlbuilder.insert().toString();
80
            this.preparedStatement = this.connection.prepareStatement(sql);
81
            this.connection.setAutoCommit(false);
82
            
83
        } catch (SQLException ex) {
84
            throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex);
85
        }
86

    
87
    }
88
    
89
    protected void clean() {
90
        JDBCUtils.closeQuietly(this.preparedStatement);
91
        this.helper.closeConnection(this.connection);
92
        this.connection = null;
93
        this.preparedStatement = null;
94
        this.sqlbuilder = null;
95
        this.sql = null;        
96
    }
97
    
98
    public void end() {
99
        try {
100
            this.connection.commit();
101
        } catch (SQLException ex) {
102
            try {
103
                this.connection.rollback();
104
            } catch (SQLException ex1) {
105
            }
106
            throw new RuntimeException("Can't commit transaction", ex);
107
        } finally {
108
            clean();
109
        }
110
    }
111
    
112
    public void abort() {        
113
        try {
114
            this.connection.rollback();
115
        } catch (SQLException ex) {
116
        }
117
        clean();
118
    }
119
    
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
}