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

History | View | Annotate | Download (6.67 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
25

    
26
import java.sql.Connection;
27
import java.sql.PreparedStatement;
28
import java.sql.SQLException;
29
import java.util.Collections;
30
import java.util.List;
31
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
32
import org.gvsig.fmap.dal.DataTypes;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException;
37
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
38
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException;
39
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
40
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
41
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
42
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
43
import org.gvsig.tools.dispose.Disposable;
44
import org.gvsig.tools.dispose.DisposeUtils;
45

    
46

    
47
public class AppendOperation {
48

    
49
    protected Connection connection = null;
50
    protected final JDBCHelper helper;
51
    protected final TableReference table;
52
    protected final FeatureType type;
53
    
54
    protected JDBCSQLBuilderBase sqlbuilder = null;
55
    protected GeometryExpressionBuilder expbuilder;
56

    
57
    protected PreparedStatement preparedStatement;
58
    protected String insertSQL;
59
    
60
    public AppendOperation(
61
            JDBCHelper helper, 
62
            TableReference table, 
63
            FeatureType type
64
        ) {
65
        this.helper = helper;
66
        this.table = table;
67
        this.type = type;
68
    }
69
    
70
    public void begin() throws DataException {
71
        if (this.sqlbuilder != null) {
72
            throw new AlreadyEditingException(this.helper.getSourceId());
73
        }
74

    
75
        try {
76
            this.connection = this.helper.getConnectionWritable();
77
            
78
            this.sqlbuilder = this.helper.createSQLBuilder();
79
            this.expbuilder = this.sqlbuilder.expression();
80

    
81
            this.sqlbuilder.insert().table()
82
                    .database(this.table.getDatabase())
83
                    .schema(this.table.getSchema())
84
                    .name(this.table.getTable()
85
            );
86
            for (FeatureAttributeDescriptor attr : type) {
87
                if( attr.isAutomatic() || attr.isComputed() ) {
88
                    continue;
89
                }
90
                if (attr.getType() == DataTypes.GEOMETRY) {
91
                        this.sqlbuilder.insert().column().name(attr.getName()).with_value( 
92
                            expbuilder.parameter(attr.getName()).as_geometry_variable().srs( 
93
                                    expbuilder.parameter().value(attr.getSRS()) 
94
                            ) 
95
                        );
96
                    } else {
97
                        this.sqlbuilder.insert().column().name(attr.getName()).with_value(
98
                        expbuilder.parameter(attr.getName()).as_variable()
99
                    );
100
                }
101
            }
102

    
103
            this.insertSQL = this.sqlbuilder.insert().toString();
104
            if( this.connection != null ) { // Not in test mode ???
105
              this.preparedStatement = this.connection.prepareStatement(insertSQL);
106
              this.connection.setAutoCommit(false);
107
              for (String sql : this.getPreviousSQLs()) {
108
                JDBCUtils.execute(this.connection, sql);
109
              }
110
            }
111
            
112
        } catch (SQLException ex) {
113
            throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex);
114
        }
115

    
116
    }
117
    
118
    protected void clean() {
119
        JDBCUtils.closeQuietly(this.preparedStatement);
120
        this.helper.closeConnection(this.connection);
121
        this.connection = null;
122
        this.preparedStatement = null;
123
        this.sqlbuilder = null;
124
        this.insertSQL = null;        
125
    }
126
    
127
    public void end() {
128
        try {
129
            if( this.connection == null ) {
130
              return; // In test mode ???
131
            }
132
            this.connection.commit();
133
            for (String sql : this.getPostSQLs()) {
134
              JDBCUtils.execute(this.connection, sql);
135
            }
136
        } catch (SQLException ex) {
137
            try {
138
                this.connection.rollback();
139
            } catch (SQLException ex1) {
140
            }
141
            throw new RuntimeException("Can't commit transaction", ex);
142
        } finally {
143
            clean();
144
        }
145
    }
146
    
147
    public void abort() {        
148
        try {
149
            if( this.connection == null ) {
150
              return; // In test mode ???
151
            }
152
            this.connection.rollback();
153
            for (String sql : this.getPostSQLs()) {
154
              JDBCUtils.execute(this.connection, sql);
155
            }
156
        } catch (SQLException ex) {
157
        } finally {
158
          clean();
159
        }
160
    }
161

    
162
    public String getSQL() { // For test
163
      return this.insertSQL;
164
    }
165
    
166
    public List<String> getPreviousSQLs() {
167
      return Collections.EMPTY_LIST;
168
    }
169
    
170
    public List<String> getPostSQLs() {
171
      return Collections.EMPTY_LIST;
172
    }    
173
    
174
    public List<Object> getSQLParameters(FeatureProvider feature) {
175
      return this.sqlbuilder.getParameters(feature);
176
    }
177
    
178
    public void append(FeatureProvider feature) throws DataException {
179
        int n;
180
        Disposable paramsDisposer = null;
181
        try {
182
            paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature);
183
            n = JDBCUtils.executeUpdate(this.preparedStatement,this.insertSQL);
184
        } catch(Exception ex) {
185
            throw new RuntimeException("Can't insert feature.", ex);
186
        } finally {
187
            DisposeUtils.disposeQuietly(paramsDisposer);
188
        }
189
        if( n<1 ) {
190
            throw new RuntimeException("Can't insert feature (n="+n+").");
191
        }
192
    }
193
}