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