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 @ 46714
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.PreparedStatement; |
27 |
import java.sql.SQLException; |
28 |
import java.sql.Statement; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
import java.util.Objects; |
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.jdbc2.JDBCConnection; |
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 |
import org.gvsig.tools.util.Invocable; |
60 |
|
61 |
@SuppressWarnings("UseSpecificCatch") |
62 |
public class PerformChangesOperation extends AbstractConnectionWritableOperation { |
63 |
|
64 |
protected TableReference table;
|
65 |
protected FeatureType featureType;
|
66 |
protected FeatureType featureTypeSource;
|
67 |
protected FeatureType featureTypeTarget;
|
68 |
protected Iterator<FeatureReferenceProviderServices> deleteds; |
69 |
protected Iterator<FeatureProvider> updateds; |
70 |
protected Iterator<FeatureProvider> inserteds; |
71 |
|
72 |
protected boolean typeChanged = false; |
73 |
|
74 |
public PerformChangesOperation(JDBCHelper helper) {
|
75 |
this(helper, null, null, null, null, null, null); |
76 |
} |
77 |
|
78 |
public PerformChangesOperation(JDBCHelper helper,
|
79 |
TableReference table, |
80 |
FeatureType featureType, |
81 |
Iterator<FeatureReferenceProviderServices> deleteds,
|
82 |
Iterator<FeatureProvider> inserteds,
|
83 |
Iterator<FeatureProvider> updateds,
|
84 |
Iterator<FeatureTypeChanged> featureTypesChanged) {
|
85 |
super(helper);
|
86 |
this.deleteds = deleteds;
|
87 |
this.inserteds = inserteds;
|
88 |
this.updateds = updateds;
|
89 |
this.table = table;
|
90 |
this.featureType = featureType;
|
91 |
if (featureTypesChanged.hasNext()) {
|
92 |
FeatureTypeChanged item = featureTypesChanged.next(); |
93 |
this.featureTypeSource = item.getSource();
|
94 |
this.featureTypeTarget = item.getTarget();
|
95 |
typeChanged = true;
|
96 |
} else {
|
97 |
this.featureTypeSource = null; |
98 |
this.featureTypeTarget = null; |
99 |
typeChanged = false;
|
100 |
} |
101 |
} |
102 |
|
103 |
public boolean isTypeChanged() { |
104 |
return typeChanged;
|
105 |
} |
106 |
|
107 |
@Override
|
108 |
public Object perform(JDBCConnection conn) throws DataException { |
109 |
if (typeChanged) {
|
110 |
this.performUpdateTable(conn);
|
111 |
} |
112 |
if (inserteds.hasNext()) {
|
113 |
performInserts(conn); |
114 |
} |
115 |
if (updateds.hasNext()) {
|
116 |
performUpdates(conn); |
117 |
} |
118 |
if (deleteds.hasNext()) {
|
119 |
performDeletes(conn); |
120 |
} |
121 |
return true; |
122 |
} |
123 |
|
124 |
public String getDeleteSQL() { |
125 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
126 |
return this.getDeleteSQL(sqlbuilder); |
127 |
} |
128 |
|
129 |
public String getDeleteSQL(JDBCSQLBuilderBase sqlbuilder) { |
130 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
131 |
|
132 |
sqlbuilder.delete().table() |
133 |
.database(this.table.getDatabase())
|
134 |
.schema(this.table.getSchema())
|
135 |
.name(this.table.getTable());
|
136 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
137 |
if (attr.isPrimaryKey()) {
|
138 |
sqlbuilder.delete().where().and( |
139 |
expbuilder.eq( |
140 |
expbuilder.column(attr.getName()), |
141 |
expbuilder.parameter(attr.getName()).as_variable() |
142 |
) |
143 |
); |
144 |
} |
145 |
} |
146 |
if (!sqlbuilder.delete().has_where() ) {
|
147 |
throw new RuntimeException("Operation requires missing pk"); |
148 |
} |
149 |
sqlbuilder.setProperties( |
150 |
Variable.class, |
151 |
PROP_TABLE, table |
152 |
); |
153 |
String sql = sqlbuilder.delete().toString();
|
154 |
return sql;
|
155 |
} |
156 |
|
157 |
public void performDeletes(JDBCConnection conn) throws DataException { |
158 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
159 |
String sql = getDeleteSQL(sqlbuilder);
|
160 |
perform_batchmode(conn, this.deleteds, sqlbuilder, sql, new Invocable() { |
161 |
@Override
|
162 |
public Object call(Object... args) { |
163 |
JDBCSQLBuilderBase sqlbuilder = (JDBCSQLBuilderBase) args[0];
|
164 |
PreparedStatement preparedStatement = (PreparedStatement) args[1]; |
165 |
FeatureReference reference = (FeatureReference) args[2];
|
166 |
Disposable theParametersDisposable = sqlbuilder.setParameters(preparedStatement, reference); |
167 |
return theParametersDisposable;
|
168 |
} |
169 |
}); |
170 |
} |
171 |
|
172 |
public String getInsertSQL() { |
173 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
174 |
return this.getInsertSQL(sqlbuilder); |
175 |
} |
176 |
|
177 |
public String getInsertSQL(JDBCSQLBuilderBase sqlbuilder) { |
178 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
179 |
|
180 |
sqlbuilder.insert().table() |
181 |
.database(this.table.getDatabase())
|
182 |
.schema(this.table.getSchema())
|
183 |
.name(this.table.getTable());
|
184 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
185 |
// Cuando insertamos registros hay que permitir meter un valor
|
186 |
// en la claves automaticas. Es responsable de quien los inserta,
|
187 |
// en caso de que no meta nulos, que se asegure que no hay valores
|
188 |
// duplicados, ya que que de haberlos fallara el insert.
|
189 |
if( /*attr.isAutomatic() ||*/ attr.isComputed() ) { |
190 |
continue;
|
191 |
} |
192 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
193 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
194 |
expbuilder.parameter(attr.getName()).as_variable() |
195 |
.srs( |
196 |
expbuilder.parameter().value( |
197 |
attr.getSRS()).as_constant() |
198 |
) |
199 |
); |
200 |
} else {
|
201 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
202 |
expbuilder.parameter(attr.getName()) |
203 |
); |
204 |
} |
205 |
} |
206 |
|
207 |
sqlbuilder.setProperties( |
208 |
Variable.class, |
209 |
PROP_FEATURE_TYPE, featureType, |
210 |
PROP_TABLE, table |
211 |
); |
212 |
sqlbuilder.setProperties( |
213 |
Parameter.class, |
214 |
PROP_FEATURE_TYPE, featureType, |
215 |
PROP_TABLE, table |
216 |
); |
217 |
String sql = sqlbuilder.insert().toString();
|
218 |
return sql;
|
219 |
} |
220 |
|
221 |
public void performInserts(JDBCConnection conn) throws DataException { |
222 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
223 |
String sql = getInsertSQL(sqlbuilder);
|
224 |
perform_batchmode(conn, this.inserteds, sqlbuilder, sql, new Invocable() { |
225 |
@Override
|
226 |
public Object call(Object... args) { |
227 |
JDBCSQLBuilderBase sqlbuilder = (JDBCSQLBuilderBase) args[0];
|
228 |
PreparedStatement preparedStatement = (PreparedStatement) args[1]; |
229 |
FeatureProvider feature = (FeatureProvider) args[2];
|
230 |
Disposable theParametersDisposable = sqlbuilder.setParameters(preparedStatement, feature); |
231 |
return theParametersDisposable;
|
232 |
} |
233 |
}); |
234 |
} |
235 |
|
236 |
public String getUpdateSQL() { |
237 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
238 |
return this.getUpdateSQL(sqlbuilder); |
239 |
} |
240 |
|
241 |
public String getUpdateSQL(JDBCSQLBuilderBase sqlbuilder) { |
242 |
if(!this.featureType.hasPrimaryKey()) { |
243 |
throw new RuntimeException("Operation requires missing pk"); |
244 |
} |
245 |
GeometryExpressionBuilder expbuilder = sqlbuilder.expression(); |
246 |
|
247 |
sqlbuilder.update().table() |
248 |
.database(this.table.getDatabase())
|
249 |
.schema(this.table.getSchema())
|
250 |
.name(this.table.getTable());
|
251 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
252 |
if (attr.isPrimaryKey()) {
|
253 |
sqlbuilder.update().where().and( |
254 |
expbuilder.eq( |
255 |
expbuilder.column(attr.getName()), |
256 |
expbuilder.parameter(attr.getName()).as_variable() |
257 |
) |
258 |
); |
259 |
continue;
|
260 |
} |
261 |
if ( attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
|
262 |
continue;
|
263 |
} |
264 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
265 |
if (attr.getSRS()==null) { |
266 |
throw new RuntimeException("Geometries with null srs are not valid. SRS is required in "+this.table.toString()+"in the field:"+attr.getName()); |
267 |
} |
268 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
269 |
expbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
270 |
expbuilder.parameter().value(attr.getSRS()).as_constant() |
271 |
) |
272 |
); |
273 |
} else {
|
274 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
275 |
expbuilder.parameter(attr.getName()).as_variable() |
276 |
); |
277 |
} |
278 |
} |
279 |
if (!sqlbuilder.update().has_where() ) {
|
280 |
throw new RuntimeException("Operation requires missing pk"); |
281 |
} |
282 |
sqlbuilder.setProperties( |
283 |
Variable.class, |
284 |
PROP_FEATURE_TYPE, this.featureType,
|
285 |
PROP_TABLE, table |
286 |
); |
287 |
sqlbuilder.setProperties( |
288 |
Parameter.class, |
289 |
PROP_FEATURE_TYPE, featureType, |
290 |
PROP_TABLE, table |
291 |
); |
292 |
|
293 |
String sql = sqlbuilder.update().toString();
|
294 |
return sql;
|
295 |
} |
296 |
|
297 |
public void performUpdates(JDBCConnection conn) throws DataException { |
298 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
299 |
String sql = getUpdateSQL(sqlbuilder);
|
300 |
perform_batchmode(conn, this.updateds, sqlbuilder, sql, new Invocable() { |
301 |
@Override
|
302 |
public Object call(Object... args) { |
303 |
JDBCSQLBuilderBase sqlbuilder = (JDBCSQLBuilderBase) args[0];
|
304 |
PreparedStatement preparedStatement = (PreparedStatement) args[1]; |
305 |
FeatureProvider feature = (FeatureProvider) args[2];
|
306 |
Disposable theParametersDisposable = sqlbuilder.setParameters(preparedStatement, feature); |
307 |
return theParametersDisposable;
|
308 |
} |
309 |
}); |
310 |
} |
311 |
|
312 |
|
313 |
private void perform_batchmode(JDBCConnection conn, Iterator elements, JDBCSQLBuilderBase sqlbuilder, String sql, Invocable fillPreparedStatement) throws DataException { |
314 |
PreparedStatement preparedStatement;
|
315 |
Disposable[] disposableParameters;
|
316 |
int batchSize = this.helper.getConnectionParameters().getBatchSize(); |
317 |
if( batchSize==0 ) { |
318 |
try {
|
319 |
preparedStatement = conn.prepareStatement(sql); |
320 |
while (elements.hasNext()) {
|
321 |
Object element = elements.next();
|
322 |
|
323 |
Disposable theParametersDisposable = (Disposable) fillPreparedStatement.call(sqlbuilder,preparedStatement, element ); |
324 |
JDBCUtils.execute(preparedStatement, sql); |
325 |
DisposeUtils.disposeQuietly(theParametersDisposable); |
326 |
} |
327 |
} catch (Exception ex) { |
328 |
throw new JDBCExecuteSQLException(sql + " ("+Objects.toString(elements)+")",ex); |
329 |
} |
330 |
} else {
|
331 |
try {
|
332 |
preparedStatement = conn.prepareStatement(sql); |
333 |
if( batchSize<1 ) { |
334 |
batchSize = 200;
|
335 |
} |
336 |
int batchCount = 0; |
337 |
disposableParameters = new Disposable[batchSize];
|
338 |
|
339 |
while (elements.hasNext()) {
|
340 |
Object element = elements.next();
|
341 |
|
342 |
Disposable theParametersDisposable = (Disposable) fillPreparedStatement.call(sqlbuilder,preparedStatement, element ); |
343 |
JDBCUtils.addBatch(preparedStatement,sql); |
344 |
disposableParameters[batchCount++] = theParametersDisposable; |
345 |
|
346 |
if( batchCount >= batchSize || (!elements.hasNext() && batchCount>0) ) { |
347 |
int[] status = JDBCUtils.executeBatch(preparedStatement,sql); |
348 |
|
349 |
preparedStatement.clearParameters(); |
350 |
preparedStatement.clearBatch(); |
351 |
for (int i = 0; i < batchCount && i < disposableParameters.length; i++) { |
352 |
DisposeUtils.dispose(disposableParameters[i]); |
353 |
disposableParameters[i] = null;
|
354 |
} |
355 |
batchCount = 0;
|
356 |
|
357 |
for (int n : status) { |
358 |
if( n<=Statement.EXECUTE_FAILED ) { //-3 |
359 |
throw new RuntimeException("Can't process element (n="+n+")."); |
360 |
} |
361 |
} |
362 |
} |
363 |
} |
364 |
} catch (Exception ex) { |
365 |
throw new JDBCExecuteSQLException(sql,ex); |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
public List<String> getUpdateTableSQLs() { |
371 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
372 |
sqlbuilder.alter_table().table() |
373 |
.database(this.table.getDatabase())
|
374 |
.schema(this.table.getSchema())
|
375 |
.name(this.table.getTable());
|
376 |
|
377 |
// Primero comprobamos si hay que eliminar la primary-key o algun indice
|
378 |
for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
|
379 |
if( attrOrgiginal.isComputed() ) {
|
380 |
continue;
|
381 |
} |
382 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor( |
383 |
attrOrgiginal.getName() |
384 |
); |
385 |
if (attrTarget == null) { |
386 |
continue;
|
387 |
} |
388 |
if( attrOrgiginal.isPrimaryKey() && !attrTarget.isPrimaryKey() ) {
|
389 |
sqlbuilder.alter_table().drop_primary_key(attrTarget.getName()); |
390 |
} |
391 |
} |
392 |
|
393 |
// Comprobamos si hay que eliminar o modificar alguna columna existente
|
394 |
for (FeatureAttributeDescriptor attrOrgiginal : featureTypeSource) {
|
395 |
if( attrOrgiginal.isComputed() ) {
|
396 |
continue;
|
397 |
} |
398 |
FeatureAttributeDescriptor attrTarget = featureTypeTarget.getAttributeDescriptor( |
399 |
attrOrgiginal.getName() |
400 |
); |
401 |
if (attrTarget == null) { |
402 |
sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
403 |
} else if( !this.areEquals(attrOrgiginal, attrTarget) ) { |
404 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
405 |
sqlbuilder.alter_table().alter_geometry_column( |
406 |
attrTarget.getName(), |
407 |
attrTarget.getGeomType().getType(), |
408 |
attrTarget.getGeomType().getSubType(), |
409 |
attrTarget.getSRS(), |
410 |
attrTarget.isIndexed(), |
411 |
attrTarget.allowNull() |
412 |
); |
413 |
} else {
|
414 |
Object defaultValue = attrTarget.getDefaultValue();
|
415 |
if( defaultValue instanceof CharSequence ) { |
416 |
if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
|
417 |
defaultValue = null;
|
418 |
} |
419 |
} |
420 |
sqlbuilder.alter_table().alter_column( |
421 |
attrTarget.getName(), |
422 |
attrTarget.getType(), |
423 |
attrTarget.getSize(), |
424 |
attrTarget.getPrecision(), |
425 |
attrTarget.getScale(), |
426 |
attrTarget.isPrimaryKey(), |
427 |
attrTarget.isIndexed(), |
428 |
attrTarget.allowNull(), |
429 |
attrTarget.isAutomatic(), |
430 |
defaultValue |
431 |
); |
432 |
} |
433 |
} |
434 |
} |
435 |
|
436 |
// Por ultimi comprobamos si hay que anadir alguna columna nueva.
|
437 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
438 |
if( attrTarget.isComputed() ) {
|
439 |
continue;
|
440 |
} |
441 |
FeatureAttributeDescriptor attrOrgiginal = featureTypeSource.getAttributeDescriptor( |
442 |
attrTarget.getName() |
443 |
); |
444 |
if (attrOrgiginal == null) { |
445 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
446 |
sqlbuilder.alter_table().add_geometry_column( |
447 |
attrTarget.getName(), |
448 |
attrTarget.getGeomType().getType(), |
449 |
attrTarget.getGeomType().getSubType(), |
450 |
attrTarget.getSRS(), |
451 |
attrTarget.isIndexed(), |
452 |
attrTarget.allowNull() |
453 |
); |
454 |
} else {
|
455 |
Object defaultValue = attrTarget.getDefaultValue();
|
456 |
if( defaultValue instanceof CharSequence ) { |
457 |
if( ExpressionUtils.isDynamicText(defaultValue.toString())) {
|
458 |
defaultValue = null;
|
459 |
} |
460 |
} |
461 |
sqlbuilder.alter_table().add_column( |
462 |
attrTarget.getName(), |
463 |
attrTarget.getType(), |
464 |
attrTarget.getSize(), |
465 |
attrTarget.getPrecision(), |
466 |
attrTarget.getScale(), |
467 |
attrTarget.isPrimaryKey(), |
468 |
attrTarget.isIndexed(), |
469 |
attrTarget.allowNull(), |
470 |
attrTarget.isAutomatic(), |
471 |
defaultValue |
472 |
); |
473 |
} |
474 |
} |
475 |
} |
476 |
|
477 |
sqlbuilder.setProperties( |
478 |
Variable.class, |
479 |
PROP_TABLE, table |
480 |
); |
481 |
List<String> sqls = sqlbuilder.alter_table().toStrings(); |
482 |
sqls.addAll(buildCreateIndexSQL()); |
483 |
return sqls;
|
484 |
} |
485 |
|
486 |
protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) { |
487 |
// No interesa si son o no iguales en general, solo si son iguales en lo
|
488 |
// que a los atributos usados para crear la columna de la tabla se refiere.
|
489 |
if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) {
|
490 |
return false; |
491 |
} |
492 |
if( attr1.getType() != attr2.getType() ) {
|
493 |
return false; |
494 |
} |
495 |
if( attr1.getPrecision() != attr2.getPrecision() ) {
|
496 |
return false; |
497 |
} |
498 |
if( attr1.getScale() != attr2.getScale() ) {
|
499 |
return false; |
500 |
} |
501 |
if( attr1.getSize() != attr2.getSize() ) {
|
502 |
return false; |
503 |
} |
504 |
if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) {
|
505 |
return false; |
506 |
} |
507 |
// if( attr1.isIndexed() != attr2.isIndexed() ) {
|
508 |
// return false;
|
509 |
// }
|
510 |
if( attr1.allowNull() != attr2.allowNull() ) {
|
511 |
return false; |
512 |
} |
513 |
if( attr1.isAutomatic() != attr2.isAutomatic() ) {
|
514 |
return false; |
515 |
} |
516 |
if( attr1.getDefaultValue() != attr2.getDefaultValue() ) {
|
517 |
if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) { |
518 |
return false; |
519 |
} |
520 |
if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) {
|
521 |
return false; |
522 |
} |
523 |
} |
524 |
return true; |
525 |
} |
526 |
|
527 |
protected List<String> buildCreateIndexSQL() { |
528 |
ArrayList<String> sqls = new ArrayList<>(); |
529 |
|
530 |
for (FeatureAttributeDescriptor attrTarget : featureTypeTarget) {
|
531 |
boolean createIndex;
|
532 |
boolean dropIndex;
|
533 |
FeatureAttributeDescriptor attrOriginal = featureTypeSource.getAttributeDescriptor(attrTarget.getName()); |
534 |
if( attrOriginal != null ) { |
535 |
createIndex = (attrTarget.isIndexed() && !attrOriginal.isIndexed()); |
536 |
dropIndex = (!attrTarget.isIndexed() && attrOriginal.isIndexed()); |
537 |
} else {
|
538 |
createIndex = attrTarget.isIndexed(); |
539 |
dropIndex = false;
|
540 |
} |
541 |
if( createIndex ) {
|
542 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
543 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) {
|
544 |
sqlbuilder.create_index().spatial(); |
545 |
} |
546 |
sqlbuilder.create_index().if_not_exist(); |
547 |
sqlbuilder.create_index().name(table.getTable(), attrTarget.getName()); |
548 |
sqlbuilder.create_index().column(attrTarget.getName()); |
549 |
sqlbuilder.create_index().table() |
550 |
.database(this.table.getDatabase())
|
551 |
.schema(this.table.getSchema())
|
552 |
.name(this.table.getTable());
|
553 |
sqlbuilder.setProperties( |
554 |
Variable.class, |
555 |
PROP_TABLE, table |
556 |
); |
557 |
sqls.addAll(sqlbuilder.create_index().toStrings()); |
558 |
} else if( dropIndex ) { |
559 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
560 |
sqlbuilder.drop_index().if_not_exist(); |
561 |
sqlbuilder.drop_index().name( table.getTable(), attrTarget.getName()); |
562 |
} |
563 |
} |
564 |
return sqls;
|
565 |
} |
566 |
|
567 |
public void performUpdateTable(JDBCConnection conn) throws DataException { |
568 |
|
569 |
List<String> sqls = this.getUpdateTableSQLs(); |
570 |
|
571 |
if( !CollectionUtils.isEmpty(sqls) ) {
|
572 |
Statement st = null; |
573 |
String currentsql = null; |
574 |
try {
|
575 |
st = conn.createStatement(); |
576 |
for (String sql : sqls) { |
577 |
currentsql = sql; |
578 |
if( !StringUtils.isBlank(sql) ) {
|
579 |
JDBCUtils.execute(st, sql); |
580 |
} |
581 |
} |
582 |
} catch (SQLException e) { |
583 |
throw new JDBCSQLException(e,currentsql); |
584 |
} finally {
|
585 |
JDBCUtils.closeQuietly(st); |
586 |
} |
587 |
} |
588 |
} |
589 |
} |