Revision 38057

View differences:

trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/postgis/PostGisDriver.java
193 193

  
194 194
	/*
195 195
	 * (non-Javadoc)
196
	 * 
196
	 *
197 197
	 * @see
198 198
	 * com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
199 199
	 */
......
203 203

  
204 204
	/*
205 205
	 * (non-Javadoc)
206
	 * 
206
	 *
207 207
	 * @see com.hardcode.driverManager.Driver#getName()
208 208
	 */
209 209
	public String getName() {
......
222 222
			// geom = parser.read(strAux);
223 223
			if (rs != null) {
224 224
				byte[] data = rs.getBytes(1);
225
				if (data == null) // null geometry.
226
					return null;
225
				if (data == null) {
226
				    return null;
227
				}
227 228
				geom = parser.parse(data);
228 229
			}
229 230
		} catch (SQLException e) {
......
235 236

  
236 237
	/**
237 238
	 * First, the geometry field. After, the rest of fields
238
	 * 
239
	 *
239 240
	 * @return
240 241
	 */
241 242
	/*
......
248 249
	/**
249 250
	 * Antes de llamar a esta funci?n hay que haber fijado el workingArea si se
250 251
	 * quiere usar.
251
	 * 
252
	 *
252 253
	 * @param conn
253 254
	 * @throws DBException
254 255
	 */
......
286 287

  
287 288
			myCursorId++;
288 289
			cursorName = "wkb_cursor_" + myCursorId + getTableName();
289
			
290

  
290 291
			st.execute("declare " + cursorName + " binary scroll cursor with hold for " + sqlTotal);
291
			
292

  
292 293
			rs = st.executeQuery("fetch forward " + FETCH_SIZE + " in " + cursorName);
293 294
			fetch_min = 0;
294 295
			fetch_max = FETCH_SIZE - 1;
......
300 301
			writer.initialize(lyrDef);
301 302

  
302 303
		} catch (SQLException e) {
303
			
304

  
304 305
			try {
305 306
				((ConnectionJDBC) conn).getConnection().rollback();
306 307
			} catch (SQLException e1) {
307 308
				logger.warn("Unable to rollback connection after problem (" + e.getMessage() + ") in setData()");
308 309
			}
309
			
310

  
310 311
			try {
311 312
				if (rs != null) { rs.close(); }
312 313
			} catch (SQLException e1) {
......
365 366

  
366 367
	/*
367 368
	 * (non-Javadoc)
368
	 * 
369
	 *
369 370
	 * @see
370 371
	 * com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator
371 372
	 * (java.lang.String)
......
386 387
	    String provCursorName = "wkb_cursor_prov_"
387 388
		    + Long.toString(Math.abs(new Random().nextLong()))
388 389
		    + getTableName();
390
	    // jlopez: if the cursor is longer than 64 chars, we strip it to
391
	    // that length. We should use a different name convention which
392
	    // avoids this kind of problems.
393
	    if (provCursorName.length() > 64) {
394
		provCursorName = provCursorName.substring(0, 63);
395
	    }
396

  
389 397
	    bCursorActivo = true;
390 398
	    geomIterator = new PostGisFeatureIterator(
391 399
		    ((ConnectionJDBC) conn).getConnection(), provCursorName,
......
418 426
	 * Le pasas el rect?ngulo que quieres pedir. La primera vez es el
419 427
	 * workingArea, y las siguientes una interseccion de este rectangulo con el
420 428
	 * workingArea
421
	 * 
429
	 *
422 430
	 * @param r
423 431
	 * @param strEPSG
424 432
	 * @return
425 433
	 */
426 434
	private String getCompoundWhere(String sql, Rectangle2D r, String strEPSG) {
427
		if (r == null)
428
			return getWhereClause();
435
		if (r == null) {
436
		    return getWhereClause();
437
		}
429 438

  
430 439
		double xMin = r.getMinX();
431 440
		double yMin = r.getMinY();
......
435 444
		+ ", " + xMax + " " + yMin + ", " + xMax + " " + yMax + ", "
436 445
		+ xMin + " " + yMax + ")', " + strEPSG + ")";
437 446
		String sqlAux;
438
		if (getWhereClause().toUpperCase().indexOf("WHERE") != -1)
447
		if (getWhereClause().toUpperCase().indexOf("WHERE") != -1) {
439 448
		    sqlAux = getWhereClause() + " AND \"" + getLyrDef().getFieldGeometry() + "\" && " + wktBox;
440
		else
449
		} else {
441 450
		    sqlAux = "WHERE \"" + getLyrDef().getFieldGeometry() + "\" && "
442 451
			+ wktBox;
452
		}
443 453
		return sqlAux;
444 454
	}
445 455

  
......
452 462

  
453 463
	/*
454 464
	 * (non-Javadoc)
455
	 * 
465
	 *
456 466
	 * @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#getFieldValue(long,
457 467
	 * int)
458 468
	 */
......
476 486
	static Value getFieldValue(ResultSet aRs, int fieldId) throws SQLException {
477 487
		ResultSetMetaData metaData = aRs.getMetaData();
478 488
		byte[] byteBuf = aRs.getBytes(fieldId);
479
		if (byteBuf == null)
480
			return ValueFactory.createNullValue();
481
		else {
489
		if (byteBuf == null) {
490
		    return ValueFactory.createNullValue();
491
		} else {
482 492
			ByteBuffer buf = ByteBuffer.wrap(byteBuf);
483
			if (metaData.getColumnType(fieldId) == Types.VARCHAR)
484
				return ValueFactory.createValue(aRs.getString(fieldId));
493
			if (metaData.getColumnType(fieldId) == Types.VARCHAR) {
494
			    return ValueFactory.createValue(aRs.getString(fieldId));
495
			}
485 496
			if (metaData.getColumnType(fieldId) == Types.CHAR){
486 497
				String character = aRs.getString(fieldId);
487 498
				if (character != null){
......
490 501
					return ValueFactory.createValue(character);
491 502
				}
492 503
			}
493
			if (metaData.getColumnType(fieldId) == Types.FLOAT)
494
				return ValueFactory.createValue(buf.getFloat());
495
			if (metaData.getColumnType(fieldId) == Types.DOUBLE)
496
				return ValueFactory.createValue(buf.getDouble());
497
			if (metaData.getColumnType(fieldId) == Types.REAL)
498
				return ValueFactory.createValue(buf.getFloat());
499
			if (metaData.getColumnType(fieldId) == Types.INTEGER)
500
				return ValueFactory.createValue(buf.getInt());
501
			if (metaData.getColumnType(fieldId) == Types.SMALLINT)
502
				return ValueFactory.createValue(buf.getShort());
503
			if (metaData.getColumnType(fieldId) == Types.BIGINT)
504
				return ValueFactory.createValue(buf.getLong());
505
			if (metaData.getColumnType(fieldId) == Types.BIT)
506
				return ValueFactory.createValue((byteBuf[0] == 1));
507
			if (metaData.getColumnType(fieldId) == Types.BOOLEAN)
508
				return ValueFactory.createValue(aRs.getBoolean(fieldId));
504
			if (metaData.getColumnType(fieldId) == Types.FLOAT) {
505
			    return ValueFactory.createValue(buf.getFloat());
506
			}
507
			if (metaData.getColumnType(fieldId) == Types.DOUBLE) {
508
			    return ValueFactory.createValue(buf.getDouble());
509
			}
510
			if (metaData.getColumnType(fieldId) == Types.REAL) {
511
			    return ValueFactory.createValue(buf.getFloat());
512
			}
513
			if (metaData.getColumnType(fieldId) == Types.INTEGER) {
514
			    return ValueFactory.createValue(buf.getInt());
515
			}
516
			if (metaData.getColumnType(fieldId) == Types.SMALLINT) {
517
			    return ValueFactory.createValue(buf.getShort());
518
			}
519
			if (metaData.getColumnType(fieldId) == Types.BIGINT) {
520
			    return ValueFactory.createValue(buf.getLong());
521
			}
522
			if (metaData.getColumnType(fieldId) == Types.BIT) {
523
			    return ValueFactory.createValue((byteBuf[0] == 1));
524
			}
525
			if (metaData.getColumnType(fieldId) == Types.BOOLEAN) {
526
			    return ValueFactory.createValue(aRs.getBoolean(fieldId));
527
			}
509 528
			if (metaData.getColumnType(fieldId) == Types.DATE) {
510 529
				long daysAfter2000 = buf.getInt();
511 530
				DateTime year2000 = new DateTime(2000, 1,1,0,0,0);
......
695 714
				throw new SQLException(e);
696 715
			}
697 716
		}
698
			
699
		// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS 
717

  
718
		// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
700 719
		// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
701 720
		// LO HAGA
702 721

  
703 722
		if ((index >= fetch_min) && (index <= fetch_max)) {
704 723
			// Est? en el intervalo, as? que lo podemos posicionar
705
				
724

  
706 725
		} else {
707 726
			// calculamos el intervalo correcto
708 727
			fetch_min = (index / FETCH_SIZE) * FETCH_SIZE;
709 728
			fetch_max = fetch_min + FETCH_SIZE - 1;
710 729
			// y cogemos ese cacho
711 730
			rs.close();
712
			
731

  
713 732
			Statement st = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
714 733
			ResultSet.CONCUR_READ_ONLY);
715
			
734

  
716 735
//			myCursorId++;
717 736
//			st.execute("declare "
718 737
//					+ getTableName()
......
722 741
			st.executeQuery("fetch absolute " + fetch_min + " in " + cursorName);
723 742
//					+ getTableName() + myCursorId
724 743
//					+ "_wkb_cursorAbsolutePosition");
725
			
744

  
726 745
			rs = st.executeQuery("fetch forward " + FETCH_SIZE + " in " + cursorName);
727 746
//					+ getTableName() + myCursorId
728 747
//					+ "_wkb_cursorAbsolutePosition");
729
			
730 748

  
749

  
731 750
		}
732 751
		rs.absolute(index - fetch_min + 1);
733 752

  
......
765 784

  
766 785
	/*
767 786
	 * (non-Javadoc)
768
	 * 
787
	 *
769 788
	 * @see com.iver.cit.gvsig.fmap.core.ICanReproject#getSourceProjection()
770 789
	 */
771 790
	public String getSourceProjection(IConnection conn, DBLayerDefinition dbld) {
772
		if (originalEPSG == null)
773
			getTableEPSG_and_shapeType(conn, dbld);
791
		if (originalEPSG == null) {
792
		    getTableEPSG_and_shapeType(conn, dbld);
793
		}
774 794
		return originalEPSG;
775 795
	}
776 796

  
......
810 830
				originalEPSG = "" + rs.getInt("SRID");
811 831
				String geometryType = rs.getString("TYPE");
812 832
				int shapeType = FShape.MULTI;
813
				if (geometryType.compareToIgnoreCase("POINT") == 0)
814
					shapeType = FShape.POINT;
815
				else if (geometryType.compareToIgnoreCase("LINESTRING") == 0)
816
					shapeType = FShape.LINE;
817
				else if (geometryType.compareToIgnoreCase("POLYGON") == 0)
818
					shapeType = FShape.POLYGON;
819
				else if (geometryType.compareToIgnoreCase("MULTIPOINT") == 0)
820
					shapeType = FShape.MULTIPOINT;
821
				else if (geometryType.compareToIgnoreCase("MULTILINESTRING") == 0)
822
					shapeType = FShape.LINE;
823
				else if (geometryType.compareToIgnoreCase("MULTILINESTRINGM") == 0) // MCoord
824
					shapeType = FShape.LINE | FShape.M;
825
				else if (geometryType.compareToIgnoreCase("MULTIPOLYGON") == 0)
826
					shapeType = FShape.POLYGON;
833
				if (geometryType.compareToIgnoreCase("POINT") == 0) {
834
				    shapeType = FShape.POINT;
835
				} else if (geometryType.compareToIgnoreCase("LINESTRING") == 0) {
836
				    shapeType = FShape.LINE;
837
				} else if (geometryType.compareToIgnoreCase("POLYGON") == 0) {
838
				    shapeType = FShape.POLYGON;
839
				} else if (geometryType.compareToIgnoreCase("MULTIPOINT") == 0) {
840
				    shapeType = FShape.MULTIPOINT;
841
				} else if (geometryType.compareToIgnoreCase("MULTILINESTRING") == 0) {
842
				    shapeType = FShape.LINE;
843
				} else if (geometryType.compareToIgnoreCase("MULTILINESTRINGM") == 0) {
844
				    shapeType = FShape.LINE | FShape.M;
845
				} else if (geometryType.compareToIgnoreCase("MULTIPOLYGON") == 0) {
846
				    shapeType = FShape.POLYGON;
847
				}
827 848
				dbld.setShapeType(shapeType);
828
				
849

  
829 850
				//jomarlla
830 851
				int dimension  = rs.getInt("COORD_DIMENSION");
831 852
				dbld.setDimension(dimension);
......
846 867

  
847 868
	/*
848 869
	 * (non-Javadoc)
849
	 * 
870
	 *
850 871
	 * @see com.iver.cit.gvsig.fmap.core.ICanReproject#getDestProjection()
851 872
	 */
852 873
	public String getDestProjection() {
......
855 876

  
856 877
	/*
857 878
	 * (non-Javadoc)
858
	 * 
879
	 *
859 880
	 * @see
860 881
	 * com.iver.cit.gvsig.fmap.core.ICanReproject#setDestProjection(java.lang
861 882
	 * .String)
......
866 887

  
867 888
	/*
868 889
	 * (non-Javadoc)
869
	 * 
890
	 *
870 891
	 * @see
871 892
	 * com.iver.cit.gvsig.fmap.core.ICanReproject#canReproject(java.lang.String)
872 893
	 */
......
877 898

  
878 899
	/*
879 900
	 * (non-Javadoc)
880
	 * 
901
	 *
881 902
	 * @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#doRelateID_FID()
882 903
	 */
883 904
	protected void doRelateID_FID() throws DBException {
......
903 924
			String gid;
904 925
			while (r.next()) {
905 926
				gid = r.getString(1);
906
				
927

  
907 928
				if (gid == null) {
908 929
					throw new SQLException(
909 930
							PluginServices.getText(null, "Found_null_id_in_table") + ": " +
910 931
							getLyrDef().getComposedTableName());
911 932
				}
912
				
933

  
913 934
				Value aux = ValueFactory.createValue(gid);
914 935
				hashRelate.put(aux, new Integer(id));
915 936
				// System.out.println("ASOCIANDO CLAVE " + aux + " CON VALOR " +
......
928 949
			 */
929 950
			/*
930 951
			 * int index = 0;
931
			 * 
952
			 *
932 953
			 * while (rs.next()) { Value aux = getFieldValue(index,
933 954
			 * idFID_FieldName-2); hashRelate.put(aux, new Integer(index));
934 955
			 * index++; System.out.println("Row " + index + " clave=" + aux); }
......
957 978

  
958 979
	/*
959 980
	 * (non-Javadoc)
960
	 * 
981
	 *
961 982
	 * @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#close()
962 983
	 */
963 984
	public void close() {
......
974 995

  
975 996
	/*
976 997
	 * (non-Javadoc)
977
	 * 
998
	 *
978 999
	 * @see
979 1000
	 * com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getFeatureIterator
980 1001
	 * (java.awt.geom.Rectangle2D, java.lang.String, java.lang.String[])
......
988 1009
																						// new
989 1010
																						// FieldDescription[alphaNumericFieldsNeeded.length+1];
990 1011
		try {
991
			if (workingArea != null)
992
				r = r.createIntersection(workingArea);
1012
			if (workingArea != null) {
1013
			    r = r.createIntersection(workingArea);
1014
			}
993 1015
			// if (getLyrDef()==null){
994 1016
			// load();
995 1017
			// throw new DriverException("Fallo de la conexi?n");
......
1062 1084

  
1063 1085
	/*
1064 1086
	 * public void preProcess() throws EditionException { writer.preProcess(); }
1065
	 * 
1087
	 *
1066 1088
	 * public void process(IRowEdited row) throws EditionException {
1067 1089
	 * writer.process(row); }
1068
	 * 
1090
	 *
1069 1091
	 * public void postProcess() throws EditionException { writer.postProcess();
1070 1092
	 * }
1071
	 * 
1093
	 *
1072 1094
	 * public String getCapability(String capability) { return
1073 1095
	 * writer.getCapability(capability); }
1074
	 * 
1096
	 *
1075 1097
	 * public void setCapabilities(Properties capabilities) {
1076 1098
	 * writer.setCapabilities(capabilities); }
1077
	 * 
1099
	 *
1078 1100
	 * public boolean canWriteAttribute(int sqlType) { return
1079 1101
	 * writer.canWriteAttribute(sqlType); }
1080
	 * 
1102
	 *
1081 1103
	 * public boolean canWriteGeometry(int gvSIGgeometryType) { return
1082 1104
	 * writer.canWriteGeometry(gvSIGgeometryType); }
1083
	 * 
1105
	 *
1084 1106
	 * public void initialize(ITableDefinition layerDef) throws EditionException
1085 1107
	 * { writer.setCreateTable(false); writer.setWriteAll(false); // Obtenemos
1086 1108
	 * el DBLayerDefinition a partir del driver
1087
	 * 
1109
	 *
1088 1110
	 * DBLayerDefinition dbLyrDef = getLyrDef();
1089
	 * 
1090
	 * 
1111
	 *
1112
	 *
1091 1113
	 * writer.initialize(dbLyrDef); }
1092 1114
	 */
1093 1115
	public boolean isWritable() {
......
1204 1226

  
1205 1227
	/**
1206 1228
	 * Gets all field names of a given table.
1207
	 * 
1229
	 *
1208 1230
	 * This method comes from DefaultJDBC.java class. Postgis driver has no method to check
1209
	 *  the status of the connection -if it is valid or not. So, as it's not possible to assure 
1210
	 *  its status, close the connection when an exception happens and re-open it on demand 
1231
	 *  the status of the connection -if it is valid or not. So, as it's not possible to assure
1232
	 *  its status, close the connection when an exception happens and re-open it on demand
1211 1233
	 *  on the proper method will solve the problems related to an invalid status.
1212
	 * 
1234
	 *
1213 1235
	 * @param conn connection object
1214 1236
	 * @param table_name table name
1215 1237
	 * @return all field names of the given table
......
1246 1268

  
1247 1269
	/**
1248 1270
	 * Gets all field type names of a given table.
1249
	 * 
1271
	 *
1250 1272
	 * This method comes from DefaultJDBC.java class. Postgis driver has no method to check
1251
	 *  the status of the connection -if it is valid or not. So, as it's not possible to assure 
1252
	 *  its status, close the connection when an exception happens and re-open it on demand 
1273
	 *  the status of the connection -if it is valid or not. So, as it's not possible to assure
1274
	 *  its status, close the connection when an exception happens and re-open it on demand
1253 1275
	 *  on the proper method will solve the problems related to an invalid status.
1254
	 * 
1276
	 *
1255 1277
	 * @param conn connection object
1256 1278
	 * @param table_name table name
1257 1279
	 * @return all field type names of the given table
......
1368 1390

  
1369 1391
	/**
1370 1392
	 * Tells if user can read contents of the table.
1371
	 * 
1393
	 *
1372 1394
	 *  @param iconn connection with the database where the user is connected
1373 1395
	 *  @param tablename to get the permissions over
1374 1396
	 *  @return true if can read, either false
......
1408 1430
			} else {
1409 1431
				return false;
1410 1432
			}
1411
		} else return false;
1433
		} else {
1434
		    return false;
1435
		}
1412 1436
	}
1413 1437

  
1414
  
1438

  
1415 1439
	private Integer getGidFieldIndex(String ret[]){
1416 1440
	    for (int i=0; i<ret.length; i++) {
1417 1441
	        if (ret[i].equalsIgnoreCase("gid")) {
1418 1442
	            return new Integer(i);
1419
	        }	    
1443
	        }
1420 1444
	    }
1421 1445
	    return null;
1422 1446
	}
1423
	
1447

  
1424 1448
	private void swapIndexes(String[] ret, int i, int j){
1425 1449
	    if(i!=j && i>=0 && i<ret.length && j>=0 && j<ret.length){
1426 1450
	        String aux = ret[i];
......
1439 1463
	        for (int i = 0; i < ret.length; i++) {
1440 1464
	            if (pk.equals(ret[i])) {
1441 1465
	                    //swap possible gid col with the first element
1442
	                    //in order to make it the default selection on 
1466
	                    //in order to make it the default selection on
1443 1467
	                    //combobox
1444 1468
	                    swapIndexes(ret, i, 0);
1445 1469
	                    break;
......
1449 1473
	        Integer gidFieldIndex = getGidFieldIndex(ret);
1450 1474
	        if (gidFieldIndex!=null){
1451 1475
	            //swap possible gid col with the first element
1452
	            //in order to make it the default selection on 
1476
	            //in order to make it the default selection on
1453 1477
	            //combobox
1454 1478
	            int index = gidFieldIndex.intValue();
1455 1479
	            swapIndexes(ret, index, 0);
......
1457 1481
	            for (int i = 0; i < ret.length; i++) {
1458 1482
	                if (isAutoIncrement(conn, table_name, ret[i])) {
1459 1483
	                    //swap possible gid col with the first element
1460
	                    //in order to make it the default selection on 
1484
	                    //in order to make it the default selection on
1461 1485
	                    //combobox
1462 1486
	                    swapIndexes(ret, i, 0);
1463 1487
	                    break;
......
1469 1493
	}
1470 1494

  
1471 1495
	private boolean isAutoIncrement(IConnection con, String table_name, String colName) {
1472
		
1496

  
1473 1497
		String query = "SELECT column_default SIMILAR TO 'nextval%regclass%' AS isautoincremental "
1474
			+ "FROM information_schema.columns " 
1475
			+ "WHERE table_name = ? AND table_schema=? " 
1498
			+ "FROM information_schema.columns "
1499
			+ "WHERE table_name = ? AND table_schema=? "
1476 1500
			+ "AND column_name=?";
1477
		
1501

  
1478 1502
		try {
1479 1503
			// get schema and table from the composed tablename
1480 1504
			String[] tokens = table_name.split("\\u002E", 2);
......
1486 1510
				schema = tokens[0];
1487 1511
				tableName = tokens[1];
1488 1512
			}
1489
			
1490
			
1513

  
1514

  
1491 1515
			Connection c = ((ConnectionJDBC)con).getConnection();
1492 1516
			PreparedStatement st = c.prepareStatement(query);
1493 1517
			st.setString(1, tableName);
1494 1518
			st.setString(2, schema);
1495 1519
			st.setString(3, colName);
1496
			
1520

  
1497 1521
			ResultSet rs = st.executeQuery();
1498 1522
			boolean isAutoincrement = false;
1499 1523
			if (rs.next()) {
1500 1524
				isAutoincrement = rs.getBoolean("isautoincremental");
1501 1525
			}
1502
			
1526

  
1503 1527
			rs.close();
1504 1528
			st.close();
1505
			
1529

  
1506 1530
			return isAutoincrement;
1507 1531
		} catch (SQLException e) {
1508 1532
			try {
......
1510 1534
			} catch (DBException e2) {
1511 1535
				// TODO Auto-generated catch block
1512 1536
				e.printStackTrace();
1513
			} 
1537
			}
1514 1538
			return false;
1515 1539

  
1516 1540
		}
1517
		
1518
		
1541

  
1542

  
1519 1543
	}
1520 1544

  
1521 1545
	   private String getPrimaryKey(IConnection con, String table_name) {
......
1534 1558
	                schema = tokens[0];
1535 1559
	                tableName = tokens[1];
1536 1560
	            }
1537
	            
1538
	            
1561

  
1562

  
1539 1563
	            Connection c = ((ConnectionJDBC)con).getConnection();
1540 1564
	            PreparedStatement st = c.prepareStatement(query);
1541 1565
	            st.setString(1, tableName);
1542 1566
	            st.setString(2, schema);
1543 1567
	            st.setString(3, tableName+"_pkey");
1544
	            
1568

  
1545 1569
	            ResultSet rs = st.executeQuery();
1546
	            
1570

  
1547 1571
	            String primaryKey = "";
1548 1572
	            if (rs.next()) {
1549 1573
	                primaryKey = rs.getString("column_name");
1550 1574
	            }
1551
	            
1575

  
1552 1576
	            rs.close();
1553 1577
	            st.close();
1554
	            
1578

  
1555 1579
	            return primaryKey;
1556 1580
	        } catch (SQLException e) {
1557 1581
	            try {
......
1559 1583
	            } catch (DBException e2) {
1560 1584
	                // TODO Auto-generated catch block
1561 1585
	                e.printStackTrace();
1562
	            } 
1586
	            }
1563 1587
	            return "";
1564 1588
	        }
1565 1589
	    }
1566
	   
1567 1590

  
1591

  
1568 1592
		public void validateData(IConnection _conn, DBLayerDefinition lyrDef) throws DBException {
1569
			
1593

  
1570 1594
			this.conn = _conn;
1571 1595
			lyrDef.setConnection(conn);
1572 1596
			setLyrDef(lyrDef);
......
1598 1622
						ResultSet.CONCUR_READ_ONLY);
1599 1623
				// st.setFetchSize(FETCH_SIZE);
1600 1624
				// myCursorId++;
1601
				String temp_index_name = getTableName() + "_temp_wkb_cursor"; 
1625
				String temp_index_name = getTableName() + "_temp_wkb_cursor";
1602 1626
				st.execute("declare " + temp_index_name + " binary scroll cursor with hold for "
1603 1627
						+ sqlAux);
1604 1628
				rs = st.executeQuery("fetch forward 50 in " + temp_index_name);
......
1607 1631
				st.close();
1608 1632

  
1609 1633
			} catch (SQLException e) {
1610
				
1634

  
1611 1635
				try {
1612 1636
					((ConnectionJDBC) conn).getConnection().rollback();
1613 1637
				} catch (SQLException e1) {
1614 1638
					logger.warn("Unable to rollback connection after problem (" + e.getMessage() + ") in setData()");
1615 1639
				}
1616
				
1640

  
1617 1641
				try {
1618 1642
					if (rs != null) { rs.close(); }
1619 1643
				} catch (SQLException e1) {
trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/postgis/PostGisFeatureIterator.java
97 97

  
98 98
	String cursorName;
99 99

  
100
    boolean closed = false;
101

  
100 102
	/**
101 103
	 * @throws SQLException
102 104
	 * @throws SQLException
......
109 111
		// de geometria
110 112
		// Debe ser forward only
111 113
		st = conn.createStatement();
112
		
114

  
113 115
		// CodeSprint 2010 (Manuel L?pez S?nchez)
114 116
		try{
115
			st.execute("BEGIN");  
117
			st.execute("BEGIN");
116 118
		}catch(SQLException e){
117 119
			st.execute("END"); // Cerramos la transacci?n para anular los cursores binarios
118
									// que pueden quedar colgados (from CodeSprint 2010)								
120
									// que pueden quedar colgados (from CodeSprint 2010)
119 121
			st.execute("BEGIN"); // Si salta otra excepci?n, no la capturamos
120 122
		}
121 123
		// End CodeSprint 2010
122
		st.execute("declare " + cursorName + " binary cursor for " + sql);
124
	st.execute("declare " + cursorName + " binary cursor with hold for "
125
		+ sql);
123 126

  
124 127
		this.rs = st.executeQuery("fetch forward " + FETCH_SIZE + " in "
125 128
				+ cursorName);
......
138 141
	 */
139 142
	public boolean hasNext() throws ReadDriverException {
140 143
		try {
141
			if (numReg > 0)
142
				if ((numReg % FETCH_SIZE) == 0) {
143
					rs = st.executeQuery("fetch forward " + FETCH_SIZE + " in "
144
							+ cursorName);
145
					// System.out.println("ejecutando la query otra vez");
146
				}
144
			if (numReg > 0) {
145
			    if ((numReg % FETCH_SIZE) == 0) {
146
			    	rs = st.executeQuery("fetch forward " + FETCH_SIZE + " in "
147
			    			+ cursorName);
148
			    	// System.out.println("ejecutando la query otra vez");
149
			    }
150
			}
147 151
			// System.out.println("hasNext con numReg=" + numReg);
148
			if (rs.next())
149
				return true;
150
			else {
152
			if (rs.next()) {
153
			    return true;
154
			} else {
151 155
				closeIterator();
152 156
				return false;
153 157
			}
......
217 221
	 */
218 222
    public void closeIterator() throws ReadDriverException {
219 223
	try {
220
	    numReg = 0;
221
	    // st.execute("CLOSE " + cursorName); fpuga: Not need as we are
222
	    // using non-holdable cursor
223
	    st.execute("COMMIT");
224
	    rs.close();
225
	    st.close();
224
	    if (!closed) {
225
		numReg = 0;
226
		st.execute("CLOSE " + cursorName);
227
		st.execute("END");
228
		rs.close();
229
		st.close();
230
		closed = true;
231
	    }
232

  
226 233
	} catch (SQLException e) {
227 234
	    throw new ReadDriverException("PostGIS Driver", e);
228 235
	}
229 236
    }
230 237

  
238

  
231 239
	public void setLyrDef(DBLayerDefinition lyrDef) {
232 240
		this.lyrDef = lyrDef;
233 241
		// Aunque no nos hayan pedido todos los campos, devolveremos

Also available in: Unified diff