Statistics
| Revision:

root / branches / FMap_SLD / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySQLdriver.java @ 1755

History | View | Annotate | Download (7.34 KB)

1
/*
2
 * Created on 04-mar-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 * 
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *  
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 * 
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *  
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 * 
34
 *    or
35
 * 
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 * 
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql;
45

    
46
import java.awt.geom.Rectangle2D;
47
import java.sql.Connection;
48
import java.sql.ResultSet;
49
import java.sql.SQLException;
50
import java.sql.Statement;
51

    
52
import com.iver.cit.gvsig.fmap.DriverException;
53
import com.iver.cit.gvsig.fmap.core.IGeometry;
54
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
55
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
56
import com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator;
57
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisGeometryIterator;
58
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.WKTParser;
59
import com.vividsolutions.jts.io.ParseException;
60

    
61
/**
62
 * @author FJP
63
 *
64
 * TODO To change the template for this generated type comment go to
65
 * Window - Preferences - Java - Code Generation - Code and Comments
66
 */
67
public class MySQLdriver extends DefaultDBDriver {
68
    private WKTParser parser = new WKTParser();
69
    private int fetch_min=-1;
70
    private int fetch_max=-1;
71
    private Statement st;
72
    private Rectangle2D fullExtent = null;
73
    private String strAux;
74
    
75

    
76
    /**
77
     * 
78
     */
79
    public MySQLdriver() {
80
    }
81
    /* (non-Javadoc)
82
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
83
     */
84
    public DriverAttributes getDriverAttributes() {
85
        return null;
86
    }
87

    
88
    /* (non-Javadoc)
89
     * @see com.hardcode.driverManager.Driver#getName()
90
     */
91
    public String getName() {
92
        return "PostGIS JDBC Driver";
93
    }
94
    
95
        /**
96
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
97
         */
98
        public IGeometry getShape(int index) {
99
            IGeometry geom = null;
100
            boolean resul;
101
                try {
102
                    // EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
103
                    // FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
104
                    // LO HAGA
105
                    // System.out.println("getShape " + index);
106
                    if (index < fetch_min)
107
                    {
108
                        rs.close();
109
                            
110
                            rs = st.executeQuery(sqlOrig);
111
                        fetch_min = 0;
112
                        fetch_max = rs.getFetchSize();
113
                    }
114
                    while (index >= fetch_max)
115
                    {
116
                        rs.last();
117
                        // forzamos una carga
118
                        rs.next();
119
                        fetch_min = fetch_max;
120
                        fetch_max = fetch_max + rs.getFetchSize();
121
                        // System.out.println("fetchSize = " + rs.getFetchSize() + " " + fetch_min + "-" + fetch_max);
122
                    }
123
                    rs.absolute(index+1 - fetch_min);
124
                    strAux = rs.getString(1);                
125
                    geom = parser.read(strAux);                
126
            } catch (SQLException e) {
127
                e.printStackTrace();
128
            } catch (ParseException e) {
129
                e.printStackTrace();
130
            }
131
                
132
            return geom;
133
        }
134
        /**
135
         * @param conn
136
         * @param tableName
137
         * @param fields OJO: EL PRIMER CAMPO HA DE SER EL DE GEOMETRIA
138
         * @param whereClause
139
         */
140
        public void setData(Connection conn, String tableName, String fields, String whereClause)
141
        {
142
            this.conn = conn;            
143
            this.tableName = tableName;
144
            this.fields = fields;
145
            this.whereClause = whereClause;
146
            this.sqlOrig = "SELECT " + fields + " FROM " + tableName + " " + whereClause;
147
            try {
148
                st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
149
                st.setFetchSize(500);
150
                rs = st.executeQuery(sqlOrig);
151
            fetch_min = 0;
152
            fetch_max = rs.getFetchSize();
153
            metaData = rs.getMetaData();
154
        } catch (SQLException e) {
155
            // TODO Auto-generated catch block
156
            e.printStackTrace();
157
        }
158
        }
159
        
160
        /**
161
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
162
         */
163
        public Rectangle2D getFullExtent(){
164
            if (fullExtent == null)
165
            {
166
                try
167
            {
168
                    GeometryIterator itGeom = getGeometryIterator("SELECT ASTEXT(ogc_geom) AS the_geom FROM " + tableName);
169
                    IGeometry geom;
170
                    int cont = 0;
171
                    while (itGeom.hasNext())
172
                    {
173
                        geom = itGeom.next();
174
                        if (cont==0)
175
                            fullExtent = geom.getBounds2D();
176
                        else
177
                            fullExtent.add(geom.getBounds2D());
178
                        cont++;
179
                    }
180
            }
181
                catch (SQLException e)
182
                {
183
                    System.err.println(e.getMessage());
184
                } catch (DriverException e) {
185
                // TODO Auto-generated catch block
186
                e.printStackTrace();
187
            }
188
                
189
            }
190
            return fullExtent;
191
        }
192
    /* (non-Javadoc)
193
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
194
     */
195
    public GeometryIterator getGeometryIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException {
196
        Statement st;
197
        MySqlGeometryIterator geomIterator = null;
198
        try {
199
            st = conn.createStatement();
200
            st.setFetchSize(2000);
201
            ResultSet rs = st.executeQuery(sql);
202
            geomIterator = new MySqlGeometryIterator(rs);
203
        } catch (SQLException e) {
204
            e.printStackTrace();
205
            throw new com.iver.cit.gvsig.fmap.DriverException(e);
206
        }
207
            
208
        return geomIterator;
209
    }
210
    /* (non-Javadoc)
211
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D)
212
     */
213
    public GeometryIterator getGeometryIterator(Rectangle2D r, String strEPSG) throws DriverException {
214
        double xMin = r.getMinX();
215
        double yMin = r.getMinY();
216
        double xMax = r.getMaxX();
217
        double yMax = r.getMaxY();
218
        
219
        String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", "
220
                + xMax + " " + yMin + ", "
221
                + xMax + " " + yMax + ", "
222
                + xMin + " " + yMax + ")', "
223
                + strEPSG + ")";
224
        String sqlAux;
225
        if (getWhereClause().startsWith("WHERE")) 
226
            sqlAux = sqlOrig + " MBRIntersects(" + wktBox + ",ogc_geom);" ;
227
        else
228
            sqlAux = sqlOrig + "WHERE MBRIntersects(" + wktBox + ",ogc_geom);" ;
229
        
230

    
231
        return getGeometryIterator(sqlAux);
232
    }
233
        
234
    
235
    
236
}