svn-gvsig-desktop / tags / Root_Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / DefaultDBDriver.java @ 1826
History | View | Annotate | Download (8.69 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.jdbc.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 boolean bCursorActivo = false; |
83 |
protected Statement st; |
84 |
protected int numReg=-1; |
85 |
|
86 |
protected ResultSetMetaData metaData = null; |
87 |
|
88 |
/**
|
89 |
* @return devuelve la Conexi?n a la base de datos, para que
|
90 |
* el usuario pueda hacer la consulta que quiera, si lo desea.
|
91 |
* Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
|
92 |
* avanazado y lanzar peticiones del tipo "Devuelveme un buffer
|
93 |
* a las autopistas", y con el resultset que te venga, escribir
|
94 |
* un shape, o cosas as?.
|
95 |
*/
|
96 |
public Connection getConnection() |
97 |
{ |
98 |
return conn;
|
99 |
} |
100 |
public String getFields() |
101 |
{ |
102 |
return fields;
|
103 |
} |
104 |
public String getWhereClause() |
105 |
{ |
106 |
return whereClause;
|
107 |
} |
108 |
public String getTableName() |
109 |
{ |
110 |
return tableName;
|
111 |
} |
112 |
|
113 |
|
114 |
/**
|
115 |
* @throws DriverIOException
|
116 |
* @throws DriverException
|
117 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
|
118 |
*/
|
119 |
public int getShapeCount() throws IOException { |
120 |
if (numReg == -1) |
121 |
{ |
122 |
try
|
123 |
{ |
124 |
Statement s = conn.createStatement();
|
125 |
ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + tableName + " " + whereClause); |
126 |
r.next(); |
127 |
numReg = r.getInt(1);
|
128 |
System.err.println("numReg = " + numReg); |
129 |
} |
130 |
catch (SQLException e) |
131 |
{ |
132 |
throw new IOException(e.getMessage()); |
133 |
} |
134 |
} |
135 |
|
136 |
return numReg;
|
137 |
} |
138 |
|
139 |
|
140 |
|
141 |
/**
|
142 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
143 |
*/
|
144 |
public int getShapeType() { |
145 |
return FShape.MULTI;
|
146 |
} |
147 |
|
148 |
public Class getFieldType(int idField) throws com.hardcode.gdbms.engine.data.DriverException |
149 |
{ |
150 |
String str = ""; |
151 |
try {
|
152 |
int i = idField + 2; // idField viene basado en 1, y |
153 |
// adem?s nos saltamos el campo de geometry
|
154 |
str = metaData.getColumnClassName(i); |
155 |
if (metaData.getColumnType(i) == Types.VARCHAR) |
156 |
return Value.VARCHAR;
|
157 |
if (metaData.getColumnType(i) == Types.FLOAT) |
158 |
return Value.FLOAT;
|
159 |
if (metaData.getColumnType(i) == Types.DOUBLE) |
160 |
return Value.DOUBLE;
|
161 |
if (metaData.getColumnType(i) == Types.INTEGER) |
162 |
return Value.INTEGER;
|
163 |
if (metaData.getColumnType(i) == Types.BIGINT) |
164 |
return Value.BIGINT;
|
165 |
if (metaData.getColumnType(i) == Types.BIT) |
166 |
return Value.BIT;
|
167 |
if (metaData.getColumnType(i) == Types.DATE) |
168 |
return Value.DATE;
|
169 |
} catch (SQLException e) { |
170 |
throw new com.hardcode.gdbms.engine.data.DriverException("Tipo no soportado: " + str); |
171 |
} |
172 |
return null; |
173 |
|
174 |
} |
175 |
/**
|
176 |
* Obtiene el valor que se encuentra en la fila y columna indicada
|
177 |
* Esta es la implementaci?n por defecto. Si lo del absolute
|
178 |
* no va bien, como es el caso del PostGis, el driver lo
|
179 |
* tiene que reimplementar
|
180 |
*
|
181 |
* @param rowIndex fila
|
182 |
* @param fieldId columna
|
183 |
*
|
184 |
* @return subclase de Value con el valor del origen de datos
|
185 |
*
|
186 |
* @throws DriverException Si se produce un error accediendo al DataSource
|
187 |
*/
|
188 |
public Value getFieldValue(long rowIndex, int idField) |
189 |
throws com.hardcode.gdbms.engine.data.DriverException
|
190 |
{ |
191 |
int i = (int) (rowIndex + 1); |
192 |
int fieldId = idField+2; |
193 |
try {
|
194 |
rs.absolute(i); |
195 |
if (metaData.getColumnType(fieldId) == Types.VARCHAR) |
196 |
return ValueFactory.createValue(rs.getString(fieldId));
|
197 |
if (metaData.getColumnType(fieldId) == Types.FLOAT) |
198 |
return ValueFactory.createValue(rs.getFloat(fieldId));
|
199 |
if (metaData.getColumnType(fieldId) == Types.DOUBLE) |
200 |
return ValueFactory.createValue(rs.getDouble(fieldId));
|
201 |
if (metaData.getColumnType(fieldId) == Types.INTEGER) |
202 |
return ValueFactory.createValue(rs.getInt(fieldId));
|
203 |
if (metaData.getColumnType(fieldId) == Types.BIGINT) |
204 |
return ValueFactory.createValue(rs.getLong(fieldId));
|
205 |
if (metaData.getColumnType(fieldId) == Types.BIT) |
206 |
return ValueFactory.createValue(rs.getBoolean(fieldId));
|
207 |
if (metaData.getColumnType(fieldId) == Types.DATE) |
208 |
return ValueFactory.createValue(rs.getDate(fieldId));
|
209 |
} catch (SQLException e) { |
210 |
throw new com.hardcode.gdbms.engine.data.DriverException("Tipo no soportado: columna " + fieldId ); |
211 |
} |
212 |
return null; |
213 |
|
214 |
|
215 |
} |
216 |
|
217 |
/**
|
218 |
* Obtiene el n?mero de campos del DataSource
|
219 |
*
|
220 |
* @return
|
221 |
*
|
222 |
* @throws DriverException Si se produce alg?n error accediendo al
|
223 |
* DataSource
|
224 |
*/
|
225 |
public int getFieldCount() throws com.hardcode.gdbms.engine.data.DriverException |
226 |
{ |
227 |
try {
|
228 |
// Suponemos que el primer campo es el de las geometries, y no lo
|
229 |
// contamos
|
230 |
return rs.getMetaData().getColumnCount()-1; |
231 |
} catch (SQLException e) { |
232 |
throw new com.hardcode.gdbms.engine.data.DriverException(e); |
233 |
} |
234 |
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Devuelve el nombre del campo fieldId-?simo
|
239 |
*
|
240 |
* @param fieldId ?ndice del campo cuyo nombre se quiere obtener
|
241 |
*
|
242 |
* @return
|
243 |
* @throws com.hardcode.gdbms.engine.data.DriverException
|
244 |
*
|
245 |
* @throws DriverException Si se produce alg?n error accediendo al
|
246 |
* DataSource
|
247 |
*/
|
248 |
public String getFieldName(int fieldId) throws com.hardcode.gdbms.engine.data.DriverException |
249 |
{ |
250 |
try {
|
251 |
return rs.getMetaData().getColumnName(fieldId+2); |
252 |
} catch (SQLException e) { |
253 |
throw new com.hardcode.gdbms.engine.data.DriverException(e); |
254 |
} |
255 |
} |
256 |
|
257 |
/**
|
258 |
* Obtiene el n?mero de registros del DataSource
|
259 |
*
|
260 |
* @return
|
261 |
*
|
262 |
* @throws DriverException Si se produce alg?n error accediendo al
|
263 |
* DataSource
|
264 |
*/
|
265 |
public long getRowCount() |
266 |
{ |
267 |
try {
|
268 |
return getShapeCount();
|
269 |
} catch (IOException e) { |
270 |
// TODO Auto-generated catch block
|
271 |
e.printStackTrace(); |
272 |
} |
273 |
return -1; |
274 |
} |
275 |
|
276 |
public void close() |
277 |
{ |
278 |
try {
|
279 |
if (bCursorActivo)
|
280 |
{ |
281 |
st.execute("CLOSE wkb_cursor");
|
282 |
bCursorActivo = false;
|
283 |
} |
284 |
} catch (SQLException e) { |
285 |
// TODO Auto-generated catch block
|
286 |
e.printStackTrace(); |
287 |
} |
288 |
} |
289 |
|
290 |
} |