svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGisFeatureIterator.java @ 10626
History | View | Annotate | Download (6.85 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.postgis; |
45 |
|
46 |
import java.sql.Connection; |
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.values.Value; |
54 |
import com.iver.cit.gvsig.fmap.SqlDriveExceptionType; |
55 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
56 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
57 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
58 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
59 |
import com.iver.cit.gvsig.fmap.drivers.FeatureWithoutIdExceptionType; |
60 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
61 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
62 |
import com.iver.cit.gvsig.fmap.drivers.XTypes; |
63 |
|
64 |
/**
|
65 |
* @author FJP
|
66 |
*
|
67 |
* TODO To change the template for this generated type comment go to Window -
|
68 |
* Preferences - Java - Code Generation - Code and Comments
|
69 |
*/
|
70 |
public class PostGisFeatureIterator implements IFeatureIterator { |
71 |
private static int FETCH_SIZE = 5000; |
72 |
|
73 |
private WKBParser2 parser = new WKBParser2(); |
74 |
|
75 |
private ResultSetMetaData metaData = null; |
76 |
|
77 |
ResultSet rs;
|
78 |
|
79 |
Statement st;
|
80 |
|
81 |
String strAux;
|
82 |
|
83 |
IGeometry geom; |
84 |
|
85 |
int numColumns;
|
86 |
|
87 |
Value[] regAtt;
|
88 |
|
89 |
/**
|
90 |
* Array con la correspondencia entre un campo de la consulta y el campo
|
91 |
* dentro de regAtt
|
92 |
*/
|
93 |
int[] relIds; |
94 |
|
95 |
private DBLayerDefinition lyrDef;
|
96 |
|
97 |
int numReg = 0; |
98 |
|
99 |
int idFieldID = -1; |
100 |
|
101 |
String cursorName;
|
102 |
|
103 |
/**
|
104 |
* @throws SQLException
|
105 |
* @throws SQLException
|
106 |
*
|
107 |
*/
|
108 |
public PostGisFeatureIterator(Connection conn, String cursorName, String sql) |
109 |
throws SQLException { |
110 |
|
111 |
// Basado en 0, es decir, no cuenta el campo
|
112 |
// de geometria
|
113 |
// Debe ser forward only
|
114 |
st = conn.createStatement(); |
115 |
st.execute("BEGIN");
|
116 |
st.execute("declare " + cursorName + " binary cursor for " + sql); |
117 |
|
118 |
this.rs = st.executeQuery("fetch forward " + FETCH_SIZE + " in " |
119 |
+ cursorName); |
120 |
|
121 |
this.cursorName = cursorName;
|
122 |
numColumns = rs.getMetaData().getColumnCount(); |
123 |
metaData = rs.getMetaData(); |
124 |
numReg = 0;
|
125 |
|
126 |
} |
127 |
|
128 |
/*
|
129 |
* (non-Javadoc)
|
130 |
*
|
131 |
* @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
|
132 |
*/
|
133 |
public boolean hasNext() throws ReadDriverException { |
134 |
try {
|
135 |
if (numReg > 0) |
136 |
if ((numReg % FETCH_SIZE) == 0) { |
137 |
rs = st.executeQuery("fetch forward " + FETCH_SIZE + " in " |
138 |
+ cursorName); |
139 |
// System.out.println("ejecutando la query otra vez");
|
140 |
} |
141 |
// System.out.println("hasNext con numReg=" + numReg);
|
142 |
if (rs.next())
|
143 |
return true; |
144 |
else {
|
145 |
closeIterator(); |
146 |
return false; |
147 |
} |
148 |
} catch (SQLException e) { |
149 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
150 |
// type.setDriverName("PostGIS Driver");
|
151 |
// try {
|
152 |
// type.setSql(rs.getStatement().toString());
|
153 |
// } catch (SQLException e1) {
|
154 |
// e1.printStackTrace();
|
155 |
// }
|
156 |
throw new ReadDriverException("PostGIS Driver",e); |
157 |
// throw new DriverException(e);
|
158 |
} |
159 |
|
160 |
} |
161 |
|
162 |
/*
|
163 |
* (non-Javadoc)
|
164 |
*
|
165 |
* @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
|
166 |
*/
|
167 |
public IFeature next() throws ReadDriverException { |
168 |
byte[] data; |
169 |
try {
|
170 |
data = rs.getBytes(1);
|
171 |
geom = parser.parse(data); |
172 |
for (int fieldId = 2; fieldId <= numColumns; fieldId++) { |
173 |
Value val = XTypes.getValue(rs, fieldId); |
174 |
regAtt[relIds[fieldId - 2]] = val;
|
175 |
} |
176 |
|
177 |
// TODO: Aqu? habr?a que usar una Factor?a.
|
178 |
IFeature feat = null;
|
179 |
if (idFieldID != -1) { |
180 |
String theID = regAtt[lyrDef.getIdFieldID()].toString();
|
181 |
feat = new DefaultFeature(geom, regAtt, theID);
|
182 |
} |
183 |
else
|
184 |
{ |
185 |
// // feat = new DefaultFeature(geom, regAtt);
|
186 |
// FeatureWithoutIdExceptionType type = new FeatureWithoutIdExceptionType();
|
187 |
// type.setSchema(lyrDef);
|
188 |
throw new ReadDriverException("PostGIS Driver",null); |
189 |
} |
190 |
numReg++; |
191 |
return feat;
|
192 |
} catch (SQLException e) { |
193 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
194 |
// type.setDriverName("PostGIS Driver");
|
195 |
// try {
|
196 |
// type.setSql(rs.getStatement().toString());
|
197 |
// } catch (SQLException e1) {
|
198 |
// e1.printStackTrace();
|
199 |
// }
|
200 |
throw new ReadDriverException("PostGIS Driver",e); |
201 |
// throw new DriverException(e);
|
202 |
} |
203 |
|
204 |
} |
205 |
|
206 |
|
207 |
/*
|
208 |
* (non-Javadoc)
|
209 |
*
|
210 |
* @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
|
211 |
*/
|
212 |
public void closeIterator() throws ReadDriverException { |
213 |
try {
|
214 |
numReg = 0;
|
215 |
|
216 |
// st.execute("CLOSE " + cursorName);
|
217 |
// st.execute("COMMIT");
|
218 |
rs.close(); |
219 |
} catch (SQLException e) { |
220 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
221 |
// type.setDriverName("PostGIS Driver");
|
222 |
// try {
|
223 |
// type.setSql(rs.getStatement().toString());
|
224 |
// } catch (SQLException e1) {
|
225 |
// e1.printStackTrace();
|
226 |
// }
|
227 |
throw new ReadDriverException("PostGIS Driver",e); |
228 |
// throw new DriverException(e);
|
229 |
} |
230 |
// st.execute("COMMIT");
|
231 |
// st.close();
|
232 |
|
233 |
} |
234 |
|
235 |
public void setLyrDef(DBLayerDefinition lyrDef) { |
236 |
this.lyrDef = lyrDef;
|
237 |
// Aunque no nos hayan pedido todos los campos, devolveremos
|
238 |
// tantos atributos como la capa tiene. Eso s?, puestos a null
|
239 |
regAtt = new Value[lyrDef.getFieldNames().length];
|
240 |
relIds = new int[numColumns - 1]; |
241 |
|
242 |
try {
|
243 |
for (int i = 2; i <= metaData.getColumnCount(); i++) { |
244 |
int idRel = lyrDef.getFieldIdByName(metaData.getColumnName(i));
|
245 |
if (idRel == -1) |
246 |
{ |
247 |
throw new RuntimeException("No se ha encontrado el nombre de campo " + metaData.getColumnName(i)); |
248 |
} |
249 |
relIds[i - 2] = idRel;
|
250 |
if (lyrDef.getFieldID().equals(metaData.getColumnName(i))) {
|
251 |
idFieldID = i; |
252 |
// break;
|
253 |
} |
254 |
} |
255 |
} catch (SQLException e) { |
256 |
// Si no est?, no pasa nada
|
257 |
e.printStackTrace(); |
258 |
} |
259 |
|
260 |
} |
261 |
|
262 |
} |