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