Revision 20058 trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/postgresqlbin/PostgresqlBinStore.java
PostgresqlBinStore.java | ||
---|---|---|
1 | 1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresqlbin; |
2 | 2 |
|
3 | 3 |
import java.lang.ref.WeakReference; |
4 |
import java.math.BigDecimal; |
|
5 |
import java.nio.ByteBuffer; |
|
4 | 6 |
import java.sql.Connection; |
7 |
import java.sql.Date; |
|
5 | 8 |
import java.sql.ResultSet; |
9 |
import java.sql.SQLException; |
|
6 | 10 |
import java.sql.Statement; |
11 |
import java.sql.Timestamp; |
|
12 |
import java.sql.Types; |
|
7 | 13 |
import java.util.ArrayList; |
8 | 14 |
import java.util.List; |
9 | 15 |
import java.util.Locale; |
... | ... | |
11 | 17 |
import org.gvsig.data.IDataCollection; |
12 | 18 |
import org.gvsig.data.IDataExplorer; |
13 | 19 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
20 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCAttributeDescriptor; |
|
14 | 21 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeaturesWriter; |
15 | 22 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore; |
23 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostGIS2Geometry; |
|
16 | 24 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlFeatureID; |
17 | 25 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStore; |
18 | 26 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStoreParameters; |
... | ... | |
23 | 31 |
import org.gvsig.data.exception.ReadException; |
24 | 32 |
import org.gvsig.data.spatialprovisional.IExtent; |
25 | 33 |
import org.gvsig.data.vectorial.IFeature; |
34 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
|
26 | 35 |
import org.gvsig.data.vectorial.IFeatureCollection; |
27 | 36 |
import org.gvsig.data.vectorial.IFeatureID; |
28 | 37 |
import org.gvsig.data.vectorial.IFeatureType; |
29 | 38 |
import org.gvsig.metadata.IMetadata; |
30 |
import org.gvsig.metadata.IMetadataManager; |
|
31 |
import org.gvsig.metadata.MetadataManager; |
|
39 |
import org.postgis.PGgeometry; |
|
32 | 40 |
|
41 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
42 |
import com.iver.cit.gvsig.fmap.drivers.XTypes; |
|
43 |
|
|
33 | 44 |
public class PostgresqlBinStore extends PostgresqlStore{ |
34 | 45 |
public static final String CONNECTION_STRING = "postgresql"; |
35 | 46 |
public static String DATASTORE_NAME = "PostgresqlStore"; |
36 | 47 |
protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
37 |
private DBFeatureType featureType; |
|
38 |
protected IMetadata metadata; |
|
39 |
|
|
40 |
PostgresqlBinStoreParameters getParametersPostgresql(){ |
|
48 |
PostgresqlBinStoreParameters getParametersPostgresql(){ |
|
41 | 49 |
return (PostgresqlBinStoreParameters)this.parameters; |
42 | 50 |
} |
43 | 51 |
|
... | ... | |
97 | 105 |
ResultSet rs=null; |
98 | 106 |
try{ |
99 | 107 |
this.open(); |
108 |
|
|
100 | 109 |
Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
101 |
String sql = this.getSqlSelectPart() + " WHERE "+ PostgresqlBinStoreUtils.getFliterForID(this.featureType, featureKey); |
|
102 |
rs=st.executeQuery(sql); |
|
110 |
String sql = this.getSqlSelectPart() + " WHERE "+ this.getFilterForID(this.featureType, featureKey); |
|
111 |
String cursorName = PostgresqlBinStoreUtils.createCursorName(); |
|
112 |
String mSql="BEGIN"; |
|
113 |
st.execute(mSql); |
|
114 |
mSql="declare " + cursorName + " binary cursor for " + sql; |
|
115 |
st.execute(mSql); |
|
116 |
rs=st.executeQuery("fetch 1 in "+ cursorName); |
|
103 | 117 |
if (rs.isLast()) { |
104 | 118 |
|
105 | 119 |
return null; |
106 | 120 |
}else{ |
107 | 121 |
if(rs.next()){ |
108 |
return PostgresqlBinStoreUtils.createFeature(this, rs, this.featureType);
|
|
122 |
return this.createFeature(rs, this.featureType);
|
|
109 | 123 |
} |
110 | 124 |
|
111 | 125 |
} |
... | ... | |
160 | 174 |
return null; |
161 | 175 |
} |
162 | 176 |
|
163 |
public IMetadata getMetadata() { |
|
164 |
if (metadata==null){ |
|
165 |
IMetadataManager manager=MetadataManager.getManager(); |
|
166 |
metadata=manager.create(DATASTORE_NAME); |
|
167 |
//TODO: Apadir los meteadatos |
|
168 |
} |
|
169 |
return metadata; |
|
170 |
} |
|
171 |
|
|
172 |
|
|
173 | 177 |
public IDataExplorer getExplorer() { |
174 | 178 |
// TODO Auto-generated method stub |
175 | 179 |
return null; |
... | ... | |
181 | 185 |
// return super.isEditable(); |
182 | 186 |
} |
183 | 187 |
|
188 |
protected void loadValueFromResulset(ResultSet rs, IFeature feature, IFeatureAttributeDescriptor attr) throws ReadException { |
|
189 |
String name = attr.getName(); |
|
190 |
Object value = null; |
|
191 |
try { |
|
192 |
value = getFieldValueFromBinaryCursor(rs, (JDBCAttributeDescriptor)attr); |
|
193 |
if (attr.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) { |
|
194 |
feature.setGeometry(name,value); |
|
195 |
} else { |
|
196 |
feature.set(name, value); |
|
197 |
} |
|
198 |
} catch (java.sql.SQLException e) { |
|
199 |
throw new ReadException("CreateFeature",e); |
|
200 |
} |
|
201 |
} |
|
184 | 202 |
|
203 |
public IMetadata getMetadata() { |
|
204 |
if (metadata==null){ |
|
205 |
IMetadata tmp=super.getMetadata(); |
|
206 |
|
|
207 |
return tmp; |
|
208 |
}else{ |
|
209 |
return super.getMetadata(); |
|
210 |
} |
|
211 |
|
|
212 |
} |
|
213 |
|
|
214 |
static Object getFieldValueFromBinaryCursor(ResultSet aRs, JDBCAttributeDescriptor attrDescriptor) throws java.sql.SQLException { |
|
215 |
int fieldId = attrDescriptor.ordinal(); |
|
216 |
int sqlType = attrDescriptor.getSqlType(); |
|
217 |
byte[] byteBuf = aRs.getBytes(fieldId+1); |
|
218 |
if (byteBuf == null) |
|
219 |
return null; |
|
220 |
else { |
|
221 |
|
|
222 |
ByteBuffer buf = ByteBuffer.wrap(byteBuf); |
|
223 |
|
|
224 |
if (attrDescriptor.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){ |
|
225 |
if (attrDescriptor.getDataType() == IFeatureAttributeDescriptor.TYPE_GEOMETRY){ |
|
226 |
if (byteBuf == null) |
|
227 |
return null; |
|
228 |
// return wkbParser.parse(byteBuf); |
|
229 |
return null; |
|
230 |
} |
|
231 |
|
|
232 |
} |
|
233 |
|
|
234 |
switch (sqlType) { |
|
235 |
case Types.VARCHAR: |
|
236 |
//FIXME Error |
|
237 |
return aRs.getString(fieldId); |
|
238 |
// return new String(buf.toString()); |
|
239 |
case Types.FLOAT: |
|
240 |
return new Float(buf.getFloat()); |
|
241 |
case Types.DOUBLE: |
|
242 |
return new Double(buf.getDouble()); |
|
243 |
case Types.REAL: |
|
244 |
return new Float(buf.getFloat()); |
|
245 |
case Types.INTEGER: |
|
246 |
return new Integer(buf.getInt()); |
|
247 |
case Types.BIGINT: |
|
248 |
return new Long(buf.getLong()); |
|
249 |
case Types.BIT: |
|
250 |
return new Boolean(byteBuf[0] == 1); |
|
251 |
case Types.BOOLEAN: |
|
252 |
return new Boolean(aRs.getBoolean(fieldId)); |
|
253 |
case Types.DATE: |
|
254 |
long daysAfter2000 = buf.getInt() + 1; |
|
255 |
long msecs = daysAfter2000*24*60*60*1000; |
|
256 |
long real_msecs_date1 = (long) (XTypes.NUM_msSecs2000 + msecs); |
|
257 |
Date realDate1 = new Date(real_msecs_date1); |
|
258 |
return realDate1; |
|
259 |
case Types.TIME: |
|
260 |
// TODO: |
|
261 |
// throw new RuntimeException("TIME type not implemented yet"); |
|
262 |
return "NOT IMPLEMENTED YET"; |
|
263 |
case Types.TIMESTAMP: |
|
264 |
double segsReferredTo2000 = buf.getDouble(); |
|
265 |
long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000); |
|
266 |
Timestamp valTimeStamp = new Timestamp(real_msecs); |
|
267 |
return valTimeStamp; |
|
268 |
case Types.NUMERIC: |
|
269 |
// System.out.println(metaData.getColumnName(fieldId) + " " |
|
270 |
// + metaData.getColumnClassName(fieldId)); |
|
271 |
short ndigits = buf.getShort(); |
|
272 |
short weight = buf.getShort(); |
|
273 |
short sign = buf.getShort(); |
|
274 |
short dscale = buf.getShort(); |
|
275 |
String strAux; |
|
276 |
if (sign == 0) |
|
277 |
strAux = "+"; |
|
278 |
else |
|
279 |
strAux = "-"; |
|
280 |
|
|
281 |
for (int iDigit = 0; iDigit < ndigits; iDigit++) { |
|
282 |
short digit = buf.getShort(); |
|
283 |
strAux = strAux + digit; |
|
284 |
if (iDigit == weight) |
|
285 |
strAux = strAux + "."; |
|
286 |
|
|
287 |
} |
|
288 |
strAux = strAux + "0"; |
|
289 |
BigDecimal dec; |
|
290 |
dec = new BigDecimal(strAux); |
|
291 |
// System.out.println(ndigits + "_" + weight + "_" + dscale |
|
292 |
// + "_" + strAux); |
|
293 |
// System.out.println(strAux + " Big= " + dec); |
|
294 |
return new Double(dec.doubleValue()); |
|
295 |
|
|
296 |
|
|
297 |
default: |
|
298 |
//TODO ??? |
|
299 |
throw new RuntimeException("Unsuported Type"); |
|
300 |
} |
|
301 |
|
|
302 |
} |
|
303 |
} |
|
304 |
|
|
305 |
protected Object[] getPkFromResulsetBinary(ResultSet rs, DBFeatureType featureType) throws SQLException { |
|
306 |
String[] fieldsId = featureType.getFieldsId(); |
|
307 |
Object[] result = new Object[fieldsId.length]; |
|
308 |
for (int i=0;i<fieldsId.length;i++){ |
|
309 |
result[i] = getFieldValueFromBinaryCursor( |
|
310 |
rs, |
|
311 |
(JDBCAttributeDescriptor)featureType.get( |
|
312 |
featureType.getFieldIndex(fieldsId[i]) |
|
313 |
) |
|
314 |
); |
|
315 |
|
|
316 |
} |
|
317 |
return result; |
|
318 |
} |
|
319 |
|
|
320 |
IFeature createFeatureFromResulset(ResultSet rs, DBFeatureType featureType2) throws ReadException { |
|
321 |
return this.createFeature(rs, featureType2); |
|
322 |
} |
|
323 |
|
|
185 | 324 |
} |
Also available in: Unified diff