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