Revision 19610 trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/jdbc/h2/H2FeaturesWriter.java
H2FeaturesWriter.java | ||
---|---|---|
8 | 8 |
import java.io.UnsupportedEncodingException; |
9 | 9 |
import java.nio.channels.WritableByteChannel; |
10 | 10 |
import java.sql.Connection; |
11 |
import java.sql.DatabaseMetaData; |
|
11 | 12 |
import java.sql.ResultSet; |
12 | 13 |
import java.sql.SQLException; |
13 | 14 |
import java.sql.Statement; |
... | ... | |
37 | 38 |
private Statement st; |
38 | 39 |
private boolean bCreateTable=false; |
39 | 40 |
private String toEncode; |
41 |
private H2Driver driver; |
|
42 |
private H2DriverParameters parameters; |
|
40 | 43 |
|
44 |
|
|
41 | 45 |
public void init(IDriver driver) { |
42 |
H2DriverParameters parameters=(H2DriverParameters)driver.getParameters(); |
|
43 |
this.featureType = parameters.getFeatureType(); |
|
44 |
conex = featureType.getConnection(); |
|
46 |
this.driver = (H2Driver)driver; |
|
47 |
this.parameters=(H2DriverParameters)driver.getParameters(); |
|
48 |
|
|
49 |
this.featureType = (DBFeatureType)this.driver.getFeatureType(); |
|
50 |
conex = this.driver.getConnection(); |
|
45 | 51 |
try { |
46 | 52 |
st = conex.createStatement(); |
47 | 53 |
|
... | ... | |
176 | 182 |
bCreateTable = createTable; |
177 | 183 |
} |
178 | 184 |
private boolean dropTableIfExist() throws SQLException{ |
179 |
if (!this.existTable(featureType.getSchema(), featureType.getTableName())){
|
|
185 |
if (!this.existTable(parameters.getSchema(), parameters.getTableName())){
|
|
180 | 186 |
return false; |
181 | 187 |
} |
182 | 188 |
st = conex.createStatement(); |
183 |
st.execute("DROP TABLE " + featureType.getComposedTableName() + ";");
|
|
189 |
st.execute("DROP TABLE " + parameters.tableID() + ";");
|
|
184 | 190 |
st.close(); |
185 | 191 |
return false; |
186 | 192 |
} |
187 | 193 |
private boolean existTable(String schema, String tableName) throws SQLException{ |
188 | 194 |
boolean exists =false; |
189 |
if (schema == null || schema.equals("")){ |
|
190 |
schema = " current_schema()::Varchar "; |
|
191 |
} else { |
|
192 |
schema = "'" + schema + "'"; |
|
193 |
} |
|
195 |
DatabaseMetaData metadata = conex.getMetaData(); |
|
194 | 196 |
|
195 |
String sql = "select relname,nspname " + |
|
196 |
"from pg_class inner join pg_namespace " + |
|
197 |
"on relnamespace = pg_namespace.oid where "+ |
|
198 |
" relkind = 'r' and relname = '" + tableName +"' and nspname = " + schema; |
|
197 |
ResultSet rs = metadata.getTables(null, schema, tableName, null); |
|
199 | 198 |
|
200 |
st = conex.createStatement(); |
|
201 |
ResultSet rs = st.executeQuery(sql); |
|
202 |
if (rs.next()){ |
|
203 |
exists = true; |
|
204 |
} |
|
199 |
exists = !rs.isAfterLast(); |
|
205 | 200 |
rs.close(); |
206 |
st.close(); |
|
207 | 201 |
|
208 | 202 |
return exists; |
209 | 203 |
} |
... | ... | |
224 | 218 |
public void deleteAttribute(IFeatureAttributeDescriptor attribute) throws WriteException { |
225 | 219 |
try { |
226 | 220 |
st = conex.createStatement(); |
227 |
String sql = "ALTER TABLE " + featureType.getTableName() + " DROP COLUMN "
|
|
221 |
String sql = "ALTER TABLE " + parameters.tableID() + " DROP COLUMN "
|
|
228 | 222 |
+ attribute.getName() + ";"; |
229 | 223 |
st.execute(sql); |
230 | 224 |
} catch (SQLException e) { |
... | ... | |
235 | 229 |
public void updateAttribute(IFeatureAttributeDescriptor oldAttribute, IFeatureAttributeDescriptor attribute) throws WriteException, ReadException { |
236 | 230 |
try { |
237 | 231 |
st = conex.createStatement(); |
238 |
String sql = "ALTER TABLE " + featureType.getTableName() + " RENAME COLUMN "
|
|
232 |
String sql = "ALTER TABLE " + parameters.tableID() + " RENAME COLUMN "
|
|
239 | 233 |
+ oldAttribute.getName() + " TO " + attribute.getName() + ";"; |
240 | 234 |
st.execute(sql); |
241 | 235 |
} catch (SQLException e) { |
... | ... | |
248 | 242 |
st = conex.createStatement(); |
249 | 243 |
|
250 | 244 |
String sql = "ALTER TABLE " |
251 |
+ featureType.getTableName()
|
|
245 |
+ parameters.tableID()
|
|
252 | 246 |
+ " ADD COLUMN " |
253 | 247 |
+ attribute.getName() |
254 | 248 |
+ " " |
... | ... | |
262 | 256 |
} |
263 | 257 |
} |
264 | 258 |
|
265 |
public String getSqlCreateSpatialTable(DBFeatureType dbFeatureType) {
|
|
259 |
public String getSqlCreateSpatialTable(DBFeatureType featureType) {
|
|
266 | 260 |
|
267 | 261 |
String resul; |
268 | 262 |
/* boolean bExistGID = false; |
... | ... | |
276 | 270 |
resul = "CREATE TABLE " + dbLayerDef.getTableName() + " ("; |
277 | 271 |
else */ |
278 | 272 |
// FJP: NUEVO: NO TOLERAMOS CAMPOS QUE SE LLAMEN GID. Lo reservamos para uso nuestro. |
279 |
resul = "CREATE TABLE " + dbFeatureType.getComposedTableName()
|
|
280 |
+ " ( " + dbFeatureType.getFieldId() +" serial PRIMARY KEY ";
|
|
273 |
resul = "CREATE TABLE " + this.parameters.tableID()
|
|
274 |
+ " ( " + featureType.getFieldId() +" serial PRIMARY KEY ";
|
|
281 | 275 |
int j=0; |
282 |
for (int i = 0; i < dbFeatureType.size(); i++) {
|
|
283 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)dbFeatureType.get(i);
|
|
276 |
for (int i = 0; i < featureType.size(); i++) {
|
|
277 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)this.featureType.get(i);
|
|
284 | 278 |
String fieldType = fad.getDataType(); |
285 | 279 |
String strType = JDBCTypes.fieldTypeToString(fieldType); |
286 | 280 |
/* |
287 | 281 |
* if (fieldType == Types.VARCHAR) strType = strType + "(" + |
288 | 282 |
* fieldsDescr[i].getFieldLength() + ")"; |
289 | 283 |
*/ |
290 |
if (fad.getName().equalsIgnoreCase(dbFeatureType.getFieldId()))
|
|
284 |
if (fad.getName().equalsIgnoreCase(featureType.getFieldId()))
|
|
291 | 285 |
continue; |
292 | 286 |
resul = resul + ", " + fad.getName() + " " + strType; |
293 | 287 |
j++; |
... | ... | |
314 | 308 |
break; |
315 | 309 |
} |
316 | 310 |
|
317 |
String schema = dbFeatureType.getSchema();
|
|
311 |
String schema = this.parameters.getSchema();
|
|
318 | 312 |
if (schema == null || schema.equals("")){ |
319 | 313 |
schema = " current_schema()::Varchar "; |
320 | 314 |
} else { |
... | ... | |
323 | 317 |
|
324 | 318 |
String result = "SELECT AddGeometryColumn(" |
325 | 319 |
+ schema + ", '" |
326 |
+ dbFeatureType.getTableName() + "', '"
|
|
320 |
+ this.parameters.getTableName() + "', '"
|
|
327 | 321 |
+ dbFeatureType.getDefaultGeometry() + "', " |
328 | 322 |
+ removePrefix(dbFeatureType.getDefaultSRS()) + ", '" + strGeometryFieldType + "', " |
329 | 323 |
+ dbFeatureType.getDimension() + ");"; |
... | ... | |
384 | 378 |
public String getSqlInsertFeature(DBFeatureType dbFeatureType, |
385 | 379 |
IFeature feat) { |
386 | 380 |
StringBuffer sqlBuf = new StringBuffer("INSERT INTO " |
387 |
+ dbFeatureType.getComposedTableName() + " (");
|
|
381 |
+ this.parameters.tableID() + " (");
|
|
388 | 382 |
String sql = null; |
389 | 383 |
int numAlphanumericFields = dbFeatureType.size(); |
390 | 384 |
|
... | ... | |
489 | 483 |
WHERE date > '1994-11-28'; |
490 | 484 |
*/ |
491 | 485 |
StringBuffer sqlBuf = new StringBuffer("UPDATE " |
492 |
+ dbFeatureType.getComposedTableName() + " SET");
|
|
486 |
+ this.parameters.tableID() + " SET");
|
|
493 | 487 |
String sql = null; |
494 | 488 |
int numAlphanumericFields = dbFeatureType.size(); |
495 | 489 |
|
... | ... | |
531 | 525 |
// nos sirve dentro de un writer para modificar y/o borrar entidades |
532 | 526 |
// Por ahora, cojo el ID del campo que me indica el dbLayerDev |
533 | 527 |
StringBuffer sqlBuf = new StringBuffer("DELETE FROM " |
534 |
+ dbFeatureType.getComposedTableName() + " WHERE ");
|
|
528 |
+ this.parameters.tableID() + " WHERE ");
|
|
535 | 529 |
String sql = null; |
536 | 530 |
String indexFieldId = dbFeatureType.getFieldId(); |
537 | 531 |
sqlBuf.append(dbFeatureType.getFieldId() + " = " + feature.get(indexFieldId).toString()); |
Also available in: Unified diff