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