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 | 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 | 43020 | 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 | |||
43 | 44058 | jjdelcerro | this.sqlbuilder.insert().table()
|
44 | .database(this.table.getDatabase())
|
||
45 | .schema(this.table.getSchema())
|
||
46 | .name(this.table.getTable());
|
||
47 | 43020 | jjdelcerro | 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 | 43093 | jjdelcerro | sqlbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
54 | sqlbuilder.parameter().value(attr.getSRS()) |
||
55 | 43020 | jjdelcerro | ) |
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 | 43377 | jjdelcerro | this.connection.setAutoCommit(false); |
68 | 43650 | jjdelcerro | 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 | 43020 | jjdelcerro | |
72 | } catch (SQLException ex) { |
||
73 | throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex); |
||
74 | } |
||
75 | |||
76 | } |
||
77 | |||
78 | 43650 | jjdelcerro | @Override
|
79 | 43377 | jjdelcerro | protected void clean() { |
80 | 43020 | jjdelcerro | JDBCUtils.closeQuietly(this.preparedStatement);
|
81 | 43377 | jjdelcerro | this.helper.closeConnection(this.connection); |
82 | 43020 | jjdelcerro | this.connection = null; |
83 | this.preparedStatement = null; |
||
84 | this.sqlbuilder = null; |
||
85 | 43377 | jjdelcerro | this.sql = null; |
86 | 43020 | jjdelcerro | } |
87 | |||
88 | 43650 | jjdelcerro | @Override
|
89 | 43377 | jjdelcerro | public void end() { |
90 | try {
|
||
91 | this.connection.commit();
|
||
92 | 43650 | jjdelcerro | 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 | 43377 | jjdelcerro | } 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 | 43020 | jjdelcerro | } |
105 | |||
106 | 43650 | jjdelcerro | @Override
|
107 | 43377 | jjdelcerro | public void abort() { |
108 | try {
|
||
109 | this.connection.rollback();
|
||
110 | 43650 | jjdelcerro | 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 | 43377 | jjdelcerro | } catch (SQLException ex) { |
114 | } |
||
115 | clean(); |
||
116 | } |
||
117 | |||
118 | 43650 | jjdelcerro | @Override
|
119 | 43020 | jjdelcerro | public void append(FeatureProvider feature) throws DataException { |
120 | 43377 | jjdelcerro | int n;
|
121 | 43629 | jjdelcerro | Disposable paramsDisposer = null;
|
122 | 43020 | jjdelcerro | try {
|
123 | 43629 | jjdelcerro | paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature); |
124 | 43377 | jjdelcerro | n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql); |
125 | } catch(Exception ex) { |
||
126 | throw new RuntimeException("Can't insert feature.", ex); |
||
127 | 43629 | jjdelcerro | } finally {
|
128 | DisposeUtils.disposeQuietly(paramsDisposer); |
||
129 | 43020 | jjdelcerro | } |
130 | 43377 | jjdelcerro | if( n<1 ) { |
131 | throw new RuntimeException("Can't insert feature (n="+n+")."); |
||
132 | } |
||
133 | 43020 | jjdelcerro | } |
134 | } |