Revision 20058 trunk/libraries/libDataSourceDBBaseDrivers/src/org/gvsig/data/datastores/vectorial/db/jdbc/postgresqlbin/PostgresqlBinStoreUtils.java
PostgresqlBinStoreUtils.java | ||
---|---|---|
41 | 41 |
private static int cursorCount=0; |
42 | 42 |
private static String baseCursorName=null; |
43 | 43 |
|
44 |
|
|
45 |
static IFeature createFeatureFromBinaryCursor(JDBCStore store, ResultSet rs, DBFeatureType featureType) throws ReadException { |
|
46 |
|
|
47 |
PostgresqlBinFeature feature=null; |
|
48 |
|
|
49 |
|
|
50 |
Object[] pk; |
|
51 |
try { |
|
52 |
pk = getPkFromResulsetBinary(rs, featureType); |
|
53 |
|
|
54 |
feature=new PostgresqlBinFeature(featureType,store,pk); |
|
55 |
|
|
56 |
Iterator iter = featureType.iterator(); |
|
57 |
IGeometry geom = null; |
|
58 |
while (iter.hasNext()) { |
|
59 |
JDBCAttributeDescriptor fad=(JDBCAttributeDescriptor)iter.next(); |
|
60 |
feature.set(fad.getName(), getFieldValueFromBinaryCursor(rs, fad)); |
|
61 |
} |
|
62 |
|
|
63 |
return feature; |
|
64 |
} catch (java.sql.SQLException e) { |
|
65 |
throw new ReadException("CreateFeature",e); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 | 44 |
static Connection getConnection(String dbUrl, String dbUser, String dbPass) throws InitializeException { |
70 | 45 |
//TODO: Aqu? habria que implementar la llamada |
71 | 46 |
// al Resource Manager para comprobar si ya hay |
... | ... | |
100 | 75 |
|
101 | 76 |
} |
102 | 77 |
|
103 |
static Object getFieldValueFromBinaryCursor(ResultSet aRs, JDBCAttributeDescriptor attrDescriptor) throws java.sql.SQLException { |
|
104 |
int fieldId = attrDescriptor.ordinal(); |
|
105 |
int sqlType = attrDescriptor.getSqlType(); |
|
106 |
byte[] byteBuf = aRs.getBytes(fieldId+1); |
|
107 |
if (byteBuf == null) |
|
108 |
return null; |
|
109 |
else { |
|
110 |
ByteBuffer buf = ByteBuffer.wrap(byteBuf); |
|
111 | 78 |
|
112 |
switch (sqlType) { |
|
113 |
case Types.VARCHAR: |
|
114 |
//FIXME Error |
|
115 |
return aRs.getString(fieldId); |
|
116 |
// return new String(buf.toString()); |
|
117 |
case Types.FLOAT: |
|
118 |
return new Float(buf.getFloat()); |
|
119 |
case Types.DOUBLE: |
|
120 |
return new Double(buf.getDouble()); |
|
121 |
case Types.REAL: |
|
122 |
return new Float(buf.getFloat()); |
|
123 |
case Types.INTEGER: |
|
124 |
return new Integer(buf.getInt()); |
|
125 |
case Types.BIGINT: |
|
126 |
return new Long(buf.getLong()); |
|
127 |
case Types.BIT: |
|
128 |
return new Boolean(byteBuf[0] == 1); |
|
129 |
case Types.BOOLEAN: |
|
130 |
return new Boolean(aRs.getBoolean(fieldId)); |
|
131 |
case Types.DATE: |
|
132 |
long daysAfter2000 = buf.getInt() + 1; |
|
133 |
long msecs = daysAfter2000*24*60*60*1000; |
|
134 |
long real_msecs_date1 = (long) (XTypes.NUM_msSecs2000 + msecs); |
|
135 |
Date realDate1 = new Date(real_msecs_date1); |
|
136 |
return realDate1; |
|
137 |
case Types.TIME: |
|
138 |
// TODO: |
|
139 |
// throw new RuntimeException("TIME type not implemented yet"); |
|
140 |
return "NOT IMPLEMENTED YET"; |
|
141 |
case Types.TIMESTAMP: |
|
142 |
double segsReferredTo2000 = buf.getDouble(); |
|
143 |
long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000); |
|
144 |
Timestamp valTimeStamp = new Timestamp(real_msecs); |
|
145 |
return valTimeStamp; |
|
146 |
case Types.NUMERIC: |
|
147 |
// System.out.println(metaData.getColumnName(fieldId) + " " |
|
148 |
// + metaData.getColumnClassName(fieldId)); |
|
149 |
short ndigits = buf.getShort(); |
|
150 |
short weight = buf.getShort(); |
|
151 |
short sign = buf.getShort(); |
|
152 |
short dscale = buf.getShort(); |
|
153 |
String strAux; |
|
154 |
if (sign == 0) |
|
155 |
strAux = "+"; |
|
156 |
else |
|
157 |
strAux = "-"; |
|
158 |
|
|
159 |
for (int iDigit = 0; iDigit < ndigits; iDigit++) { |
|
160 |
short digit = buf.getShort(); |
|
161 |
strAux = strAux + digit; |
|
162 |
if (iDigit == weight) |
|
163 |
strAux = strAux + "."; |
|
164 |
|
|
165 |
} |
|
166 |
strAux = strAux + "0"; |
|
167 |
BigDecimal dec; |
|
168 |
dec = new BigDecimal(strAux); |
|
169 |
// System.out.println(ndigits + "_" + weight + "_" + dscale |
|
170 |
// + "_" + strAux); |
|
171 |
// System.out.println(strAux + " Big= " + dec); |
|
172 |
return new Double(dec.doubleValue()); |
|
173 |
|
|
174 |
|
|
175 |
default: |
|
176 |
if (attrDescriptor.getDataType() == IFeatureAttributeDescriptor.TYPE_GEOMETRY){ |
|
177 |
if (byteBuf == null) |
|
178 |
return null; |
|
179 |
return wkbParser.parse(byteBuf); |
|
180 |
} |
|
181 |
return null; |
|
182 |
} |
|
183 |
|
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
|
|
188 |
static Object[] getPkFromResulsetBinary(ResultSet rs, DBFeatureType featureType) throws java.sql.SQLException{ |
|
189 |
String[] fieldsId = featureType.getFieldsId(); |
|
190 |
Object[] result = new Object[fieldsId.length]; |
|
191 |
for (int i=0;i<fieldsId.length;i++){ |
|
192 |
result[i] = getFieldValueFromBinaryCursor( |
|
193 |
rs, |
|
194 |
(JDBCAttributeDescriptor)featureType.get( |
|
195 |
featureType.getFieldIndex(fieldsId[i]) |
|
196 |
) |
|
197 |
); |
|
198 |
|
|
199 |
} |
|
200 |
return result; |
|
201 |
|
|
202 |
} |
|
203 |
|
|
204 |
|
|
205 |
static IFeature createFeature(JDBCStore store,ResultSet rs,DBFeatureType featureType) throws ReadException{ |
|
206 |
|
|
207 |
JDBCFeature feature=null; |
|
208 |
|
|
209 |
|
|
210 |
Object[] pk; |
|
211 |
try { |
|
212 |
pk = getPkFromResulset(rs, featureType); |
|
213 |
|
|
214 |
feature=new PostgresqlBinFeature(featureType,store,pk); |
|
215 |
|
|
216 |
Iterator iter = featureType.iterator(); |
|
217 |
IGeometry geom = null; |
|
218 |
while (iter.hasNext()) { |
|
219 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)iter.next(); |
|
220 |
if (fad.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) { |
|
221 |
byte[] data = rs.getBytes(fad.getName()); |
|
222 |
|
|
223 |
if (data == null) { |
|
224 |
geom = null; |
|
225 |
} else{ |
|
226 |
geom = wkbParser.parse(data); |
|
227 |
} |
|
228 |
feature.setGeometry(fad.getName(),geom); |
|
229 |
} else { |
|
230 |
feature.set(fad.getName(), rs.getObject(fad.getName())); |
|
231 |
} |
|
232 |
|
|
233 |
} |
|
234 |
return feature; |
|
235 |
} catch (java.sql.SQLException e) { |
|
236 |
throw new ReadException("CreateFeature",e); |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
240 | 79 |
static String addLimitsToSQL(String aSql,int fetchSize,int page){ |
241 | 80 |
return aSql+ " limit " + fetchSize + " offset " + (fetchSize*page); |
242 | 81 |
} |
... | ... | |
369 | 208 |
|
370 | 209 |
} |
371 | 210 |
|
372 |
|
|
373 |
static String getFliterForID(DBFeatureType featureType, Object[] featureKey) { |
|
374 |
// TODO Auto-generated method stub |
|
375 |
return null; |
|
376 |
} |
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
protected static String getFliterForID(DBFeatureType fType, IFeature feature){ |
|
381 |
return getFliterForID(fType, getPkFromFeature(feature,fType)); |
|
382 |
} |
|
383 |
|
|
384 |
|
|
385 |
protected static Object[] getPkFromResulset(ResultSet rs, DBFeatureType featureType) throws java.sql.SQLException{ |
|
386 |
String[] fieldsId = featureType.getFieldsId(); |
|
387 |
Object[] result = new Object[fieldsId.length]; |
|
388 |
for (int i=0;i<fieldsId.length;i++){ |
|
389 |
result[i] = rs.getObject(fieldsId[i]); |
|
390 |
} |
|
391 |
return result; |
|
392 |
|
|
393 |
} |
|
394 |
|
|
395 |
|
|
396 |
|
|
397 |
protected static Object[] getPkFromFeature(IFeature feature, DBFeatureType featureType){ |
|
398 |
String[] fieldsId = featureType.getFieldsId(); |
|
399 |
Object[] result = new Object[fieldsId.length]; |
|
400 |
for (int i=0;i<fieldsId.length;i++){ |
|
401 |
result[i] = feature.get(fieldsId[i]); |
|
402 |
} |
|
403 |
return result; |
|
404 |
|
|
405 |
} |
|
406 |
|
|
407 | 211 |
static String getJDBCUrl(String host, String db, String port) { |
408 | 212 |
String url; |
409 | 213 |
url = "jdbc:postgresql://"+host+":" + port +"/"+db; |
Also available in: Unified diff