Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / DefaultDBDriver.java @ 1691

History | View | Annotate | Download (8.3 KB)

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

    
43
import java.awt.geom.Rectangle2D;
44
import java.io.IOException;
45
import java.sql.Connection;
46
import java.sql.ResultSet;
47
import java.sql.ResultSetMetaData;
48
import java.sql.SQLException;
49
import java.sql.Statement;
50
import java.sql.Types;
51
import java.util.Date;
52

    
53
import org.postgis.Geometry;
54
import org.postgis.PGbox3d;
55
import org.postgis.Point;
56

    
57
import com.hardcode.gdbms.engine.data.DataSource;
58
import com.hardcode.gdbms.engine.data.ReadDriver;
59
import com.hardcode.gdbms.engine.values.Value;
60
import com.hardcode.gdbms.engine.values.ValueFactory;
61
import com.iver.cit.gvsig.fmap.DriverException;
62
import com.iver.cit.gvsig.fmap.core.FShape;
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.drivers.postgis.WKTParser;
65
import com.vividsolutions.jts.io.ParseException;
66

    
67

    
68

    
69
/**
70
 * Clase abstracta qu
71
 */
72
public abstract class DefaultDBDriver implements VectorialDatabaseDriver, ReadDriver {    
73
    /**
74
     * 
75
     */
76
    protected Connection conn;
77
    protected String tableName;
78
    protected String whereClause;
79
    protected String fields;
80
    protected String sqlOrig;
81
    protected ResultSet rs;
82
    protected int numReg=-1;
83
   
84
    protected ResultSetMetaData metaData = null;
85

    
86
        /**
87
         * @return devuelve la Conexi?n a la base de datos, para que 
88
         * el usuario pueda hacer la consulta que quiera, si lo desea.
89
         * Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
90
         * avanazado y lanzar peticiones del tipo "Devuelveme un buffer
91
         * a las autopistas", y con el resultset que te venga, escribir
92
         * un shape, o cosas as?.
93
         */
94
        public Connection getConnection()
95
        {
96
            return conn;
97
        }
98
        public String getFields()
99
        {
100
            return fields;            
101
        }
102
        public String getWhereClause()
103
        {
104
            return whereClause;
105
        }
106
        public String getTableName()
107
        {
108
            return tableName;
109
        }
110
        
111

    
112
        /**
113
         * @throws DriverIOException
114
         * @throws DriverException
115
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
116
         */
117
        public int getShapeCount() throws IOException {
118
                    if (numReg == -1)
119
                    {
120
                        try
121
                    {
122
                            Statement s = conn.createStatement();                    
123
                            ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + tableName + " " + whereClause);
124
                            r.next();
125
                            numReg = r.getInt(1);
126
                            System.err.println("numReg = " + numReg);
127
                    }
128
                        catch (SQLException e)
129
                        {
130
                            throw new IOException(e.getMessage());
131
                        }
132
                    }
133
                    
134
            return numReg;
135
        }
136

    
137

    
138

    
139
        /**
140
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
141
         */
142
        public int getShapeType() {
143
                return FShape.MULTI;
144
        }
145
        
146
        public Class getFieldType(int idField) throws com.hardcode.gdbms.engine.data.DriverException
147
        {
148
            String str = "";
149
            try {
150
                int i = idField + 2; // idField viene basado en 1, y
151
                                        // adem?s nos saltamos el campo de geometry
152
                str = metaData.getColumnClassName(i);
153
            if (metaData.getColumnType(i) == Types.VARCHAR)
154
                return Value.VARCHAR;
155
                    if (metaData.getColumnType(i) == Types.FLOAT)
156
                        return Value.FLOAT;
157
                    if (metaData.getColumnType(i) == Types.DOUBLE)
158
                        return Value.DOUBLE;
159
                    if (metaData.getColumnType(i) == Types.INTEGER)
160
                        return Value.INTEGER;
161
                    if (metaData.getColumnType(i) == Types.BIGINT)
162
                        return Value.BIGINT;
163
                    if (metaData.getColumnType(i) == Types.BIT)
164
                        return Value.BIT;
165
                    if (metaData.getColumnType(i) == Types.DATE)
166
                        return Value.DATE;
167
            } catch (SQLException e) {
168
            throw new com.hardcode.gdbms.engine.data.DriverException("Tipo no soportado: " + str);
169
            }
170
            return null;
171

    
172
        }
173
    /**
174
     * Obtiene el valor que se encuentra en la fila y columna indicada
175
     * Esta es la implementaci?n por defecto. Si lo del absolute
176
     * no va bien, como es el caso del PostGis, el driver lo
177
     * tiene que reimplementar
178
     *
179
     * @param rowIndex fila
180
     * @param fieldId columna
181
     *
182
     * @return subclase de Value con el valor del origen de datos
183
     *
184
     * @throws DriverException Si se produce un error accediendo al DataSource
185
     */
186
    public Value getFieldValue(long rowIndex, int idField)
187
        throws com.hardcode.gdbms.engine.data.DriverException
188
        {
189
                int i = (int) (rowIndex + 1);
190
                int fieldId = idField+2;
191
                try {
192
                    rs.absolute(i);
193
                if (metaData.getColumnType(fieldId) == Types.VARCHAR)
194
                    return ValueFactory.createValue(rs.getString(fieldId));
195
                        if (metaData.getColumnType(fieldId) == Types.FLOAT)
196
                            return ValueFactory.createValue(rs.getFloat(fieldId));
197
                        if (metaData.getColumnType(fieldId) == Types.DOUBLE)
198
                            return ValueFactory.createValue(rs.getDouble(fieldId));
199
                        if (metaData.getColumnType(fieldId) == Types.INTEGER)
200
                            return ValueFactory.createValue(rs.getInt(fieldId));
201
                        if (metaData.getColumnType(fieldId) == Types.BIGINT)
202
                            return ValueFactory.createValue(rs.getLong(fieldId));
203
                        if (metaData.getColumnType(fieldId) == Types.BIT)
204
                            return ValueFactory.createValue(rs.getBoolean(fieldId));
205
                        if (metaData.getColumnType(fieldId) == Types.DATE)
206
                            return ValueFactory.createValue(rs.getDate(fieldId));
207
                } catch (SQLException e) {
208
                throw new com.hardcode.gdbms.engine.data.DriverException("Tipo no soportado: columna " + fieldId );
209
                }
210
                return null;
211
                
212
                
213
        }
214

    
215
    /**
216
     * Obtiene el n?mero de campos del DataSource
217
     *
218
     * @return
219
     *
220
     * @throws DriverException Si se produce alg?n error accediendo al
221
     *         DataSource
222
     */
223
    public int getFieldCount() throws com.hardcode.gdbms.engine.data.DriverException
224
    {
225
        try {
226
            // Suponemos que el primer campo es el de las geometries, y no lo
227
            // contamos
228
            return rs.getMetaData().getColumnCount()-1;
229
        } catch (SQLException e) {
230
            throw new com.hardcode.gdbms.engine.data.DriverException(e);
231
        }
232
        
233
    }
234

    
235
    /**
236
     * Devuelve el nombre del campo fieldId-?simo
237
     *
238
     * @param fieldId ?ndice del campo cuyo nombre se quiere obtener
239
     *
240
     * @return
241
     * @throws com.hardcode.gdbms.engine.data.DriverException
242
     *
243
     * @throws DriverException Si se produce alg?n error accediendo al
244
     *         DataSource
245
     */
246
    public String getFieldName(int fieldId) throws com.hardcode.gdbms.engine.data.DriverException
247
    {
248
        try {
249
            return rs.getMetaData().getColumnName(fieldId+2);
250
        } catch (SQLException e) {
251
            throw new com.hardcode.gdbms.engine.data.DriverException(e);
252
        }
253
    }
254

    
255
    /**
256
     * Obtiene el n?mero de registros del DataSource
257
     *
258
     * @return
259
     *
260
     * @throws DriverException Si se produce alg?n error accediendo al
261
     *         DataSource
262
     */
263
    public long getRowCount()
264
    {
265
        try {
266
            return getShapeCount();
267
        } catch (IOException e) {
268
            // TODO Auto-generated catch block
269
            e.printStackTrace();
270
        }
271
        return -1;
272
    }
273

    
274

    
275
}