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

View differences:

OracleSpatialDriver.java
119 119

  
120 120
import java.io.File;
121 121
import java.io.IOException;
122
import java.math.BigDecimal;
122 123

  
123 124
import java.sql.Connection;
124 125
import java.sql.DatabaseMetaData;
......
601 602
            // -----------------------
602 603
            _rs = st.executeQuery(not_restricted_sql + " where (rownum = 1)");
603 604
            metaData = _rs.getMetaData();
605
            userName = conn.getMetaData().getUserName();
606
            
604 607

  
605 608
            // geoColInd = _rs.findColumn(geoColName);
606 609
            oneBasedGeoColInd = metaData.getColumnCount() + 1;
......
2759 2762
     * @param withsrid False if the SRS is set to NULL. True otherwise.
2760 2763
     * @return the SQL sentence to perform the update
2761 2764
     */
2762
    public static String getMetadataUpdateSql(String tName, String ora_srid,
2765
    public static String getMetadataUpdateSql(String schema, String tName, String ora_srid,
2763 2766
        Rectangle2D bbox, int dim, boolean withsrid) {
2764 2767
        String[] dim_name = new String[dim];
2765 2768
        double tolerance = 0.5;
......
2782 2785
        }
2783 2786

  
2784 2787
        String resp = "INSERT INTO " + ORACLE_GEOMETADATA_VIEW + " " +
2785
            " ( TABLE_NAME, COLUMN_NAME, DIMINFO, SRID ) " + " VALUES (" + "'" +
2786
            tName + "', " + "'" + DEFAULT_GEO_FIELD + "', " +
2787
            "SDO_DIM_ARRAY( " + "SDO_DIM_ELEMENT ('" + dim_name[0] + "', " +
2788
            " ( OWNER, TABLE_NAME, COLUMN_NAME, DIMINFO, SRID ) " + " VALUES ("
2789
            + "'" + schema + "', "
2790
            + "'" + tName + "', "
2791
            + "'" + DEFAULT_GEO_FIELD + "', " +
2792
            "MDSYS.SDO_DIM_ARRAY( " + "MDSYS.SDO_DIM_ELEMENT ('" + dim_name[0] + "', " +
2788 2793
            bbox.getMinX() + ", " + bbox.getMaxX() + ", " + tolerance + " ), " +
2789
            "SDO_DIM_ELEMENT ('" + dim_name[1] + "', " + bbox.getMinY() + ", " +
2794
            "MDSYS.SDO_DIM_ELEMENT ('" + dim_name[1] + "', " + bbox.getMinY() + ", " +
2790 2795
            bbox.getMaxY() + ", " + tolerance + " ))";
2791 2796

  
2792 2797
        if (dim > 2) {
2793 2798
            resp = resp.substring(0, resp.length() - 1) + ",";
2794
            resp = resp + "SDO_DIM_ELEMENT ('" + dim_name[2] +
2799
            resp = resp + "MDSYS.SDO_DIM_ELEMENT ('" + dim_name[2] +
2795 2800
                "', 0.0, 100.0, " + tolerance + " ))";
2796 2801

  
2797 2802
            if (dim > 3) {
2798 2803
                resp = resp.substring(0, resp.length() - 1) + ",";
2799
                resp = resp + "SDO_DIM_ELEMENT ('" + dim_name[3] +
2804
                resp = resp + "MDSYS.SDO_DIM_ELEMENT ('" + dim_name[3] +
2800 2805
                    "', 0.0, 100.0, " + tolerance + " ))";
2801 2806
            }
2802 2807
        }
......
3767 3772
        throws SQLException {
3768 3773
        DatabaseMetaData dbmd = conn.getMetaData();
3769 3774
        String[] types = { "TABLE", "VIEW" };
3775
        // String[] types = { "VIEW" };
3770 3776

  
3771 3777
        ResultSet rs = null;
3772 3778
        rs = getTableNamesFromTable(dbmd.getTables(catalog, ORACLE_GEO_SCHEMA,
......
3851 3857
     */
3852 3858
    public String[] getIdFieldsCandidates(Connection conn, String table_name)
3853 3859
        throws SQLException {
3854
        String[] resp = { "ROWID" };
3860
    	
3861
    	String rowid_avail_test = "SELECT ROWID FROM " + table_name + " WHERE ROWNUM = 1";
3862
    	Statement _st = conn.createStatement();
3863
    	ResultSet _rs = _st.executeQuery(rowid_avail_test);
3864
    	_rs.close();
3865
    	_st.close();
3855 3866

  
3867
    	String[] resp = { "ROWID" };
3856 3868
        return resp;
3857 3869
    }
3858 3870

  
......
3862 3874
     */
3863 3875
    public String[] getGeometryFieldsCandidates(Connection conn,
3864 3876
        String table_name) throws SQLException {
3877
    	
3865 3878
        Statement _st = conn.createStatement();
3866 3879
        String[] tokens = table_name.split("\\u002E", 2);
3867 3880
        String qry;
......
3889 3902
        _rs.close();
3890 3903
        _st.close();
3891 3904

  
3892
        return (String[]) aux.toArray(new String[0]);
3905
        String[] resp = (String[]) aux.toArray(new String[0]); 
3906
        
3907
        return checkIndexes(conn, resp, table_name);
3893 3908
    }
3894 3909

  
3895
    /**
3910
    private String[] checkIndexes(Connection c, String[] all, String __t) throws SQLException {
3911

  
3912
    	ArrayList good_ones = new ArrayList();
3913
    	String t = __t;
3914
    	if (t.lastIndexOf(".") != -1) t = t.substring(t.lastIndexOf(".") + 1, t.length());
3915

  
3916
    	for (int i=0; i<all.length; i++) {
3917
    		
3918
        	String qry = "SELECT SRID, DIMINFO FROM " + ORACLE_GEOMETADATA_VIEW +
3919
            " WHERE TABLE_NAME = " + "'" + t.toUpperCase() +
3920
            "' AND COLUMN_NAME = '" + all[i].toUpperCase() + "'";
3921
        	
3922
        	Statement _st = c.createStatement();
3923
        	ResultSet _rs = _st.executeQuery(qry);
3924
        	if (_rs.next()) {
3925
        		String _srid = toString((BigDecimal) _rs.getObject(1));
3926
        		ARRAY diminfo = (ARRAY) _rs.getObject(2);
3927
        		int len = diminfo.getOracleArray().length;
3928
        		if (allowsGeoQueries(c, t, all[i], _srid, len)) {
3929
        			good_ones.add(all[i]);
3930
        		}
3931
        	}
3932
        	_rs.close();
3933
        	_st.close();
3934
    	}
3935
    	
3936
    	if (good_ones.size() == 0) {
3937
    		throw new SQLException("no_indexes_on_declared_geo_fields");
3938
    	}
3939
    	return (String[]) good_ones.toArray(new String[0]);
3940
    }
3941
    
3942
    private String toString(BigDecimal number) {
3943
    	
3944
    	if (number == null) return "NULL";
3945
    	return "" + number.intValue();
3946
	}
3947

  
3948
	private boolean allowsGeoQueries(Connection c, String _t, String gf, String _srid, int dims) {
3949
    	String p = getPointConstructor(dims, _srid);
3950
    	String qry = "SELECT * FROM " + _t.toUpperCase() + " WHERE (ROWNUM = 1)";
3951
    	qry = "SELECT * FROM (" + qry + ") WHERE SDO_RELATE(" + "\"" + gf + "\", " + p + ", 'mask=TOUCH') = 'TRUE'";
3952

  
3953
    	try {
3954
			Statement _st = c.createStatement();
3955
			ResultSet _rs = _st.executeQuery(qry);
3956
			_rs.close();
3957
			_st.close();
3958
		} catch (Exception ex) {
3959
			return false;
3960
		}
3961
		return true;
3962
	}
3963

  
3964
	private String getPointConstructor(int dims, String _srid) {
3965
		
3966
		String coord = "";
3967
		for (int i=0; i<dims; i++) coord = coord + "0, ";
3968
		coord = coord.substring(0, coord.length() - 2);
3969
		
3970
		return "MDSYS.SDO_GEOMETRY(" + (dims * 1000 + 1) + ", " + _srid + ", NULL, " +
3971
		"MDSYS.SDO_ELEM_INFO_ARRAY(1, 1, 1), MDSYS.SDO_ORDINATE_ARRAY(" + coord + "))";
3972
	}
3973

  
3974
	private boolean stringInArrayListOfStrings(ArrayList l, String str) {
3975
    	
3976
    	if (l == null) return false;
3977
    	if (str == null) return false;
3978
    	
3979
    	String item = "";
3980
    	for (int i=0; i<l.size(); i++) {
3981
    		if (l.get(i) instanceof String) {
3982
    			item = (String) l.get(i);
3983
    			if (item.compareToIgnoreCase(str) == 0) return true;
3984
    		}
3985
    	}
3986
    	return false;
3987
    }
3988

  
3989
	/**
3896 3990
     * Utility method to check if a given table is empty.
3897 3991
     */
3898 3992
    public boolean isEmptyTable(Connection conn, String tableName) {
......
4271 4365
		return resp_aux;
4272 4366
    }
4273 4367
    
4368
    public void setUserName(String u) {
4369
    	userName = u;
4370
    }
4274 4371
    
4372
    public String getUserName() {
4373
    	return userName;
4374
    }
4275 4375
    
4376
    
4377
    
4276 4378
}

Also available in: Unified diff