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 @ 47779
History | View | Annotate | Download (11 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.PreparedStatement; |
27 |
import java.sql.SQLException; |
28 |
import java.sql.Statement; |
29 |
import java.util.Collections; |
30 |
import java.util.List; |
31 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
34 |
import static org.gvsig.fmap.dal.feature.FeatureStore.SUBMODE_MERGE; |
35 |
import static org.gvsig.fmap.dal.feature.FeatureStore.SUBMODE_NONE; |
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException; |
38 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
39 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
40 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
41 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
42 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
43 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
44 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
45 |
import org.gvsig.fmap.geom.DataTypes; |
46 |
import org.gvsig.tools.dispose.Disposable; |
47 |
import org.gvsig.tools.dispose.DisposeUtils; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
|
51 |
@SuppressWarnings("UseSpecificCatch") |
52 |
public class AppendOperation { |
53 |
protected static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AppendOperation.class); |
54 |
|
55 |
protected JDBCConnection connection = null; |
56 |
protected final JDBCHelper helper; |
57 |
protected final TableReference table; |
58 |
protected final FeatureType type; |
59 |
|
60 |
protected JDBCSQLBuilderBase sqlbuilder = null; |
61 |
protected GeometryExpressionBuilder expbuilder;
|
62 |
|
63 |
protected PreparedStatement preparedStatement; |
64 |
protected String insertSQL; |
65 |
protected int batchCount; |
66 |
private int batchSize; //No es final para facilitar la depuracion |
67 |
private final Disposable[] disposableParameters; |
68 |
|
69 |
public AppendOperation(
|
70 |
JDBCHelper helper, |
71 |
TableReference table, |
72 |
FeatureType type |
73 |
) { |
74 |
this.helper = helper;
|
75 |
this.table = table;
|
76 |
this.type = type;
|
77 |
this.batchSize = this.helper.getConnectionParameters().getBatchSize(); |
78 |
if( this.batchSize>0 ) { |
79 |
this.disposableParameters = new Disposable[this.batchSize]; |
80 |
} else {
|
81 |
this.disposableParameters = null; |
82 |
} |
83 |
} |
84 |
|
85 |
public void begin(int submode) throws DataException { |
86 |
if (this.sqlbuilder != null) { |
87 |
throw new AlreadyEditingException(this.helper.getSourceId()); |
88 |
} |
89 |
|
90 |
try {
|
91 |
this.connection = this.helper.getConnectionWritable(); |
92 |
switch(submode) {
|
93 |
case SUBMODE_MERGE:
|
94 |
this.insertSQL = this.getMergeSQL(); |
95 |
break;
|
96 |
case SUBMODE_NONE:
|
97 |
default:
|
98 |
this.insertSQL = this.getInsertSQL(); |
99 |
break;
|
100 |
} |
101 |
|
102 |
if( this.connection != null ) { // Not in test mode ??? |
103 |
this.preparedStatement = this.connection.prepareStatement(insertSQL); |
104 |
this.connection.begin();
|
105 |
for (String sql : this.getPreviousSQLs()) { |
106 |
this.connection.execute(sql);
|
107 |
} |
108 |
} |
109 |
|
110 |
} catch (SQLException ex) { |
111 |
throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex); |
112 |
} |
113 |
|
114 |
} |
115 |
|
116 |
protected String getInsertSQL() { |
117 |
this.sqlbuilder = this.helper.createSQLBuilder(); |
118 |
this.expbuilder = this.sqlbuilder.expression(); |
119 |
|
120 |
this.sqlbuilder.insert().table()
|
121 |
.database(this.table.getDatabase())
|
122 |
.schema(this.table.getSchema())
|
123 |
.name(this.table.getTable()
|
124 |
); |
125 |
for (FeatureAttributeDescriptor attr : type) {
|
126 |
if( attr.isAutomatic() || attr.isComputed() ) {
|
127 |
continue;
|
128 |
} |
129 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
130 |
this.sqlbuilder.insert().column().name(attr.getName()).with_value(
|
131 |
expbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
132 |
expbuilder.parameter().value(attr.getSRS()) |
133 |
) |
134 |
); |
135 |
} else {
|
136 |
this.sqlbuilder.insert().column().name(attr.getName()).with_value(
|
137 |
expbuilder.parameter(attr.getName()).as_variable() |
138 |
); |
139 |
} |
140 |
} |
141 |
|
142 |
return this.sqlbuilder.insert().toString(); |
143 |
} |
144 |
|
145 |
protected String getMergeSQL() { |
146 |
this.sqlbuilder = this.helper.createSQLBuilder(); |
147 |
this.expbuilder = this.sqlbuilder.expression(); |
148 |
|
149 |
this.sqlbuilder.merge().table()
|
150 |
.database(this.table.getDatabase())
|
151 |
.schema(this.table.getSchema())
|
152 |
.name(this.table.getTable()
|
153 |
); |
154 |
for (FeatureAttributeDescriptor pk : type.getPrimaryKey()) {
|
155 |
this.sqlbuilder.merge().key(pk.getName());
|
156 |
} |
157 |
|
158 |
for (FeatureAttributeDescriptor attr : type) {
|
159 |
if( attr.isAutomatic() || attr.isComputed() ) {
|
160 |
continue;
|
161 |
} |
162 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
163 |
this.sqlbuilder.merge().column().name(attr.getName()).with_value(
|
164 |
expbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
165 |
expbuilder.parameter().value(attr.getSRS()) |
166 |
) |
167 |
); |
168 |
} else {
|
169 |
this.sqlbuilder.merge().column().name(attr.getName()).with_value(
|
170 |
expbuilder.parameter(attr.getName()).as_variable() |
171 |
); |
172 |
} |
173 |
} |
174 |
|
175 |
return this.sqlbuilder.merge().toString(); |
176 |
} |
177 |
|
178 |
protected void clean() { |
179 |
if( this.batchCount > 0 ) { |
180 |
this.clearBatch();
|
181 |
} |
182 |
JDBCUtils.closeQuietly(this.preparedStatement);
|
183 |
if( this.connection!=null ) { |
184 |
this.connection.closeQuietly();
|
185 |
this.connection = null; |
186 |
} |
187 |
this.preparedStatement = null; |
188 |
this.sqlbuilder = null; |
189 |
this.insertSQL = null; |
190 |
} |
191 |
|
192 |
public void end() { |
193 |
try {
|
194 |
if( this.connection == null ) { |
195 |
return; // In test mode ??? |
196 |
} |
197 |
if( this.batchCount > 0 ) { |
198 |
this.executeBatch();
|
199 |
} |
200 |
this.connection.commit();
|
201 |
for (String sql : this.getPostSQLs()) { |
202 |
this.connection.execute(sql);
|
203 |
} |
204 |
} catch (Exception ex) { |
205 |
try {
|
206 |
this.connection.rollback();
|
207 |
} catch (Exception ex1) { |
208 |
} |
209 |
throw new RuntimeException("Can't commit transaction", ex); |
210 |
} finally {
|
211 |
clean(); |
212 |
} |
213 |
} |
214 |
|
215 |
public void abort() { |
216 |
try {
|
217 |
if( this.connection == null ) { |
218 |
return; // In test mode ??? |
219 |
} |
220 |
if( this.batchCount > 0 ) { |
221 |
this.clearBatch();
|
222 |
} |
223 |
this.connection.rollback();
|
224 |
for (String sql : this.getPostSQLs()) { |
225 |
this.connection.execute(sql);
|
226 |
} |
227 |
} catch (Exception ex) { |
228 |
LOGGER.debug("",ex);
|
229 |
} finally {
|
230 |
clean(); |
231 |
} |
232 |
} |
233 |
|
234 |
public String getSQL() { // For test |
235 |
return this.insertSQL; |
236 |
} |
237 |
|
238 |
public List<String> getPreviousSQLs() { |
239 |
return Collections.EMPTY_LIST; |
240 |
} |
241 |
|
242 |
public List<String> getPostSQLs() { |
243 |
return Collections.EMPTY_LIST; |
244 |
} |
245 |
|
246 |
public List<Object> getSQLParameters(FeatureProvider feature) { |
247 |
return this.sqlbuilder.getParameters(feature); |
248 |
} |
249 |
|
250 |
@SuppressWarnings("UseSpecificCatch") |
251 |
public void append(FeatureProvider feature) throws DataException { |
252 |
try {
|
253 |
if( this.batchSize>0 ) { |
254 |
this.addBatch(feature);
|
255 |
if( this.batchCount >= this.batchSize ) { |
256 |
this.executeBatch();
|
257 |
} |
258 |
} else {
|
259 |
Disposable theParametersDisposable = null;
|
260 |
try {
|
261 |
theParametersDisposable = this.sqlbuilder.setParameters(this.preparedStatement, feature); |
262 |
int n = JDBCUtils.executeUpdate(this.preparedStatement,this.insertSQL); |
263 |
if( n<1 ) { |
264 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
265 |
} |
266 |
} finally {
|
267 |
DisposeUtils.disposeQuietly(theParametersDisposable); |
268 |
} |
269 |
} |
270 |
} catch(Exception ex) { |
271 |
throw new RuntimeException("Can't insert feature.", ex); |
272 |
} |
273 |
} |
274 |
|
275 |
private void addBatch(FeatureProvider feature) throws SQLException { |
276 |
Disposable theParametersDisposable = this.sqlbuilder.setParameters(this.preparedStatement, feature); |
277 |
JDBCUtils.addBatch(this.preparedStatement,this.insertSQL); |
278 |
this.disposableParameters[this.batchCount++] = theParametersDisposable; |
279 |
} |
280 |
|
281 |
private void executeBatch() throws SQLException { |
282 |
int[] status = JDBCUtils.executeBatch(this.preparedStatement,this.insertSQL); |
283 |
this.clearBatch();
|
284 |
for (int n : status) { |
285 |
if( n<=Statement.EXECUTE_FAILED ) { //-3 |
286 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
287 |
} |
288 |
} |
289 |
} |
290 |
|
291 |
private void clearBatch() { |
292 |
try {
|
293 |
this.preparedStatement.clearParameters();
|
294 |
this.preparedStatement.clearBatch();
|
295 |
for (int i = 0; i < this.batchCount && i < this.disposableParameters.length; i++) { |
296 |
DisposeUtils.dispose(this.disposableParameters[i]);
|
297 |
this.disposableParameters[i] = null; |
298 |
} |
299 |
this.batchCount = 0; |
300 |
} catch (SQLException ex) { |
301 |
LOGGER.warn("Can't clear batch statement", ex);
|
302 |
} |
303 |
} |
304 |
} |