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