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