Statistics
| Revision:

root / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / hsqldb / HSQLDBDriver.java @ 5007

History | View | Annotate | Download (9.47 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.hsqldb;
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.hardcode.gdbms.engine.data.edition.DataWare;
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
56
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
57
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
58
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
59
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser2;
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 HSQLDBDriver extends DefaultDBDriver {
68
    private WKBParser2 parser = new WKBParser2();
69
    /* private int fetch_min=-1;
70
    private int fetch_max=-1; */
71
    private Statement st;
72
    private Rectangle2D fullExtent = null;
73
    private String sqlOrig;
74
    private String sqlTotal;
75

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

    
89
    /* (non-Javadoc)
90
     * @see com.hardcode.driverManager.Driver#getName()
91
     */
92
    public String getName() {
93
        return "HSQLDB Driver";
94
    }
95
    
96
        /**
97
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
98
         */
99
        public IGeometry getShape(int index) {
100
            IGeometry geom = null;
101
            boolean resul;
102
                try {
103
                    // EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
104
                    // FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
105
                    // LO HAGA
106
                    // System.out.println("getShape " + index);
107
                    /* if (index < fetch_min)
108
                    {
109
                        rs.close();
110
                            
111
                            rs = st.executeQuery(sqlOrig);
112
                        fetch_min = 0;
113
                        fetch_max = rs.getFetchSize();
114
                    }
115
                    while (index >= fetch_max)
116
                    {
117
                        rs.last();
118
                        // forzamos una carga
119
                        rs.next();
120
                        fetch_min = fetch_max;
121
                        fetch_max = fetch_max + rs.getFetchSize();
122
                        // System.out.println("fetchSize = " + rs.getFetchSize() + " " + fetch_min + "-" + fetch_max);
123
                    } 
124
                    rs.absolute(index+1 - fetch_min); */
125
                    rs.absolute(index+1);
126
                    // strAux = rs.getString(1);                
127
                    // geom = parser.read(strAux);
128
                    byte[] data = rs.getBytes(1);                
129
                    geom = parser.parse(data);
130
                    
131
                    
132
            } catch (SQLException e) {
133
                e.printStackTrace();
134
            }
135
                
136
            return geom;
137
        }
138
        /**
139
         * @param conn
140
         */
141
        public void setData(Connection conn, DBLayerDefinition lyrDef)
142
        {
143
            this.conn = conn;
144
        setLyrDef(lyrDef);
145
            try {
146
            sqlOrig = "SELECT " + getTotalFields() + " FROM " + getLyrDef().getTableName()
147
            + " WHERE " + getLyrDef().getWhereClause(); 
148
            // sqlOrig = getCompoundWhere(workingArea, lyrDef.getSRID_EPSG());
149
            sqlOrig = sqlOrig + " ORDER BY " + getLyrDef().getFieldID();
150

    
151
                st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
152
                rs = st.executeQuery(sqlOrig);
153
            metaData = rs.getMetaData();
154
            // Le pegamos un primera pasada para poder relacionar
155
            // un campo de identificador ?nico (parecido al OID en
156
            // postgresql) con el ?ndice dentro del recordset.
157
            // Esto cuando haya ediciones, no es v?lido, y hay
158
            // que refrescarlo.
159
            doRelateID_FID();
160
            
161
        } catch (SQLException e) {
162
            // TODO Auto-generated catch block
163
            e.printStackTrace();
164
        }
165
        }
166
        
167
        /**
168
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
169
         */
170
        public Rectangle2D getFullExtent(){
171
            if (fullExtent == null)
172
            {
173
                try
174
            {
175
                    IFeatureIterator itGeom = getFeatureIterator("SELECT the_geom AS the_geom FROM " + getLyrDef().getTableName());
176
                    IGeometry geom;
177
                    int cont = 0;
178
                    while (itGeom.hasNext())
179
                    {
180
                        geom = itGeom.next().getGeometry();
181
                        if (cont==0)
182
                            fullExtent = geom.getBounds2D();
183
                        else
184
                            fullExtent.add(geom.getBounds2D());
185
                        cont++;
186
                    }
187
            }
188
                catch (DriverException e) {
189
                // TODO Auto-generated catch block
190
                e.printStackTrace();
191
            }
192
                
193
            }
194
            return fullExtent;
195
        }
196
    /* (non-Javadoc)
197
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
198
     */
199
    public IFeatureIterator getFeatureIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException {
200
        Statement st;
201
        HSQLDBFeatureIterator geomIterator = null;
202
        try {
203
            st = conn.createStatement();
204
            // st.setFetchSize(2000);
205
            ResultSet rs = st.executeQuery(sql);
206
            geomIterator = new HSQLDBFeatureIterator(rs);
207
        } catch (SQLException e) {
208
            e.printStackTrace();
209
            throw new com.iver.cit.gvsig.fmap.DriverException(e);
210
        }
211
            
212
        return geomIterator;
213
    }
214
    /* (non-Javadoc)
215
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D)
216
     */
217
    public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException {
218
        if (workingArea != null){
219
        r = r.createIntersection(workingArea);
220
        }
221
        double xMin = r.getMinX();
222
        double yMin = r.getMinY();
223
        double xMax = r.getMaxX();
224
        double yMax = r.getMaxY();
225
        
226
        String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", "
227
                + xMax + " " + yMin + ", "
228
                + xMax + " " + yMax + ", "
229
                + xMin + " " + yMax + ")', "
230
                + strEPSG + ")";
231
        String sqlAux = sqlOrig;
232
        if (getWhereClause().startsWith("WHERE")) 
233
            sqlAux += " 1=1" ;
234

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

    
256
        /**
257
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
258
         */
259
        public String getGeometryField(String fieldName) {
260
                return fieldName;
261
        }
262
    /**
263
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
264
     */
265
    public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException {
266
        return null;
267
    }
268
    /**
269
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort()
270
     */
271
    public int getDefaultPort() {
272
        return 0;
273
    }
274
    /**
275
     * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
276
     */
277
    public void write(DataWare arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException {
278
        // TODO Auto-generated method stub
279
        
280
    }
281
    
282
    public String getSqlTotal()
283
    {
284
        return sqlTotal;
285
    }
286
    /* (non-Javadoc)
287
     * @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#getCompleteWhere()
288
     */
289
    public String getCompleteWhere() {
290
        // TODO Auto-generated method stub
291
        return null;
292
    }
293
    /* (non-Javadoc)
294
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String, java.lang.String[])
295
     */
296
    public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, String[] alphaNumericFieldsNeeded) throws DriverException {
297
        // TODO Auto-generated method stub
298
        return null;
299
    }
300

    
301
}