Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / postgresqlbin / PostgresqlBinFeature.java @ 22373

History | View | Annotate | Download (5.53 KB)

1
package org.gvsig.fmap.data.feature.db.jdbc.postgresqlbin;
2

    
3
import java.math.BigDecimal;
4
import java.nio.ByteBuffer;
5
import java.sql.Date;
6
import java.sql.ResultSet;
7
import java.sql.SQLException;
8
import java.sql.Timestamp;
9
import java.sql.Types;
10

    
11
import org.gvsig.fmap.data.DataException;
12
import org.gvsig.fmap.data.ReadException;
13
import org.gvsig.fmap.data.feature.Feature;
14
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.data.feature.FeatureID;
16
import org.gvsig.fmap.data.feature.FeatureType;
17
import org.gvsig.fmap.data.feature.IsNotFeatureSettingException;
18
import org.gvsig.fmap.data.feature.db.DBAttributeDescriptor;
19
import org.gvsig.fmap.data.feature.db.DBFeatureType;
20
import org.gvsig.fmap.data.feature.db.jdbc.JDBCFeature;
21
import org.gvsig.fmap.data.feature.db.jdbc.JDBCStore;
22

    
23

    
24
public class PostgresqlBinFeature extends JDBCFeature{
25

    
26

    
27

    
28
        /**
29
         *
30
         */
31
        private static final long serialVersionUID = -1215877232314904198L;
32

    
33
        PostgresqlBinFeature(FeatureType featureType, JDBCStore store, ResultSet rs) throws ReadException {
34
                super(featureType, store, rs);
35
        }
36

    
37

    
38
        public FeatureID getID() {
39
                return new PostgresqlBinFeatureID(this.store,featureKey);
40
        }
41
        protected void loading() {
42
                // TODO Auto-generated method stub
43
                super.loading();
44
        }
45

    
46
        protected void stopLoading() {
47
                // TODO Auto-generated method stub
48
                super.stopLoading();
49
        }
50

    
51

    
52
        /* (non-Javadoc)
53
         * @see org.gvsig.fmap.data.feature.db.jdbc.JDBCFeature#loadValueFromResulset(java.sql.ResultSet, org.gvsig.fmap.data.feature.FeatureAttributeDescriptor)
54
         */
55
        protected void loadValueFromResulset(ResultSet rs, FeatureAttributeDescriptor attr) throws ReadException {
56
                String name = attr.getName();
57
                Object value = null;
58
                try {
59
                        value = getFieldValueFromBinaryCursor(rs, (DBAttributeDescriptor)attr);
60
                        if (attr.getDataType().equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)) {
61
                                this.setGeometry(name,value);
62
                        } else {
63
                                this.set(name, value);
64
                        }
65
                } catch (java.sql.SQLException e) {
66
                        throw new ReadException("CreateFeature",e);
67
                } catch (IsNotFeatureSettingException e) {
68
                        throw new ReadException(this.store.getName(),e);
69
                }
70

    
71
        }
72

    
73
        private Object getFieldValueFromBinaryCursor(ResultSet aRs, DBAttributeDescriptor attrDescriptor) throws java.sql.SQLException {
74
                int fieldId = attrDescriptor.ordinal();
75
                int sqlType = attrDescriptor.getSqlType();
76
                byte[] byteBuf = aRs.getBytes(fieldId+1);
77
                if (byteBuf == null)
78
                        return null;
79
                else {
80

    
81
                        ByteBuffer buf = ByteBuffer.wrap(byteBuf);
82

    
83
                        if (attrDescriptor.getDataType().equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){
84
                                if (attrDescriptor.getDataType() == FeatureAttributeDescriptor.TYPE_GEOMETRY){
85
                                        if (byteBuf == null)
86
                                                return null;
87
//                                        return wkbParser.parse(byteBuf);
88
                                        return null;
89
                                }
90

    
91
                        }
92

    
93
                        switch (sqlType) {
94
                        case Types.VARCHAR:
95
                                //FIXME Error
96
                                return aRs.getString(fieldId);
97
//                                return new String(buf.toString());
98
                        case Types.FLOAT:
99
                                return new Float(buf.getFloat());
100
                        case Types.DOUBLE:
101
                                return new Double(buf.getDouble());
102
                        case Types.REAL:
103
                                return new Float(buf.getFloat());
104
                        case Types.INTEGER:
105
                                return new Integer(buf.getInt());
106
                        case Types.BIGINT:
107
                                return new Long(buf.getLong());
108
                        case Types.BIT:
109
                                return new Boolean(byteBuf[0] == 1);
110
                        case Types.BOOLEAN:
111
                                return new Boolean(aRs.getBoolean(fieldId));
112
                        case Types.DATE:
113
                                long daysAfter2000 = buf.getInt() + 1;
114
                                long msecs = daysAfter2000*24*60*60*1000;
115
                                long real_msecs_date1 = (long) (XTypes.NUM_msSecs2000 + msecs);
116
                                Date realDate1 = new Date(real_msecs_date1);
117
                                return realDate1;
118
                        case Types.TIME:
119
                                // TODO:
120
                                // throw new RuntimeException("TIME type not implemented yet");
121
                                return "NOT IMPLEMENTED YET";
122
                        case Types.TIMESTAMP:
123
                                double segsReferredTo2000 = buf.getDouble();
124
                                long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000);
125
                                Timestamp valTimeStamp = new Timestamp(real_msecs);
126
                                return valTimeStamp;
127
                        case Types.NUMERIC:
128
                                // System.out.println(metaData.getColumnName(fieldId) + " "
129
                                // + metaData.getColumnClassName(fieldId));
130
                                short ndigits = buf.getShort();
131
                                short weight = buf.getShort();
132
                                short sign = buf.getShort();
133
                                short dscale = buf.getShort();
134
                                String strAux;
135
                                if (sign == 0)
136
                                        strAux = "+";
137
                                else
138
                                        strAux = "-";
139

    
140
                                for (int iDigit = 0; iDigit < ndigits; iDigit++) {
141
                                        short digit = buf.getShort();
142
                                        strAux = strAux + digit;
143
                                        if (iDigit == weight)
144
                                                strAux = strAux + ".";
145

    
146
                                }
147
                                strAux = strAux + "0";
148
                                BigDecimal dec;
149
                                dec = new BigDecimal(strAux);
150
                                // System.out.println(ndigits + "_" + weight + "_" + dscale
151
                                // + "_" + strAux);
152
                                // System.out.println(strAux + " Big= " + dec);
153
                                return new Double(dec.doubleValue());
154

    
155

    
156
                        default:
157
                                //TODO ???
158
                                throw new RuntimeException("Unsuported Type");
159
                        }
160

    
161
                }
162
        }
163

    
164
        protected Object[] getPkFromResulsetBinary(ResultSet rs, DBFeatureType featureType) throws SQLException {
165
                String[] fieldsId = featureType.getFieldsId();
166
                Object[] result = new Object[fieldsId.length];
167
                for (int i=0;i<fieldsId.length;i++){
168
                        result[i] = getFieldValueFromBinaryCursor(
169
                                        rs,
170
                                        (DBAttributeDescriptor)featureType.get(fieldsId[i])
171
                                );
172

    
173
                }
174
                return result;
175
        }
176

    
177

    
178
        /* (non-Javadoc)
179
         * @see org.gvsig.fmap.data.feature.AbstractFeature#cloneFeature()
180
         */
181
        protected Feature cloneFeature() throws DataException {
182
                // TODO Auto-generated method stub
183
                return null;
184
        }
185

    
186
}