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.h2 / src / main / java / org / gvsig / fmap / dal / store / h2 / operations / H2SpatialAppendOperation.java @ 44058
History | View | Annotate | Download (4.75 KB)
1 |
|
---|---|
2 |
package org.gvsig.fmap.dal.store.h2.operations; |
3 |
|
4 |
import java.sql.PreparedStatement; |
5 |
import java.sql.SQLException; |
6 |
import org.gvsig.fmap.dal.DataTypes; |
7 |
import org.gvsig.fmap.dal.exception.DataException; |
8 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
9 |
import org.gvsig.fmap.dal.feature.FeatureType; |
10 |
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException; |
11 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
12 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
13 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
14 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
15 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
16 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.AppendOperation; |
17 |
import org.gvsig.tools.dispose.Disposable; |
18 |
import org.gvsig.tools.dispose.DisposeUtils; |
19 |
|
20 |
|
21 |
@SuppressWarnings("UseSpecificCatch") |
22 |
public class H2SpatialAppendOperation extends AppendOperation { |
23 |
|
24 |
public H2SpatialAppendOperation(
|
25 |
JDBCHelper helper, |
26 |
TableReference table, |
27 |
FeatureType type |
28 |
) { |
29 |
super(helper, table, type);
|
30 |
} |
31 |
|
32 |
@Override
|
33 |
public void begin() throws DataException { |
34 |
if (this.sqlbuilder != null) { |
35 |
throw new AlreadyEditingException(this.helper.getSourceId()); |
36 |
} |
37 |
|
38 |
try {
|
39 |
this.connection = this.helper.getConnectionWritable(); |
40 |
|
41 |
this.sqlbuilder = this.helper.createSQLBuilder(); |
42 |
|
43 |
this.sqlbuilder.insert().table()
|
44 |
.database(this.table.getDatabase())
|
45 |
.schema(this.table.getSchema())
|
46 |
.name(this.table.getTable());
|
47 |
for (FeatureAttributeDescriptor attr : type) {
|
48 |
if( attr.isAutomatic() ) {
|
49 |
continue;
|
50 |
} |
51 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
52 |
this.sqlbuilder.insert().column().name(attr.getName()).with_value(
|
53 |
sqlbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
54 |
sqlbuilder.parameter().value(attr.getSRS()) |
55 |
) |
56 |
); |
57 |
} else {
|
58 |
this.sqlbuilder.insert().column().name(attr.getName()).with_value(
|
59 |
sqlbuilder.parameter(attr.getName()).as_variable() |
60 |
); |
61 |
} |
62 |
} |
63 |
|
64 |
PreparedStatement st;
|
65 |
this.sql = this.sqlbuilder.insert().toString(); |
66 |
this.preparedStatement = this.connection.prepareStatement(sql); |
67 |
this.connection.setAutoCommit(false); |
68 |
JDBCUtils.execute(this.connection, "SET LOG 1"); |
69 |
JDBCUtils.execute(this.connection, "SET LOCK_MODE 1"); |
70 |
JDBCUtils.execute(this.connection, "SET UNDO_LOG 0"); |
71 |
|
72 |
} catch (SQLException ex) { |
73 |
throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex); |
74 |
} |
75 |
|
76 |
} |
77 |
|
78 |
@Override
|
79 |
protected void clean() { |
80 |
JDBCUtils.closeQuietly(this.preparedStatement);
|
81 |
this.helper.closeConnection(this.connection); |
82 |
this.connection = null; |
83 |
this.preparedStatement = null; |
84 |
this.sqlbuilder = null; |
85 |
this.sql = null; |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public void end() { |
90 |
try {
|
91 |
this.connection.commit();
|
92 |
JDBCUtils.execute(this.connection, "SET LOG 2"); |
93 |
JDBCUtils.execute(this.connection, "SET LOCK_MODE 3"); |
94 |
JDBCUtils.execute(this.connection, "SET UNDO_LOG 1"); |
95 |
} catch (SQLException ex) { |
96 |
try {
|
97 |
this.connection.rollback();
|
98 |
} catch (SQLException ex1) { |
99 |
} |
100 |
throw new RuntimeException("Can't commit transaction", ex); |
101 |
} finally {
|
102 |
clean(); |
103 |
} |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void abort() { |
108 |
try {
|
109 |
this.connection.rollback();
|
110 |
JDBCUtils.execute(this.connection, "SET LOG 2"); |
111 |
JDBCUtils.execute(this.connection, "SET LOCK_MODE 3"); |
112 |
JDBCUtils.execute(this.connection, "SET UNDO_LOG 1"); |
113 |
} catch (SQLException ex) { |
114 |
} |
115 |
clean(); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public void append(FeatureProvider feature) throws DataException { |
120 |
int n;
|
121 |
Disposable paramsDisposer = null;
|
122 |
try {
|
123 |
paramsDisposer = 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 |
} finally {
|
128 |
DisposeUtils.disposeQuietly(paramsDisposer); |
129 |
} |
130 |
if( n<1 ) { |
131 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
132 |
} |
133 |
} |
134 |
} |