root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / main / java / org / gvsig / postgresql / dal / operations / PostgreSQLPerformChangesOperation.java @ 363
History | View | Annotate | Download (4.86 KB)
1 |
package org.gvsig.postgresql.dal.operations; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.PreparedStatement; |
5 |
import java.sql.SQLException; |
6 |
import java.sql.Statement; |
7 |
import java.util.Iterator; |
8 |
import org.gvsig.fmap.dal.exception.DataException; |
9 |
import org.gvsig.fmap.dal.feature.FeatureType; |
10 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
11 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
12 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
13 |
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
14 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
15 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
16 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
17 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
18 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
20 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.PerformChangesOperation; |
21 |
import org.gvsig.postgresql.dal.PostgreSQLHelper; |
22 |
|
23 |
public class PostgreSQLPerformChangesOperation extends PerformChangesOperation { |
24 |
|
25 |
|
26 |
public PostgreSQLPerformChangesOperation(JDBCHelper helper) {
|
27 |
this(helper, null, null, null, null, null, null, null, null); |
28 |
} |
29 |
|
30 |
public PostgreSQLPerformChangesOperation(JDBCHelper helper,
|
31 |
String dbName,
|
32 |
String schemaName,
|
33 |
String tableName,
|
34 |
FeatureType featureType, |
35 |
Iterator<FeatureReferenceProviderServices> deleteds,
|
36 |
Iterator<FeatureProvider> inserteds,
|
37 |
Iterator<FeatureProvider> updateds,
|
38 |
Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged) {
|
39 |
super(helper);
|
40 |
this.dbName = dbName;
|
41 |
this.deleteds = deleteds;
|
42 |
this.inserteds = inserteds;
|
43 |
this.updateds = updateds;
|
44 |
this.schemaName = schemaName;
|
45 |
this.tableName = tableName;
|
46 |
this.featureType = featureType;
|
47 |
this.featureTypesChanged = featureTypesChanged;
|
48 |
} |
49 |
|
50 |
private PostgreSQLHelper getHelper() {
|
51 |
return (PostgreSQLHelper) this.helper; |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public void performInserts(Connection conn, |
56 |
String database,
|
57 |
String schema,
|
58 |
String table,
|
59 |
FeatureType type, |
60 |
Iterator<FeatureProvider> inserteds) throws DataException { |
61 |
JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(database, schema, table, type); |
62 |
|
63 |
PreparedStatement st;
|
64 |
String sql = sqlbuilder.insert().toString();
|
65 |
try {
|
66 |
st = conn.prepareStatement(sql); |
67 |
while (inserteds.hasNext()) {
|
68 |
FeatureProvider feature = inserteds.next(); |
69 |
getHelper().setPreparedStatementParameters(st, sqlbuilder, type, feature); |
70 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
71 |
throw new JDBCExecuteSQLException( |
72 |
sqlbuilder.insert().toString(), |
73 |
null
|
74 |
); |
75 |
} |
76 |
|
77 |
} |
78 |
} catch (JDBCExecuteSQLException ex) {
|
79 |
throw ex;
|
80 |
} catch (Exception ex) { |
81 |
throw new JDBCExecuteSQLException(sql,ex); |
82 |
} |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public void performUpdates(Connection conn, |
87 |
String database,
|
88 |
String schema,
|
89 |
String table,
|
90 |
FeatureType type, |
91 |
Iterator<FeatureProvider> updateds) throws DataException { |
92 |
|
93 |
JDBCSQLBuilderBase sqlbuilder = buildUpdateSQL(database, schema, table, type); |
94 |
|
95 |
PreparedStatement st = null; |
96 |
String sql = sqlbuilder.update().toString();
|
97 |
try {
|
98 |
st = conn.prepareStatement(sql); |
99 |
while (updateds.hasNext()) {
|
100 |
FeatureProvider feature = (FeatureProvider) updateds.next(); |
101 |
getHelper().setPreparedStatementParameters(st, sqlbuilder, type, feature); |
102 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
103 |
throw new JDBCUpdateWithoutChangesException(sql,null); |
104 |
} |
105 |
} |
106 |
} catch (SQLException e) { |
107 |
throw new JDBCSQLException(e); |
108 |
} finally {
|
109 |
JDBCUtils.closeQuietly(st); |
110 |
} |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void performUpdateTable(Connection conn, |
115 |
String database,
|
116 |
String schema,
|
117 |
String table,
|
118 |
FeatureType original, |
119 |
FeatureType target) throws DataException {
|
120 |
|
121 |
SQLBuilderBase sqlbuilder = buildUpdateTableSQL(database, schema, table, original, target); |
122 |
Statement st = null; |
123 |
try {
|
124 |
st = conn.createStatement(); |
125 |
for (String sql : sqlbuilder.alter_table().toStrings()) { |
126 |
JDBCUtils.execute(st, sql); |
127 |
} |
128 |
} catch (SQLException e) { |
129 |
throw new JDBCSQLException(e); |
130 |
} finally {
|
131 |
JDBCUtils.closeQuietly(st); |
132 |
} |
133 |
} |
134 |
|
135 |
} |