Revision 43650
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialSQLBuilder.java | ||
---|---|---|
5 | 5 |
import java.util.ArrayList; |
6 | 6 |
import java.util.Date; |
7 | 7 |
import java.util.List; |
8 |
import org.apache.commons.lang3.tuple.Pair; |
|
8 | 9 |
import org.gvsig.fmap.dal.DataTypes; |
9 | 10 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
10 | 11 |
import org.gvsig.fmap.dal.feature.FeatureType; |
... | ... | |
45 | 46 |
|
46 | 47 |
// config.set(SQLConfig.UPDATE_TABLE_STATISTICS_table,"ANALYZE TABLE {0} SAMPLE_SIZE 0"); |
47 | 48 |
config.set(SQLConfig.UPDATE_TABLE_STATISTICS_table,"ANALYZE SAMPLE_SIZE 0"); |
48 |
config.set(SQLConfig.CREATE_INDEX_name_ON_table_column, "CREATE INDEX {0} ON {1} (\"{2}\")"); |
|
49 |
config.set(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column, "CREATE INDEX {0} ON {1} (\"{2}\")");
|
|
49 |
config.set(SQLConfig.CREATE_INDEX_name_ON_table_column, "CREATE INDEX IF NOT EXISTS {0} ON {1} (\"{2}\")");
|
|
50 |
config.set(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column, "CREATE SPATIAL INDEX IF NOT EXISTS {0} ON {1} (\"{2}\")");
|
|
50 | 51 |
|
51 | 52 |
config.set(SQLConfig.ST_GeomFromEWKB, "GeomFromWKB({0}, {1})"); |
52 | 53 |
config.set(SQLConfig.ST_AsEWKB, "AsWKB(({0}))"); |
... | ... | |
97 | 98 |
|
98 | 99 |
} |
99 | 100 |
|
101 |
protected class H2SpatialAlterTableBuilderBase extends AlterTableBuilderBase { |
|
102 |
@Override |
|
103 |
public List<String> toStrings() { |
|
104 |
List<String> sqls = new ArrayList<>(); |
|
105 |
for (String column : drops) { |
|
106 |
StringBuilder builder = new StringBuilder(); |
|
107 |
builder.append("ALTER TABLE "); |
|
108 |
builder.append(this.table.toString()); |
|
109 |
builder.append(" DROP COLUMN IF EXISTS "); |
|
110 |
builder.append(identifier(column)); |
|
111 |
sqls.add(builder.toString()); |
|
112 |
} |
|
113 |
for (ColumnDescriptorBuilderBase column : adds) { |
|
114 |
StringBuilder builder = new StringBuilder(); |
|
115 |
builder.append("ALTER TABLE "); |
|
116 |
builder.append(this.table.toString()); |
|
117 |
builder.append(" ADD COLUMN "); |
|
118 |
builder.append(identifier(column.getName())); |
|
119 |
builder.append(" "); |
|
120 |
if( column.getType() == DataTypes.INT && column.isAutomatic() ) { |
|
121 |
builder.append(" SERIAL"); |
|
122 |
} else { |
|
123 |
builder.append( |
|
124 |
sqltype( |
|
125 |
column.getType(), |
|
126 |
column.getPrecision(), |
|
127 |
column.getSize(), |
|
128 |
column.getGeometryType(), |
|
129 |
column.getGeometrySubtype() |
|
130 |
) |
|
131 |
); |
|
132 |
} |
|
133 |
if (column.getDefaultValue() == null) { |
|
134 |
if (column.allowNulls()) { |
|
135 |
builder.append(" DEFAULT NULL"); |
|
136 |
} |
|
137 |
} else { |
|
138 |
builder.append(" DEFAULT '"); |
|
139 |
builder.append(column.getDefaultValue().toString()); |
|
140 |
builder.append("'"); |
|
141 |
} |
|
142 |
if (column.allowNulls()) { |
|
143 |
builder.append(" NULL"); |
|
144 |
} else { |
|
145 |
builder.append(" NOT NULL"); |
|
146 |
} |
|
147 |
if (column.isPrimaryKey()) { |
|
148 |
builder.append(" PRIMARY KEY"); |
|
149 |
} |
|
150 |
sqls.add(builder.toString()); |
|
151 |
if( column.isIndexed() ) { |
|
152 |
String sql; |
|
153 |
String name = "idx_" + this.table().getName() + "_" + column.getName(); |
|
154 |
if( column.isGeometry() ) { |
|
155 |
sql = MessageFormat.format( |
|
156 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column), |
|
157 |
name, |
|
158 |
this.table().toString(), |
|
159 |
column.getName() |
|
160 |
); |
|
161 |
} else { |
|
162 |
sql = MessageFormat.format( |
|
163 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_column), |
|
164 |
name, |
|
165 |
this.table().toString(), |
|
166 |
column.getName() |
|
167 |
); |
|
168 |
} |
|
169 |
sqls.add(sql); |
|
170 |
} |
|
171 |
} |
|
172 |
for (ColumnDescriptorBuilderBase column : alters) { |
|
173 |
StringBuilder builder = new StringBuilder(); |
|
174 |
builder.append("ALTER TABLE "); |
|
175 |
builder.append(this.table.toString()); |
|
176 |
builder.append(" ALTER COLUMN "); |
|
177 |
builder.append(identifier(column.getName())); |
|
178 |
builder.append(" "); |
|
179 |
builder.append( |
|
180 |
sqltype( |
|
181 |
column.getType(), |
|
182 |
column.getPrecision(), |
|
183 |
column.getSize(), |
|
184 |
column.getGeometryType(), |
|
185 |
column.getGeometrySubtype() |
|
186 |
) |
|
187 |
); |
|
188 |
if (column.getDefaultValue() == null) { |
|
189 |
if (column.allowNulls()) { |
|
190 |
builder.append(" DEFAULT NULL"); |
|
191 |
} |
|
192 |
} else { |
|
193 |
builder.append(" DEFAULT '"); |
|
194 |
builder.append(column.getDefaultValue().toString()); |
|
195 |
builder.append("'"); |
|
196 |
} |
|
197 |
if( column.isAutomatic() ) { |
|
198 |
builder.append(" AUTO_INCREMENT"); |
|
199 |
} |
|
200 |
sqls.add(builder.toString()); |
|
201 |
if( column.isIndexed() ) { |
|
202 |
String sql; |
|
203 |
String name = "idx_" + this.table().getName() + "_" + column.getName(); |
|
204 |
if( column.isGeometry() ) { |
|
205 |
sql = MessageFormat.format( |
|
206 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column), |
|
207 |
name, |
|
208 |
this.table().toString(), |
|
209 |
column.getName() |
|
210 |
); |
|
211 |
} else { |
|
212 |
sql = MessageFormat.format( |
|
213 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_column), |
|
214 |
name, |
|
215 |
this.table().toString(), |
|
216 |
column.getName() |
|
217 |
); |
|
218 |
} |
|
219 |
sqls.add(sql); |
|
220 |
} |
|
221 |
} |
|
222 |
for (Pair<String,String> pair : renames) { |
|
223 |
StringBuilder builder = new StringBuilder(); |
|
224 |
builder.append("ALTER TABLE "); |
|
225 |
builder.append(this.table.toString()); |
|
226 |
builder.append(" RENAME COLUMN "); |
|
227 |
builder.append(identifier(pair.getLeft())); |
|
228 |
builder.append(" TO "); |
|
229 |
builder.append(identifier(pair.getRight())); |
|
230 |
sqls.add(builder.toString()); |
|
231 |
} |
|
232 |
return sqls; |
|
233 |
} |
|
234 |
|
|
235 |
} |
|
236 |
|
|
100 | 237 |
protected class H2SpatialCreateTableBuilder extends CreateTableBuilderBase { |
101 | 238 |
|
102 | 239 |
@Override |
... | ... | |
118 | 255 |
} |
119 | 256 |
builder.append(identifier(column.getName())); |
120 | 257 |
builder.append(" "); |
121 |
if( column.isGeometry() ) { |
|
122 |
String h2geomtype = getH2SpatialGeometryTypeFromGeometryType( |
|
258 |
builder.append( |
|
259 |
sqltype( |
|
260 |
column.getType(), |
|
261 |
column.getPrecision(), |
|
262 |
column.getSize(), |
|
123 | 263 |
column.getGeometryType(), |
124 | 264 |
column.getGeometrySubtype() |
125 |
); |
|
126 |
builder.append(h2geomtype); |
|
127 |
} else { |
|
128 |
builder.append(sqltype(column.getType(), column.getPrecision(), column.getSize())); |
|
265 |
) |
|
266 |
); |
|
267 |
if( !column.isGeometry() ) { |
|
129 | 268 |
if (column.isPrimaryKey()) { |
130 | 269 |
builder.append(" PRIMARY KEY"); |
131 | 270 |
} |
... | ... | |
147 | 286 |
builder.append(column.getDefaultValue().toString()); |
148 | 287 |
builder.append("'"); |
149 | 288 |
} |
289 |
} |
|
150 | 290 |
} |
151 |
} |
|
152 | 291 |
if (!column.allowNulls()) { |
153 | 292 |
builder.append(" NOT NULL"); |
154 | 293 |
} |
... | ... | |
180 | 319 |
|
181 | 320 |
return sqls; |
182 | 321 |
} |
183 |
|
|
184 |
public String getH2SpatialGeometryTypeFromGeometryType(int geomtype, int geomsubtype) { |
|
185 |
// |
|
186 |
// https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view |
|
187 |
// |
|
188 |
|
|
189 |
switch(geomtype) { |
|
190 |
case Geometry.TYPES.POINT: |
|
191 |
return "POINT"; |
|
192 |
case Geometry.TYPES.MULTIPOINT: |
|
193 |
return "MULTIPOINT"; |
|
194 |
case Geometry.TYPES.LINE: |
|
195 |
return "LINESTRING"; |
|
196 |
case Geometry.TYPES.MULTILINE: |
|
197 |
return "MULTILINESTRING"; |
|
198 |
case Geometry.TYPES.POLYGON: |
|
199 |
return "POLYGON"; |
|
200 |
case Geometry.TYPES.MULTIPOLYGON: |
|
201 |
return "MULTIPOLYGON"; |
|
202 |
default: |
|
203 |
return "GEOMETRY"; |
|
204 |
} |
|
205 |
} |
|
206 | 322 |
} |
207 | 323 |
|
208 | 324 |
public class H2SpatialSelectBuilderBase extends SelectBuilderBase { |
... | ... | |
317 | 433 |
} |
318 | 434 |
} |
319 | 435 |
|
436 |
@Override |
|
437 |
public String sqltype(int type, int p, int s, int geomtype, int geomSubtype) { |
|
438 |
if( type!=DataTypes.GEOMETRY ) { |
|
439 |
return super.sqltype(type, p, s, geomtype, geomSubtype); |
|
440 |
} |
|
441 |
// |
|
442 |
// https://github.com/orbisgis/h2gis/wiki/1.-Spatial-data#geometry-columns-view |
|
443 |
// |
|
444 |
switch(geomtype) { |
|
445 |
case Geometry.TYPES.POINT: |
|
446 |
return "POINT"; |
|
447 |
case Geometry.TYPES.MULTIPOINT: |
|
448 |
return "MULTIPOINT"; |
|
449 |
case Geometry.TYPES.LINE: |
|
450 |
return "LINESTRING"; |
|
451 |
case Geometry.TYPES.MULTILINE: |
|
452 |
return "MULTILINESTRING"; |
|
453 |
case Geometry.TYPES.POLYGON: |
|
454 |
return "POLYGON"; |
|
455 |
case Geometry.TYPES.MULTIPOLYGON: |
|
456 |
return "MULTIPOLYGON"; |
|
457 |
default: |
|
458 |
return "GEOMETRY"; |
|
459 |
} |
|
460 |
} |
|
461 |
|
|
320 | 462 |
public H2SpatialHelper getHelper() { |
321 | 463 |
return this.helper; |
322 | 464 |
} |
... | ... | |
365 | 507 |
throw new RuntimeException("Can't set parameters to prepared statement from the feature (" + f + ")", ex); |
366 | 508 |
} |
367 | 509 |
} |
510 |
|
|
511 |
@Override |
|
512 |
protected AlterTableBuilder createAlterTableBuilder() { |
|
513 |
return new H2SpatialAlterTableBuilderBase(); |
|
514 |
} |
|
368 | 515 |
|
369 | 516 |
@Override |
370 | 517 |
protected TableNameBuilder createTableNameBuilder() { |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/operations/H2SpatialAppendOperation.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.fmap.dal.store.h2.operations; |
|
3 |
|
|
4 |
import java.sql.PreparedStatement; |
|
5 |
import java.sql.SQLException; |
|
6 |
import org.gvsig.fmap.dal.DataTypes; |
|
7 |
import org.gvsig.fmap.dal.exception.DataException; |
|
8 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
9 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
10 |
import org.gvsig.fmap.dal.feature.exception.AlreadyEditingException; |
|
11 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
12 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
|
13 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
|
14 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
|
15 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.AppendOperation; |
|
16 |
import org.gvsig.tools.dispose.Disposable; |
|
17 |
import org.gvsig.tools.dispose.DisposeUtils; |
|
18 |
|
|
19 |
|
|
20 |
public class H2SpatialAppendOperation extends AppendOperation { |
|
21 |
|
|
22 |
public H2SpatialAppendOperation( |
|
23 |
JDBCHelper helper, |
|
24 |
String database, |
|
25 |
String schema, |
|
26 |
String table, |
|
27 |
FeatureType type |
|
28 |
) { |
|
29 |
super(helper, database, schema, table, type); |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public void begin() throws DataException { |
|
34 |
if (this.sqlbuilder != null) { |
|
35 |
throw new AlreadyEditingException(this.helper.getSourceId()); |
|
36 |
} |
|
37 |
|
|
38 |
try { |
|
39 |
this.connection = this.helper.getConnectionWritable(); |
|
40 |
|
|
41 |
this.sqlbuilder = this.helper.createSQLBuilder(); |
|
42 |
|
|
43 |
this.sqlbuilder.insert().table().database(this.database).schema(this.schema).name(this.table); |
|
44 |
for (FeatureAttributeDescriptor attr : type) { |
|
45 |
if( attr.isAutomatic() ) { |
|
46 |
continue; |
|
47 |
} |
|
48 |
if (attr.getType() == DataTypes.GEOMETRY) { |
|
49 |
this.sqlbuilder.insert().column().name(attr.getName()).with_value( |
|
50 |
sqlbuilder.parameter(attr.getName()).as_geometry_variable().srs( |
|
51 |
sqlbuilder.parameter().value(attr.getSRS()) |
|
52 |
) |
|
53 |
); |
|
54 |
} else { |
|
55 |
this.sqlbuilder.insert().column().name(attr.getName()).with_value( |
|
56 |
sqlbuilder.parameter(attr.getName()).as_variable() |
|
57 |
); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
PreparedStatement st; |
|
62 |
this.sql = this.sqlbuilder.insert().toString(); |
|
63 |
this.preparedStatement = this.connection.prepareStatement(sql); |
|
64 |
this.connection.setAutoCommit(false); |
|
65 |
JDBCUtils.execute(this.connection, "SET LOG 1"); |
|
66 |
JDBCUtils.execute(this.connection, "SET LOCK_MODE 1"); |
|
67 |
JDBCUtils.execute(this.connection, "SET UNDO_LOG 0"); |
|
68 |
|
|
69 |
} catch (SQLException ex) { |
|
70 |
throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex); |
|
71 |
} |
|
72 |
|
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
protected void clean() { |
|
77 |
JDBCUtils.closeQuietly(this.preparedStatement); |
|
78 |
this.helper.closeConnection(this.connection); |
|
79 |
this.connection = null; |
|
80 |
this.preparedStatement = null; |
|
81 |
this.sqlbuilder = null; |
|
82 |
this.sql = null; |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public void end() { |
|
87 |
try { |
|
88 |
this.connection.commit(); |
|
89 |
JDBCUtils.execute(this.connection, "SET LOG 2"); |
|
90 |
JDBCUtils.execute(this.connection, "SET LOCK_MODE 3"); |
|
91 |
JDBCUtils.execute(this.connection, "SET UNDO_LOG 1"); |
|
92 |
} catch (SQLException ex) { |
|
93 |
try { |
|
94 |
this.connection.rollback(); |
|
95 |
} catch (SQLException ex1) { |
|
96 |
} |
|
97 |
throw new RuntimeException("Can't commit transaction", ex); |
|
98 |
} finally { |
|
99 |
clean(); |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
@Override |
|
104 |
public void abort() { |
|
105 |
try { |
|
106 |
this.connection.rollback(); |
|
107 |
JDBCUtils.execute(this.connection, "SET LOG 2"); |
|
108 |
JDBCUtils.execute(this.connection, "SET LOCK_MODE 3"); |
|
109 |
JDBCUtils.execute(this.connection, "SET UNDO_LOG 1"); |
|
110 |
} catch (SQLException ex) { |
|
111 |
} |
|
112 |
clean(); |
|
113 |
} |
|
114 |
|
|
115 |
@Override |
|
116 |
public void append(FeatureProvider feature) throws DataException { |
|
117 |
int n; |
|
118 |
Disposable paramsDisposer = null; |
|
119 |
try { |
|
120 |
paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature); |
|
121 |
n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql); |
|
122 |
} catch(Exception ex) { |
|
123 |
throw new RuntimeException("Can't insert feature.", ex); |
|
124 |
} finally { |
|
125 |
DisposeUtils.disposeQuietly(paramsDisposer); |
|
126 |
} |
|
127 |
if( n<1 ) { |
|
128 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
|
129 |
} |
|
130 |
} |
|
131 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/operations/H2SpatialOperationsFactory.java | ||
---|---|---|
4 | 4 |
import java.util.List; |
5 | 5 |
import org.cresques.cts.IProjection; |
6 | 6 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
7 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
7 | 8 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
8 | 9 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
10 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.AppendOperation; |
|
9 | 11 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
10 | 12 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation; |
11 | 13 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.OperationsFactoryBase; |
... | ... | |
29 | 31 |
public ListTablesOperation createListTables(int mode, JDBCStoreParameters baseParameters, boolean informationTables) { |
30 | 32 |
return new H2SpatialListTablesOperation(helper, mode, baseParameters, informationTables); |
31 | 33 |
} |
34 |
|
|
35 |
@Override |
|
36 |
public AppendOperation createAppend(String database, String schema, String table, FeatureType type) { |
|
37 |
return new H2SpatialAppendOperation(helper, database, schema, table, type); |
|
38 |
} |
|
32 | 39 |
|
33 | 40 |
|
34 | 41 |
|
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 | ||
---|---|---|
5 | 5 |
import java.sql.SQLException; |
6 | 6 |
import java.sql.Statement; |
7 | 7 |
import java.util.Iterator; |
8 |
import org.apache.commons.lang3.StringUtils; |
|
8 | 9 |
import org.gvsig.fmap.dal.DataTypes; |
9 | 10 |
import org.gvsig.fmap.dal.exception.DataException; |
10 | 11 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
... | ... | |
184 | 185 |
JDBCSQLBuilderBase sqlbuilder = buildInsertSQL(database, schema, table, type); |
185 | 186 |
|
186 | 187 |
PreparedStatement st; |
187 |
Disposable paramsDisposer = null;
|
|
188 |
Disposable paramsDisposer; |
|
188 | 189 |
String sql = sqlbuilder.insert().toString(); |
189 | 190 |
try { |
190 | 191 |
st = conn.prepareStatement(sql); |
... | ... | |
283 | 284 |
FeatureType target |
284 | 285 |
) { |
285 | 286 |
JDBCSQLBuilderBase sqlbuilder = createSQLBuilder(); |
286 |
sqlbuilder.update().table().database(database).schema(schema).name(table);
|
|
287 |
sqlbuilder.alter_table().table().database(database).schema(schema).name(table);
|
|
287 | 288 |
|
288 | 289 |
for (FeatureAttributeDescriptor attrOrgiginal : original) { |
289 | 290 |
FeatureAttributeDescriptor attrTarget = target.getAttributeDescriptor( |
... | ... | |
291 | 292 |
); |
292 | 293 |
if (attrTarget == null) { |
293 | 294 |
sqlbuilder.alter_table().drop_column(attrOrgiginal.getName()); |
294 |
} else { |
|
295 |
sqlbuilder.alter_table().alter_column( |
|
296 |
attrTarget.getName(), |
|
297 |
attrTarget.getType(), |
|
298 |
attrTarget.getPrecision(), |
|
299 |
attrTarget.getSize(), |
|
300 |
attrTarget.isPrimaryKey(), |
|
301 |
attrTarget.isIndexed(), |
|
302 |
attrTarget.allowNull(), |
|
303 |
attrTarget.isAutomatic(), |
|
304 |
attrTarget.getDefaultValue() |
|
305 |
); |
|
295 |
} else if( !this.areEquals(attrOrgiginal, attrTarget) ) { |
|
296 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) { |
|
297 |
sqlbuilder.alter_table().alter_geometry_column( |
|
298 |
attrTarget.getName(), |
|
299 |
attrTarget.getGeomType().getType(), |
|
300 |
attrTarget.getGeomType().getSubType(), |
|
301 |
attrTarget.getSRS(), |
|
302 |
attrTarget.isIndexed(), |
|
303 |
attrTarget.allowNull() |
|
304 |
); |
|
305 |
} else { |
|
306 |
sqlbuilder.alter_table().alter_column( |
|
307 |
attrTarget.getName(), |
|
308 |
attrTarget.getType(), |
|
309 |
attrTarget.getPrecision(), |
|
310 |
attrTarget.getSize(), |
|
311 |
attrTarget.isPrimaryKey(), |
|
312 |
attrTarget.isIndexed(), |
|
313 |
attrTarget.allowNull(), |
|
314 |
attrTarget.isAutomatic(), |
|
315 |
attrTarget.getDefaultValue() |
|
316 |
); |
|
317 |
} |
|
306 | 318 |
} |
307 | 319 |
} |
308 | 320 |
for (FeatureAttributeDescriptor attrTarget : target) { |
309 | 321 |
if (original.getAttributeDescriptor(attrTarget.getName()) == null) { |
310 |
sqlbuilder.alter_table().add_column( |
|
311 |
attrTarget.getName(), |
|
312 |
attrTarget.getType(), |
|
313 |
attrTarget.getPrecision(), |
|
314 |
attrTarget.getSize(), |
|
315 |
attrTarget.isPrimaryKey(), |
|
316 |
attrTarget.isIndexed(), |
|
317 |
attrTarget.allowNull(), |
|
318 |
attrTarget.isAutomatic(), |
|
319 |
attrTarget.getDefaultValue() |
|
320 |
); |
|
322 |
if( attrTarget.getType()==DataTypes.GEOMETRY ) { |
|
323 |
sqlbuilder.alter_table().add_geometry_column( |
|
324 |
attrTarget.getName(), |
|
325 |
attrTarget.getGeomType().getType(), |
|
326 |
attrTarget.getGeomType().getSubType(), |
|
327 |
attrTarget.getSRS(), |
|
328 |
attrTarget.isIndexed(), |
|
329 |
attrTarget.allowNull() |
|
330 |
); |
|
331 |
} else { |
|
332 |
sqlbuilder.alter_table().add_column( |
|
333 |
attrTarget.getName(), |
|
334 |
attrTarget.getType(), |
|
335 |
attrTarget.getPrecision(), |
|
336 |
attrTarget.getSize(), |
|
337 |
attrTarget.isPrimaryKey(), |
|
338 |
attrTarget.isIndexed(), |
|
339 |
attrTarget.allowNull(), |
|
340 |
attrTarget.isAutomatic(), |
|
341 |
attrTarget.getDefaultValue() |
|
342 |
); |
|
343 |
} |
|
321 | 344 |
} |
322 | 345 |
} |
323 | 346 |
return sqlbuilder; |
324 | 347 |
} |
348 |
|
|
349 |
protected boolean areEquals(FeatureAttributeDescriptor attr1, FeatureAttributeDescriptor attr2) { |
|
350 |
// No interesa si son o no iguales en general, solo si son iguales en lo |
|
351 |
// que a los atributos usados para crear la columna de la tabla se refiere. |
|
352 |
if( !StringUtils.equals(attr1.getName(), attr2.getName()) ) { |
|
353 |
return false; |
|
354 |
} |
|
355 |
if( attr1.getType() != attr2.getType() ) { |
|
356 |
return false; |
|
357 |
} |
|
358 |
if( attr1.getPrecision() != attr2.getPrecision() ) { |
|
359 |
return false; |
|
360 |
} |
|
361 |
if( attr1.getSize() != attr2.getSize() ) { |
|
362 |
return false; |
|
363 |
} |
|
364 |
if( attr1.isPrimaryKey() != attr2.isPrimaryKey() ) { |
|
365 |
return false; |
|
366 |
} |
|
367 |
if( attr1.isIndexed() != attr2.isIndexed() ) { |
|
368 |
return false; |
|
369 |
} |
|
370 |
if( attr1.allowNull() != attr2.allowNull() ) { |
|
371 |
return false; |
|
372 |
} |
|
373 |
if( attr1.isAutomatic() != attr2.isAutomatic() ) { |
|
374 |
return false; |
|
375 |
} |
|
376 |
if( attr1.getDefaultValue() != attr2.getDefaultValue() ) { |
|
377 |
if( attr1.getDefaultValue()==null || attr2.getDefaultValue()==null) { |
|
378 |
return false; |
|
379 |
} |
|
380 |
if( !attr1.getDefaultValue().equals(attr2.getDefaultValue()) ) { |
|
381 |
return false; |
|
382 |
} |
|
383 |
} |
|
384 |
return true; |
|
385 |
} |
|
325 | 386 |
|
326 | 387 |
public void performUpdateTable(Connection conn, |
327 | 388 |
String database, |
... | ... | |
329 | 390 |
String table, |
330 | 391 |
FeatureType original, |
331 | 392 |
FeatureType target) throws DataException { |
332 |
|
|
333 | 393 |
SQLBuilderBase sqlbuilder = buildUpdateTableSQL(null, null, table, original, target); |
334 | 394 |
Statement st = null; |
335 | 395 |
try { |
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/JDBCUtils.java | ||
---|---|---|
33 | 33 |
st.execute(sql); |
34 | 34 |
} |
35 | 35 |
|
36 |
public static void execute(Connection connection, String sql) throws SQLException { |
|
37 |
logger.debug("execute SQL: " + sql); |
|
38 |
Statement st = connection.createStatement(); |
|
39 |
st.execute(sql); |
|
40 |
} |
|
41 |
|
|
36 | 42 |
public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException { |
37 | 43 |
logger.debug("execute query SQL: " + sql); |
38 | 44 |
ResultSet rs = st.executeQuery(); |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.api/src/main/java/org/gvsig/fmap/dal/SQLBuilder.java | ||
---|---|---|
217 | 217 |
public TableNameBuilder table(); |
218 | 218 |
public AlterTableBuilder drop_column(String columnName); |
219 | 219 |
public AlterTableBuilder add_column(String columnName, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue); |
220 |
public AlterTableBuilder add_geometry_column(String columnName, int geom_type, int geom_subtype, IProjection proj, boolean isIndexed, boolean allowNulls); |
|
220 | 221 |
public AlterTableBuilder alter_column(String columnName, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue); |
222 |
public AlterTableBuilder alter_geometry_column(String columnName, int geom_type, int geom_subtype, IProjection proj, boolean isIndexed, boolean allowNulls); |
|
221 | 223 |
public AlterTableBuilder rename_column(String source, String target); |
222 | 224 |
public List<String> toStrings(); |
223 | 225 |
} |
... | ... | |
262 | 264 |
|
263 | 265 |
public boolean supportSchemas(); |
264 | 266 |
|
267 |
@Deprecated |
|
265 | 268 |
public String sqltype(int dataType, int p, int s); |
266 | 269 |
|
270 |
public String sqltype(int dataType, int p, int s, int geomType, int geomSubtype); |
|
271 |
|
|
267 | 272 |
public Object sqlgeometrytype(int type, int subtype); |
268 | 273 |
|
269 | 274 |
public Object sqlgeometrydimension(int type, int subtype); |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.spi/src/main/java/org/gvsig/fmap/dal/feature/spi/SQLBuilderBase.java | ||
---|---|---|
1198 | 1198 |
protected List<String> drops; |
1199 | 1199 |
protected List<ColumnDescriptorBuilderBase> adds; |
1200 | 1200 |
protected List<ColumnDescriptorBuilderBase> alters; |
1201 |
protected List<Pair> renames; |
|
1201 |
protected List<Pair<String,String>> renames;
|
|
1202 | 1202 |
|
1203 | 1203 |
public AlterTableBuilderBase() { |
1204 | 1204 |
this.drops = new ArrayList<>(); |
... | ... | |
1241 | 1241 |
} |
1242 | 1242 |
|
1243 | 1243 |
@Override |
1244 |
public AlterTableBuilder add_geometry_column(String columnName, int type, int subtype, IProjection proj, boolean isIndexed, boolean allowNulls) { |
|
1245 |
if( StringUtils.isEmpty(columnName) ) { |
|
1246 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
|
1247 |
} |
|
1248 |
this.adds.add(new ColumnDescriptorBuilderBase(columnName, type, subtype, proj, isIndexed, allowNulls)); |
|
1249 |
return this; |
|
1250 |
} |
|
1251 |
|
|
1252 |
@Override |
|
1244 | 1253 |
public AlterTableBuilder alter_column(String columnName, int type, int type_p, int type_s, boolean isPk, boolean isIndexed, boolean allowNulls, boolean isAutomatic, Object defaultValue) { |
1245 | 1254 |
if (isPk || isAutomatic) { |
1246 | 1255 |
allowNulls = false; |
... | ... | |
1250 | 1259 |
} |
1251 | 1260 |
|
1252 | 1261 |
@Override |
1262 |
public AlterTableBuilder alter_geometry_column(String columnName, int type, int subtype, IProjection proj, boolean isIndexed, boolean allowNulls) { |
|
1263 |
if( StringUtils.isEmpty(columnName) ) { |
|
1264 |
throw new IllegalArgumentException("Argument 'columnName' can't be empty."); |
|
1265 |
} |
|
1266 |
this.alters.add(new ColumnDescriptorBuilderBase(columnName, type, subtype, proj, isIndexed, allowNulls)); |
|
1267 |
return this; |
|
1268 |
} |
|
1269 |
|
|
1270 |
@Override |
|
1253 | 1271 |
public AlterTableBuilder rename_column(String source, String target) { |
1254 | 1272 |
this.renames.add(new ImmutablePair(source, target)); |
1255 | 1273 |
return this; |
... | ... | |
1330 | 1348 |
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] |
1331 | 1349 |
|
1332 | 1350 |
*/ |
1333 |
StringBuilder builder = new StringBuilder(); |
|
1334 | 1351 |
|
1335 |
builder.append("ALTER TABLE"); |
|
1336 |
builder.append(this.table.toString()); |
|
1337 |
builder.append(" "); |
|
1338 |
boolean first = true; |
|
1339 | 1352 |
for (String column : drops) { |
1340 |
if (first) { |
|
1341 |
first = false; |
|
1342 |
} else { |
|
1343 |
builder.append(", "); |
|
1344 |
} |
|
1345 |
builder.append("DROP COLUMN IF EXISTS "); |
|
1346 |
builder.append(column); |
|
1353 |
StringBuilder builder = new StringBuilder(); |
|
1354 |
builder.append("ALTER TABLE "); |
|
1355 |
builder.append(this.table.toString()); |
|
1356 |
builder.append(" DROP COLUMN IF EXISTS "); |
|
1357 |
builder.append(identifier(column)); |
|
1358 |
sqls.add(builder.toString()); |
|
1347 | 1359 |
} |
1348 |
first = drops.isEmpty(); |
|
1349 | 1360 |
for (ColumnDescriptorBuilderBase column : adds) { |
1350 |
if (first) { |
|
1351 |
first = false; |
|
1352 |
} else { |
|
1353 |
builder.append(", "); |
|
1354 |
} |
|
1355 |
builder.append("ADD COLUMN "); |
|
1356 |
builder.append(column.getName()); |
|
1361 |
StringBuilder builder = new StringBuilder(); |
|
1362 |
builder.append("ALTER TABLE "); |
|
1363 |
builder.append(this.table.toString()); |
|
1364 |
builder.append(" ADD COLUMN "); |
|
1365 |
builder.append(identifier(column.getName())); |
|
1357 | 1366 |
builder.append(" "); |
1358 | 1367 |
if( column.getType() == DataTypes.INT && column.isAutomatic() ) { |
1359 | 1368 |
builder.append(" SERIAL"); |
... | ... | |
1377 | 1386 |
if (column.isPrimaryKey()) { |
1378 | 1387 |
builder.append(" PRIMARY KEY"); |
1379 | 1388 |
} |
1389 |
sqls.add(builder.toString()); |
|
1390 |
if( column.isIndexed() ) { |
|
1391 |
String sql; |
|
1392 |
String name = "idx_" + this.table().getName() + "_" + column.getName(); |
|
1393 |
if( column.isGeometry() ) { |
|
1394 |
sql = MessageFormat.format( |
|
1395 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column), |
|
1396 |
name, |
|
1397 |
this.table().toString(), |
|
1398 |
column.getName() |
|
1399 |
); |
|
1400 |
} else { |
|
1401 |
sql = MessageFormat.format( |
|
1402 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_column), |
|
1403 |
name, |
|
1404 |
this.table().toString(), |
|
1405 |
column.getName() |
|
1406 |
); |
|
1407 |
} |
|
1408 |
sqls.add(sql); |
|
1409 |
} |
|
1380 | 1410 |
} |
1381 |
first = drops.isEmpty() && adds.isEmpty(); |
|
1382 | 1411 |
for (ColumnDescriptorBuilderBase column : alters) { |
1383 |
if (first) { |
|
1384 |
first = false; |
|
1385 |
} else { |
|
1386 |
builder.append(", "); |
|
1387 |
} |
|
1388 |
builder.append("ALTER COLUMN "); |
|
1389 |
builder.append(column.getName()); |
|
1390 |
builder.append("SET DATA TYPE "); |
|
1412 |
StringBuilder builder = new StringBuilder(); |
|
1413 |
builder.append("ALTER TABLE "); |
|
1414 |
builder.append(this.table.toString()); |
|
1415 |
builder.append(" ALTER COLUMN "); |
|
1416 |
builder.append(identifier(column.getName())); |
|
1417 |
builder.append(" SET DATA TYPE "); |
|
1391 | 1418 |
if( column.getType() == DataTypes.INT && column.isAutomatic() ) { |
1392 | 1419 |
builder.append(" SERIAL"); |
1393 | 1420 |
} else { |
1394 | 1421 |
builder.append(sqltype(column.getType(), column.getPrecision(), column.getSize())); |
1395 | 1422 |
} |
1396 |
builder.append(", "); |
|
1397 | 1423 |
if (column.getDefaultValue() == null) { |
1398 | 1424 |
if (column.allowNulls()) { |
1399 |
builder.append("ALTER COLUMN "); |
|
1400 |
builder.append(column.getName()); |
|
1401 |
builder.append(" SET DEFAULT NULL"); |
|
1425 |
builder.append(" DEFAULT NULL"); |
|
1402 | 1426 |
} else { |
1403 |
builder.append("ALTER COLUMN "); |
|
1404 |
builder.append(column.getName()); |
|
1405 | 1427 |
builder.append(" DROP DEFAULT"); |
1406 | 1428 |
} |
1407 | 1429 |
} else { |
1408 |
builder.append("ALTER COLUMN "); |
|
1409 |
builder.append(column.getName()); |
|
1410 |
builder.append(" SET DEFAULT '"); |
|
1430 |
builder.append(" DEFAULT '"); |
|
1411 | 1431 |
builder.append(column.getDefaultValue().toString()); |
1412 | 1432 |
builder.append("'"); |
1413 | 1433 |
} |
1434 |
sqls.add(builder.toString()); |
|
1435 |
if( column.isIndexed() ) { |
|
1436 |
String sql; |
|
1437 |
String name = "idx_" + this.table().getName() + "_" + column.getName(); |
|
1438 |
if( column.isGeometry() ) { |
|
1439 |
sql = MessageFormat.format( |
|
1440 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_USING_GIST_column), |
|
1441 |
name, |
|
1442 |
this.table().toString(), |
|
1443 |
column.getName() |
|
1444 |
); |
|
1445 |
} else { |
|
1446 |
sql = MessageFormat.format( |
|
1447 |
config.getString(SQLConfig.CREATE_INDEX_name_ON_table_column), |
|
1448 |
name, |
|
1449 |
this.table().toString(), |
|
1450 |
column.getName() |
|
1451 |
); |
|
1452 |
} |
|
1453 |
sqls.add(sql); |
|
1454 |
} |
|
1414 | 1455 |
} |
1415 |
first = drops.isEmpty() && adds.isEmpty() && alters.isEmpty(); |
|
1416 |
for (Pair pair : renames) { |
|
1417 |
if (first) { |
|
1418 |
first = false; |
|
1419 |
} else { |
|
1420 |
builder.append(", "); |
|
1421 |
} |
|
1422 |
builder.append("RENAME COLUMN "); |
|
1423 |
builder.append(pair.getLeft()); |
|
1456 |
for (Pair<String,String> pair : renames) { |
|
1457 |
StringBuilder builder = new StringBuilder(); |
|
1458 |
builder.append("ALTER TABLE "); |
|
1459 |
builder.append(this.table.toString()); |
|
1460 |
builder.append(" RENAME COLUMN "); |
|
1461 |
builder.append(identifier(pair.getLeft())); |
|
1424 | 1462 |
builder.append(" TO "); |
1425 |
builder.append(pair.getRight()); |
|
1463 |
builder.append(identifier(pair.getRight())); |
|
1464 |
sqls.add(builder.toString()); |
|
1426 | 1465 |
} |
1427 |
sqls.add(builder.toString()); |
|
1428 |
|
|
1429 | 1466 |
return sqls; |
1430 | 1467 |
} |
1431 | 1468 |
|
... | ... | |
1845 | 1882 |
} |
1846 | 1883 |
|
1847 | 1884 |
@Override |
1885 |
@Deprecated |
|
1848 | 1886 |
public String sqltype(int type, int p, int s) { |
1887 |
return this.sqltype(type, p, s, Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.UNKNOWN); |
|
1888 |
} |
|
1889 |
|
|
1890 |
@Override |
|
1891 |
public String sqltype(int type, int p, int s, int geomType, int geomSubtype) { |
|
1849 | 1892 |
switch (type) { |
1850 | 1893 |
case DataTypes.BOOLEAN: |
1851 | 1894 |
return config.getString(SQLConfig.type_boolean); |
Also available in: Unified diff