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 @ 46213
History | View | Annotate | Download (24.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
25 |
|
26 |
import java.sql.Connection; |
27 |
import java.sql.PreparedStatement; |
28 |
import java.sql.SQLException; |
29 |
import java.sql.Statement; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import org.apache.commons.collections.CollectionUtils; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
36 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter; |
37 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
38 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
39 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
40 |
import org.gvsig.fmap.dal.DataTypes; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
43 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType.FeatureTypeChanged; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
47 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
48 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
49 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
50 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCUpdateWithoutChangesException; |
51 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
52 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
53 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
54 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
55 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
56 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
57 |
import org.gvsig.tools.dispose.Disposable; |
58 |
import org.gvsig.tools.dispose.DisposeUtils; |
59 |
|
60 |
@SuppressWarnings("UseSpecificCatch") |
61 |
public class PerformChangesOperation extends AbstractConnectionWritableOperation { |
62 |
|
63 |
protected TableReference table;
|
64 |
protected FeatureType featureType;
|
65 |
protected FeatureType featureTypeSource;
|
66 |
protected FeatureType featureTypeTarget;
|
67 |
protected Iterator<FeatureReferenceProviderServices> deleteds; |
68 |
protected Iterator<FeatureProvider> updateds; |
69 |
protected Iterator<FeatureProvider> inserteds; |
70 |
|
71 |
protected boolean typeChanged = false; |
72 |
|
73 |
public PerformChangesOperation(JDBCHelper helper) {
|
74 |
this(helper, null, null, null, null, null, null); |
75 |
} |
76 |
|
77 |
public PerformChangesOperation(JDBCHelper helper,
|
78 |
TableReference table, |
79 |
FeatureType featureType, |
80 |
Iterator<FeatureReferenceProviderServices> deleteds,
|
81 |
Iterator<FeatureProvider> inserteds,
|
82 |
Iterator<FeatureProvider> updateds,
|
83 |
Iterator<FeatureTypeChanged> featureTypesChanged) {
|
84 |
super(helper);
|
85 |
this.deleteds = deleteds;
|
86 |
this.inserteds = inserteds;
|
87 |
this.updateds = updateds;
|
88 |
this.table = table;
|
89 |
this.featureType = featureType;
|
90 |
if (featureTypesChanged.hasNext()) {
|
91 |
FeatureTypeChanged item = featureTypesChanged.next(); |
92 |
this.featureTypeSource = item.getSource();
|
93 |
this.featureTypeTarget = item.getTarget();
|
94 |
typeChanged = true;
|
95 |
} else {
|
96 |
this.featureTypeSource = null; |
97 |
this.featureTypeTarget = null; |
98 |
typeChanged = false;
|
99 |
} |
100 |
} |
101 |
|
102 |
public boolean isTypeChanged() { |
103 |
return typeChanged;
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public Object perform(Connection conn) throws DataException { |
108 |
if (typeChanged) {
|
109 |
this.performUpdateTable(conn);
|
110 |
} |
111 |
if (inserteds.hasNext()) {
|
112 |
performInserts(conn); |
113 |
} |
114 |
if (updateds.hasNext()) {
|
115 |
performUpdates(conn); |
116 |
} |
117 |
if (deleteds.hasNext()) {
|
118 |
performDeletes(conn); |
119 |
} |
120 |
return true; |
121 |
} |
122 |
|
123 |
public String getDeleteSQL() { |
124 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
125 |
return this.getDeleteSQL(sqlbuilder); |
126 |
} |
127 |
|
128 |
public String getDeleteSQL(JDBCSQLBuilderBase sqlbuilder) { |
129 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
130 |
|
131 |
sqlbuilder.delete().table() |
132 |
.database(this.table.getDatabase())
|
133 |
.schema(this.table.getSchema())
|
134 |
.name(this.table.getTable());
|
135 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
136 |
if (attr.isPrimaryKey()) {
|
137 |
sqlbuilder.delete().where().and( |
138 |
expbuilder.eq( |
139 |
expbuilder.column(attr.getName()), |
140 |
expbuilder.parameter(attr.getName()).as_variable() |
141 |
) |
142 |
); |
143 |
} |
144 |
} |
145 |
if (!sqlbuilder.delete().has_where() ) {
|
146 |
throw new RuntimeException("Operation requires missing pk"); |
147 |
} |
148 |
sqlbuilder.setProperties( |
149 |
Variable.class, |
150 |
PROP_TABLE, table |
151 |
); |
152 |
String sql = sqlbuilder.delete().toString();
|
153 |
return sql;
|
154 |
} |
155 |
|
156 |
public void performDeletes(Connection conn) throws DataException { |
157 |
|
158 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
159 |
String sql = getDeleteSQL(sqlbuilder);
|
160 |
|
161 |
PreparedStatement st = null; |
162 |
Disposable paramsDisposer = null;
|
163 |
try {
|
164 |
st = conn.prepareStatement(sql); |
165 |
while (deleteds.hasNext()) {
|
166 |
FeatureReference reference = (FeatureReference) deleteds.next(); |
167 |
paramsDisposer = sqlbuilder.setParameters(st, reference); |
168 |
int nAffected = JDBCUtils.executeUpdate(st,sql);
|
169 |
if (nAffected == 0) { |
170 |
throw new JDBCUpdateWithoutChangesException( |
171 |
sqlbuilder.delete().toString(), |
172 |
null
|
173 |
); |
174 |
} |
175 |
if (nAffected > 1) { |
176 |
LOGGER.warn("Remove statement affectst to {} rows ( {} )",
|
177 |
nAffected, sql |
178 |
); |
179 |
} |
180 |
} |
181 |
} catch (SQLException e) { |
182 |
throw new JDBCSQLException(e,sql); |
183 |
} finally {
|
184 |
JDBCUtils.closeQuietly(st); |
185 |
DisposeUtils.disposeQuietly(paramsDisposer); |
186 |
} |
187 |
} |
188 |
|
189 |
public String getInsertSQL() { |
190 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
191 |
return this.getInsertSQL(sqlbuilder); |
192 |
} |
193 |
|
194 |
public String getInsertSQL(JDBCSQLBuilderBase sqlbuilder) { |
195 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
196 |
|
197 |
sqlbuilder.insert().table() |
198 |
.database(this.table.getDatabase())
|
199 |
.schema(this.table.getSchema())
|
200 |
.name(this.table.getTable());
|
201 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
202 |
if( attr.isAutomatic() || attr.isComputed() ) {
|
203 |
continue;
|
204 |
} |
205 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
206 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
207 |
expbuilder.parameter(attr.getName()).as_variable() |
208 |
.srs( |
209 |
expbuilder.parameter().value( |
210 |
attr.getSRS()).as_constant() |
211 |
) |
212 |
); |
213 |
} else {
|
214 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
215 |
expbuilder.parameter(attr.getName()) |
216 |
); |
217 |
} |
218 |
} |
219 |
|
220 |
sqlbuilder.setProperties( |
221 |
Variable.class, |
222 |
PROP_FEATURE_TYPE, featureType, |
223 |
PROP_TABLE, table |
224 |
); |
225 |
sqlbuilder.setProperties( |
226 |
Parameter.class, |
227 |
PROP_FEATURE_TYPE, featureType, |
228 |
PROP_TABLE, table |
229 |
); |
230 |
String sql = sqlbuilder.insert().toString();
|
231 |
return sql;
|
232 |
} |
233 |
|
234 |
public void performInserts(Connection conn) throws DataException { |
235 |
// performInserts_normalmode(conn);
|
236 |
performInserts_batchmode(conn); |
237 |
} |
238 |
|
239 |
private void performInserts_normalmode(Connection conn) throws DataException { |
240 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
241 |
String sql = getInsertSQL(sqlbuilder);
|
242 |
|
243 |
PreparedStatement st;
|
244 |
Disposable paramsDisposer; |
245 |
try {
|
246 |
st = conn.prepareStatement(sql); |
247 |
while (inserteds.hasNext()) {
|
248 |
FeatureProvider feature = inserteds.next(); |
249 |
paramsDisposer = sqlbuilder.setParameters(st, feature); |
250 |
try {
|
251 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
252 |
throw new JDBCExecuteSQLException( |
253 |
sqlbuilder.insert().toString(), |
254 |
null
|
255 |
); |
256 |
} |
257 |
} finally {
|
258 |
DisposeUtils.disposeQuietly(paramsDisposer); |
259 |
} |
260 |
} |
261 |
} catch (JDBCExecuteSQLException ex) {
|
262 |
throw ex;
|
263 |
} catch (Exception ex) { |
264 |
throw new JDBCExecuteSQLException(sql,ex); |
265 |
} |
266 |
} |
267 |
|
268 |
@SuppressWarnings("UnusedAssignment") |
269 |
private void performInserts_batchmode(Connection conn) throws DataException { |
270 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
271 |
String sql = getInsertSQL(sqlbuilder);
|
272 |
|
273 |
PreparedStatement preparedStatement;
|
274 |
Disposable[] disposableParameters;
|
275 |
try {
|
276 |
preparedStatement = conn.prepareStatement(sql); |
277 |
int batchSize = this.helper.getConnectionParameters().getBatchSize(); |
278 |
if( batchSize<1 ) { |
279 |
batchSize = 200;
|
280 |
} |
281 |
int batchCount = 0; |
282 |
disposableParameters = new Disposable[batchSize];
|
283 |
|
284 |
while (inserteds.hasNext()) {
|
285 |
FeatureProvider feature = inserteds.next(); |
286 |
|
287 |
Disposable theParametersDisposable = sqlbuilder.setParameters(preparedStatement, feature); |
288 |
JDBCUtils.addBatch(preparedStatement,sql); |
289 |
disposableParameters[batchCount++] = theParametersDisposable; |
290 |
|
291 |
if( batchCount >= batchSize || (!inserteds.hasNext() && batchCount>0) ) { |
292 |
int[] status = JDBCUtils.executeBatch(preparedStatement,sql); |
293 |
|
294 |
preparedStatement.clearParameters(); |
295 |
preparedStatement.clearBatch(); |
296 |
for (int i = 0; i < batchCount && i < disposableParameters.length; i++) { |
297 |
DisposeUtils.dispose(disposableParameters[i]); |
298 |
disposableParameters[i] = null;
|
299 |
} |
300 |
batchCount = 0;
|
301 |
|
302 |
for (int n : status) { |
303 |
if( n<=Statement.EXECUTE_FAILED ) { //-3 |
304 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
305 |
} |
306 |
} |
307 |
} |
308 |
} |
309 |
|
310 |
} catch (Exception ex) { |
311 |
throw new JDBCExecuteSQLException(sql,ex); |
312 |
} |
313 |
} |
314 |
|
315 |
public String getUpdateSQL() { |
316 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
317 |
return this.getUpdateSQL(sqlbuilder); |
318 |
} |
319 |
|
320 |
public String getUpdateSQL(JDBCSQLBuilderBase sqlbuilder) { |
321 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
322 |
|
323 |
sqlbuilder.update().table() |
324 |
.database(this.table.getDatabase())
|
325 |
.schema(this.table.getSchema())
|
326 |
.name(this.table.getTable());
|
327 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
328 |
if (attr.isPrimaryKey()) {
|
329 |
sqlbuilder.update().where().and( |
330 |
expbuilder.eq( |
331 |
expbuilder.column(attr.getName()), |
332 |
expbuilder.parameter(attr.getName()).as_variable() |
333 |
) |
334 |
); |
335 |
continue;
|
336 |
} |
337 |
if ( attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
|
338 |
continue;
|
339 |
} |
340 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
341 |
if (attr.getSRS()==null) { |
342 |
throw new RuntimeException("Geometries with null srs are not valid. SRS is required in "+this.table.toString()+"in the field:"+attr.getName()); |
343 |
} |
344 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
345 |
expbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
346 |
expbuilder.parameter().value(attr.getSRS()).as_constant() |
347 |
) |
348 |
); |
349 |
} else {
|
350 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
351 |
expbuilder.parameter(attr.getName()).as_variable() |
352 |
); |
353 |
} |
354 |
} |
355 |
if (!sqlbuilder.update().has_where() ) {
|
356 |
throw new RuntimeException("Operation requires missing pk"); |
357 |
} |
358 |
sqlbuilder.setProperties( |
359 |
Variable.class, |
360 |
PROP_FEATURE_TYPE, this.featureType,
|
361 |
PROP_TABLE, table |
362 |
); |
363 |
sqlbuilder.setProperties( |
364 |
Parameter.class, |
365 |
PROP_FEATURE_TYPE, featureType, |
366 |
PROP_TABLE, table |
367 |
); |
368 |
|
369 |
String sql = sqlbuilder.update().toString();
|
370 |
return sql;
|
371 |
} |
372 |
|
373 |
public void performUpdates(Connection conn) throws DataException { |
374 |
|
375 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
376 |
String sql = getUpdateSQL(sqlbuilder);
|
377 |
|
378 |
PreparedStatement st = null; |
379 |
Disposable paramsDisposer = null;
|
380 |
try {
|
381 |
st = conn.prepareStatement(sql); |
382 |
while (updateds.hasNext()) {
|
383 |
FeatureProvider featureProvider = (FeatureProvider) updateds.next(); |
384 |
paramsDisposer = sqlbuilder.setParameters(st, featureProvider); |
385 |
if (JDBCUtils.executeUpdate(st,sql) == 0) { |
386 |
throw new JDBCUpdateWithoutChangesException(sql,null); |
387 |
} |
388 |
} |
389 |
} catch (SQLException e) { |
390 |
throw new JDBCSQLException(e,sql); |
391 |
} finally {
|
392 |
JDBCUtils.closeQuietly(st); |
393 |
DisposeUtils.disposeQuietly(paramsDisposer); |
394 |
} |
395 |
} |
396 |
|
397 |
public List<String> getUpdateTableSQLs() { |
398 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
399 |
sqlbuilder.alter_table().table() |
400 |
.database(this.table.getDatabase())
|
401 |
.schema(this.table.getSchema())
|
402 |
.name(this.table.getTable());
|
403 |
|
404 |
// Primero comprobamos si hay que eliminar la primary-key o algun indice
|
405 |
for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
|
406 |
if( attrOrgiginal.isComputed() ) {
|
407 |
continue;
|
408 |
} |
409 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor( |
410 |
attrOrgiginal.getName() |
411 |
); |
412 |
if (attrTarget == null) { |
413 |
continue;
|
414 |
} |
415 |
if( attrOrgiginal.isPrimaryKey() && !attrTarget.isPrimaryKey() ) {
|
416 |
sqlbuilder.alter_table().drop_primary_key(attrTarget.getName()); |
417 |
} |
418 |
} |
419 |
|
420 |
// Comprobamos si hay que eliminar o modificar alguna columna existente
|
421 |
for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
|
422 |
if( attrOrgiginal.isComputed() ) {
|
423 |
continue;
|
424 |
} |
425 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor( |
426 |
attrOrgiginal.getName() |
427 |
); |
428 |
if (attrTarget == null) { |
429 |
sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
430 |
} else if( !this.areEquals(attrOrgiginal, attrTarget) ) { |
431 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
432 |
sqlbuilder.alter_table().alter_geometry_column( |
433 |
attrTarget.getName(), |
434 |
attrTarget.getGeomType().getType(), |
435 |
attrTarget.getGeomType().getSubType(), |
436 |
attrTarget.getSRS(), |
437 |
attrTarget.isIndexed(), |
438 |
attrTarget.allowNull() |
439 |
); |
440 |
} else {
|
441 |
Object defaultValue = attrTarget.getDefaultValue();
|
442 |
if( defaultValue instanceof CharSequence ) { |
443 |
if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
|
444 |
defaultValue = null;
|
445 |
} |
446 |
} |
447 |
sqlbuilder.alter_table().alter_column( |
448 |
attrTarget.getName(), |
449 |
attrTarget.getType(), |
450 |
attrTarget.getSize(), |
451 |
attrTarget.getPrecision(), |
452 |
attrTarget.getScale(), |
453 |
attrTarget.isPrimaryKey(), |
454 |
attrTarget.isIndexed(), |
455 |
attrTarget.allowNull(), |
456 |
attrTarget.isAutomatic(), |
457 |
defaultValue |
458 |
); |
459 |
} |
460 |
} |
461 |
} |
462 |
|
463 |
// Por ultimi comprobamos si hay que anadir alguna columna nueva.
|
464 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
465 |
if( attrTarget.isComputed() ) {
|
466 |
continue;
|
467 |
} |
468 |
FeatureAttributeDescriptor attrOrgiginal = featureTypeSource.getAttributeDescriptor( |
469 |
attrTarget.getName() |
470 |
); |
471 |
if (attrOrgiginal == null) { |
472 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
473 |
sqlbuilder.alter_table().add_geometry_column( |
474 |
attrTarget.getName(), |
475 |
attrTarget.getGeomType().getType(), |
476 |
attrTarget.getGeomType().getSubType(), |
477 |
attrTarget.getSRS(), |
478 |
attrTarget.isIndexed(), |
479 |
attrTarget.allowNull() |
480 |
); |
481 |
} else {
|
482 |
Object defaultValue = attrTarget.getDefaultValue();
|
483 |
if( defaultValue instanceof CharSequence ) { |
484 |
if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
|
485 |
defaultValue = null;
|
486 |
} |
487 |
} |
488 |
sqlbuilder.alter_table().add_column( |
489 |
attrTarget.getName(), |
490 |
attrTarget.getType(), |
491 |
attrTarget.getSize(), |
492 |
attrTarget.getPrecision(), |
493 |
attrTarget.getScale(), |
494 |
attrTarget.isPrimaryKey(), |
495 |
attrTarget.isIndexed(), |
496 |
attrTarget.allowNull(), |
497 |
attrTarget.isAutomatic(), |
498 |
defaultValue |
499 |
); |
500 |
} |
501 |
} |
502 |
} |
503 |
|
504 |
sqlbuilder.setProperties( |
505 |
Variable.class, |
506 |
PROP_TABLE, table |
507 |
); |
508 |
List<String> sqls = sqlbuilder.alter_table().toStrings(); |
509 |
sqls.addAll(buildCreateIndexSQL()); |
510 |
return sqls;
|
511 |
} |
512 |
|
513 |
protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) { |
514 |
// No interesa si son o no iguales en general, solo si son iguales en lo
|
515 |
// que a los atributos usados para crear la columna de la tabla se refiere.
|
516 |
if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
|
517 |
return false; |
518 |
} |
519 |
if( attr1.getType() != attr2.getType() ) {
|
520 |
return false; |
521 |
} |
522 |
if( attr1.getPrecision() != attr2.getPrecision() ) {
|
523 |
return false; |
524 |
} |
525 |
if( attr1.getScale() != attr2.getScale() ) {
|
526 |
return false; |
527 |
} |
528 |
if( attr1.getSize() != attr2.getSize() ) {
|
529 |
return false; |
530 |
} |
531 |
if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
|
532 |
return false; |
533 |
} |
534 |
// if( attr1.isIndexed() != attr2.isIndexed() ) {
|
535 |
// return false;
|
536 |
// }
|
537 |
if( attr1.allowNull() != attr2.allowNull() ) {
|
538 |
return false; |
539 |
} |
540 |
if( attr1.isAutomatic() != attr2.isAutomatic() ) {
|
541 |
return false; |
542 |
} |
543 |
if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
|
544 |
if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) { |
545 |
return false; |
546 |
} |
547 |
if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
|
548 |
return false; |
549 |
} |
550 |
} |
551 |
return true; |
552 |
} |
553 |
|
554 |
protected List<String> buildCreateIndexSQL() { |
555 |
ArrayList<String> sqls = new ArrayList<>(); |
556 |
|
557 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
558 |
boolean createIndex;
|
559 |
boolean dropIndex;
|
560 |
FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName()); |
561 |
if( attrOriginal != null ) { |
562 |
createIndex = (attrTarget.isIndexed() && !attrOriginal.isIndexed()); |
563 |
dropIndex = (!attrTarget.isIndexed() && attrOriginal.isIndexed()); |
564 |
} else {
|
565 |
createIndex = attrTarget.isIndexed(); |
566 |
dropIndex = false;
|
567 |
} |
568 |
if( createIndex ) {
|
569 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
570 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
571 |
sqlbuilder.create_index().spatial(); |
572 |
} |
573 |
sqlbuilder.create_index().if_not_exist(); |
574 |
sqlbuilder.create_index().name(table.getTable(), attrTarget.getName()); |
575 |
sqlbuilder.create_index().column(attrTarget.getName()); |
576 |
sqlbuilder.create_index().table() |
577 |
.database(this.table.getDatabase())
|
578 |
.schema(this.table.getSchema())
|
579 |
.name(this.table.getTable());
|
580 |
sqlbuilder.setProperties( |
581 |
Variable.class, |
582 |
PROP_TABLE, table |
583 |
); |
584 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
585 |
} else if( dropIndex ) { |
586 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
587 |
sqlbuilder.drop_index().if_not_exist(); |
588 |
sqlbuilder.drop_index().name( table.getTable(), attrTarget.getName()); |
589 |
} |
590 |
} |
591 |
return sqls;
|
592 |
} |
593 |
|
594 |
public void performUpdateTable(Connection conn) throws DataException { |
595 |
|
596 |
List<String> sqls = this.getUpdateTableSQLs(); |
597 |
|
598 |
if( !CollectionUtils.isEmpty(sqls) ) {
|
599 |
Statement st = null; |
600 |
String currentsql = null; |
601 |
try {
|
602 |
st = conn.createStatement(); |
603 |
for (String sql : sqls) { |
604 |
currentsql = sql; |
605 |
if( !StringUtils.isBlank(sql) ) {
|
606 |
JDBCUtils.execute(st, sql); |
607 |
} |
608 |
} |
609 |
} catch (SQLException e) { |
610 |
throw new JDBCSQLException(e,currentsql); |
611 |
} finally {
|
612 |
JDBCUtils.closeQuietly(st); |
613 |
} |
614 |
} |
615 |
} |
616 |
} |