Revision 363 trunk/org.gvsig.postgresql/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/operations/PostgreSQLPerformChangesOperation.java
PostgreSQLPerformChangesOperation.java | ||
---|---|---|
4 | 4 |
import java.sql.PreparedStatement; |
5 | 5 |
import java.sql.SQLException; |
6 | 6 |
import java.sql.Statement; |
7 |
import java.util.ArrayList; |
|
8 | 7 |
import java.util.Iterator; |
9 |
import java.util.List; |
|
10 |
import org.gvsig.fmap.dal.DataTypes; |
|
11 |
import org.gvsig.fmap.dal.ExpressionBuilder.Parameter; |
|
12 | 8 |
import org.gvsig.fmap.dal.exception.DataException; |
13 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
14 | 9 |
import org.gvsig.fmap.dal.feature.FeatureType; |
15 | 10 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
16 | 11 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
... | ... | |
23 | 18 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
24 | 19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
25 | 20 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.PerformChangesOperation; |
26 |
import org.gvsig.fmap.geom.Geometry; |
|
27 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
28 |
import org.gvsig.fmap.geom.GeometryManager; |
|
29 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
|
30 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
|
31 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
|
32 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
33 |
import org.gvsig.fmap.geom.primitive.Primitive; |
|
34 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
35 | 21 |
import org.gvsig.postgresql.dal.PostgreSQLHelper; |
36 | 22 |
|
37 | 23 |
public class PostgreSQLPerformChangesOperation extends PerformChangesOperation { |
38 | 24 |
|
39 |
private GeometryManager geometryManager; |
|
40 | 25 |
|
41 | 26 |
public PostgreSQLPerformChangesOperation(JDBCHelper helper) { |
42 | 27 |
this(helper, null, null, null, null, null, null, null, null); |
... | ... | |
60 | 45 |
this.tableName = tableName; |
61 | 46 |
this.featureType = featureType; |
62 | 47 |
this.featureTypesChanged = featureTypesChanged; |
63 |
this.geometryManager = GeometryLocator.getGeometryManager(); |
|
64 | 48 |
} |
65 | 49 |
|
66 | 50 |
private PostgreSQLHelper getHelper() { |
... | ... | |
74 | 58 |
String table, |
75 | 59 |
FeatureType type, |
76 | 60 |
Iterator<FeatureProvider> inserteds) throws DataException { |
77 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder();
|
|
61 |
JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(database, schema, table, type);
|
|
78 | 62 |
|
79 |
sqlbuilder.insert().table().database(database).schema(schema).name(table); |
|
80 |
for (FeatureAttributeDescriptor attr : type) { |
|
81 |
if (attr.getType() == DataTypes.GEOMETRY) { |
|
82 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
|
83 |
sqlbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
|
84 |
sqlbuilder.parameter().value(attr.getSRS()).as_constant() |
|
85 |
) |
|
86 |
); |
|
87 |
} else { |
|
88 |
sqlbuilder.insert().column().name(attr.getName()).with_value( |
|
89 |
sqlbuilder.parameter(attr.getName()) |
|
90 |
); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 | 63 |
PreparedStatement st; |
95 | 64 |
String sql = sqlbuilder.insert().toString(); |
96 | 65 |
try { |
... | ... | |
121 | 90 |
FeatureType type, |
122 | 91 |
Iterator<FeatureProvider> updateds) throws DataException { |
123 | 92 |
|
124 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder(); |
|
125 |
sqlbuilder.update().table().database(database).schema(schema).name(table); |
|
126 |
for (FeatureAttributeDescriptor attr : type) { |
|
127 |
if (attr.isPrimaryKey()) { |
|
128 |
sqlbuilder.update().where().and( |
|
129 |
sqlbuilder.eq( |
|
130 |
sqlbuilder.column(attr.getName()), |
|
131 |
sqlbuilder.parameter(attr.getName()).as_variable() |
|
132 |
) |
|
133 |
); |
|
134 |
} if ( ! attr.isAutomatic() && !attr.isReadOnly() ) { |
|
135 |
if (attr.getType() == DataTypes.GEOMETRY) { |
|
136 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
|
137 |
sqlbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
|
138 |
sqlbuilder.parameter().value(attr.getSRS()).as_constant() |
|
139 |
) |
|
140 |
); |
|
141 |
} else { |
|
142 |
sqlbuilder.update().column().name(attr.getName()).with_value( |
|
143 |
sqlbuilder.parameter(attr.getName()).as_variable() |
|
144 |
); |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
if (!sqlbuilder.update().has_where() ) { |
|
149 |
throw new RuntimeException("Operation requires missing pk"); |
|
150 |
} |
|
93 |
JDBCSQLBuilderBase sqlbuilder = buildUpdateSQL(database, schema, table, type); |
|
151 | 94 |
|
152 | 95 |
PreparedStatement st = null; |
153 | 96 |
String sql = sqlbuilder.update().toString(); |
... | ... | |
169 | 112 |
|
170 | 113 |
@Override |
171 | 114 |
public void performUpdateTable(Connection conn, |
115 |
String database, |
|
116 |
String schema, |
|
172 | 117 |
String table, |
173 | 118 |
FeatureType original, |
174 | 119 |
FeatureType target) throws DataException { |
175 | 120 |
|
176 |
SQLBuilderBase sqlbuilder = createSQLBuilder(); |
|
177 |
sqlbuilder.update().table().name(table); |
|
178 |
|
|
179 |
for (FeatureAttributeDescriptor attrOrgiginal : original) { |
|
180 |
FeatureAttributeDescriptor attrTarget = target.getAttributeDescriptor( |
|
181 |
attrOrgiginal.getName() |
|
182 |
); |
|
183 |
if (attrTarget == null) { |
|
184 |
sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
|
185 |
} else { |
|
186 |
sqlbuilder.alter_table().alter_column( |
|
187 |
attrTarget.getName(), |
|
188 |
attrTarget.getType(), |
|
189 |
attrTarget.getPrecision(), |
|
190 |
attrTarget.getSize(), |
|
191 |
attrTarget.isPrimaryKey(), |
|
192 |
attrTarget.isIndexed(), |
|
193 |
attrTarget.allowNull(), |
|
194 |
attrTarget.isAutomatic(), |
|
195 |
attrTarget.getDefaultValue() |
|
196 |
); |
|
197 |
} |
|
198 |
} |
|
199 |
for (FeatureAttributeDescriptor attrTarget : target) { |
|
200 |
if (original.getAttributeDescriptor(attrTarget.getName()) == null) { |
|
201 |
sqlbuilder.alter_table().add_column( |
|
202 |
attrTarget.getName(), |
|
203 |
attrTarget.getType(), |
|
204 |
attrTarget.getPrecision(), |
|
205 |
attrTarget.getSize(), |
|
206 |
attrTarget.isPrimaryKey(), |
|
207 |
attrTarget.isIndexed(), |
|
208 |
attrTarget.allowNull(), |
|
209 |
attrTarget.isAutomatic(), |
|
210 |
attrTarget.getDefaultValue() |
|
211 |
); |
|
212 |
} |
|
213 |
} |
|
121 |
SQLBuilderBase sqlbuilder = buildUpdateTableSQL(database, schema, table, original, target); |
|
214 | 122 |
Statement st = null; |
215 | 123 |
try { |
216 | 124 |
st = conn.createStatement(); |
Also available in: Unified diff