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 @ 43020
History | View | Annotate | Download (4.38 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.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.JDBCExecuteSQLException; |
14 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
15 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException; |
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.spi.JDBCSQLBuilderBase; |
19 |
|
20 |
|
21 |
public class AppendOperation { |
22 |
|
23 |
protected Connection connection = null; |
24 |
protected final JDBCHelper helper; |
25 |
protected final String database; |
26 |
protected final String schema; |
27 |
protected final String table; |
28 |
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 |
String database,
|
37 |
String schema,
|
38 |
String table,
|
39 |
FeatureType type |
40 |
) { |
41 |
this.helper = helper;
|
42 |
this.database = database;
|
43 |
this.schema = schema;
|
44 |
this.table = table;
|
45 |
this.type = type;
|
46 |
} |
47 |
|
48 |
public void begin() throws DataException { |
49 |
if (this.sqlbuilder != null) { |
50 |
throw new AlreadyEditingException(this.helper.getSourceId()); |
51 |
} |
52 |
|
53 |
try {
|
54 |
this.connection = this.helper.getConnection(); |
55 |
|
56 |
this.sqlbuilder = this.helper.createSQLBuilder(); |
57 |
|
58 |
this.sqlbuilder.insert().table().database(this.database).schema(this.schema).name(this.table); |
59 |
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 |
sqlbuilder.parameter(attr.getName()).as_geometry().srs( |
66 |
sqlbuilder.parameter(attr.getSRS()) |
67 |
) |
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 |
|
80 |
} catch (SQLException ex) { |
81 |
throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex); |
82 |
} |
83 |
|
84 |
} |
85 |
|
86 |
public void end() { |
87 |
JDBCUtils.closeQuietly(this.connection);
|
88 |
JDBCUtils.closeQuietly(this.preparedStatement);
|
89 |
this.connection = null; |
90 |
this.preparedStatement = null; |
91 |
this.sqlbuilder = null; |
92 |
this.sql = null; |
93 |
} |
94 |
|
95 |
public void abort() { |
96 |
this.end();
|
97 |
} |
98 |
|
99 |
public void append(FeatureProvider feature) throws DataException { |
100 |
try {
|
101 |
this.connection.setAutoCommit(false); |
102 |
try {
|
103 |
this.sqlbuilder.setParameters(this.preparedStatement, feature); |
104 |
int n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql); |
105 |
if ( n > 0) { |
106 |
this.connection.commit();
|
107 |
return;
|
108 |
} |
109 |
this.connection.rollback();
|
110 |
} catch(Exception ex) { |
111 |
try {
|
112 |
this.connection.rollback();
|
113 |
} catch (Exception e1) { |
114 |
throw new JDBCTransactionRollbackException(e1, ex); |
115 |
} |
116 |
throw new JDBCExecuteSQLException(this.sql,ex); |
117 |
} |
118 |
throw new JDBCExecuteSQLException(this.sql,null); |
119 |
|
120 |
} catch (JDBCExecuteSQLException ex) {
|
121 |
throw ex;
|
122 |
} catch (Exception ex) { |
123 |
throw new JDBCExecuteSQLException(this.sql,ex); |
124 |
} |
125 |
} |
126 |
} |