Revision 32518 trunk/extensions/extOracleSpatial/src/es/prodevelop/cit/gvsig/fmap/drivers/jdbc/oracle/OracleSpatialDriver.java

View differences:

OracleSpatialDriver.java
82 82
import com.hardcode.gdbms.engine.values.Value;
83 83
import com.hardcode.gdbms.engine.values.ValueFactory;
84 84
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
85
import com.iver.cit.gvsig.fmap.core.FGeometry;
85 86
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
86 87
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
87 88
import com.iver.cit.gvsig.fmap.core.FShape;
......
135 136
public class OracleSpatialDriver extends DefaultJDBCDriver
136 137
    implements IDelayedDriver, ICanReproject, IWriteable {
137 138
    private static Logger logger = Logger.getLogger(OracleSpatialDriver.class.getName());
138
    private static int FETCH_SIZE = 15000;
139 139

  
140 140
    // constants
141 141
    public static final int GEODETIC_FULLEXTENT_SAMPLE_SIZE = 50;
142 142
    public static final String GEODETIC_SRID = "8307";
143 143
    // public static final String ASSUMED_ORACLE_SRID = "8307";
144 144
    
145
    // ------------------------------------------------
145
    // -----------------------------------------------
146 146
    public static final String NAME = "Oracle Spatial Database Driver";
147 147
    public static final int ID_COLUMN_INDEX = 1;
148 148
    
......
159 159
    public static final String ORACLE_GEO_SCHEMA = "MDSYS";
160 160
    public static final String CONN_STR_BEGIN = "jdbc:oracle:thin:";
161 161
    
162
    public static final int VARCHAR2_STANDARD_SIZE = 80;
163
    public static final int VARCHAR2_LONG_SIZE = 256;
162
    public static final int VARCHAR2_DEFAULT_SIZE = 2000; //  512;  
163
    public static final int VARCHAR2_MAX_SIZE = 2000;
164
    
164 165
    public static final int MAX_ID_LENGTH = 30;
165 166
    private final static GeometryFactory geomFactory = new GeometryFactory();
166 167
    public static final double IRRELEVANT_DISTANCE = 0.00000001;
......
168 169

  
169 170
	public static final String ORACLE_JAR_FILE_NAME = "oracle.jdbc.driver.OracleDriver";
170 171

  
172
	
171 173
    static {
172 174
        try {
173 175
            Class.forName(ORACLE_JAR_FILE_NAME);
176
    		logger.info("*-----------------------------------------------------------------------*");
177
    		logger.info("* Found the Oracle JDBC library! :)                                     *");
178
    		logger.info("*-----------------------------------------------------------------------*");
179
            
180
        } catch (ClassNotFoundException e) {
181
       		logger.warn("*-----------------------------------------------------------------------*");
182
       		logger.warn("* Oracle JDBC library (ojdbc*.jar) not found. You need to copy that");
183
       		logger.warn("* jar file to gvSIG's main lib folder if you intend to access");
184
       		logger.warn("* Oracle Spatial/Locator databases.");
185
       		logger.warn("* Read gvSIG manual (Oracle driver section) for details.");
186
       		logger.warn("*-----------------------------------------------------------------------*");
174 187
        }
175
        catch (ClassNotFoundException e) {
176
            throw new RuntimeException(e);
177
        }
178 188
    }
179 189

  
180 190
    private OracleSpatialWriter writer = null;
......
225 235
	private String destProjOracle;
226 236
	private boolean isDestGeogCS = false;
227 237

  
238
	private int adaptedFetchSize = 100;
239
    // private static int FETCH_SIZE = 15000;
240

  
241

  
228 242
    public OracleSpatialDriver() {
229 243
        drvAtts = new DriverAttributes();
230 244
        drvAtts.setLoadedInMemory(false);
......
535 549
//            _igeom = getGeotoolsIGeometry(theStruct);
536 550
        }
537 551
        else { // jgeometry
538
            _igeom = getFMapGeometry(theStruct, false);
552
        	// lastGeom = printStruct(theStruct.getOracleAttributes());
553
            _igeom = getFMapGeometry(theStruct, false, 0);
539 554
        }
540 555

  
541 556
        return _igeom;
542 557
    }
543 558
    
559
    // private static String lastGeom = "";
560
    
544 561
    /**
545 562
     * Utility method to transform a struct into a IGeometry.
546 563
     *
......
548 565
     * @param complex comes from a complex sdo geometry
549 566
     * @return the IGeometry
550 567
     */
551
    private IGeometry getFMapGeometry(STRUCT st, boolean complex) {
568
    private IGeometry getFMapGeometry(STRUCT st, boolean complex, int rec_iter) {
552 569

  
553 570
    	if (st == null) {
554 571
    		return new FNullGeometry();
555 572
    	}
556 573
    	
557

  
558 574
        Datum[] the_data = null;
559 575

  
576
        
577
        
560 578
        try {
561 579
            the_data = st.getOracleAttributes();
580
            
581
            /*
582
        	if (rec_iter > 4) {
583
        		logger.error(lastGeom);
584
        		return new FNullGeometry();
585
        	}
586
        	*/
562 587

  
563
            int jgtype = ((NUMBER) the_data[0]).intValue() % 1000;
564
            jgtype = OracleSpatialUtils.oracleGTypeToFShapeType(jgtype, complex);
588
            int full_gtype = ((NUMBER) the_data[0]).intValue();
589
            // int jgtype = ((NUMBER) the_data[0]).intValue() % 1000;
590
            int fshape_type = OracleSpatialUtils.oracleGTypeToFShapeType(full_gtype, complex);
565 591

  
566 592
            int dim = ((NUMBER) the_data[0]).intValue() / 1000;
567 593

  
......
570 596
            }
571 597

  
572 598
            IGeometry ig = null;
573

  
574
            if (OracleSpatialUtils.isActuallyACollection(the_data)) {
575
                jgtype = FShape.MULTI;
599
            // ========================== collection issue with rects
600
            int collec_val = OracleSpatialUtils.isCollection(the_data);
601
            switch (collec_val) {
602
            case OracleSpatialUtils.COLLECTION_VALUE_YES_COLLECTION:
603
            	fshape_type = FShape.MULTI;
604
            	break;
605
            case OracleSpatialUtils.COLLECTION_VALUE_MERGE_COLLECTION_IN_POLYGON:
606
            	fshape_type = FShape.MULTI;
607
            	break;
608
            case OracleSpatialUtils.COLLECTION_VALUE_NOT_COLLECTION:
609
            	break;
576 610
            }
611
            // ========================================================
577 612

  
578
            switch (jgtype) {
613
            switch (fshape_type) {
579 614
            case FShape.MULTI:
580

  
581
                // int srid = ((NUMBER) the_data[1]).intValue();
582
                ig = getFMapGeometryCollection(the_data, dim);
583

  
615
                ig = getFMapGeometryCollection(the_data, dim, rec_iter);
616
            	if (collec_val == OracleSpatialUtils.COLLECTION_VALUE_MERGE_COLLECTION_IN_POLYGON) {
617
            		ig = OracleSpatialUtils.mergePolygons((FGeometryCollection) ig);
618
            	}
584 619
                break;
585 620

  
586 621
            case FShape.POINT:
......
609 644
    }
610 645

  
611 646

  
612
    private Rectangle2D getRectangle(double minx, double maxx, double miny,
647
	private static String printStruct(Datum[] datt) {
648
    	
649
    	String resp = "\n";
650
    	resp = resp + "============= START === PRINTING STRUCT:\n";
651
    	int sz = datt.length;
652
    	for (int i=0; i<sz; i++) {
653
    		resp = resp + printDatum(i, datt[i]);
654
    	}
655
    	resp = resp + "============= END === PRINTING STRUCT\n";
656
    	return resp;
657
	}
658
    
659
    
660

  
661
	private static String printDatum(int n, Datum d) {
662
		
663
		String resp = "";
664
		if (d == null) {
665
			resp = "============= DATUM: " + n + " ; VALUE = NULL\n";
666
			return resp;
667
		}
668
		
669
		if (d instanceof ARRAY) {
670
			ARRAY arr = (ARRAY) d;
671
			double[] darr = null;
672
			int[] iarr = null;
673
			try {
674
				
675
				resp = "============= DATUM: " + n + " ; VALUE:\n";
676
				darr = arr.getDoubleArray();
677
				// String valstr = "";
678
				int sz = darr.length;
679
				for (int i=0; i<sz; i++) {
680
					resp = resp + darr[i] + " , "; 
681
				}
682
				resp = resp + "\n"; 
683
				
684
			} catch (SQLException e) {
685
				try {
686
					resp = "============= DATUM: " + n + " ; VALUE:\n";
687
					iarr = arr.getIntArray();
688
					// String valstr = "";
689
					int sz = iarr.length;
690
					for (int i=0; i<sz; i++) {
691
						resp = resp + iarr[i] + " , "; 
692
					}
693
					resp = resp + "\n"; 
694
				} catch (SQLException e2) {
695
					resp = "============= DATUM: " + n + " ; VALUE = " + d.toString() + " (CAST FAILED)\n";
696
				}
697
			}
698
		} else {
699
			if (d instanceof NUMBER) {
700
				int v = -10;
701
				try {
702
					v = ((NUMBER) d).intValue();
703
					resp = "============= DATUM: " + n + " ; VALUE = " + v + "\n";
704
				} catch (SQLException e) {
705
					resp = "============= DATUM: " + n + " ; VALUE = " + d.toString() + " (CAST TO INT FAILED)\n";
706
				}
707
			} else {
708
				resp = "============= DATUM: " + n + " ; VALUE = " + d.toString() + " (NOT NUMBER)\n";
709
			}
710
		}
711
		return resp;
712
		
713
	}
714

  
715
	private Rectangle2D getRectangle(double minx, double maxx, double miny,
613 716
        double maxy) {
614 717
        Rectangle2D resp = new Rectangle2D.Double(minx, miny, maxx - minx,
615 718
                maxy - miny);
......
626 729
            shapeType = guessShapeType();
627 730
            // -----------------------
628 731
            _rs = st.executeQuery(not_restricted_sql + " where (rownum = 1)");
629
            metaData = _rs.getMetaData();
732
            metaData = _rs.getMetaData(); 
733
            
734
            this.setAdaptedFetchSize(OracleSpatialUtils.estimateGoodFetchSize(metaData));
735
            
630 736
            userName = ((ConnectionJDBC)conn).getConnection().getMetaData().getUserName();
631 737

  
632 738
            // geoColInd = _rs.findColumn(geoColName);
......
922 1028

  
923 1029
            // st = ((ConnectionJDBC)conn).getConnection().createStatement();
924 1030

  
925
             st.setFetchSize(FETCH_SIZE);
926
             logger.info("FETCH_SIZE = " + FETCH_SIZE);
1031
             st.setFetchSize(getAdaptedFetchSize());
1032
             logger.info("FETCH_SIZE = " + getAdaptedFetchSize());
927 1033

  
928 1034
            ResultSet _r = null;
929 1035
            _r = st.executeQuery(_sql);
......
1043 1149
            i = idField + 1; // idField viene basado en 0
1044 1150

  
1045 1151
            int __type = metaData.getColumnType(i);
1046
            int size = metaData.getPrecision(i);
1152
            
1153
            int size = 1;
1154
            // avoid exception
1155
            try { size = metaData.getPrecision(i); } catch (Exception ex) { }
1156
            
1047 1157
            int dec_pos = metaData.getScale(i);
1048 1158

  
1049 1159
            // we must add this entry because we did not remove the 'geometry' column
......
1210 1320

  
1211 1321

  
1212 1322

  
1213
    private IGeometry getFMapGeometryCollection(Datum[] the_data, int dim) {
1323
    private IGeometry getFMapGeometryCollection(Datum[] the_data, int dim, int reciter) {
1214 1324
        // int __srid) {
1215 1325

  
1216 1326
    	NUMBER _srid = new NUMBER(0);
1217 1327
        NUMBER main_type = new NUMBER((dim * 1000) +
1218 1328
                OracleSpatialUtils.getStructType(the_data));
1219 1329

  
1220

  
1221 1330
        Datum[] all_info_array = null;
1222 1331
        Object[] elems_info_aray = null;
1223 1332
        Datum[] all_ords = null;
......
1232 1341
            ords_of_groups = OracleSpatialUtils.getOrdOfGroups(all_ords, elems_info_aray);
1233 1342
            _elems_info_aray = new Object[elems_info_aray.length];
1234 1343
        }
1235
        catch (SQLException e) {
1344
        catch (SQLException e) { 
1236 1345
            logger.error("Unexpected error: " + e.getMessage());
1237 1346
        }
1238 1347

  
......
1280 1389
                        item_info_array, item_ords, ((ConnectionJDBC)conn).getConnection());
1281 1390
            }
1282 1391

  
1283
            geoms[i] = getFMapGeometry(itemst, true);
1392
            geoms[i] = getFMapGeometry(itemst, true, reciter + 1);
1284 1393
        }
1285 1394

  
1286 1395
        return new FGeometryCollection(geoms);
......
1469 1578
            _stmnt = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,
1470 1579
                    ResultSet.CONCUR_READ_ONLY);
1471 1580
            _stmnt.setFetchDirection(ResultSet.FETCH_FORWARD);
1472
            _stmnt.setFetchSize(FETCH_SIZE);
1581
            _stmnt.setFetchSize(getAdaptedFetchSize());
1473 1582

  
1474 1583
            _rs = _stmnt.executeQuery(main_sel);
1475 1584

  
......
1610 1719

  
1611 1720

  
1612 1721
    public static String fieldTypeToSqlStringType(FieldDescription fieldDesc) {
1613
        String aux = "VARCHAR2(" + VARCHAR2_STANDARD_SIZE + " BYTE)"; // Por defecto.
1722
        String aux = "VARCHAR2(" + VARCHAR2_DEFAULT_SIZE + " BYTE)"; // Por defecto.
1614 1723

  
1615 1724
        switch (fieldDesc.getFieldType()) {
1616 1725
        case Types.SMALLINT:
......
3631 3740
	
3632 3741
	public static int nvarchar2Limited(int n) {
3633 3742
		
3634
		if (n <= VARCHAR2_STANDARD_SIZE) return VARCHAR2_STANDARD_SIZE;
3635
		if (n <= VARCHAR2_LONG_SIZE) return n;
3743
		if (n <= VARCHAR2_DEFAULT_SIZE) return VARCHAR2_DEFAULT_SIZE;
3744
		if (n <= VARCHAR2_MAX_SIZE) return n;
3636 3745
		
3637
		return VARCHAR2_LONG_SIZE;
3746
		return VARCHAR2_MAX_SIZE;
3638 3747
	}
3639 3748
	
3640 3749

  
......
3742 3851
		} else {
3743 3852
			return writer.canWriteGeometry(gvSIGgeometryType);
3744 3853
		}
3745
		
3746 3854
	}
3747 3855

  
3748 3856
	public static String getTableExistsSql(DBLayerDefinition dbLayerDef) {
3749 3857
		
3750 3858
        return "SELECT * FROM " + dbLayerDef.getTableName();
3751 3859
	}
3860

  
3861
	public int getAdaptedFetchSize() {
3862
		return adaptedFetchSize;
3863
	}
3864

  
3865
	public void setAdaptedFetchSize(int v) {
3866
		adaptedFetchSize = v;
3867
	}
3752 3868
        
3753 3869
    
3754 3870

  

Also available in: Unified diff