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