root / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySqlFeatureIterator.java @ 13881
History | View | Annotate | Download (7.51 KB)
1 |
/*
|
---|---|
2 |
* Created on 11-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.mysql; |
45 |
|
46 |
import java.sql.ResultSet; |
47 |
import java.sql.ResultSetMetaData; |
48 |
import java.sql.SQLException; |
49 |
import java.sql.Types; |
50 |
|
51 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
52 |
import com.hardcode.gdbms.engine.values.Value; |
53 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
54 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
55 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
56 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
57 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
58 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
59 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
60 |
|
61 |
/**
|
62 |
* Iterator over the features of a MySQL driver.
|
63 |
*
|
64 |
*
|
65 |
*
|
66 |
* @author FJP
|
67 |
*/
|
68 |
public class MySqlFeatureIterator implements IFeatureIterator { |
69 |
private WKBParser2 parser = new WKBParser2(); |
70 |
ResultSet rs;
|
71 |
String strAux;
|
72 |
IGeometry geom; |
73 |
int numColumns;
|
74 |
int idFieldID = -1; |
75 |
|
76 |
int[] relIds; |
77 |
private DBLayerDefinition lyrDef;
|
78 |
|
79 |
private ResultSetMetaData metaData = null; |
80 |
Value[] regAtt;
|
81 |
/**
|
82 |
* @throws SQLException
|
83 |
*
|
84 |
*/
|
85 |
public MySqlFeatureIterator(ResultSet rs) { |
86 |
// Debe ser forward only
|
87 |
this.rs = rs;
|
88 |
try {
|
89 |
numColumns = rs.getMetaData().getColumnCount(); |
90 |
regAtt = new Value[numColumns-1]; |
91 |
metaData = rs.getMetaData(); |
92 |
} catch (SQLException e) { |
93 |
// TODO Auto-generated catch block
|
94 |
e.printStackTrace(); |
95 |
} |
96 |
} |
97 |
|
98 |
/* (non-Javadoc)
|
99 |
* @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
|
100 |
*/
|
101 |
public boolean hasNext() throws ReadDriverException { |
102 |
try {
|
103 |
if (rs.next())
|
104 |
return true; |
105 |
closeIterator(); |
106 |
return false; |
107 |
} catch (SQLException e) { |
108 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
109 |
// type.setDriverName("MySQL Driver");
|
110 |
// try {
|
111 |
// type.setSql(rs.getStatement().toString());
|
112 |
// } catch (SQLException e1) {
|
113 |
// e1.printStackTrace();
|
114 |
// }
|
115 |
throw new ReadDriverException("MySQL Driver",e); |
116 |
// throw new DriverException(e);
|
117 |
} |
118 |
} |
119 |
|
120 |
/* (non-Javadoc)
|
121 |
* @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
|
122 |
*/
|
123 |
public IFeature next() throws ReadDriverException { |
124 |
byte[] data; |
125 |
|
126 |
try {
|
127 |
data = rs.getBytes(1);
|
128 |
geom = parser.parse(data); |
129 |
|
130 |
for (int fieldId=2; fieldId <= numColumns; fieldId++ ) |
131 |
{ |
132 |
Value val = null;
|
133 |
if (metaData.getColumnType(fieldId) == Types.VARCHAR) |
134 |
{ |
135 |
String strAux = rs.getString(fieldId);
|
136 |
if (strAux == null) strAux = ""; |
137 |
val = ValueFactory.createValue(strAux); |
138 |
}else if (metaData.getColumnType(fieldId) == Types.FLOAT) |
139 |
val = ValueFactory.createValue(rs.getFloat(fieldId)); |
140 |
else if (metaData.getColumnType(fieldId) == Types.DOUBLE) |
141 |
val = ValueFactory.createValue(rs.getDouble(fieldId)); |
142 |
else if (metaData.getColumnType(fieldId) == Types.INTEGER) |
143 |
val = ValueFactory.createValue(rs.getInt(fieldId)); |
144 |
else if (metaData.getColumnType(fieldId) == Types.BIGINT) |
145 |
val = ValueFactory.createValue(rs.getLong(fieldId)); |
146 |
else if (metaData.getColumnType(fieldId) == Types.BIT) |
147 |
val = ValueFactory.createValue(rs.getBoolean(fieldId)); |
148 |
else if (metaData.getColumnType(fieldId) == Types.DATE) |
149 |
val = ValueFactory.createValue(rs.getDate(fieldId)); |
150 |
|
151 |
regAtt[relIds[fieldId-2]] = val;
|
152 |
} |
153 |
IFeature feat = null;
|
154 |
if (idFieldID != -1) |
155 |
{//TODO Review if we could find problems when the table has only geom and gid
|
156 |
int fieldId = lyrDef.getIdFieldID();
|
157 |
Value idValue = regAtt[fieldId]; |
158 |
String theID = ""; |
159 |
if(idValue != null)//azabala: sometimes we found problems with gid (null pointer exceptions) |
160 |
theID = idValue.toString(); |
161 |
feat = new DefaultFeature(geom, regAtt, theID);
|
162 |
} |
163 |
else
|
164 |
{ |
165 |
// // feat = new DefaultFeature(geom, regAtt);
|
166 |
// FeatureWithoutIdExceptionType type = new FeatureWithoutIdExceptionType();
|
167 |
// type.setSchema(lyrDef);
|
168 |
throw new ReadDriverException("MySQL Driver",null); |
169 |
} |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
return feat;
|
175 |
} catch (SQLException e) { |
176 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
177 |
// type.setDriverName("MySQL Driver");
|
178 |
// try {
|
179 |
// type.setSql(rs.getStatement().toString());
|
180 |
// } catch (SQLException e1) {
|
181 |
// e1.printStackTrace();
|
182 |
// }
|
183 |
throw new ReadDriverException("MySQL Driver",e); |
184 |
// throw new DriverException(e);
|
185 |
} |
186 |
} |
187 |
|
188 |
/* (non-Javadoc)
|
189 |
* @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
|
190 |
*/
|
191 |
public void closeIterator() throws ReadDriverException { |
192 |
try {
|
193 |
rs.close(); |
194 |
} catch (SQLException e) { |
195 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
196 |
// type.setDriverName("MySQL Driver");
|
197 |
// try {
|
198 |
// type.setSql(rs.getStatement().toString());
|
199 |
// } catch (SQLException e1) {
|
200 |
// e1.printStackTrace();
|
201 |
// }
|
202 |
throw new ReadDriverException("MySQL Driver",e); |
203 |
// throw new DriverException(e);
|
204 |
} |
205 |
} |
206 |
|
207 |
public void setLyrDef(DBLayerDefinition lyrDef) |
208 |
{ |
209 |
this.lyrDef =lyrDef;
|
210 |
// Aunque no nos hayan pedido todos los campos, devolveremos
|
211 |
// tantos atributos como la capa tiene. Eso s?, puestos a null
|
212 |
regAtt = new Value[lyrDef.getFieldNames().length];
|
213 |
//no deber?a ser numColums - 2 ??
|
214 |
relIds = new int[numColumns-1]; |
215 |
|
216 |
try {
|
217 |
// for (int i=2; i<= metaData.getColumnCount(); i++)
|
218 |
for (int i=2; i <= numColumns; i++) |
219 |
{ |
220 |
int idRel = lyrDef.getFieldIdByName(metaData.getColumnName(i));
|
221 |
if (idRel == -1) |
222 |
{ |
223 |
throw new RuntimeException("No se ha encontrado el nombre de campo " + metaData.getColumnName(i)); |
224 |
} |
225 |
|
226 |
relIds[i-2] = idRel;
|
227 |
if (lyrDef.getFieldID().equals(metaData.getColumnName(i)))
|
228 |
{ |
229 |
idFieldID = i; |
230 |
break;
|
231 |
} |
232 |
} |
233 |
} catch (SQLException e) { |
234 |
// Si no est?, no pasa nada
|
235 |
} |
236 |
|
237 |
} |
238 |
|
239 |
|
240 |
} |