Revision 35327 trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/postgis/PostGIS.java

View differences:

PostGIS.java
46 46
import java.io.ByteArrayOutputStream;
47 47
import java.io.PrintStream;
48 48
import java.io.UnsupportedEncodingException;
49
import java.nio.charset.Charset;
49 50
import java.sql.Date;
50 51
import java.sql.SQLException;
51 52
import java.sql.Types;
......
58 59
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
59 60
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
60 61
import com.iver.cit.gvsig.fmap.core.FShape;
62
import com.iver.cit.gvsig.fmap.core.FShape3D;
61 63
import com.iver.cit.gvsig.fmap.core.FShapeM;
62 64
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
63 65
import com.iver.cit.gvsig.fmap.core.IFeature;
......
212 214
		return retString;
213 215
	}
214 216

  
217
	/**
218
	 * FIXME: Maybe we don't need to test if encoding to the database is possible or not. This conversion may be slow.
219
	 * But in the other hand, the user may be able to store his data and don't loose all the changes...
220
	 * @param obj
221
	 * @return
222
	 */
215 223
	private String doubleQuote(Object obj) {
216 224
		String aux = obj.toString().replaceAll("'", "''");
217 225
		StringBuffer strBuf = new StringBuffer(aux);
218 226
		ByteArrayOutputStream out = new ByteArrayOutputStream(strBuf.length());
219
		PrintStream printStream = new PrintStream(out);
220
		printStream.print(aux);
221
		String aux2 = "ERROR";
227
		String aux2 = "Encoding ERROR";
228

  
222 229
		try {
230
			PrintStream printStream = new PrintStream(out, true, toEncode);
231
			printStream.print(aux);			
223 232
			aux2 = out.toString(toEncode);
224
			System.out.println(aux + " " + aux2);
233
//			System.out.println(aux + " " + aux2);
225 234
		} catch (UnsupportedEncodingException e) {
226 235
			// TODO Auto-generated catch block
227 236
			e.printStackTrace();
......
292 301
				geometry=ShapeMFactory.createPolyline2DM(new GeneralPathX(geometry.getInternalShape()),((IGeometryM)geometry).getMs()); //MCoord
293 302
			}
294 303
		}
304
		
295 305
		if (!isCorrectGeometry(geometry, type))
296 306
			throw new ProcessVisitorException("incorrect_geometry",new Exception());
297 307
		//MCoord
298
		if ((type == (FShape.LINE|FShape.M)) || (type == (FShape.POINT|FShape.M))){
308
		if (((type & FShape.M) != 0) && ((type & FShape.MULTIPOINT) == 0)) {
299 309
			sqlBuf.append(" GeometryFromText( '"
300 310
					+ ((FShapeM)geometry.getInternalShape()).toText() + "', "
301 311
					+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
302
		}else{
303
			Geometry jtsGeom=geometry.toJTSGeometry();
304
			if (jtsGeom==null || !isCorrectType(jtsGeom, type)){
305
				throw new ProcessVisitorException("incorrect_geometry",new Exception());
306
			}
312
		} else
313
			//ZCoord
314
			if ((type & FShape.Z) != 0) {
315
				if ((type & FShape.MULTIPOINT) != 0) {
316
					//TODO: Metodo toText 3D o 2DM 					
317
				} else {
318
				//Its not a multipoint
319
				sqlBuf.append(" GeometryFromText( '"
320
						+ ((FShape3D)feat.getGeometry().getInternalShape()).toText() + "', "
321
						+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
322
				}
323

  
324
			}	
325
			//XYCoord
326
			else {
327
				Geometry jtsGeom=geometry.toJTSGeometry();
328
				if (jtsGeom==null || !isCorrectType(jtsGeom, type)){
329
					throw new ProcessVisitorException("incorrect_geometry",new Exception());
330
				}
331
				
332
		
333
			//If they layer is a 2D layer writing a 2D geometry will throw an error
334
			//With st_force_3d it is avoid		
335
			if (dbLayerDef.getDimension() == 3) sqlBuf.append("ST_Force_3D (");
336
			
307 337
			sqlBuf.append(" GeometryFromText( '"
308 338
				+ jtsGeom.toText() + "', "
309 339
				+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
340

  
341
			if (dbLayerDef.getDimension() == 3) sqlBuf.append(")");			
310 342
		}
311 343

  
312 344
		// sqlBuf.deleteCharAt(sqlBuf.lastIndexOf(","));
......
388 420
			sqlBuf.append(" = ");
389 421
			//MCoord
390 422
			int type = feat.getGeometry().getGeometryType();
391
			if ((type == (FShape.LINE|FShape.M)) || (type == (FShape.POINT|FShape.M))){
423
			if (((type & FShape.M) != 0) && ((type & FShape.MULTIPOINT) == 0)) {
392 424
				sqlBuf.append(" GeometryFromText( '"
393 425
						+ ((FShapeM)feat.getGeometry().getInternalShape()).toText() + "', "
394 426
						+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
395
			}else{
427
			} else
428
				
429
			//ZCoord
430
				if ((type & FShape.Z) != 0) {
431
					if ((type & FShape.MULTIPOINT) != 0) {
432
						//TODO: Metodo toText 3D o 2DM 											
433
					} else {
434
					//Its not a multipoint
435
					sqlBuf.append(" GeometryFromText( '"
436
							+ ((FShape3D)feat.getGeometry().getInternalShape()).toText() + "', "
437
							+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
438
					}
439
					
440
				}
441
			
442
			//XYCoord
443
			else{
444
				
445
				//If they layer is a 2D layer writing a 2D geometry will throw an error
446
				//With st_force_3d it is avoid		
447
				if (dbLayerDef.getDimension() == 3) sqlBuf.append("ST_Force_3D (");
448
				
396 449
				sqlBuf.append(" GeometryFromText( '"
397 450
				+ feat.getGeometry().toJTSGeometry().toText() + "', "
398 451
				+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
452
				
453
				if (dbLayerDef.getDimension() == 3) sqlBuf.append(")");
399 454
			}
400 455
		}
401 456
		sqlBuf.append(" WHERE ");
......
438 493
			this.toEncode = "ASCII";
439 494
		} else if (toEncode.compareToIgnoreCase("WIN1252") == 0) {
440 495
			this.toEncode = "Latin1";
496
		} else if (toEncode.compareToIgnoreCase("UTF8") == 0) {
497
			this.toEncode = "UTF-8";
441 498
		} else {
442 499
			this.toEncode = toEncode;
443 500
		}

Also available in: Unified diff