Revision 20058 trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/postgresql/PostgresqlStoreUtils.java

View differences:

PostgresqlStoreUtils.java
284 284
	}
285 285

  
286 286

  
287
	static String getFilterForID(DBFeatureType fType, IFeature feature){
288
		return getFilterForID(fType, getPkFromFeature(feature,fType));
289
	}
290 287

  
291
	static String getFilterForID(DBFeatureType fType, Object[] featureKey){
292
		//TODO: Ojo para los multiples
293
		if (fType.getFieldsId().length != 1)
294
			throw new UnsupportedOperationException("ID fields > 1");
295
		String id =fType.getFieldsId()[0];
296
		return id + " = " + objectToSqlString(featureKey[0]);
297
	}
298 288

  
299
	static String objectToSqlString(Object obj){
300
		if (obj instanceof String){
301
			return "'"+ scapeString((String)obj) +"'";
302
		} else if (obj == null){
303
			return "null";
304
		}else{
305
			// OJO con otros tipos!!
306
			return obj.toString();
307
		}
308

  
309
	}
310

  
311
	static String scapeString(String str){
312
		return str.replace("'", "''");
313
	}
314

  
315

  
316

  
317 289
	static Connection getConnection(String dbUrl, String dbUser, String dbPass) throws InitializeException {
318 290
		//TODO: Aqu? habria que implementar la llamada
319 291
		//      al Resource Manager para comprobar si ya hay
......
350 322

  
351 323

  
352 324

  
353
	protected static Object[] getPkFromResulset(ResultSet rs, DBFeatureType featureType) throws java.sql.SQLException{
354
		String[] fieldsId = featureType.getFieldsId();
355
		Object[] result = new Object[fieldsId.length];
356
		for (int i=0;i<fieldsId.length;i++){
357
			result[i] = rs.getObject(fieldsId[i]);
358
		}
359
		return result;
360 325

  
361
	}
362

  
363

  
364

  
365
	protected static Object[] getPkFromFeature(IFeature feature, DBFeatureType featureType){
366
		String[] fieldsId = featureType.getFieldsId();
367
		Object[] result = new Object[fieldsId.length];
368
		for (int i=0;i<fieldsId.length;i++){
369
			result[i] = feature.get(fieldsId[i]);
370
		}
371
		return result;
372

  
373
	}
374

  
375
	static IFeature createFeature(JDBCStore store,ResultSet rs,DBFeatureType featureType) throws ReadException{
376

  
377
		JDBCFeature feature=null;
378

  
379

  
380
		Object[] pk;
381
		try {
382
			pk = getPkFromResulset(rs, featureType);
383

  
384
			feature=new PostgresqlFeature(featureType,store,pk);
385

  
386
			Iterator iter = featureType.iterator();
387
			IGeometry geom = null;
388
			while (iter.hasNext()) {
389
				IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)iter.next();
390
				if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
391

  
392
					PGgeometry data =(PGgeometry)rs.getObject(fad.getName());
393
					if (data == null) {
394
						geom = null;
395
					} else{
396
						geom = PostGIS2Geometry.getGeneralPath(data);
397
					}
398
					feature.setGeometry(fad.getName(),geom);
399
				} else {
400
					feature.set(fad.getName(), rs.getObject(fad.getName()));
401
				}
402

  
403
			}
404
			return feature;
405
		} catch (java.sql.SQLException e) {
406
			throw new ReadException("CreateFeature",e);
407
		}
408
	}
409

  
410 326
	static String addLimitsToSQL(String aSql,int fetchSize,int page){
411 327
		return aSql+ " limit " + fetchSize + " offset " + (fetchSize*page);
412 328
	}
413 329

  
330
	static String getDefaultSchema(Connection conn, String catalog) throws InitializeException {
331
		//TODO
332
		return null;
333
	}
414 334

  
415 335
}
416 336

  

Also available in: Unified diff