Revision 14028 branches/v10/extensions/extOracleSpatial/src/es/prodevelop/cit/gvsig/fmap/drivers/jdbc/oracle/OracleSpatialDriver.java

View differences:

OracleSpatialDriver.java
71 71
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
72 72
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
73 73
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
74
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC;
75
import com.iver.cit.gvsig.fmap.drivers.DBException;
74 76
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
75
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
77
import com.iver.cit.gvsig.fmap.drivers.DefaultJDBCDriver;
76 78
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
77 79
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
80
import com.iver.cit.gvsig.fmap.drivers.IConnection;
78 81
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
82
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
83
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleVectorialDBConnectionManager;
79 84
import com.iver.cit.gvsig.fmap.drivers.dbf.DBFDriver;
80
import com.iver.cit.gvsig.fmap.drivers.jdbc.utils.ConnectionWithParams;
81
import com.iver.cit.gvsig.fmap.drivers.jdbc.utils.SingleJDBCConnectionManager;
82 85
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
83 86
import com.iver.cit.gvsig.fmap.edition.EditionException;
84 87
import com.iver.cit.gvsig.fmap.edition.IWriteable;
......
162 165
 * @author jldominguez
163 166
 *
164 167
 */
165
public class OracleSpatialDriver extends DefaultDBDriver
168
public class OracleSpatialDriver extends DefaultJDBCDriver
166 169
    implements IDelayedDriver, ICanReproject, IWriteable {
167 170
    private static Logger logger = Logger.getLogger(OracleSpatialDriver.class.getName());
168 171
    private static int FETCH_SIZE = 15000;
169 172

  
170
    // constants 
173
    // constants
171 174
    public static final int GEODETIC_FULLEXTENT_SAMPLE_SIZE = 50;
172 175
    public static final String GEODETIC_SRID = "8307";
173 176
    public static final String ASSUMED_ORACLE_SRID = "8307";
......
180 183
    public static final String ORACLE_EPSG_FILE_NAME = "ORA_EPSG.DBF";
181 184
    public static final String DEFAULT_GEO_FIELD = "GEOMETRY";
182 185
    // public static final String DEFAULT_GEO_FIELD = "MERGEDGEOMETRY";
183
    
186

  
184 187
    public static final String ORACLE_ID_FIELD = "ROWID";
185 188
    public static final String DEFAULT_ID_FIELD = "GID";
186 189
    public static final String ORACLE_GEO_SCHEMA = "MDSYS";
......
193 196
	private static final long ID_MIN_DELAY = 1000;
194 197

  
195 198
	public static final String ORACLE_JAR_FILE_NAME = "oracle.jdbc.driver.OracleDriver";
196
	
199

  
197 200
    static {
198 201
        try {
199 202
            Class.forName(ORACLE_JAR_FILE_NAME);
......
221 224
    private int[] pkOneBasedIndexes;
222 225
    private String[] fieldNames;
223 226
    private String not_restricted_sql = "";
224
    
227

  
225 228
    private Rectangle2D workingAreaInViewsCS = null;
226 229
    private Rectangle2D workingAreaInTablesCS = null;
227 230
    private STRUCT workingAreaInTablesCSStruct = null;
......
259 262
        drvAtts = new DriverAttributes();
260 263
        drvAtts.setLoadedInMemory(false);
261 264
    }
262
    
265

  
263 266
	public String getWhereClause() {
264 267
	    return lyrDef.getWhereClause();
265 268
	}
266
    
267 269

  
270

  
268 271
    /**
269 272
     * This method is called when the user creates a new oracle
270 273
     * table from a vectorial layer
......
273 276
     * <tt>DBLayerDefinition</tt>
274 277
     */
275 278
    public void setData(Object[] params) {
276
        setData((Connection) params[0], (DBLayerDefinition) params[1]);
279
        setData((IConnection) params[0], (DBLayerDefinition) params[1]);
277 280
    }
278 281

  
279 282
    private void adjustLyrDef() throws SQLException {
......
308 311
    /**
309 312
     * Standard initializing method.
310 313
     */
311
    public void setData(Connection _conn, DBLayerDefinition lyrDef) {
314
    public void setData(IConnection _conn, DBLayerDefinition lyrDef) {
312 315
        conn = _conn;
313 316

  
314 317
        // This metadata is required to store layer in GVP
315 318
        // without problems:
316 319
        ConnectionWithParams _cwp =
317
        	SingleJDBCConnectionManager.instance().findConnection(conn);
320
        	SingleVectorialDBConnectionManager.instance().findConnection(conn);
318 321
		host = _cwp.getHost();
319 322
		port = _cwp.getPort();
320 323
		dbName = _cwp.getDb();
......
334 337
        cleanWhereClause();
335 338
        loadSdoMetadata();
336 339
        oneRowMetadata();
337
        
340

  
338 341
        setDestProjection(lyrDef.getSRID_EPSG());
339
        
342

  
340 343
        IProjection viewProj = CRSFactory.getCRS("EPSG:" + destProj);
341 344
        IProjection tableProj = CRSFactory.getCRS("EPSG:" + epsgSRID);
342 345
        ICoordTrans reprojecter = viewProj.getCT(tableProj);
......
363 366
    }
364 367

  
365 368
    private void getMetadata() {
366
    	
369

  
367 370
    	long id_load_start = System.currentTimeMillis();
368 371
        setIdRowTable();
369 372
        long id_load_end = System.currentTimeMillis();
370
        
373

  
371 374
        long delay = id_load_end - id_load_start;
372 375
        if (delay < ID_MIN_DELAY) {
373 376
        	logger.info("Ids thread delayed by: " + (ID_MIN_DELAY - delay) + " ms.");
......
397 400
                getTableName() + " c";
398 401

  
399 402
            // SDO_ELEM_INFO
400
            Statement _st = conn.createStatement();
403
            Statement _st = ((ConnectionJDBC)conn).getConnection().createStatement();
401 404
            ResultSet _rs = _st.executeQuery(qry);
402 405

  
403 406
            ArrayList types = new ArrayList();
......
526 529

  
527 530
    private void loadSdoMetadata() {
528 531
        try {
529
            Statement _st = conn.createStatement();
532
            Statement _st = ((ConnectionJDBC)conn).getConnection().createStatement();
530 533
            String[] tokens = getTableName().split("\\u002E", 2);
531 534
            String qry;
532 535
            if (tokens.length > 1)
533 536
            {
534 537
            	qry = "select * from " + ORACLE_GEOMETADATA_VIEW +
535
                " where OWNER = '" + tokens[0] + "' AND TABLE_NAME = '" + 
538
                " where OWNER = '" + tokens[0] + "' AND TABLE_NAME = '" +
536 539
                tokens[1] + "'";
537 540
            }
538 541
            else
......
557 560
					tableHasSrid = false;
558 561
				}
559 562
                full_Extent = getFullExtentFromCurrentRecord(_rs);
560
                
561
                hasRealiableExtent = realiableExtent(full_Extent, isGeogCS); 
562
                
563

  
564
                hasRealiableExtent = realiableExtent(full_Extent, isGeogCS);
565

  
563 566
                if (!hasRealiableExtent) {
564 567
                	full_Extent = getFastEstimatedGeodeticExtent(
565 568
                			getTableName(), geoColName, conn, 20, 10);
......
619 622
            String _sql = "select " + getStandardSelectExpression() + ", c." +
620 623
                geoColName + " from " + getTableName() + " c ";
621 624

  
622
            st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
625
            st = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
623 626
                    ResultSet.CONCUR_READ_ONLY);
624 627

  
625 628
            ResultSet _rs = st.executeQuery(_sql + " where (rownum = 1)");
......
635 638
            // -----------------------
636 639
            _rs = st.executeQuery(not_restricted_sql + " where (rownum = 1)");
637 640
            metaData = _rs.getMetaData();
638
            
639
            userName = conn.getMetaData().getUserName();
640
            
641

  
642
            userName = ((ConnectionJDBC)conn).getConnection().getMetaData().getUserName();
643

  
641 644
            // geoColInd = _rs.findColumn(geoColName);
642 645
            oneBasedGeoColInd = metaData.getColumnCount() + 1;
643 646

  
644
            DatabaseMetaData dbmd = conn.getMetaData();
647
            DatabaseMetaData dbmd = ((ConnectionJDBC)conn).getConnection().getMetaData();
645 648
            pkOneBasedIndexes = getPKIndexes(dbmd, _rs);
646 649

  
647 650
            int cnt = metaData.getColumnCount();
......
655 658

  
656 659
            adjustLyrDef();
657 660

  
658
            _rs.close(); // st no debe cerrarse ya que las llamadas a metadata lo encesitan 
661
            _rs.close(); // st no debe cerrarse ya que las llamadas a metadata lo encesitan
659 662
        }
660 663
        catch (SQLException se) {
661 664
            logger.error("While getting metadata. " + se.getMessage());
......
664 667

  
665 668
    private int getShapeTypeOfStruct(STRUCT sample) throws SQLException {
666 669
        int code = ((NUMBER) sample.getOracleAttributes()[0]).intValue() % 10;
667
        
670

  
668 671
        switch (code) {
669 672
        case 1:
670 673
            return FShape.POINT;
......
775 778
        throws DriverException {
776 779
        if (isNotAvailableYet) {
777 780
            return null;
778
        } 
781
        }
779 782

  
780 783
        singleCachedFeatureRowNum = -1;
781 784

  
......
836 839
            " where rowid = ?";
837 840

  
838 841
        try {
839
            java.sql.PreparedStatement ps = conn.prepareStatement(_sql);
842
            java.sql.PreparedStatement ps = ((ConnectionJDBC)conn).getConnection().prepareStatement(_sql);
840 843
            ps.setObject(1, r_id);
841 844

  
842 845
            // Statement stmnt = conn.createStatement();
......
892 895
            logger.debug("SQL para leer ids: " + _sql);
893 896
            Statement st = null;
894 897

  
895
            
896
            st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
898

  
899
            st = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,
897 900
                        ResultSet.CONCUR_READ_ONLY);
898
            
901

  
899 902
            st.setFetchSize(FETCH_SIZE);
900 903
            logger.info("FETCH_SIZE = " + FETCH_SIZE);
901
            
904

  
902 905
            ResultSet _r = null;
903 906
            _r = st.executeQuery(_sql);
904 907

  
......
1162 1165

  
1163 1166
        return res;
1164 1167
    }
1165
    
1168

  
1166 1169
    public Value[] getAttributesUsingMainMetadata(ResultSet rs) {
1167 1170
        Value[] res = null;
1168 1171

  
......
1235 1238

  
1236 1239
        return res;
1237 1240
    }
1238
    
1239 1241

  
1242

  
1240 1243
    public String getFieldName(int fieldId)
1241 1244
        throws com.hardcode.gdbms.engine.data.driver.DriverException {
1242 1245
        return fieldNames[fieldId];
......
1279 1282
            String rnq = getSearchId();
1280 1283
            ROWID _id = (ROWID) rowToId.get(new Integer((int) rowIndex));
1281 1284

  
1282
            ps = conn.prepareStatement(rnq);
1285
            ps = ((ConnectionJDBC)conn).getConnection().prepareStatement(rnq);
1283 1286
            ps.setObject(1, _id);
1284 1287

  
1285 1288
            ps.execute();
......
1401 1404

  
1402 1405
    private IGeometry getFMapGeometryCollection(Datum[] the_data, int dim) {
1403 1406
        // int __srid) {
1404
    	
1407

  
1405 1408
    	NUMBER _srid = new NUMBER(0);
1406 1409
        NUMBER main_type = new NUMBER((dim * 1000) +
1407 1410
                OracleSpatialUtils.getStructType(the_data));
1408
        
1409 1411

  
1412

  
1410 1413
        Datum[] all_info_array = null;
1411 1414
        Object[] elems_info_aray = null;
1412 1415
        Datum[] all_ords = null;
......
1417 1420
            all_info_array = ((ARRAY) the_data[3]).getOracleArray();
1418 1421
            elems_info_aray = groupByElement(all_info_array);
1419 1422
            all_ords = ((ARRAY) the_data[4]).getOracleArray();
1420
            
1423

  
1421 1424
            ords_of_groups = getOrdOfGroups(all_ords, elems_info_aray);
1422 1425
            _elems_info_aray = new Object[elems_info_aray.length];
1423 1426
        }
......
1445 1448

  
1446 1449
                gtype = new NUMBER((dim * 1000) +
1447 1450
                        (item_info_array[1].intValue() % 1000));
1448
                
1451

  
1449 1452
                if (tableHasSrid) {
1450 1453
                	_srid = new NUMBER(Integer.parseInt(oracleSRID));
1451 1454
                }
......
1456 1459

  
1457 1460
            // if it's the first geometry, the type is the collection's main type (no?) - no
1458 1461
            // if (i == 0) gtype = main_type;
1459
            
1462

  
1460 1463
            STRUCT itemst = null;
1461 1464

  
1462 1465
            if (tableHasSrid) {
1463
            	
1466

  
1464 1467
                itemst = OracleSpatialUtils.createStruct(gtype, _srid,
1465
                        item_info_array, item_ords, conn);
1468
                        item_info_array, item_ords, ((ConnectionJDBC)conn).getConnection());
1466 1469
            }
1467 1470
            else {
1468 1471
                itemst = OracleSpatialUtils.createStruct(gtype, null,
1469
                        item_info_array, item_ords, conn);
1472
                        item_info_array, item_ords, ((ConnectionJDBC)conn).getConnection());
1470 1473
            }
1471 1474

  
1472 1475
            geoms[i] = getFMapGeometry(itemst, true);
......
1483 1486
     * @return the IGeometry
1484 1487
     */
1485 1488
    public IGeometry getFMapGeometry(STRUCT st, boolean force_not_collection) {
1486
    	
1489

  
1487 1490
    	if (st == null) {
1488 1491
    		return new FNullGeometry();
1489 1492
    	}
1490
    	
1493

  
1491 1494
        Datum[] the_data = null;
1492 1495

  
1493 1496
        try {
......
1895 1898
        }
1896 1899

  
1897 1900
        if (size == 2) {
1898
            return ((info[1] % 1000) != (info[4] % 1000)) && 
1901
            return ((info[1] % 1000) != (info[4] % 1000)) &&
1899 1902
            ( ! ((info[1] == 1005) && (info[4] == 2)) );
1900 1903
        }
1901 1904

  
......
2324 2327
        getLyrDef().setWhereClause("");
2325 2328
        emptyWhereClause = true;
2326 2329
    }
2327
    
2330

  
2328 2331
    private String getValidViewConstructor(
2329 2332
    		STRUCT _st,
2330 2333
    		String ora_srid,
2331 2334
    		boolean _hassrid,
2332 2335
    		boolean _isgeocs) {
2333
    	
2336

  
2334 2337
    	String sdo = getSdoConstructor(_st, _hassrid, _isgeocs);
2335 2338
    	String resp = "";
2336 2339
    	if ((_hassrid) && (_isgeocs)) {
......
2338 2341
    	} else {
2339 2342
    		resp = sdo;
2340 2343
    	}
2341
    	 
2344

  
2342 2345
    	return resp;
2343 2346
    }
2344 2347

  
......
2353 2356
                resp = "select " + getStandardSelectExpression() + ", c." +
2354 2357
                    geoColName + " from " + getTableName() + " c where ";
2355 2358
                if (idsLoadWhere.length() > 0) {
2356
                	resp = resp + " (" + idsLoadWhere + ") AND "; 
2359
                	resp = resp + " (" + idsLoadWhere + ") AND ";
2357 2360
                }
2358 2361
                resp = resp + "(" + vport + ")";
2359 2362
        }
......
2361 2364
                resp = "select " + getStandardSelectExpression() + ", c." +
2362 2365
                    geoColName + " from " + getTableName() + " c where ";
2363 2366
                if (idsLoadWhere.length() > 0) {
2364
                	resp = resp + " (" + idsLoadWhere + ") AND "; 
2367
                	resp = resp + " (" + idsLoadWhere + ") AND ";
2365 2368
                }
2366 2369
                resp = resp + "(" + "sdo_relate(" + geoColName + ", " + viewsdo +
2367 2370
                ", 'mask=anyinteract querytype=window') = 'TRUE')";
2368 2371
        }
2369
        
2372

  
2370 2373
//        return "select " + getStandardSelectExpression() + ", c." +
2371 2374
//        geoColName + " from " + getTableName() + " c";
2372 2375
        return resp;
......
2431 2434
    }
2432 2435

  
2433 2436
    private static STRUCT rectangleToStruct(Rectangle2D r, boolean hasSrid,
2434
        boolean isView, boolean _isGeogCS, String _oracleSRID, Connection __conn) {
2437
        boolean isView, boolean _isGeogCS, String _oracleSRID, IConnection __conn) {
2435 2438
        Point2D c1 = new Point2D.Double(r.getMinX(), r.getMinY());
2436 2439
        Point2D c2 = new Point2D.Double(r.getMaxX(), r.getMaxY());
2437 2440

  
......
2477 2480

  
2478 2481
            // StructDescriptor dsc = StructDescriptor.createDescriptor("STRUCT", conn);
2479 2482
            StructDescriptor dsc = StructDescriptor.createDescriptor("MDSYS.SDO_GEOMETRY",
2480
                    __conn);
2483
            		((ConnectionJDBC)__conn).getConnection());
2481 2484

  
2482
            resp = new STRUCT(dsc, __conn, new_obj);
2485
            resp = new STRUCT(dsc,((ConnectionJDBC)__conn).getConnection(), new_obj);
2483 2486
        }
2484 2487
        catch (Exception ex) {
2485 2488
            logger.error("Error while creating rect struct: " +
......
2510 2513
        Object[] _resp = new Object[2];
2511 2514

  
2512 2515
        try {
2513
            _stmnt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
2516
            _stmnt = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,
2514 2517
                    ResultSet.CONCUR_READ_ONLY);
2515 2518
            _stmnt.setFetchDirection(ResultSet.FETCH_FORWARD);
2516 2519
            _stmnt.setFetchSize(FETCH_SIZE);
......
2574 2577

  
2575 2578
        return resp;
2576 2579
    }
2577
    
2580

  
2578 2581
    private String getIdsQueryWhereClause(boolean with_where) {
2579 2582
		String resp = "";
2580
		
2583

  
2581 2584
		String _where = "";
2582 2585
		if (with_where) _where = " where ";
2583 2586

  
......
2609 2612
    public String getIdAndElemInfoFullResulltSetQuery() {
2610 2613
        String resp = "select rowid, c." + geoColName + ".SDO_ELEM_INFO from " +
2611 2614
            getTableName() + " c";
2612
        
2615

  
2613 2616
        resp = resp + getIdsQueryWhereClause(true);
2614 2617
        return resp;
2615 2618
    }
......
2795 2798

  
2796 2799
        return resp;
2797 2800
    }
2798
    
2801

  
2799 2802
    private static String getDerivedNAme(String tname, String suffix) {
2800
    	
2801
    	int ind = tname.lastIndexOf("."); 
2803

  
2804
    	int ind = tname.lastIndexOf(".");
2802 2805
    	if (ind == -1) {
2803
    		
2806

  
2804 2807
    		int l = Math.min(28, tname.length());
2805 2808
    		return tname.substring(0, l) + "_" + suffix;
2806 2809

  
2807 2810
    	} else {
2808
    		
2811

  
2809 2812
    		String pre = tname.substring(0, ind);
2810 2813
    		String post = tname.substring(ind + 1, tname.length());
2811 2814
    		int lpost = Math.min(24, post.length());
2812 2815
    		int lpre = Math.min(3, pre.length());
2813 2816
    		return pre.substring(0, lpre) + "_" + post.substring(0, lpost) + "_" + suffix;
2814 2817
    	}
2815
    	
2818

  
2816 2819
    }
2817 2820

  
2818 2821
    public static String getIndexCreationSql(DBLayerDefinition dbLayerDef) {
......
2825 2828
    }
2826 2829

  
2827 2830
    public static String getRemoveMetadataSql(DBLayerDefinition dbLayerDef) {
2828
    	
2831

  
2829 2832
    	String tname = dbLayerDef.getTableName();
2830 2833
    	int ind = tname.lastIndexOf(".");
2831 2834
    	if (ind != -1) {
......
2833 2836
    		tname = tname.substring(ind + 1, tname.length());
2834 2837
            return "DELETE FROM " + ORACLE_GEOMETADATA_VIEW +
2835 2838
            " WHERE TABLE_NAME = '" + tname + "' AND OWNER = '" + schema + "'";
2836
    		 
2839

  
2837 2840
    	} else{
2838 2841
            return "DELETE FROM " + ORACLE_GEOMETADATA_VIEW +
2839 2842
            " WHERE TABLE_NAME = '" + tname + "'";
......
3004 3007
    }
3005 3008

  
3006 3009
    private static boolean isUserEditableType(int ftype, String item_name, String geo_name) {
3007
    	
3010

  
3008 3011
    	if (item_name.compareToIgnoreCase(geo_name) == 0) {
3009 3012
    		return true;
3010 3013
    	}
3011
    	
3014

  
3012 3015
    	if ((ftype == Types.BINARY)
3013 3016
        	|| (ftype == Types.ARRAY)
3014 3017
        	|| (ftype == Types.BLOB)
......
3453 3456
        try {
3454 3457
			destProjOracle = epsgSridToOracleSrid(destProj);
3455 3458
			isDestGeogCS = getIsGCS(destProjOracle, true);
3456
			
3459

  
3457 3460
		} catch (Exception e) {
3458 3461
			logger.error("Unknown EPSG code: " + destProj);
3459 3462
			destProjOracle = oracleSRID;
3460 3463
			isDestGeogCS = false;
3461 3464
		}
3462
        
3465

  
3463 3466
    }
3464
    
3467

  
3465 3468
    public String getDestProjectionOracleCode() {
3466 3469
    	return destProjOracle;
3467 3470
    }
3468
    
3471

  
3469 3472
    public boolean getIsDestProjectionGeog() {
3470 3473
    	return isDestGeogCS;
3471 3474
    }
3472
    
3475

  
3473 3476
    public String getTableProjectionOracleCode() {
3474 3477
    	return oracleSRID;
3475 3478
    }
......
3558 3561
        Rectangle2D resp = null;
3559 3562

  
3560 3563
        try {
3561
            Statement st = conn.createStatement();
3564
            Statement st = ((ConnectionJDBC)conn).getConnection().createStatement();
3562 3565
            ResultSet rs = st.executeQuery(_qry);
3563 3566

  
3564 3567
            if (rs.next()) {
......
3657 3660
    /**
3658 3661
     * Does what it says, reverses the order of the Coordinates in the linestring.
3659 3662
     * @param ls The ls to reverse.
3660
     * @param gf a GeometryFactory object 
3663
     * @param gf a GeometryFactory object
3661 3664
     * @return A new ls with the reversed Coordinates.
3662 3665
     *
3663 3666
     */
......
3722 3725
     * @return the generated STRUCT
3723 3726
     */
3724 3727
    public static STRUCT iGeometryToSTRUCT(IGeometry ig, int _forced_type,
3725
        Connection _conn, String _o_srid, boolean withSrid, boolean agu_bien,
3728
        IConnection _conn, String _o_srid, boolean withSrid, boolean agu_bien,
3726 3729
        boolean _isGeoCS) {
3727 3730
        if (ig instanceof FGeometryCollection) {
3728 3731
            FGeometryCollection coll = (FGeometryCollection) ig;
......
3743 3746

  
3744 3747
    public STRUCT shapeToStruct(Shape shp, int force_type, boolean hasSrid,
3745 3748
        boolean agu_bien, boolean isView) {
3746
    	
3747
    	if (shp == null) return null; 
3749

  
3750
    	if (shp == null) return null;
3748 3751
        return shapeToStruct(shp, force_type, conn, oracleSRID, hasSrid,
3749 3752
            agu_bien, isView, isGeogCS);
3750 3753
    }
3751 3754

  
3752 3755
    public static STRUCT shapeToStruct(Shape shp, int forced_type,
3753
        Connection _conn, String o_srid, boolean hasSrid, boolean agu_bien,
3756
        IConnection _conn, String o_srid, boolean hasSrid, boolean agu_bien,
3754 3757
        boolean isView, boolean _isGeoCS) {
3755 3758
        int _srid = -1;
3756 3759

  
......
3807 3810

  
3808 3811
    // -------------------------------------------------------
3809 3812
    // -------------------------------------------------------
3810
    public String[] getTableNames(Connection conn, String catalog)
3811
        throws SQLException {
3812
        DatabaseMetaData dbmd = conn.getMetaData();
3813
    public String[] getTableNames(IConnection conn, String catalog)
3814
        throws DBException {
3815
        try{
3816
    	DatabaseMetaData dbmd = ((ConnectionJDBC)conn).getConnection().getMetaData();
3813 3817
        String[] types = { "TABLE", "VIEW" };
3814 3818
        // String[] types = { "VIEW" };
3815 3819

  
3816 3820
        ResultSet rs = null;
3817 3821
        rs = getTableNamesFromTable(dbmd.getTables(catalog, ORACLE_GEO_SCHEMA,
3818
                    ORACLE_GEOMETADATA_VIEW, types), conn);
3822
                    ORACLE_GEOMETADATA_VIEW, types), ((ConnectionJDBC)conn).getConnection());
3819 3823
//        rs = dbmd.getTables(catalog, ORACLE_GEO_SCHEMA,
3820 3824
//                          ORACLE_GEOMETADATA_VIEW, types);
3821 3825
        TreeMap ret = new TreeMap();
......
3826 3830
        }
3827 3831

  
3828 3832
        return (String[]) ret.keySet().toArray(new String[0]);
3833
        }catch (SQLException e) {
3834
			throw new DBException(e);
3835
		}
3829 3836
    }
3830 3837

  
3831 3838
    private ResultSet getTableNamesFromTable(ResultSet res, Connection con)
3832 3839
        throws SQLException {
3833 3840
        String tablename = "";
3834
        
3835
        
3836 3841

  
3842

  
3843

  
3837 3844
        if (res.next()) {
3838 3845
            tablename = res.getString("TABLE_NAME");
3839
            
3840
            // debug 
3846

  
3847
            // debug
3841 3848
            writeMetaTableToLog(con, tablename);
3842 3849

  
3843 3850
            Statement __st = con.createStatement();
......
3858 3865
    }
3859 3866

  
3860 3867
    private void writeMetaTableToLog(Connection con, String tname) {
3861
    	
3868

  
3862 3869
    	logger.debug("======================================================");
3863 3870
    	logger.debug("=     " + ORACLE_GEOMETADATA_VIEW + "     =====================");
3864 3871
    	logger.debug("======================================================");
......
3873 3880
            	logger.debug("TABLE_NAME: " + res.getString("TABLE_NAME"));
3874 3881
            	logger.debug("COLUMN_NAME: " + res.getString("COLUMN_NAME"));
3875 3882
            	logger.debug("SRID: " + res.getString("SRID"));
3876
            	
3883

  
3877 3884
            	ARRAY _aux = (ARRAY) res.getObject("DIMINFO");
3878 3885
            	String dinfo = OracleSpatialUtils.getDimInfoAsString(_aux);
3879 3886
            	logger.debug("DIMINFO: " + dinfo);
3880 3887
            	logger.debug("======================================================");
3881
            	
3888

  
3882 3889
            }
3883 3890
    	} catch (Throwable th) {
3884
    		
3891

  
3885 3892
    	}
3886
    	
3887
    	
3888
    	
3889
    	
3890
    	
3893

  
3894

  
3895

  
3896

  
3897

  
3891 3898
		// TODO Auto-generated method stub
3892
		
3899

  
3893 3900
	}
3894 3901

  
3895 3902
	/**
3896 3903
     * Gets the field names that can act as row id (always ROWID)
3897 3904
     */
3898
    public String[] getIdFieldsCandidates(Connection conn, String table_name)
3899
        throws SQLException {
3900
    	
3905
    public String[] getIdFieldsCandidates(IConnection conn, String table_name)
3906
        throws DBException {
3907
    	try{
3901 3908
    	String rowid_avail_test = "SELECT ROWID FROM " + table_name + " WHERE ROWNUM = 1";
3902
    	Statement _st = conn.createStatement();
3909
    	Statement _st = ((ConnectionJDBC)conn).getConnection().createStatement();
3903 3910
    	ResultSet _rs = _st.executeQuery(rowid_avail_test);
3904 3911
    	_rs.close();
3905 3912
    	_st.close();
3906 3913

  
3907 3914
    	String[] resp = { "ROWID" };
3908 3915
        return resp;
3916
    	}catch (SQLException e) {
3917
			throw new DBException(e);
3918
		}
3909 3919
    }
3910 3920

  
3911 3921
    /**
3912 3922
     * Gets the field names that can act as geometry fields
3913 3923
     * (queries the user's geographic metadata).
3914 3924
     */
3915
    public String[] getGeometryFieldsCandidates(Connection conn,
3916
        String table_name) throws SQLException {
3917
    	
3918
        Statement _st = conn.createStatement();
3925
    public String[] getGeometryFieldsCandidates(IConnection conn,
3926
        String table_name) throws DBException {
3927
    	try{
3928
        Statement _st = ((ConnectionJDBC)conn).getConnection().createStatement();
3919 3929
        String[] tokens = table_name.split("\\u002E", 2);
3920 3930
        String qry;
3921 3931
        if (tokens.length > 1)
3922 3932
        {
3923 3933
        	qry = "select * from " + ORACLE_GEOMETADATA_VIEW +
3924
            " where OWNER = '" + tokens[0] + "' AND TABLE_NAME = '" + 
3934
            " where OWNER = '" + tokens[0] + "' AND TABLE_NAME = '" +
3925 3935
            tokens[1] + "'";
3926 3936
        }
3927 3937
        else
......
3942 3952
        _rs.close();
3943 3953
        _st.close();
3944 3954

  
3945
        String[] resp = (String[]) aux.toArray(new String[0]); 
3946
        
3955
        String[] resp = (String[]) aux.toArray(new String[0]);
3956

  
3947 3957
        return checkIndexes(conn, resp, table_name);
3958
    	}catch (SQLException e) {
3959
			throw new DBException(e);
3960
		}
3948 3961
    }
3949 3962

  
3950
    private String[] checkIndexes(Connection c, String[] all, String long_table_name) throws SQLException {
3963
    private String[] checkIndexes(IConnection c, String[] all, String long_table_name) throws DBException {
3951 3964

  
3952 3965
    	ArrayList good_ones = new ArrayList();
3966
    	try{
3953 3967
    	String t = long_table_name;
3954 3968
    	if (t.lastIndexOf(".") != -1) t = t.substring(t.lastIndexOf(".") + 1, t.length());
3955 3969

  
3956 3970
    	for (int i=0; i<all.length; i++) {
3957
    		
3971

  
3958 3972
        	String qry = "SELECT SRID, DIMINFO FROM " + ORACLE_GEOMETADATA_VIEW +
3959 3973
            " WHERE TABLE_NAME = " + "'" + t.toUpperCase() +
3960 3974
            "' AND COLUMN_NAME = '" + all[i].toUpperCase() + "'";
3961
        	
3962
        	Statement _st = c.createStatement();
3975

  
3976
        	Statement _st = ((ConnectionJDBC)c).getConnection().createStatement();
3963 3977
        	ResultSet _rs = _st.executeQuery(qry);
3964 3978
        	if (_rs.next()) {
3965 3979
        		String _srid = toString((BigDecimal) _rs.getObject(1));
3966 3980
        		ARRAY diminfo = (ARRAY) _rs.getObject(2);
3967 3981
        		int len = diminfo.getOracleArray().length;
3968
        		if (allowsGeoQueries(c, long_table_name, all[i], _srid, len)) {
3982
        		if (allowsGeoQueries(((ConnectionJDBC)c).getConnection(), long_table_name, all[i], _srid, len)) {
3969 3983
        			good_ones.add(all[i]);
3970 3984
        		}
3971 3985
        	}
3972 3986
        	_rs.close();
3973 3987
        	_st.close();
3974 3988
    	}
3975
    	
3989

  
3976 3990
    	if (good_ones.size() == 0) {
3977 3991
    		throw new SQLException("no_indexes_on_declared_geo_fields");
3978 3992
    	}
3993
    	}catch (SQLException e) {
3994
			throw new DBException(e);
3995
		}
3979 3996
    	return (String[]) good_ones.toArray(new String[0]);
3980 3997
    }
3981
    
3998

  
3982 3999
    private String toString(BigDecimal number) {
3983
    	
4000

  
3984 4001
    	if (number == null) return "NULL";
3985 4002
    	return "" + number.intValue();
3986 4003
	}
......
4002 4019
	}
4003 4020

  
4004 4021
	private String getPointConstructor(int dims, String _srid) {
4005
		
4022

  
4006 4023
		String coord = "";
4007 4024
		for (int i=0; i<dims; i++) coord = coord + "0, ";
4008 4025
		coord = coord.substring(0, coord.length() - 2);
4009
		
4026

  
4010 4027
		return "MDSYS.SDO_GEOMETRY(" + (dims * 1000 + 1) + ", " + _srid + ", NULL, " +
4011 4028
		"MDSYS.SDO_ELEM_INFO_ARRAY(1, 1, 1), MDSYS.SDO_ORDINATE_ARRAY(" + coord + "))";
4012 4029
	}
4013 4030

  
4014 4031
	private boolean stringInArrayListOfStrings(ArrayList l, String str) {
4015
    	
4032

  
4016 4033
    	if (l == null) return false;
4017 4034
    	if (str == null) return false;
4018
    	
4035

  
4019 4036
    	String item = "";
4020 4037
    	for (int i=0; i<l.size(); i++) {
4021 4038
    		if (l.get(i) instanceof String) {
......
4051 4068
    /**
4052 4069
     * Gets all the fields from a table name.
4053 4070
     */
4054
    public String[] getAllFields(Connection conn, String table_name)
4055
        throws SQLException {
4056
        Statement st = conn.createStatement();
4071
    public String[] getAllFields(IConnection conn, String table_name)
4072
        throws DBException {
4073
    	try{
4074
        Statement st = ((ConnectionJDBC)conn).getConnection().createStatement();
4057 4075
        ResultSet rs = st.executeQuery("select * from " + table_name +
4058 4076
                " where rownum = 1");
4059 4077
        ResultSetMetaData rsmd = rs.getMetaData();
......
4067 4085
        st.close();
4068 4086

  
4069 4087
        return ret;
4088
    	}catch (SQLException e) {
4089
			throw new DBException(e);
4090
		}
4070 4091
    }
4071 4092

  
4072 4093
    /**
4073 4094
     * Gets all field type names from a table.
4074 4095
     */
4075
    public String[] getAllFieldTypeNames(Connection conn, String table_name)
4076
        throws SQLException {
4077
        Statement st = conn.createStatement();
4096
    public String[] getAllFieldTypeNames(IConnection conn, String table_name)
4097
        throws DBException {
4098
    	try{
4099
        Statement st = ((ConnectionJDBC)conn).getConnection().createStatement();
4078 4100
        ResultSet rs = st.executeQuery("select * from " + table_name +
4079 4101
                " where rownum = 1");
4080 4102
        ResultSetMetaData rsmd = rs.getMetaData();
......
4090 4112
        close();
4091 4113

  
4092 4114
        return ret;
4115
    	}catch (SQLException e) {
4116
			throw new DBException(e);
4117
		}
4093 4118
    }
4094 4119

  
4095 4120
    /**
......
4278 4303
    public void remove() {
4279 4304
        cancelIDLoad = true;
4280 4305
    }
4281
    
4306

  
4282 4307
    private boolean realiableExtent(Rectangle2D ext, boolean isgeodetic) {
4283 4308
    	// if (!isgeodetic) return true;
4284
    	if ((ext.getMinX() > -179.9) || (ext.getMinX() < -180.1)) return true; 
4285
    	if ((ext.getMinY() > -89.9) || (ext.getMinY() < -90.1)) return true; 
4286
    	if ((ext.getWidth() < 359.9) || (ext.getWidth() > 360.1)) return true; 
4287
    	if ((ext.getHeight() < 179.9) || (ext.getHeight() > 180.1)) return true; 
4309
    	if ((ext.getMinX() > -179.9) || (ext.getMinX() < -180.1)) return true;
4310
    	if ((ext.getMinY() > -89.9) || (ext.getMinY() < -90.1)) return true;
4311
    	if ((ext.getWidth() < 359.9) || (ext.getWidth() > 360.1)) return true;
4312
    	if ((ext.getHeight() < 179.9) || (ext.getHeight() > 180.1)) return true;
4288 4313
    	return false;
4289 4314
    }
4290
    
4315

  
4291 4316
    private Rectangle2D getFastEstimatedGeodeticExtent(
4292
    		String tname, String gfield, Connection c, int sample_size, double enlargement) {
4317
    		String tname, String gfield, IConnection c, int sample_size, double enlargement) {
4293 4318

  
4294 4319
    	Rectangle2D world = new Rectangle2D.Double(-180, -90, 360, 180);
4295 4320
    	Rectangle2D resp_aux = null;
......
4297 4322
    	ResultSet _rs = null;
4298 4323

  
4299 4324
    	try {
4300
			PreparedStatement _st = c.prepareStatement(qry);
4325
			PreparedStatement _st = ((ConnectionJDBC)c).getConnection().prepareStatement(qry);
4301 4326
			_rs = _st.executeQuery();
4302 4327
			while (_rs.next()) {
4303 4328
				STRUCT aux = (STRUCT) _rs.getObject(1);
4304 4329
				IGeometry ig = getGeometryUsing(aux, false);
4305
				
4330

  
4306 4331
				if (ig == null) continue;
4307
				
4332

  
4308 4333
				if (resp_aux == null) {
4309 4334
					resp_aux = ig.getBounds2D();
4310 4335
				} else {
4311 4336
					resp_aux.add(ig.getBounds2D());
4312 4337
				}
4313
				
4338

  
4314 4339
			}
4315 4340
		} catch (Exception ex) {
4316 4341
			logger.error("While getting random sample: " + ex.getMessage());
4317 4342
			return world;
4318 4343
		}
4319
		
4344

  
4320 4345
		if (resp_aux == null) return world;
4321 4346
		double w = resp_aux.getWidth();
4322 4347
		double h = resp_aux.getHeight();
4323 4348
		double x = resp_aux.getMinX();
4324 4349
		double y = resp_aux.getMinY();
4325
		
4350

  
4326 4351
		// enlarge 10 times:
4327 4352
		double newx = x - (0.5 * (enlargement - 1)) * w;
4328 4353
		double newy = y - (0.5 * (enlargement - 1)) * w;
4329 4354
		Rectangle2D resp_aux_large = new Rectangle2D.Double(newx, newy,
4330 4355
				enlargement * w,
4331 4356
				enlargement * h);
4332
		
4333
		
4357

  
4358

  
4334 4359
		Rectangle2D.intersect(world, resp_aux_large, resp_aux);
4335
		
4360

  
4336 4361
		logger.debug("FAST BB:");
4337 4362
		logger.debug(" min x:" + resp_aux.getMinX());
4338 4363
		logger.debug(" min y:" + resp_aux.getMinY());
......
4340 4365
		logger.debug("     h:" + resp_aux.getHeight());
4341 4366
		return resp_aux;
4342 4367
    }
4343
    
4368

  
4344 4369
    private Rectangle2D getEstimatedGeodeticExtent(
4345
    		String tname, String gfield, Connection c, int sample_size, double enlargement) {
4346
    	
4370
    		String tname, String gfield, IConnection c, int sample_size, double enlargement) {
4371

  
4347 4372
    	Rectangle2D world = new Rectangle2D.Double(-180, -90, 360, 180);
4348
    	
4373

  
4349 4374
    	ArrayList ids = new ArrayList();
4350 4375
    	int _rnd_index = 0;
4351 4376
    	ROWID _id = null;
4352 4377
    	Random rnd = new Random(System.currentTimeMillis());
4353
    	
4378

  
4354 4379
    	for (int i=0; i<sample_size; i++) {
4355 4380
    		_rnd_index = rnd.nextInt(numReg);
4356 4381
    		_id = (ROWID) rowToId.get(new Integer((int) _rnd_index));
4357 4382
    		ids.add(_id.stringValue());
4358 4383
    	}
4359
    	
4384

  
4360 4385
    	String qry = "SELECT " + gfield + " FROM " + tname + " WHERE (";
4361 4386
    	for (int i=0; i<ids.size(); i++) {
4362
    		qry = qry + "(ROWID = '" + ((String) ids.get(i)) + "') OR "; 
4387
    		qry = qry + "(ROWID = '" + ((String) ids.get(i)) + "') OR ";
4363 4388
    	}
4364 4389
    	qry = qry.substring(0, qry.length() - 4) + ")";
4365
    	
4390

  
4366 4391
    	Rectangle2D resp_aux = null;
4367 4392
    	ResultSet _rs = null;
4368 4393

  
4369 4394
    	try {
4370
			PreparedStatement _st = c.prepareStatement(qry);
4395
			PreparedStatement _st = ((ConnectionJDBC)c).getConnection().prepareStatement(qry);
4371 4396
			_rs = _st.executeQuery();
4372 4397
			while (_rs.next()) {
4373 4398
				STRUCT aux = (STRUCT) _rs.getObject(1);
4374 4399
				IGeometry ig = getGeometryUsing(aux, false);
4375
				
4400

  
4376 4401
				if (ig == null) continue;
4377
				
4402

  
4378 4403
				if (resp_aux == null) {
4379 4404
					resp_aux = ig.getBounds2D();
4380 4405
				} else {
4381 4406
					resp_aux.add(ig.getBounds2D());
4382 4407
				}
4383
				
4408

  
4384 4409
			}
4385 4410
		} catch (Exception ex) {
4386 4411
			logger.error("While getting random sample: " + ex.getMessage());
4387 4412
			return world;
4388 4413
		}
4389
		
4414

  
4390 4415
		if (resp_aux == null) return world;
4391 4416
		double w = resp_aux.getWidth();
4392 4417
		double h = resp_aux.getHeight();
4393 4418
		double x = resp_aux.getMinX();
4394 4419
		double y = resp_aux.getMinY();
4395
		
4420

  
4396 4421
		// enlarge 10 times:
4397 4422
		double newx = x - (0.5 * (enlargement - 1)) * w;
4398 4423
		double newy = y - (0.5 * (enlargement - 1)) * w;
4399 4424
		Rectangle2D resp_aux_large = new Rectangle2D.Double(newx, newy,
4400 4425
				enlargement * w,
4401 4426
				enlargement * h);
4402
		
4403
		
4427

  
4428

  
4404 4429
		Rectangle2D.intersect(world, resp_aux_large, resp_aux);
4405 4430
		return resp_aux;
4406 4431
    }
4407
    
4432

  
4408 4433
    public void setUserName(String u) {
4409 4434
    	userName = u;
4410 4435
    }
4411
    
4436

  
4412 4437
    public String getUserName() {
4413 4438
    	return userName;
4414 4439
    }
......
4420 4445
    public static final int JGeometry_GTYPE_MULTIPOLYGON = 7;
4421 4446
    public static final int JGeometry_GTYPE_POINT = 1;
4422 4447
    public static final int JGeometry_GTYPE_POLYGON = 3;
4423
    
4448

  
4424 4449
    // ------------------------------
4425
    
4450

  
4426 4451
    public XMLEntity getXMLEntity() {
4427 4452
		// ---------------------
4428 4453

  
......
4449 4474
		xml.putProperty("port", port);
4450 4475
		xml.putProperty("dbName", dbName);
4451 4476
		xml.putProperty("connName", connName);
4452
		
4477

  
4453 4478
		if (getWorkingArea() != null) {
4454 4479
			xml.putProperty("minXworkArea", getWorkingArea().getMinX());
4455 4480
			xml.putProperty("minYworkArea", getWorkingArea().getMinY());
......
4459 4484

  
4460 4485
		return xml;
4461 4486
	}
4487
    public String[] getTableFields(IConnection conex, String table) throws DBException {
4488
		try{
4489
		Statement st = ((ConnectionJDBC)conex).getConnection().createStatement();
4490
        // ResultSet rs = dbmd.getTables(catalog, null, dbLayerDefinition.getTable(), null);
4491
		ResultSet rs = st.executeQuery("select * from " + table + " LIMIT 1");
4492
		ResultSetMetaData rsmd = rs.getMetaData();
4462 4493

  
4494
		String[] ret = new String[rsmd.getColumnCount()];
4463 4495

  
4496
		for (int i = 0; i < ret.length; i++) {
4497
			ret[i] = rsmd.getColumnName(i+1);
4498
		}
4499

  
4500
		return ret;
4501
		}catch (SQLException e) {
4502
			throw new DBException(e);
4503
		}
4504
	}
4505

  
4464 4506
}

Also available in: Unified diff