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

History | View | Annotate | Download (4.48 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.fmap.dal.DataTypes;
8
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
10
import org.gvsig.fmap.dal.feature.FeatureType;
11
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
12
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
13
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
15
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
16
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
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 PreparedStatement preparedStatement;
32
    protected String sql;
33
    
34
    public AppendOperation(
35
            JDBCHelper helper, 
36
            TableReference table, 
37
            FeatureType type
38
        ) {
39
        this.helper = helper;
40
        this.table = table;
41
        this.type = type;
42
    }
43
    
44
    public void begin() throws DataException {
45
        if (this.sqlbuilder != null) {
46
            throw new AlreadyEditingException(this.helper.getSourceId());
47
        }
48

    
49
        try {
50
            this.connection = this.helper.getConnectionWritable();
51
            
52
            this.sqlbuilder = this.helper.createSQLBuilder();
53

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

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

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