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 / PerformChangesOperation.java @ 44198
History | View | Annotate | Download (18.8 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
---|---|---|---|
2 | |||
3 | import java.sql.Connection; |
||
4 | import java.sql.PreparedStatement; |
||
5 | import java.sql.SQLException; |
||
6 | import java.sql.Statement; |
||
7 | 43687 | jjdelcerro | import java.util.ArrayList; |
8 | 43020 | jjdelcerro | import java.util.Iterator; |
9 | 43687 | jjdelcerro | import java.util.List; |
10 | import org.apache.commons.collections.CollectionUtils; |
||
11 | 43650 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
12 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
13 | import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
||
14 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.DataTypes; |
15 | import org.gvsig.fmap.dal.exception.DataException; |
||
16 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
17 | 43479 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureReference; |
18 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
19 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
20 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
21 | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
||
22 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
23 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
||
24 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
25 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
||
26 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
27 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
28 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
29 | 44198 | jjdelcerro | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
30 | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
||
31 | 43629 | jjdelcerro | import org.gvsig.tools.dispose.Disposable; |
32 | import org.gvsig.tools.dispose.DisposeUtils; |
||
33 | 43020 | jjdelcerro | |
34 | 43377 | jjdelcerro | public class PerformChangesOperation extends AbstractConnectionWritableOperation { |
35 | 43020 | jjdelcerro | |
36 | 44058 | jjdelcerro | protected TableReference table;
|
37 | 43355 | jjdelcerro | protected FeatureType featureType;
|
38 | protected Iterator<FeatureReferenceProviderServices> deleteds; |
||
39 | protected Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged; |
||
40 | protected Iterator<FeatureProvider> updateds; |
||
41 | protected Iterator<FeatureProvider> inserteds; |
||
42 | 43020 | jjdelcerro | |
43 | 43355 | jjdelcerro | protected boolean typeChanged = false; |
44 | 43020 | jjdelcerro | |
45 | public PerformChangesOperation(JDBCHelper helper) {
|
||
46 | 44058 | jjdelcerro | this(helper, null, null, null, null, null, null); |
47 | 43020 | jjdelcerro | } |
48 | |||
49 | public PerformChangesOperation(JDBCHelper helper,
|
||
50 | 44058 | jjdelcerro | TableReference table, |
51 | 43020 | jjdelcerro | FeatureType featureType, |
52 | Iterator<FeatureReferenceProviderServices> deleteds,
|
||
53 | Iterator<FeatureProvider> inserteds,
|
||
54 | Iterator<FeatureProvider> updateds,
|
||
55 | Iterator<FeatureStoreProvider.FeatureTypeChanged> featureTypesChanged) {
|
||
56 | super(helper);
|
||
57 | this.deleteds = deleteds;
|
||
58 | this.inserteds = inserteds;
|
||
59 | this.updateds = updateds;
|
||
60 | 44058 | jjdelcerro | this.table = table;
|
61 | 43020 | jjdelcerro | this.featureType = featureType;
|
62 | this.featureTypesChanged = featureTypesChanged;
|
||
63 | } |
||
64 | |||
65 | public boolean isTypeChanged() { |
||
66 | return typeChanged;
|
||
67 | } |
||
68 | 43377 | jjdelcerro | |
69 | 43020 | jjdelcerro | @Override
|
70 | 43629 | jjdelcerro | public Object perform(Connection conn) throws DataException { |
71 | 43020 | jjdelcerro | if (featureTypesChanged.hasNext()) {
|
72 | FeatureStoreProvider.FeatureTypeChanged item = featureTypesChanged.next(); |
||
73 | 44058 | jjdelcerro | this.performUpdateTable(conn, table, item.getSource(), item.getTarget());
|
74 | 43020 | jjdelcerro | typeChanged = true;
|
75 | } else {
|
||
76 | typeChanged = false;
|
||
77 | } |
||
78 | if (deleteds.hasNext()) {
|
||
79 | 44058 | jjdelcerro | performDeletes(conn, table, featureType, deleteds); |
80 | 43020 | jjdelcerro | } |
81 | if (updateds.hasNext()) {
|
||
82 | 44058 | jjdelcerro | performUpdates(conn, table, featureType, updateds); |
83 | 43020 | jjdelcerro | } |
84 | if (inserteds.hasNext()) {
|
||
85 | 44058 | jjdelcerro | performInserts(conn, table, featureType, inserteds); |
86 | 43020 | jjdelcerro | } |
87 | return true; |
||
88 | } |
||
89 | |||
90 | 43362 | jjdelcerro | protected JDBCSQLBuilderBase buildDeleteSQL(
|
91 | 44058 | jjdelcerro | TableReference table, |
92 | 43362 | jjdelcerro | FeatureType type |
93 | ) { |
||
94 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
95 | 44198 | jjdelcerro | ExpressionBuilder expbuilder = sqlbuilder.expression(); |
96 | |||
97 | 44058 | jjdelcerro | sqlbuilder.delete().table() |
98 | .database(this.table.getDatabase())
|
||
99 | .schema(this.table.getSchema())
|
||
100 | .name(this.table.getTable());
|
||
101 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : type) {
|
102 | if (attr.isPrimaryKey()) {
|
||
103 | sqlbuilder.delete().where().and( |
||
104 | 44198 | jjdelcerro | expbuilder.eq( |
105 | expbuilder.column(attr.getName()), |
||
106 | expbuilder.parameter(attr.getName()).as_variable() |
||
107 | 43020 | jjdelcerro | ) |
108 | ); |
||
109 | } |
||
110 | } |
||
111 | if (!sqlbuilder.delete().has_where() ) {
|
||
112 | throw new RuntimeException("Operation requires missing pk"); |
||
113 | } |
||
114 | 43362 | jjdelcerro | return sqlbuilder;
|
115 | } |
||
116 | 43020 | jjdelcerro | |
117 | 43362 | jjdelcerro | public void performDeletes(Connection conn, |
118 | 44058 | jjdelcerro | TableReference table, |
119 | 43362 | jjdelcerro | FeatureType type, |
120 | Iterator<FeatureReferenceProviderServices> deleteds) throws DataException { |
||
121 | |||
122 | 44058 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = buildDeleteSQL(table, type); |
123 | 43362 | jjdelcerro | |
124 | 43020 | jjdelcerro | PreparedStatement st = null; |
125 | 43629 | jjdelcerro | Disposable paramsDisposer = null;
|
126 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
127 | Variable.class, |
||
128 | PROP_TABLE, table |
||
129 | ); |
||
130 | 43020 | jjdelcerro | String sql = sqlbuilder.delete().toString();
|
131 | try {
|
||
132 | st = conn.prepareStatement(sql); |
||
133 | while (deleteds.hasNext()) {
|
||
134 | 43479 | jjdelcerro | FeatureReference reference = (FeatureReference) deleteds.next(); |
135 | 43629 | jjdelcerro | paramsDisposer = sqlbuilder.setParameters(st, reference); |
136 | 43020 | jjdelcerro | int nAffected = JDBCUtils.executeUpdate(st,sql);
|
137 | if (nAffected == 0) { |
||
138 | throw new JDBCUpdateWithoutChangesException( |
||
139 | sqlbuilder.delete().toString(), |
||
140 | null
|
||
141 | ); |
||
142 | } |
||
143 | if (nAffected > 1) { |
||
144 | 44058 | jjdelcerro | LOGGER.warn("Remove statement affectst to {} rows ( {} )",
|
145 | 43020 | jjdelcerro | nAffected, sql |
146 | ); |
||
147 | } |
||
148 | } |
||
149 | } catch (SQLException e) { |
||
150 | throw new JDBCSQLException(e); |
||
151 | } finally {
|
||
152 | JDBCUtils.closeQuietly(st); |
||
153 | 43629 | jjdelcerro | DisposeUtils.disposeQuietly(paramsDisposer); |
154 | 43020 | jjdelcerro | } |
155 | } |
||
156 | 43362 | jjdelcerro | |
157 | protected JDBCSQLBuilderBase buildInsertSQL(
|
||
158 | 44058 | jjdelcerro | TableReference table, |
159 | 43362 | jjdelcerro | FeatureType type |
160 | ) { |
||
161 | 43020 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
162 | 44198 | jjdelcerro | ExpressionBuilder expbuilder = sqlbuilder.expression(); |
163 | 43020 | jjdelcerro | |
164 | 44058 | jjdelcerro | sqlbuilder.insert().table() |
165 | .database(this.table.getDatabase())
|
||
166 | .schema(this.table.getSchema())
|
||
167 | .name(this.table.getTable());
|
||
168 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : type) {
|
169 | 43362 | jjdelcerro | if( attr.isAutomatic() ) {
|
170 | continue;
|
||
171 | } |
||
172 | 43020 | jjdelcerro | if (attr.getType() == DataTypes.GEOMETRY) {
|
173 | sqlbuilder.insert().column().name(attr.getName()).with_value( |
||
174 | 44198 | jjdelcerro | expbuilder.parameter(attr.getName()).as_geometry_variable() |
175 | .srs( |
||
176 | expbuilder.parameter().value( |
||
177 | attr.getSRS()).as_constant() |
||
178 | ) |
||
179 | 43020 | jjdelcerro | ); |
180 | } else {
|
||
181 | sqlbuilder.insert().column().name(attr.getName()).with_value( |
||
182 | 44198 | jjdelcerro | expbuilder.parameter(attr.getName()) |
183 | 43020 | jjdelcerro | ); |
184 | } |
||
185 | } |
||
186 | 43362 | jjdelcerro | return sqlbuilder;
|
187 | } |
||
188 | 43020 | jjdelcerro | |
189 | 43362 | jjdelcerro | public void performInserts(Connection conn, |
190 | 44058 | jjdelcerro | TableReference table, |
191 | 43362 | jjdelcerro | FeatureType type, |
192 | Iterator<FeatureProvider> inserteds) throws DataException { |
||
193 | 44058 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(table, type); |
194 | 43362 | jjdelcerro | |
195 | 43020 | jjdelcerro | PreparedStatement st;
|
196 | 43650 | jjdelcerro | Disposable paramsDisposer; |
197 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
198 | Variable.class, |
||
199 | PROP_FEATURE_TYPE, type, |
||
200 | PROP_TABLE, table |
||
201 | ); |
||
202 | 43020 | jjdelcerro | String sql = sqlbuilder.insert().toString();
|
203 | try {
|
||
204 | st = conn.prepareStatement(sql); |
||
205 | while (inserteds.hasNext()) {
|
||
206 | FeatureProvider feature = inserteds.next(); |
||
207 | 43629 | jjdelcerro | paramsDisposer = sqlbuilder.setParameters(st, feature); |
208 | try {
|
||
209 | if (JDBCUtils.executeUpdate(st,sql) == 0) { |
||
210 | throw new JDBCExecuteSQLException( |
||
211 | sqlbuilder.insert().toString(), |
||
212 | null
|
||
213 | ); |
||
214 | } |
||
215 | } finally {
|
||
216 | DisposeUtils.disposeQuietly(paramsDisposer); |
||
217 | 43020 | jjdelcerro | } |
218 | } |
||
219 | } catch (JDBCExecuteSQLException ex) {
|
||
220 | throw ex;
|
||
221 | } catch (Exception ex) { |
||
222 | throw new JDBCExecuteSQLException(sql,ex); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | 43362 | jjdelcerro | protected JDBCSQLBuilderBase buildUpdateSQL(
|
227 | 44058 | jjdelcerro | TableReference table, |
228 | 43362 | jjdelcerro | FeatureType type |
229 | ) { |
||
230 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
231 | 44198 | jjdelcerro | ExpressionBuilder expbuilder = sqlbuilder.expression(); |
232 | 43020 | jjdelcerro | |
233 | 44058 | jjdelcerro | sqlbuilder.update().table() |
234 | .database(this.table.getDatabase())
|
||
235 | .schema(this.table.getSchema())
|
||
236 | .name(this.table.getTable());
|
||
237 | 43020 | jjdelcerro | for (FeatureAttributeDescriptor attr : type) {
|
238 | if (attr.isPrimaryKey()) {
|
||
239 | sqlbuilder.update().where().and( |
||
240 | 44198 | jjdelcerro | expbuilder.eq( |
241 | expbuilder.column(attr.getName()), |
||
242 | expbuilder.parameter(attr.getName()).as_variable() |
||
243 | 43020 | jjdelcerro | ) |
244 | ); |
||
245 | } if ( ! attr.isAutomatic() && !attr.isReadOnly() ) {
|
||
246 | if (attr.getType() == DataTypes.GEOMETRY) {
|
||
247 | sqlbuilder.update().column().name(attr.getName()).with_value( |
||
248 | 44198 | jjdelcerro | expbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
249 | expbuilder.parameter().value(attr.getSRS()).as_constant() |
||
250 | 43020 | jjdelcerro | ) |
251 | ); |
||
252 | } else {
|
||
253 | sqlbuilder.update().column().name(attr.getName()).with_value( |
||
254 | 44198 | jjdelcerro | expbuilder.parameter(attr.getName()).as_variable() |
255 | 43020 | jjdelcerro | ); |
256 | } |
||
257 | } |
||
258 | } |
||
259 | if (!sqlbuilder.update().has_where() ) {
|
||
260 | throw new RuntimeException("Operation requires missing pk"); |
||
261 | } |
||
262 | 43362 | jjdelcerro | return sqlbuilder;
|
263 | } |
||
264 | |||
265 | public void performUpdates(Connection conn, |
||
266 | 44058 | jjdelcerro | TableReference table, |
267 | 43362 | jjdelcerro | FeatureType type, |
268 | Iterator<FeatureProvider> updateds) throws DataException { |
||
269 | 43020 | jjdelcerro | |
270 | 44058 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = buildUpdateSQL(table, type); |
271 | 43362 | jjdelcerro | |
272 | 43020 | jjdelcerro | PreparedStatement st = null; |
273 | 43629 | jjdelcerro | Disposable paramsDisposer = null;
|
274 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
275 | Variable.class, |
||
276 | PROP_FEATURE_TYPE, type, |
||
277 | PROP_TABLE, table |
||
278 | ); |
||
279 | 43020 | jjdelcerro | String sql = sqlbuilder.update().toString();
|
280 | try {
|
||
281 | st = conn.prepareStatement(sql); |
||
282 | while (updateds.hasNext()) {
|
||
283 | FeatureProvider featureProvider = (FeatureProvider) updateds.next(); |
||
284 | 43629 | jjdelcerro | paramsDisposer = sqlbuilder.setParameters(st, featureProvider); |
285 | 43020 | jjdelcerro | if (JDBCUtils.executeUpdate(st,sql) == 0) { |
286 | throw new JDBCUpdateWithoutChangesException(sql,null); |
||
287 | } |
||
288 | } |
||
289 | } catch (SQLException e) { |
||
290 | throw new JDBCSQLException(e); |
||
291 | } finally {
|
||
292 | JDBCUtils.closeQuietly(st); |
||
293 | 43629 | jjdelcerro | DisposeUtils.disposeQuietly(paramsDisposer); |
294 | 43020 | jjdelcerro | } |
295 | } |
||
296 | |||
297 | 43687 | jjdelcerro | protected List<String> buildUpdateTableSQL( |
298 | 44058 | jjdelcerro | TableReference table, |
299 | 43020 | jjdelcerro | FeatureType original, |
300 | 43362 | jjdelcerro | FeatureType target |
301 | ) { |
||
302 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
303 | 44058 | jjdelcerro | sqlbuilder.alter_table().table() |
304 | .database(this.table.getDatabase())
|
||
305 | .schema(this.table.getSchema())
|
||
306 | .name(this.table.getTable());
|
||
307 | 43020 | jjdelcerro | |
308 | for (FeatureAttributeDescriptor attrOrgiginal : original) {
|
||
309 | FeatureAttributeDescriptor attrTarget = target.getAttributeDescriptor( |
||
310 | attrOrgiginal.getName() |
||
311 | ); |
||
312 | if (attrTarget == null) { |
||
313 | 44190 | jjdelcerro | if( attrOrgiginal.isComputed() ) {
|
314 | continue;
|
||
315 | } |
||
316 | 43020 | jjdelcerro | sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
317 | 43650 | jjdelcerro | } else if( !this.areEquals(attrOrgiginal, attrTarget) ) { |
318 | 44190 | jjdelcerro | if( attrTarget.isComputed() ) {
|
319 | continue;
|
||
320 | } |
||
321 | 43650 | jjdelcerro | if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
322 | sqlbuilder.alter_table().alter_geometry_column( |
||
323 | attrTarget.getName(), |
||
324 | attrTarget.getGeomType().getType(), |
||
325 | attrTarget.getGeomType().getSubType(), |
||
326 | attrTarget.getSRS(), |
||
327 | attrTarget.isIndexed(), |
||
328 | attrTarget.allowNull() |
||
329 | ); |
||
330 | } else {
|
||
331 | sqlbuilder.alter_table().alter_column( |
||
332 | attrTarget.getName(), |
||
333 | attrTarget.getType(), |
||
334 | attrTarget.getPrecision(), |
||
335 | attrTarget.getSize(), |
||
336 | attrTarget.isPrimaryKey(), |
||
337 | attrTarget.isIndexed(), |
||
338 | attrTarget.allowNull(), |
||
339 | attrTarget.isAutomatic(), |
||
340 | attrTarget.getDefaultValue() |
||
341 | ); |
||
342 | } |
||
343 | 43020 | jjdelcerro | } |
344 | } |
||
345 | for (FeatureAttributeDescriptor attrTarget : target) {
|
||
346 | 44190 | jjdelcerro | if( attrTarget.isComputed() ) {
|
347 | continue;
|
||
348 | } |
||
349 | 43020 | jjdelcerro | if (original.getAttributeDescriptor(attrTarget.getName()) == null) { |
350 | 43650 | jjdelcerro | if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
351 | sqlbuilder.alter_table().add_geometry_column( |
||
352 | attrTarget.getName(), |
||
353 | attrTarget.getGeomType().getType(), |
||
354 | attrTarget.getGeomType().getSubType(), |
||
355 | attrTarget.getSRS(), |
||
356 | attrTarget.isIndexed(), |
||
357 | attrTarget.allowNull() |
||
358 | ); |
||
359 | } else {
|
||
360 | sqlbuilder.alter_table().add_column( |
||
361 | attrTarget.getName(), |
||
362 | attrTarget.getType(), |
||
363 | attrTarget.getPrecision(), |
||
364 | attrTarget.getSize(), |
||
365 | attrTarget.isPrimaryKey(), |
||
366 | attrTarget.isIndexed(), |
||
367 | attrTarget.allowNull(), |
||
368 | attrTarget.isAutomatic(), |
||
369 | attrTarget.getDefaultValue() |
||
370 | ); |
||
371 | } |
||
372 | 43020 | jjdelcerro | } |
373 | } |
||
374 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
375 | Variable.class, |
||
376 | PROP_TABLE, table |
||
377 | ); |
||
378 | 43687 | jjdelcerro | return sqlbuilder.alter_table().toStrings();
|
379 | 43362 | jjdelcerro | } |
380 | 43650 | jjdelcerro | |
381 | protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) { |
||
382 | // No interesa si son o no iguales en general, solo si son iguales en lo
|
||
383 | // que a los atributos usados para crear la columna de la tabla se refiere.
|
||
384 | if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
|
||
385 | return false; |
||
386 | } |
||
387 | if( attr1.getType() != attr2.getType() ) {
|
||
388 | return false; |
||
389 | } |
||
390 | if( attr1.getPrecision() != attr2.getPrecision() ) {
|
||
391 | return false; |
||
392 | } |
||
393 | if( attr1.getSize() != attr2.getSize() ) {
|
||
394 | return false; |
||
395 | } |
||
396 | if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
|
||
397 | return false; |
||
398 | } |
||
399 | 43687 | jjdelcerro | // if( attr1.isIndexed() != attr2.isIndexed() ) {
|
400 | // return false;
|
||
401 | // }
|
||
402 | 43650 | jjdelcerro | if( attr1.allowNull() != attr2.allowNull() ) {
|
403 | return false; |
||
404 | } |
||
405 | if( attr1.isAutomatic() != attr2.isAutomatic() ) {
|
||
406 | return false; |
||
407 | } |
||
408 | if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
|
||
409 | if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) { |
||
410 | return false; |
||
411 | } |
||
412 | if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
|
||
413 | return false; |
||
414 | } |
||
415 | } |
||
416 | return true; |
||
417 | } |
||
418 | 43362 | jjdelcerro | |
419 | 43687 | jjdelcerro | protected List<String> buildCreateIndexSQL( |
420 | 44058 | jjdelcerro | TableReference table, |
421 | 43687 | jjdelcerro | FeatureType original, |
422 | FeatureType target |
||
423 | ) { |
||
424 | ArrayList<String> sqls = new ArrayList<>(); |
||
425 | |||
426 | for (FeatureAttributeDescriptor attrTarget : target) {
|
||
427 | boolean createIndex = false; |
||
428 | if( attrTarget.isIndexed() ) {
|
||
429 | FeatureAttributeDescriptor attrOriginal = original.getAttributeDescriptor(attrTarget.getName()); |
||
430 | if ( attrOriginal == null) { |
||
431 | createIndex = true;
|
||
432 | } else {
|
||
433 | if( attrOriginal.isIndexed() ) {
|
||
434 | createIndex = false;
|
||
435 | } else {
|
||
436 | createIndex = true;
|
||
437 | } |
||
438 | } |
||
439 | } |
||
440 | if( createIndex ) {
|
||
441 | JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
||
442 | if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
||
443 | sqlbuilder.create_index().spatial(); |
||
444 | } |
||
445 | sqlbuilder.create_index().if_not_exist(); |
||
446 | sqlbuilder.create_index().name("idx_" + table + "_" + attrTarget.getName()); |
||
447 | sqlbuilder.create_index().column(attrTarget.getName()); |
||
448 | 44058 | jjdelcerro | sqlbuilder.create_index().table() |
449 | .database(this.table.getDatabase())
|
||
450 | .schema(this.table.getSchema())
|
||
451 | .name(this.table.getTable());
|
||
452 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
453 | Variable.class, |
||
454 | PROP_TABLE, table |
||
455 | ); |
||
456 | 43687 | jjdelcerro | sqls.addAll(sqlbuilder.create_index().toStrings()); |
457 | } |
||
458 | } |
||
459 | return sqls;
|
||
460 | } |
||
461 | |||
462 | 43362 | jjdelcerro | public void performUpdateTable(Connection conn, |
463 | 44058 | jjdelcerro | TableReference table, |
464 | 43362 | jjdelcerro | FeatureType original, |
465 | FeatureType target) throws DataException {
|
||
466 | 43687 | jjdelcerro | |
467 | ArrayList<String> sqls = new ArrayList<>(); |
||
468 | |||
469 | 44058 | jjdelcerro | sqls.addAll(buildUpdateTableSQL(table, original, target)); |
470 | sqls.addAll(buildCreateIndexSQL(table, original, target)); |
||
471 | 43687 | jjdelcerro | |
472 | if( !CollectionUtils.isEmpty(sqls) ) {
|
||
473 | Statement st = null; |
||
474 | try {
|
||
475 | st = conn.createStatement(); |
||
476 | for (String sql : sqls) { |
||
477 | 44190 | jjdelcerro | if( !StringUtils.isBlank(sql) ) {
|
478 | JDBCUtils.execute(st, sql); |
||
479 | } |
||
480 | 43687 | jjdelcerro | } |
481 | } catch (SQLException e) { |
||
482 | throw new JDBCSQLException(e); |
||
483 | } finally {
|
||
484 | JDBCUtils.closeQuietly(st); |
||
485 | 43020 | jjdelcerro | } |
486 | } |
||
487 | } |
||
488 | } |