Revision 43629
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/PerformChangesOperation.java | ||
---|---|---|
20 | 20 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
21 | 21 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
22 | 22 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
23 |
import org.gvsig.tools.dispose.Disposable; |
|
24 |
import org.gvsig.tools.dispose.DisposeUtils; |
|
23 | 25 |
|
24 | 26 |
public class PerformChangesOperation extends AbstractConnectionWritableOperation { |
25 | 27 |
|
... | ... | |
63 | 65 |
} |
64 | 66 |
|
65 | 67 |
@Override |
66 |
public final Object perform(Connection conn) throws DataException {
|
|
68 |
public Object perform(Connection conn) throws DataException { |
|
67 | 69 |
if (featureTypesChanged.hasNext()) { |
68 | 70 |
FeatureStoreProvider.FeatureTypeChanged item = featureTypesChanged.next(); |
69 | 71 |
this.performUpdateTable(conn, dbName, schemaName, tableName, item.getSource(), item.getTarget()); |
... | ... | |
117 | 119 |
JDBCSQLBuilderBase sqlbuilder = buildDeleteSQL(database, schema, table, type); |
118 | 120 |
|
119 | 121 |
PreparedStatement st = null; |
122 |
Disposable paramsDisposer = null; |
|
120 | 123 |
String sql = sqlbuilder.delete().toString(); |
121 | 124 |
try { |
122 | 125 |
st = conn.prepareStatement(sql); |
123 | 126 |
while (deleteds.hasNext()) { |
124 | 127 |
FeatureReference reference = (FeatureReference) deleteds.next(); |
125 |
sqlbuilder.setParameters(st, reference); |
|
128 |
paramsDisposer = sqlbuilder.setParameters(st, reference);
|
|
126 | 129 |
int nAffected = JDBCUtils.executeUpdate(st,sql); |
127 | 130 |
if (nAffected == 0) { |
128 | 131 |
throw new JDBCUpdateWithoutChangesException( |
... | ... | |
140 | 143 |
throw new JDBCSQLException(e); |
141 | 144 |
} finally { |
142 | 145 |
JDBCUtils.closeQuietly(st); |
146 |
DisposeUtils.disposeQuietly(paramsDisposer); |
|
143 | 147 |
} |
144 | 148 |
} |
145 | 149 |
|
... | ... | |
180 | 184 |
JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(database, schema, table, type); |
181 | 185 |
|
182 | 186 |
PreparedStatement st; |
187 |
Disposable paramsDisposer = null; |
|
183 | 188 |
String sql = sqlbuilder.insert().toString(); |
184 | 189 |
try { |
185 | 190 |
st = conn.prepareStatement(sql); |
186 | 191 |
while (inserteds.hasNext()) { |
187 | 192 |
FeatureProvider feature = inserteds.next(); |
188 |
sqlbuilder.setParameters(st, feature); |
|
189 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
|
190 |
throw new JDBCExecuteSQLException( |
|
191 |
sqlbuilder.insert().toString(), |
|
192 |
null |
|
193 |
); |
|
193 |
paramsDisposer = sqlbuilder.setParameters(st, feature); |
|
194 |
try { |
|
195 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
|
196 |
throw new JDBCExecuteSQLException( |
|
197 |
sqlbuilder.insert().toString(), |
|
198 |
null |
|
199 |
); |
|
200 |
} |
|
201 |
} finally { |
|
202 |
DisposeUtils.disposeQuietly(paramsDisposer); |
|
194 | 203 |
} |
195 |
|
|
196 | 204 |
} |
197 | 205 |
} catch (JDBCExecuteSQLException ex) { |
198 | 206 |
throw ex; |
... | ... | |
248 | 256 |
JDBCSQLBuilderBase sqlbuilder = buildUpdateSQL(database, schema, table, type); |
249 | 257 |
|
250 | 258 |
PreparedStatement st = null; |
259 |
Disposable paramsDisposer = null; |
|
251 | 260 |
String sql = sqlbuilder.update().toString(); |
252 | 261 |
try { |
253 | 262 |
st = conn.prepareStatement(sql); |
254 | 263 |
while (updateds.hasNext()) { |
255 | 264 |
FeatureProvider featureProvider = (FeatureProvider) updateds.next(); |
256 |
sqlbuilder.setParameters(st, featureProvider); |
|
265 |
paramsDisposer = sqlbuilder.setParameters(st, featureProvider);
|
|
257 | 266 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
258 | 267 |
throw new JDBCUpdateWithoutChangesException(sql,null); |
259 | 268 |
} |
... | ... | |
262 | 271 |
throw new JDBCSQLException(e); |
263 | 272 |
} finally { |
264 | 273 |
JDBCUtils.closeQuietly(st); |
274 |
DisposeUtils.disposeQuietly(paramsDisposer); |
|
265 | 275 |
} |
266 | 276 |
} |
267 | 277 |
|
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 | ||
---|---|---|
4 | 4 |
import java.sql.Connection; |
5 | 5 |
import java.sql.PreparedStatement; |
6 | 6 |
import java.sql.SQLException; |
7 |
import java.util.logging.Level; |
|
8 |
import java.util.logging.Logger; |
|
9 | 7 |
import org.gvsig.fmap.dal.DataTypes; |
10 | 8 |
import org.gvsig.fmap.dal.exception.DataException; |
11 | 9 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
12 | 10 |
import org.gvsig.fmap.dal.feature.FeatureType; |
13 | 11 |
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException; |
14 | 12 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
15 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
|
16 | 13 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
17 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException; |
|
18 | 14 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
19 | 15 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
20 | 16 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
17 |
import org.gvsig.tools.dispose.Disposable; |
|
18 |
import org.gvsig.tools.dispose.DisposeUtils; |
|
21 | 19 |
|
22 | 20 |
|
23 | 21 |
public class AppendOperation { |
... | ... | |
119 | 117 |
|
120 | 118 |
public void append(FeatureProvider feature) throws DataException { |
121 | 119 |
int n; |
120 |
Disposable paramsDisposer = null; |
|
122 | 121 |
try { |
123 |
this.sqlbuilder.setParameters(this.preparedStatement, feature); |
|
122 |
paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature);
|
|
124 | 123 |
n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql); |
125 | 124 |
} catch(Exception ex) { |
126 | 125 |
throw new RuntimeException("Can't insert feature.", ex); |
126 |
} finally { |
|
127 |
DisposeUtils.disposeQuietly(paramsDisposer); |
|
127 | 128 |
} |
128 | 129 |
if( n<1 ) { |
129 | 130 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
130 | 131 |
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/JDBCSQLBuilderBase.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.dal.store.jdbc2.spi; |
2 | 2 |
|
3 | 3 |
import java.sql.PreparedStatement; |
4 |
import java.sql.SQLException; |
|
4 | 5 |
import java.util.ArrayList; |
5 | 6 |
import java.util.List; |
6 | 7 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
7 | 8 |
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
8 | 9 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
9 | 10 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
10 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
|
|
11 |
import org.gvsig.tools.service.spi.ProviderServices;
|
|
11 |
import org.gvsig.fmap.geom.Geometry;
|
|
12 |
import org.gvsig.tools.dispose.Disposable;
|
|
12 | 13 |
|
13 | 14 |
public class JDBCSQLBuilderBase extends SQLBuilderBase { |
14 | 15 |
|
... | ... | |
33 | 34 |
} |
34 | 35 |
} |
35 | 36 |
|
36 |
public void setParameters(PreparedStatement st, FeatureProvider feature) {
|
|
37 |
public Disposable setParameters(PreparedStatement st, FeatureProvider feature) {
|
|
37 | 38 |
try { |
38 | 39 |
List<Object> values = new ArrayList<>(); |
39 | 40 |
for (Parameter parameter : this.getParameters()) { |
... | ... | |
44 | 45 |
values.add(feature.get(name)); |
45 | 46 |
} |
46 | 47 |
} |
47 |
JDBCUtils.setObjects(st, values, this.geometry_support_type());
|
|
48 |
return this.setStatementParameters(st, values, this.geometry_support_type());
|
|
48 | 49 |
} catch (Exception ex) { |
49 | 50 |
String f = "unknow"; |
50 | 51 |
try { |
... | ... | |
56 | 57 |
} |
57 | 58 |
} |
58 | 59 |
|
59 |
public void setParameters(PreparedStatement st, FeatureReference reference) {
|
|
60 |
public Disposable setParameters(PreparedStatement st, FeatureReference reference) {
|
|
60 | 61 |
try { |
61 | 62 |
|
62 | 63 |
List<Object> values = new ArrayList<>(); |
... | ... | |
68 | 69 |
values.add(((FeatureReferenceProviderServices)reference).getKeyValue(name)); |
69 | 70 |
} |
70 | 71 |
} |
71 |
JDBCUtils.setObjects(st, values, this.geometry_support_type());
|
|
72 |
return this.setStatementParameters(st, values, this.geometry_support_type());
|
|
72 | 73 |
} catch (Exception ex) { |
73 | 74 |
String f = "unknow"; |
74 | 75 |
try { |
... | ... | |
80 | 81 |
} |
81 | 82 |
} |
82 | 83 |
|
84 |
public Disposable setStatementParameters( |
|
85 |
PreparedStatement st, |
|
86 |
List values, |
|
87 |
GeometrySupportType geometrySupportType) throws SQLException { |
|
88 |
|
|
89 |
if (values == null) { |
|
90 |
return new Disposable() { |
|
91 |
@Override |
|
92 |
public void dispose() { |
|
93 |
} |
|
94 |
}; |
|
95 |
} |
|
96 |
try { |
|
97 |
byte[] bytes; |
|
98 |
int columnIndex = 1; |
|
99 |
for (Object value : values) { |
|
100 |
if (value instanceof Geometry) { |
|
101 |
switch(geometrySupportType) { |
|
102 |
case WKT: |
|
103 |
value = ((Geometry) value).convertToWKT(); |
|
104 |
st.setObject(columnIndex, value); |
|
105 |
break; |
|
106 |
case NATIVE: |
|
107 |
case WKB: |
|
108 |
bytes = ((Geometry) value).convertToWKB(); |
|
109 |
st.setBytes(columnIndex, bytes); |
|
110 |
break; |
|
111 |
case EWKB: |
|
112 |
bytes = ((Geometry) value).convertToEWKB(); |
|
113 |
st.setBytes(columnIndex, bytes); |
|
114 |
break; |
|
115 |
} |
|
116 |
} else { |
|
117 |
st.setObject(columnIndex, value); |
|
118 |
} |
|
119 |
columnIndex++; |
|
120 |
} |
|
121 |
return new Disposable() { |
|
122 |
@Override |
|
123 |
public void dispose() { |
|
124 |
} |
|
125 |
}; |
|
126 |
} catch(Exception ex) { |
|
127 |
throw new SQLException("Can't set values for the prepared statement.", ex); |
|
128 |
} |
|
129 |
} |
|
83 | 130 |
} |
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/impl/ResulSetControlerBase.java | ||
---|---|---|
15 | 15 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
16 | 16 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
17 | 17 |
import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler; |
18 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
19 |
import org.gvsig.tools.dispose.Disposable; |
|
20 |
import org.gvsig.tools.dispose.DisposeUtils; |
|
18 | 21 |
import org.slf4j.Logger; |
19 | 22 |
import org.slf4j.LoggerFactory; |
20 | 23 |
|
... | ... | |
71 | 74 |
return this.sql; |
72 | 75 |
} |
73 | 76 |
|
77 |
@Override |
|
74 | 78 |
public FeatureAttributeDescriptor[] getColumns() { |
75 | 79 |
return this.columns; |
76 | 80 |
} |
... | ... | |
174 | 178 |
ResultSet rs = null; |
175 | 179 |
Connection conn = null; |
176 | 180 |
PreparedStatement st = null; |
181 |
Disposable paramsDisposer = null; |
|
177 | 182 |
try { |
178 | 183 |
conn = helper.getConnection(); |
179 | 184 |
conn.setAutoCommit(false); |
180 | 185 |
st = conn.prepareStatement(sql); |
181 | 186 |
|
182 |
JDBCUtils.setObjects(st, values, helper.getGeometrySupportType()); |
|
187 |
JDBCSQLBuilderBase sqlbuilder = helper.createSQLBuilder(); |
|
188 |
paramsDisposer = sqlbuilder.setStatementParameters(st, values, helper.getGeometrySupportType()); |
|
183 | 189 |
|
184 | 190 |
if (fetchSize > 0) { |
185 | 191 |
// See parameter "SelectMethod" of SQL Server Connection Properties |
... | ... | |
190 | 196 |
if (fetchSize > 0) { |
191 | 197 |
rs.setFetchSize(fetchSize); |
192 | 198 |
} |
199 |
ResultSetEntryBase rsentry = new ResultSetEntryBase(rs, sql, columns); |
|
200 |
return rsentry; |
|
193 | 201 |
|
194 | 202 |
} catch (SQLException e) { |
195 | 203 |
JDBCUtils.closeQuietly(rs); |
196 | 204 |
JDBCUtils.closeQuietly(st); |
197 | 205 |
JDBCUtils.closeQuietly(conn); |
198 | 206 |
throw new JDBCExecutePreparedSQLException(sql, values, e); |
207 |
} finally { |
|
208 |
DisposeUtils.disposeQuietly(paramsDisposer); |
|
199 | 209 |
} |
200 |
ResultSetEntryBase rsentry = new ResultSetEntryBase(rs, sql, columns); |
|
201 |
return rsentry; |
|
202 | 210 |
} |
203 | 211 |
|
204 | 212 |
@Override |
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/JDBCUtils.java | ||
---|---|---|
92 | 92 |
logger.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e); |
93 | 93 |
} |
94 | 94 |
} |
95 |
|
|
96 |
public static void setObjects( |
|
97 |
PreparedStatement st, |
|
98 |
List values, |
|
99 |
GeometrySupportType geometrySupportType) throws SQLException { |
|
100 |
|
|
101 |
if (values == null) { |
|
102 |
return; |
|
103 |
} |
|
104 |
try { |
|
105 |
byte[] bytes; |
|
106 |
int columnIndex = 1; |
|
107 |
for (Object value : values) { |
|
108 |
if (value instanceof Geometry) { |
|
109 |
switch(geometrySupportType) { |
|
110 |
case WKT: |
|
111 |
value = ((Geometry) value).convertToWKT(); |
|
112 |
st.setObject(columnIndex, value); |
|
113 |
break; |
|
114 |
case NATIVE: |
|
115 |
case WKB: |
|
116 |
bytes = ((Geometry) value).convertToWKB(); |
|
117 |
st.setBytes(columnIndex, bytes); |
|
118 |
break; |
|
119 |
case EWKB: |
|
120 |
bytes = ((Geometry) value).convertToEWKB(); |
|
121 |
st.setBytes(columnIndex, bytes); |
|
122 |
break; |
|
123 |
} |
|
124 |
} else { |
|
125 |
st.setObject(columnIndex, value); |
|
126 |
} |
|
127 |
columnIndex++; |
|
128 |
} |
|
129 |
} catch(Exception ex) { |
|
130 |
throw new SQLException("Can't set values for the prepared statement.", ex); |
|
131 |
} |
|
132 |
} |
|
133 | 95 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialSQLBuilder.java | ||
---|---|---|
9 | 9 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
10 | 10 |
import org.gvsig.fmap.dal.feature.FeatureType; |
11 | 11 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
12 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
|
13 | 12 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
14 | 13 |
import org.gvsig.fmap.geom.Geometry; |
14 |
import org.gvsig.tools.dispose.Disposable; |
|
15 | 15 |
|
16 | 16 |
public class H2SpatialSQLBuilder extends JDBCSQLBuilderBase { |
17 | 17 |
|
... | ... | |
207 | 207 |
|
208 | 208 |
public class H2SpatialSelectBuilderBase extends SelectBuilderBase { |
209 | 209 |
|
210 |
@Override |
|
210 | 211 |
protected boolean isValid(StringBuilder message) { |
211 | 212 |
if( message == null ) { |
212 | 213 |
message = new StringBuilder(); |
... | ... | |
321 | 322 |
} |
322 | 323 |
|
323 | 324 |
@Override |
324 |
public void setParameters(PreparedStatement st, FeatureProvider feature) {
|
|
325 |
public Disposable setParameters(PreparedStatement st, FeatureProvider feature) {
|
|
325 | 326 |
try { |
326 | 327 |
FeatureType featureType = feature.getType(); |
327 | 328 |
List<Object> values = new ArrayList<>(); |
... | ... | |
353 | 354 |
} |
354 | 355 |
} |
355 | 356 |
} |
356 |
JDBCUtils.setObjects(st, values, this.geometry_support_type());
|
|
357 |
return this.setStatementParameters(st, values, this.geometry_support_type());
|
|
357 | 358 |
} catch (Exception ex) { |
358 | 359 |
String f = "unknow"; |
359 | 360 |
try { |
Also available in: Unified diff