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

History | View | Annotate | Download (4.41 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.logging.Level;
8
import java.util.logging.Logger;
9
import org.gvsig.fmap.dal.DataTypes;
10
import org.gvsig.fmap.dal.exception.DataException;
11
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
12
import org.gvsig.fmap.dal.feature.FeatureType;
13
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
14
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
15
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
16
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException;
17
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException;
18
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
19
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
20
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
21

    
22

    
23
public class AppendOperation {
24

    
25
    protected Connection connection = null;
26
    protected final JDBCHelper helper;
27
    protected final String database;
28
    protected final String schema;
29
    protected final String table;
30
    protected final FeatureType type;
31
    
32
    protected JDBCSQLBuilderBase sqlbuilder = null;
33
    protected PreparedStatement preparedStatement;
34
    protected String sql;
35
    
36
    public AppendOperation(
37
            JDBCHelper helper, 
38
            String database,
39
            String schema, 
40
            String table, 
41
            FeatureType type
42
        ) {
43
        this.helper = helper;
44
        this.database = database;
45
        this.schema = schema;
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

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

    
78
            PreparedStatement st;
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
        try {
123
            this.sqlbuilder.setParameters(this.preparedStatement, feature);
124
            n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql);
125
        } catch(Exception ex) {
126
            throw new RuntimeException("Can't insert feature.", ex);
127
        }
128
        if( n<1 ) {
129
            throw new RuntimeException("Can't insert feature (n="+n+").");
130
        }
131
    }
132
}