root / branches / v10 / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / hsqldb / HSQLDBDriver.java @ 12351
History | View | Annotate | Download (10.6 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.io.IOException; |
48 |
import java.sql.ResultSet; |
49 |
import java.sql.ResultSetMetaData; |
50 |
import java.sql.SQLException; |
51 |
import java.sql.Statement; |
52 |
|
53 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
54 |
import com.iver.cit.gvsig.fmap.DriverException; |
55 |
import com.iver.cit.gvsig.fmap.SqlDriveExceptionType; |
56 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
57 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC; |
58 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
59 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
60 |
import com.iver.cit.gvsig.fmap.drivers.DefaultJDBCDriver; |
61 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
62 |
import com.iver.cit.gvsig.fmap.drivers.IConnection; |
63 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
64 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
65 |
|
66 |
/**
|
67 |
* @author FJP
|
68 |
*
|
69 |
* TODO To change the template for this generated type comment go to
|
70 |
* Window - Preferences - Java - Code Generation - Code and Comments
|
71 |
*/
|
72 |
public class HSQLDBDriver extends DefaultJDBCDriver { |
73 |
private WKBParser2 parser = new WKBParser2(); |
74 |
/* private int fetch_min=-1;
|
75 |
private int fetch_max=-1; */
|
76 |
private Statement st; |
77 |
private Rectangle2D fullExtent = null; |
78 |
private String sqlOrig; |
79 |
private String sqlTotal; |
80 |
|
81 |
private String strAux; |
82 |
/**
|
83 |
*
|
84 |
*/
|
85 |
public HSQLDBDriver() {
|
86 |
} |
87 |
/* (non-Javadoc)
|
88 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
89 |
*/
|
90 |
public DriverAttributes getDriverAttributes() {
|
91 |
return null; |
92 |
} |
93 |
|
94 |
/* (non-Javadoc)
|
95 |
* @see com.hardcode.driverManager.Driver#getName()
|
96 |
*/
|
97 |
public String getName() { |
98 |
return "HSQLDB Driver"; |
99 |
} |
100 |
|
101 |
/**
|
102 |
* @throws IOException
|
103 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
104 |
*/
|
105 |
public IGeometry getShape(int index) throws IOException { |
106 |
IGeometry geom = null;
|
107 |
boolean resul;
|
108 |
try {
|
109 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
|
110 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
|
111 |
// LO HAGA
|
112 |
// System.out.println("getShape " + index);
|
113 |
/* if (index < fetch_min)
|
114 |
{
|
115 |
rs.close();
|
116 |
|
117 |
rs = st.executeQuery(sqlOrig);
|
118 |
fetch_min = 0;
|
119 |
fetch_max = rs.getFetchSize();
|
120 |
}
|
121 |
while (index >= fetch_max)
|
122 |
{
|
123 |
rs.last();
|
124 |
// forzamos una carga
|
125 |
rs.next();
|
126 |
fetch_min = fetch_max;
|
127 |
fetch_max = fetch_max + rs.getFetchSize();
|
128 |
// System.out.println("fetchSize = " + rs.getFetchSize() + " " + fetch_min + "-" + fetch_max);
|
129 |
}
|
130 |
rs.absolute(index+1 - fetch_min); */
|
131 |
rs.absolute(index+1);
|
132 |
// strAux = rs.getString(1);
|
133 |
// geom = parser.read(strAux);
|
134 |
byte[] data = rs.getBytes(1); |
135 |
geom = parser.parse(data); |
136 |
|
137 |
|
138 |
} catch (SQLException e) { |
139 |
throw new IOException(e.getMessage()); |
140 |
} |
141 |
|
142 |
return geom;
|
143 |
} |
144 |
/**
|
145 |
* @param conn
|
146 |
* @throws DBException
|
147 |
*/
|
148 |
public void setData(IConnection conn, DBLayerDefinition lyrDef) throws DBException |
149 |
{ |
150 |
this.conn = conn;
|
151 |
setLyrDef(lyrDef); |
152 |
try {
|
153 |
sqlOrig = "SELECT " + getTotalFields() + " FROM " + getLyrDef().getTableName() |
154 |
+ " WHERE " + getLyrDef().getWhereClause();
|
155 |
// sqlOrig = getCompoundWhere(workingArea, lyrDef.getSRID_EPSG());
|
156 |
sqlOrig = sqlOrig + " ORDER BY " + getLyrDef().getFieldID();
|
157 |
|
158 |
st = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
159 |
rs = st.executeQuery(sqlOrig); |
160 |
metaData = rs.getMetaData(); |
161 |
// Le pegamos un primera pasada para poder relacionar
|
162 |
// un campo de identificador ?nico (parecido al OID en
|
163 |
// postgresql) con el ?ndice dentro del recordset.
|
164 |
// Esto cuando haya ediciones, no es v?lido, y hay
|
165 |
// que refrescarlo.
|
166 |
doRelateID_FID(); |
167 |
|
168 |
} catch (SQLException e) { |
169 |
throw new DBException(e); |
170 |
} |
171 |
} |
172 |
|
173 |
/**
|
174 |
* @throws IOException
|
175 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
176 |
*/
|
177 |
public Rectangle2D getFullExtent() throws IOException{ |
178 |
if (fullExtent == null) |
179 |
{ |
180 |
try
|
181 |
{ |
182 |
IFeatureIterator itGeom = getFeatureIterator("SELECT the_geom AS the_geom FROM " + getLyrDef().getTableName());
|
183 |
IGeometry geom; |
184 |
int cont = 0; |
185 |
while (itGeom.hasNext())
|
186 |
{ |
187 |
geom = itGeom.next().getGeometry(); |
188 |
if (cont==0) |
189 |
fullExtent = geom.getBounds2D(); |
190 |
else
|
191 |
fullExtent.add(geom.getBounds2D()); |
192 |
cont++; |
193 |
} |
194 |
} |
195 |
catch (DriverException e) {
|
196 |
throw new IOException(e.getMessage()); |
197 |
} |
198 |
|
199 |
} |
200 |
return fullExtent;
|
201 |
} |
202 |
/* (non-Javadoc)
|
203 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
|
204 |
*/
|
205 |
public IFeatureIterator getFeatureIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException { |
206 |
Statement st;
|
207 |
HSQLDBFeatureIterator geomIterator = null;
|
208 |
try {
|
209 |
st = ((ConnectionJDBC)conn).getConnection().createStatement(); |
210 |
// st.setFetchSize(2000);
|
211 |
ResultSet rs = st.executeQuery(sql);
|
212 |
geomIterator = new HSQLDBFeatureIterator(rs);
|
213 |
} catch (SQLException e) { |
214 |
e.printStackTrace(); |
215 |
SqlDriveExceptionType type = new SqlDriveExceptionType();
|
216 |
type.setLayerName(this.getTableName());
|
217 |
type.setDriverName(this.getName());
|
218 |
type.setSchema(this.getLyrDef());
|
219 |
type.setSql(sql); |
220 |
throw new com.iver.cit.gvsig.fmap.DriverException(e, type); |
221 |
} |
222 |
|
223 |
return geomIterator;
|
224 |
} |
225 |
/* (non-Javadoc)
|
226 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D)
|
227 |
*/
|
228 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException { |
229 |
if (workingArea != null){ |
230 |
r = r.createIntersection(workingArea); |
231 |
} |
232 |
double xMin = r.getMinX();
|
233 |
double yMin = r.getMinY();
|
234 |
double xMax = r.getMaxX();
|
235 |
double yMax = r.getMaxY();
|
236 |
|
237 |
String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", " |
238 |
+ xMax + " " + yMin + ", " |
239 |
+ xMax + " " + yMax + ", " |
240 |
+ xMin + " " + yMax + ")', " |
241 |
+ strEPSG + ")";
|
242 |
String sqlAux = sqlOrig;
|
243 |
if (getWhereClause().startsWith("WHERE")) |
244 |
sqlAux += " 1=1" ;
|
245 |
|
246 |
return getFeatureIterator(sqlAux);
|
247 |
} |
248 |
public void open() { |
249 |
// TODO Auto-generated method stub
|
250 |
|
251 |
} |
252 |
/**
|
253 |
* @see com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver#getConnectionStringBeginning()
|
254 |
*/
|
255 |
public String getConnectionStringBeginning() { |
256 |
return "jdbc:mysql:"; |
257 |
} |
258 |
|
259 |
static{
|
260 |
try {
|
261 |
Class.forName("com.mysql.jdbc.Driver"); |
262 |
} catch (ClassNotFoundException e) { |
263 |
throw new RuntimeException(e); |
264 |
} |
265 |
} |
266 |
|
267 |
/**
|
268 |
* @see com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver#getGeometryField(java.lang.String)
|
269 |
*/
|
270 |
public String getGeometryField(String fieldName) { |
271 |
return fieldName;
|
272 |
} |
273 |
/**
|
274 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
275 |
*/
|
276 |
public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException { |
277 |
return null; |
278 |
} |
279 |
/**
|
280 |
* @see com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver#getDefaultPort()
|
281 |
*/
|
282 |
public int getDefaultPort() { |
283 |
return 0; |
284 |
} |
285 |
/**
|
286 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
287 |
*/
|
288 |
public void write(DataWare arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException { |
289 |
// TODO Auto-generated method stub
|
290 |
|
291 |
} |
292 |
|
293 |
public String getSqlTotal() |
294 |
{ |
295 |
return sqlTotal;
|
296 |
} |
297 |
/* (non-Javadoc)
|
298 |
* @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#getCompleteWhere()
|
299 |
*/
|
300 |
public String getCompleteWhere() { |
301 |
// TODO Auto-generated method stub
|
302 |
return null; |
303 |
} |
304 |
/* (non-Javadoc)
|
305 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String, java.lang.String[])
|
306 |
*/
|
307 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, String[] alphaNumericFieldsNeeded) throws DriverException { |
308 |
// TODO Auto-generated method stub
|
309 |
return null; |
310 |
} |
311 |
public boolean isWritable() { |
312 |
// TODO Auto-generated method stub
|
313 |
return true; |
314 |
} |
315 |
public String[] getTableFields(IConnection conex, String table) throws DBException { |
316 |
try{
|
317 |
Statement st = ((ConnectionJDBC)conex).getConnection().createStatement();
|
318 |
// ResultSet rs = dbmd.getTables(catalog, null, dbLayerDefinition.getTable(), null);
|
319 |
ResultSet rs = st.executeQuery("select * from " + table + " LIMIT 1"); |
320 |
ResultSetMetaData rsmd = rs.getMetaData();
|
321 |
|
322 |
String[] ret = new String[rsmd.getColumnCount()]; |
323 |
|
324 |
for (int i = 0; i < ret.length; i++) { |
325 |
ret[i] = rsmd.getColumnName(i+1);
|
326 |
} |
327 |
|
328 |
return ret;
|
329 |
}catch (SQLException e) { |
330 |
throw new DBException(e); |
331 |
} |
332 |
} |
333 |
} |