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