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

History | View | Annotate | Download (4.62 KB)

1 43020 jjdelcerro
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 44198 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
8 43020 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
9 44198 jjdelcerro
import org.gvsig.fmap.dal.SQLBuilder;
10 43020 jjdelcerro
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.JDBCPreparingSQLException;
16
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
17
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
18 44058 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
19 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
20 43629 jjdelcerro
import org.gvsig.tools.dispose.Disposable;
21
import org.gvsig.tools.dispose.DisposeUtils;
22 43020 jjdelcerro
23
24
public class AppendOperation {
25
26
    protected Connection connection = null;
27
    protected final JDBCHelper helper;
28 44058 jjdelcerro
    protected final TableReference table;
29 43020 jjdelcerro
    protected final FeatureType type;
30
31
    protected JDBCSQLBuilderBase sqlbuilder = null;
32 44198 jjdelcerro
    protected ExpressionBuilder expbuilder;
33
34 43020 jjdelcerro
    protected PreparedStatement preparedStatement;
35
    protected String sql;
36
37
    public AppendOperation(
38
            JDBCHelper helper,
39 44058 jjdelcerro
            TableReference table,
40 43020 jjdelcerro
            FeatureType type
41
        ) {
42
        this.helper = helper;
43
        this.table = table;
44
        this.type = type;
45
    }
46
47
    public void begin() throws DataException {
48
        if (this.sqlbuilder != null) {
49
            throw new AlreadyEditingException(this.helper.getSourceId());
50
        }
51
52
        try {
53 43377 jjdelcerro
            this.connection = this.helper.getConnectionWritable();
54 43020 jjdelcerro
55
            this.sqlbuilder = this.helper.createSQLBuilder();
56 44198 jjdelcerro
            this.expbuilder = this.sqlbuilder.expression();
57 43020 jjdelcerro
58 44058 jjdelcerro
            this.sqlbuilder.insert().table()
59
                    .database(this.table.getDatabase())
60
                    .schema(this.table.getSchema())
61
                    .name(this.table.getTable()
62
            );
63 43020 jjdelcerro
            for (FeatureAttributeDescriptor attr : type) {
64
                if( attr.isAutomatic() ) {
65
                    continue;
66
                }
67
                if (attr.getType() == DataTypes.GEOMETRY) {
68
                    this.sqlbuilder.insert().column().name(attr.getName()).with_value(
69 44198 jjdelcerro
                        expbuilder.parameter(attr.getName()).as_geometry_variable().srs(
70
                                expbuilder.parameter().value(attr.getSRS())
71 43020 jjdelcerro
                        )
72
                    );
73
                } else {
74
                    this.sqlbuilder.insert().column().name(attr.getName()).with_value(
75 44198 jjdelcerro
                        expbuilder.parameter(attr.getName()).as_variable()
76 43020 jjdelcerro
                    );
77
                }
78
            }
79
80
            PreparedStatement st;
81
            this.sql = this.sqlbuilder.insert().toString();
82
            this.preparedStatement = this.connection.prepareStatement(sql);
83 43377 jjdelcerro
            this.connection.setAutoCommit(false);
84 43020 jjdelcerro
85
        } catch (SQLException ex) {
86
            throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex);
87
        }
88
89
    }
90
91 43377 jjdelcerro
    protected void clean() {
92 43020 jjdelcerro
        JDBCUtils.closeQuietly(this.preparedStatement);
93 43377 jjdelcerro
        this.helper.closeConnection(this.connection);
94 43020 jjdelcerro
        this.connection = null;
95
        this.preparedStatement = null;
96
        this.sqlbuilder = null;
97 43377 jjdelcerro
        this.sql = null;
98 43020 jjdelcerro
    }
99
100 43377 jjdelcerro
    public void end() {
101
        try {
102
            this.connection.commit();
103
        } catch (SQLException ex) {
104
            try {
105
                this.connection.rollback();
106
            } catch (SQLException ex1) {
107
            }
108
            throw new RuntimeException("Can't commit transaction", ex);
109
        } finally {
110
            clean();
111
        }
112 43020 jjdelcerro
    }
113
114 43377 jjdelcerro
    public void abort() {
115
        try {
116
            this.connection.rollback();
117
        } catch (SQLException ex) {
118
        }
119
        clean();
120
    }
121
122 43020 jjdelcerro
    public void append(FeatureProvider feature) throws DataException {
123 43377 jjdelcerro
        int n;
124 43629 jjdelcerro
        Disposable paramsDisposer = null;
125 43020 jjdelcerro
        try {
126 43629 jjdelcerro
            paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature);
127 43377 jjdelcerro
            n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql);
128
        } catch(Exception ex) {
129
            throw new RuntimeException("Can't insert feature.", ex);
130 43629 jjdelcerro
        } finally {
131
            DisposeUtils.disposeQuietly(paramsDisposer);
132 43020 jjdelcerro
        }
133 43377 jjdelcerro
        if( n<1 ) {
134
            throw new RuntimeException("Can't insert feature (n="+n+").");
135
        }
136 43020 jjdelcerro
    }
137
}