Revision 28424 branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/postgresql/PostgreSQLStoreProviderWriter.java
PostgreSQLStoreProviderWriter.java | ||
---|---|---|
32 | 32 |
import java.sql.SQLException; |
33 | 33 |
import java.sql.Statement; |
34 | 34 |
import java.util.ArrayList; |
35 |
import java.util.Arrays; |
|
35 | 36 |
import java.util.Collections; |
36 | 37 |
import java.util.Iterator; |
37 | 38 |
import java.util.List; |
... | ... | |
44 | 45 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
45 | 46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 | 47 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
48 |
import org.gvsig.fmap.dal.feature.spi.FeatureData; |
|
47 | 49 |
import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
48 | 50 |
import org.gvsig.fmap.dal.store.jdbc.JDBCExecutePreparedSQLException; |
49 | 51 |
import org.gvsig.fmap.dal.store.jdbc.JDBCExecuteSQLException; |
... | ... | |
70 | 72 |
} |
71 | 73 |
|
72 | 74 |
public boolean allowWrite() { |
73 |
return (!directSQLMode) && params.getPkFields() != null |
|
74 |
&& params.getPkFields().length > 0; |
|
75 |
if (directSQLMode) { |
|
76 |
return false; |
|
77 |
} |
|
78 |
if (params.getPkFields() == null || params.getPkFields().length > 0) { |
|
79 |
FeatureType ft = null; |
|
80 |
try { |
|
81 |
ft = this.store.getDefaultFeatureType(); |
|
82 |
} catch (DataException e) { |
|
83 |
logger.error("Excepton get default Feature Type", e); |
|
84 |
} |
|
85 |
|
|
86 |
if (ft == null) { |
|
87 |
return false; |
|
88 |
} |
|
89 |
FeatureAttributeDescriptor attr; |
|
90 |
Iterator iter = ft.iterator(); |
|
91 |
while (iter.hasNext()){ |
|
92 |
attr = (FeatureAttributeDescriptor) iter.next(); |
|
93 |
if (attr.isPrimaryKey()){ |
|
94 |
return true; |
|
95 |
} |
|
96 |
} |
|
97 |
return false; |
|
98 |
|
|
99 |
} else { |
|
100 |
return true; |
|
101 |
} |
|
75 | 102 |
} |
76 | 103 |
|
77 | 104 |
|
... | ... | |
225 | 252 |
.getIndex()))); |
226 | 253 |
} |
227 | 254 |
} |
255 |
} |
|
228 | 256 |
|
257 |
private void addToListFeatureValues(FeatureData featureData, |
|
258 |
List attributes, List values) throws DataException { |
|
259 |
FeatureAttributeDescriptor attr, attrOfList; |
|
260 |
FeatureType fType = featureData.getType(); |
|
261 |
for (int i = 0; i < attributes.size(); i++) { |
|
262 |
attrOfList = (FeatureAttributeDescriptor) attributes.get(i); |
|
263 |
attr = fType.getAttributeDescriptor(attrOfList.getName()); |
|
264 |
if (attr == null) { |
|
265 |
if (attrOfList.isPrimaryKey()) { |
|
266 |
// FIXME excepton |
|
267 |
throw new RuntimeException("pk attribute '" |
|
268 |
+ attrOfList.getName() + "' not found in feature"); |
|
269 |
} |
|
270 |
values.add(helper.dalValueToJDBC(attr, attrOfList |
|
271 |
.getDefaultValue())); |
|
272 |
} else { |
|
273 |
values.add(helper.dalValueToJDBC(attr, featureData.get(attr |
|
274 |
.getIndex()))); |
|
275 |
if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
276 |
values.add(helper.getPostgisSRID(attr.getSRS())); |
|
277 |
} |
|
278 |
} |
|
279 |
} |
|
229 | 280 |
} |
230 | 281 |
|
231 | 282 |
private void performDeletes(Connection conn, Iterator deleteds, |
... | ... | |
297 | 348 |
|
298 | 349 |
sqlb.append(") = ("); |
299 | 350 |
for (int i = 0; i < updateAttrs.size() - 1; i++) { |
300 |
sqlb.append("?, "); |
|
351 |
attr = (FeatureAttributeDescriptor) updateAttrs.get(i); |
|
352 |
if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
353 |
sqlb.append("GeomFromWKB(?, ?), "); |
|
354 |
} else { |
|
355 |
sqlb.append("?, "); |
|
356 |
} |
|
301 | 357 |
} |
302 |
sqlb.append("? "); |
|
358 |
attr = (FeatureAttributeDescriptor) updateAttrs |
|
359 |
.get(updateAttrs.size() - 1); |
|
360 |
if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
361 |
sqlb.append("GeomFromWKB(?, ?) "); |
|
362 |
} else { |
|
363 |
sqlb.append("? "); |
|
364 |
} |
|
303 | 365 |
|
304 | 366 |
sqlb.append(") "); |
305 | 367 |
appendToSQLPreparedPkWhereClause(sqlb, pkAttributes); |
... | ... | |
318 | 380 |
} |
319 | 381 |
|
320 | 382 |
private void executePreparedStatement(Connection conn, String sql, |
321 |
List attributes, Iterator features) throws DataException { |
|
383 |
List attributes, Iterator featureDatas) throws DataException {
|
|
322 | 384 |
PreparedStatement st; |
323 | 385 |
try { |
324 | 386 |
st = conn.prepareStatement(sql); |
... | ... | |
327 | 389 |
} |
328 | 390 |
try { |
329 | 391 |
List values = new ArrayList(); |
330 |
Feature feature;
|
|
331 |
while (features.hasNext()) { |
|
392 |
FeatureData featureData;
|
|
393 |
while (featureDatas.hasNext()) {
|
|
332 | 394 |
st.clearParameters(); |
333 |
feature = (Feature) features.next();
|
|
395 |
featureData = (FeatureData) featureDatas.next();
|
|
334 | 396 |
values.clear(); |
335 |
addToListFeatureValues(feature, attributes, values); |
|
397 |
addToListFeatureValues(featureData, attributes, values);
|
|
336 | 398 |
for (int i = 0; i < values.size(); i++) { |
337 | 399 |
st.setObject(i + 1, values.get(i)); |
338 | 400 |
} |
... | ... | |
368 | 430 |
} |
369 | 431 |
try { |
370 | 432 |
while (inserteds.hasNext()) { |
371 |
perfomInsert(conn, st, sql, (Feature) inserteds.next(), attrs); |
|
433 |
perfomInsert(conn, st, sql, (FeatureData) inserteds.next(), |
|
434 |
attrs); |
|
372 | 435 |
} |
373 | 436 |
} finally { |
374 | 437 |
try { |
... | ... | |
420 | 483 |
|
421 | 484 |
sqlb.append(") VALUES ("); |
422 | 485 |
for (int i = 0; i < attributes.size() - 1; i++) { |
423 |
sqlb.append("?, "); |
|
486 |
attr = (FeatureAttributeDescriptor) attributes.get(i); |
|
487 |
if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
488 |
sqlb.append("GeomFromWKB(?,?), "); |
|
489 |
} else { |
|
490 |
sqlb.append("?, "); |
|
491 |
} |
|
424 | 492 |
} |
425 |
sqlb.append("? "); |
|
493 |
attr = (FeatureAttributeDescriptor) attributes |
|
494 |
.get(attributes.size() - 1); |
|
495 |
if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
496 |
sqlb.append("GeomFromWKB(?,?) "); |
|
497 |
} else { |
|
498 |
sqlb.append("? "); |
|
499 |
} |
|
426 | 500 |
|
427 | 501 |
sqlb.append(") "); |
428 | 502 |
|
429 | 503 |
} |
430 | 504 |
|
431 | 505 |
private void perfomInsert(Connection conn, PreparedStatement insertSt, |
432 |
String sql, Feature feature, List attributes) throws DataException { |
|
506 |
String sql, FeatureData feature, List attributes) |
|
507 |
throws DataException { |
|
433 | 508 |
|
434 | 509 |
try { |
435 | 510 |
insertSt.clearParameters(); |
436 | 511 |
List values = new ArrayList(); |
437 | 512 |
addToListFeatureValues(feature, attributes, values); |
513 |
FeatureAttributeDescriptor attr; |
|
514 |
int j = 1; |
|
438 | 515 |
for (int i = 0; i < values.size(); i++) { |
439 |
insertSt.setObject(i + 1, values.get(i)); |
|
516 |
insertSt.setObject(j, values.get(i)); |
|
517 |
j++; |
|
440 | 518 |
} |
519 |
if (logger.isDebugEnabled()) { |
|
520 |
logger.debug("Executing insert. sql={} value={}", new Object[] { |
|
521 |
sql, |
|
522 |
values }); |
|
523 |
} |
|
441 | 524 |
try { |
442 | 525 |
insertSt.execute(); |
443 | 526 |
} catch (SQLException e) { |
... | ... | |
560 | 643 |
} |
561 | 644 |
|
562 | 645 |
|
563 |
private Object getSqlGeometyFieldAdd(FeatureAttributeDescriptor attr) { |
|
564 |
// SELECT AddGeometryColumn({schema}, {table}, {field}, {srid}(int), |
|
565 |
// {geomType}(Str), {dimensions}(int)) |
|
566 |
|
|
567 |
// gemoType: |
|
568 |
/* |
|
569 |
* POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, |
|
570 |
* MULTIPOLYGON, GEOMETRYCOLLECTION POINTM, LINESTRINGM, POLYGONM, |
|
571 |
* MULTIPOINTM, MULTILINESTRINGM, MULTIPOLYGONM, GEOMETRYCOLLECTIONM |
|
572 |
*/ |
|
573 |
|
|
574 |
|
|
575 |
StringBuilder strb = new StringBuilder(); |
|
576 |
strb.append("SELECT AddGeometryColumn('"); |
|
577 |
if (params.getSchema() != null && params.getSchema().length() > 0) { |
|
578 |
strb.append(params.getSchema()); |
|
579 |
strb.append("', '"); |
|
580 |
} |
|
581 |
strb.append(params.getTable()); |
|
582 |
strb.append("', '"); |
|
583 |
strb.append(attr.getName()); |
|
584 |
strb.append("', "); |
|
585 |
strb.append(helper.getPostgisSRID(attr.getSRS())); |
|
586 |
strb.append(", '"); |
|
587 |
strb.append(helper.getPostgisGeomType(attr.getGeometryType(), attr |
|
588 |
.getGeometrySubType())); |
|
589 |
strb.append(helper.getPostgisGeomDimensions(attr.getGeometrySubType())); |
|
590 |
return null; |
|
591 |
} |
|
592 |
|
|
593 | 646 |
private String getSqlActionDropField(FeatureAttributeDescriptor attr) { |
594 | 647 |
// DROP [ COLUMN ] column |
595 | 648 |
return " DROP COLUMN " + this.helper.escapeFieldName(attr.getName()); |
... | ... | |
614 | 667 |
sqlgeomColumnsChanges.add(getSqlGeometyFieldDrop(attrOrg)); |
615 | 668 |
} |
616 | 669 |
if (attrTrg.getDataType() == DataTypes.GEOMETRY) { |
617 |
sqlgeomColumnsChanges.add(getSqlGeometyFieldAdd(attrTrg)); |
|
670 |
sqlgeomColumnsChanges.addAll(Arrays.asList(helper |
|
671 |
.getSqlGeometyFieldAdd(attrTrg, params.getTable(), |
|
672 |
params.getSchema()))); |
|
618 | 673 |
} |
619 | 674 |
} |
620 | 675 |
if (attrOrg.getDataType() == attrTrg.getDataType() |
Also available in: Unified diff