Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySQLDriver.java @ 2183

History | View | Annotate | Download (9.12 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.IFeatureIterator;
57
import com.iver.cit.gvsig.fmap.drivers.WKBParser;
58
import com.vividsolutions.jts.io.ParseException;
59

    
60
/**
61
 * @author FJP
62
 *
63
 * TODO To change the template for this generated type comment go to
64
 * Window - Preferences - Java - Code Generation - Code and Comments
65
 */
66
public class MySQLDriver extends DefaultDBDriver {
67
    private WKBParser parser = new WKBParser();
68
    /* private int fetch_min=-1;
69
    private int fetch_max=-1; */
70
    private Statement st;
71
    private Rectangle2D fullExtent = null;
72
    private String strAux;
73
    private Rectangle2D workingArea;
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 "mySQL 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
                    rs.absolute(index+1);
125
                    // strAux = rs.getString(1);                
126
                    // geom = parser.read(strAux);
127
                    byte[] data = rs.getBytes(1);                
128
                    geom = parser.parse(data);
129
                    
130
                    
131
            } catch (SQLException e) {
132
                e.printStackTrace();
133
            }
134
                
135
            return geom;
136
        }
137
        /**
138
         * @param conn
139
         * @param tableName
140
         * @param fields OJO: EL PRIMER CAMPO HA DE SER EL DE GEOMETRIA
141
         * @param whereClause
142
         */
143
        public void setData(Connection conn, String tableName, String fields, String whereClause, int id_FID_field)
144
        {
145
            this.conn = conn;            
146
            this.tableName = tableName;
147
            this.fields = fields;
148
            this.whereClause = whereClause;
149
            this.sqlOrig = "SELECT " + fields + " FROM " + tableName + " " + whereClause;
150
        this.idFID_FieldName = id_FID_field;
151
            try {
152
                st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
153
                rs = st.executeQuery(sqlOrig);
154
            metaData = rs.getMetaData();
155
            // Le pegamos un primera pasada para poder relacionar
156
            // un campo de identificador ?nico (parecido al OID en
157
            // postgresql) con el ?ndice dentro del recordset.
158
            // Esto cuando haya ediciones, no es v?lido, y hay
159
            // que refrescarlo.
160
            doRelateID_FID();
161
            
162
        } catch (SQLException e) {
163
            // TODO Auto-generated catch block
164
            e.printStackTrace();
165
        }
166
        }
167
        
168
        /**
169
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
170
         */
171
        public Rectangle2D getFullExtent(){
172
            if (fullExtent == null)
173
            {
174
                try
175
            {
176
                    IFeatureIterator itGeom = getFeatureIterator("SELECT ASBINARY(ogc_geom) AS the_geom FROM " + tableName);
177
                    IGeometry geom;
178
                    int cont = 0;
179
                    while (itGeom.hasNext())
180
                    {
181
                        geom = itGeom.next().getGeometry();
182
                        if (cont==0)
183
                            fullExtent = geom.getBounds2D();
184
                        else
185
                            fullExtent.add(geom.getBounds2D());
186
                        cont++;
187
                    }
188
            }
189
                catch (SQLException e)
190
                {
191
                    System.err.println(e.getMessage());
192
                } catch (DriverException e) {
193
                // TODO Auto-generated catch block
194
                e.printStackTrace();
195
            }
196
                
197
            }
198
            return fullExtent;
199
        }
200
    /* (non-Javadoc)
201
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
202
     */
203
    public IFeatureIterator getFeatureIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException {
204
        Statement st;
205
        MySqlFeatureIterator geomIterator = null;
206
        try {
207
            st = conn.createStatement();
208
            // st.setFetchSize(2000);
209
            ResultSet rs = st.executeQuery(sql);
210
            geomIterator = new MySqlFeatureIterator(rs);
211
        } catch (SQLException e) {
212
            e.printStackTrace();
213
            throw new com.iver.cit.gvsig.fmap.DriverException(e);
214
        }
215
            
216
        return geomIterator;
217
    }
218
    /* (non-Javadoc)
219
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D)
220
     */
221
    public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException {
222
        if (workingArea != null)
223
        r = r.createIntersection(workingArea);
224
        double xMin = r.getMinX();
225
        double yMin = r.getMinY();
226
        double xMax = r.getMaxX();
227
        double yMax = r.getMaxY();
228
        
229
        String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", "
230
                + xMax + " " + yMin + ", "
231
                + xMax + " " + yMax + ", "
232
                + xMin + " " + yMax + ")', "
233
                + strEPSG + ")";
234
        String sqlAux;
235
        if (getWhereClause().startsWith("WHERE")) 
236
            sqlAux = sqlOrig + " MBRIntersects(" + wktBox + ",ogc_geom);" ;
237
        else
238
            sqlAux = sqlOrig + "WHERE MBRIntersects(" + wktBox + ",ogc_geom);" ;
239
        
240

    
241
        return getFeatureIterator(sqlAux);
242
    }
243
    public void open() {
244
        // TODO Auto-generated method stub
245
        
246
    }
247
        /**
248
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning()
249
         */
250
        public String getConnectionStringBeginning() {
251
                return "jdbc:mysql:";
252
        }
253
            
254
    static{
255
            try {
256
                        Class.forName("com.mysql.jdbc.Driver");
257
                } catch (ClassNotFoundException e) {
258
                        throw new RuntimeException(e);
259
                }
260
    }
261

    
262
        /**
263
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
264
         */
265
        public String getGeometryField(String fieldName) {
266
                return "ASBINARY(" + fieldName +")";
267
        }
268
    /**
269
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
270
     */
271
    public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException {
272
        return new int[]{idFID_FieldName - 2};
273
    }
274
    /**
275
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort()
276
     */
277
    public int getDefaultPort() {
278
        return 3306;
279
    }
280
    /**
281
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setWorkingArea(java.awt.geom.Rectangle2D)
282
     */
283
    public void setWorkingArea(Rectangle2D rect) {
284
        this.workingArea = rect;
285
    }
286
    /**
287
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getWorkingArea()
288
     */
289
    public Rectangle2D getWorkingArea() {
290
        return workingArea;
291
    }
292
        
293
    
294
    
295
}