root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / jdbc / hsqldb / HSQLDBDriver.java @ 2183
History | View | Annotate | Download (8.94 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.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 HSQLDBDriver 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 HSQLDBDriver() {
|
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 "HSQLDB 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_SCROLL_INSENSITIVE, 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 the_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 |
HSQLDBFeatureIterator geomIterator = null;
|
206 |
try {
|
207 |
st = conn.createStatement(); |
208 |
// st.setFetchSize(2000);
|
209 |
ResultSet rs = st.executeQuery(sql);
|
210 |
geomIterator = new HSQLDBFeatureIterator(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 |
} |
225 |
double xMin = r.getMinX();
|
226 |
double yMin = r.getMinY();
|
227 |
double xMax = r.getMaxX();
|
228 |
double yMax = r.getMaxY();
|
229 |
|
230 |
String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", " |
231 |
+ xMax + " " + yMin + ", " |
232 |
+ xMax + " " + yMax + ", " |
233 |
+ xMin + " " + yMax + ")', " |
234 |
+ strEPSG + ")";
|
235 |
String sqlAux = sqlOrig;
|
236 |
if (getWhereClause().startsWith("WHERE")) |
237 |
sqlAux += " 1=1" ;
|
238 |
|
239 |
return getFeatureIterator(sqlAux);
|
240 |
} |
241 |
public void open() { |
242 |
// TODO Auto-generated method stub
|
243 |
|
244 |
} |
245 |
/**
|
246 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning()
|
247 |
*/
|
248 |
public String getConnectionStringBeginning() { |
249 |
return "jdbc:mysql:"; |
250 |
} |
251 |
|
252 |
static{
|
253 |
try {
|
254 |
Class.forName("com.mysql.jdbc.Driver"); |
255 |
} catch (ClassNotFoundException e) { |
256 |
throw new RuntimeException(e); |
257 |
} |
258 |
} |
259 |
|
260 |
/**
|
261 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
|
262 |
*/
|
263 |
public String getGeometryField(String fieldName) { |
264 |
return fieldName;
|
265 |
} |
266 |
/**
|
267 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
268 |
*/
|
269 |
public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException { |
270 |
return null; |
271 |
} |
272 |
/**
|
273 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort()
|
274 |
*/
|
275 |
public int getDefaultPort() { |
276 |
return 0; |
277 |
} |
278 |
/**
|
279 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setWorkingArea(java.awt.geom.Rectangle2D)
|
280 |
*/
|
281 |
public void setWorkingArea(Rectangle2D rect) { |
282 |
workingArea = rect; |
283 |
} |
284 |
/**
|
285 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getWorkingArea()
|
286 |
*/
|
287 |
public Rectangle2D getWorkingArea() { |
288 |
return workingArea;
|
289 |
} |
290 |
} |