svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresqlbin / PostgresqlBinStore.java @ 20058
History | View | Annotate | Download (9.33 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresqlbin; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.math.BigDecimal; |
5 |
import java.nio.ByteBuffer; |
6 |
import java.sql.Connection; |
7 |
import java.sql.Date; |
8 |
import java.sql.ResultSet; |
9 |
import java.sql.SQLException; |
10 |
import java.sql.Statement; |
11 |
import java.sql.Timestamp; |
12 |
import java.sql.Types; |
13 |
import java.util.ArrayList; |
14 |
import java.util.List; |
15 |
import java.util.Locale; |
16 |
|
17 |
import org.gvsig.data.IDataCollection; |
18 |
import org.gvsig.data.IDataExplorer; |
19 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
20 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCAttributeDescriptor; |
21 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCFeaturesWriter; |
22 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore; |
23 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostGIS2Geometry; |
24 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlFeatureID; |
25 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStore; |
26 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStoreParameters; |
27 |
import org.gvsig.data.datastores.vectorial.db.jdbc.postgresql.PostgresqlStoreUtils; |
28 |
import org.gvsig.data.exception.CloseException; |
29 |
import org.gvsig.data.exception.InitializeException; |
30 |
import org.gvsig.data.exception.OpenException; |
31 |
import org.gvsig.data.exception.ReadException; |
32 |
import org.gvsig.data.spatialprovisional.IExtent; |
33 |
import org.gvsig.data.vectorial.IFeature; |
34 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
35 |
import org.gvsig.data.vectorial.IFeatureCollection; |
36 |
import org.gvsig.data.vectorial.IFeatureID; |
37 |
import org.gvsig.data.vectorial.IFeatureType; |
38 |
import org.gvsig.metadata.IMetadata; |
39 |
import org.postgis.PGgeometry; |
40 |
|
41 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
42 |
import com.iver.cit.gvsig.fmap.drivers.XTypes; |
43 |
|
44 |
public class PostgresqlBinStore extends PostgresqlStore{ |
45 |
public static final String CONNECTION_STRING = "postgresql"; |
46 |
public static String DATASTORE_NAME = "PostgresqlStore"; |
47 |
protected static Locale ukLocale = new Locale("en", "UK"); // English, UK version |
48 |
PostgresqlBinStoreParameters getParametersPostgresql(){ |
49 |
return (PostgresqlBinStoreParameters)this.parameters; |
50 |
} |
51 |
|
52 |
public String getName() { |
53 |
return DATASTORE_NAME;
|
54 |
} |
55 |
|
56 |
protected void initConnection() throws InitializeException{ |
57 |
PostgresqlStoreParameters dParams = this.getParametersPostgresql();
|
58 |
|
59 |
String dburl = dParams.getUrl();
|
60 |
String dbuser = dParams.getUser();
|
61 |
String dbpass = dParams.getPassw();
|
62 |
|
63 |
this.connection = PostgresqlBinStoreUtils.getConnection(dburl, dbuser, dbpass);
|
64 |
|
65 |
} |
66 |
|
67 |
|
68 |
public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException { |
69 |
if (useSqlSource ){
|
70 |
if (filter != null || order != null){ |
71 |
throw new ReadException(DATASTORE_NAME, |
72 |
new UnsupportedOperationException("Unsuported filter/order in sqlSource mode")); |
73 |
} |
74 |
} |
75 |
if (type==null){ |
76 |
type=getDefaultFeatureType(); |
77 |
} |
78 |
IFeatureCollection coll=null;
|
79 |
if (featureManager == null){ |
80 |
coll=new PostgresqlBinFeatureCollection(this,type,filter,order); |
81 |
}else{
|
82 |
// if ((order != null && order != "")){
|
83 |
// coll=new H2FeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
|
84 |
// } else{
|
85 |
// if (filter == null || filter == ""){
|
86 |
// coll=new H2FeatureCollectionEditing(featureManager,this,type);
|
87 |
// } else {
|
88 |
// coll=new H2FeatureCollectionEditingFiltered(featureManager,this,type,filter);
|
89 |
// }
|
90 |
// }
|
91 |
//
|
92 |
|
93 |
} |
94 |
this.addObserver(new WeakReference(coll)); |
95 |
|
96 |
return coll;
|
97 |
} |
98 |
|
99 |
|
100 |
public IFeature getFeatureByID(IFeatureType featureType2, Object[] featureKey) throws ReadException{ |
101 |
if (useSqlSource){
|
102 |
throw new ReadException(this.getName(), |
103 |
new UnsupportedOperationException("Unsuported featureByID in sqlSource mode")); |
104 |
} |
105 |
ResultSet rs=null; |
106 |
try{
|
107 |
this.open();
|
108 |
|
109 |
Statement st=this.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
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);
|
117 |
if (rs.isLast()) {
|
118 |
|
119 |
return null; |
120 |
}else{
|
121 |
if(rs.next()){
|
122 |
return this.createFeature(rs, this.featureType); |
123 |
} |
124 |
|
125 |
} |
126 |
|
127 |
} catch (java.sql.SQLException e) {
|
128 |
e.printStackTrace(); |
129 |
throw new ReadException(this.getName(), e); |
130 |
} finally{
|
131 |
if (rs != null) |
132 |
try {
|
133 |
rs.close(); |
134 |
} catch (java.sql.SQLException e) {
|
135 |
// TODO ?????
|
136 |
e.printStackTrace(); |
137 |
} |
138 |
} |
139 |
return null; |
140 |
} |
141 |
|
142 |
Connection getCurrentConnection(){
|
143 |
return this.getConnection(); |
144 |
} |
145 |
|
146 |
|
147 |
public boolean canAlterFeatureType() { |
148 |
return true; |
149 |
} |
150 |
|
151 |
public void open() throws OpenException { |
152 |
// FIXME: Resource Manager
|
153 |
} |
154 |
|
155 |
public void close() throws CloseException { |
156 |
// FIXME: Resource Manager
|
157 |
try {
|
158 |
connection.close(); |
159 |
} catch (java.sql.SQLException e) {
|
160 |
throw new CloseException(this.getName(),e); |
161 |
} |
162 |
} |
163 |
|
164 |
public void dispose() { |
165 |
// FIXME: Resource Manager
|
166 |
|
167 |
} |
168 |
|
169 |
|
170 |
public JDBCFeaturesWriter getFeaturesWriter() {
|
171 |
// IFeaturesWriter writer = new H2FeaturesWriter();
|
172 |
// writer.init(this);
|
173 |
// return writer;
|
174 |
return null; |
175 |
} |
176 |
|
177 |
public IDataExplorer getExplorer() {
|
178 |
// TODO Auto-generated method stub
|
179 |
return null; |
180 |
} |
181 |
|
182 |
public boolean isEditable() { |
183 |
return false; |
184 |
|
185 |
// return super.isEditable();
|
186 |
} |
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 |
} |
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 |
|
324 |
} |