Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGisFeatureIterator.java @ 2183

History | View | Annotate | Download (4.79 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.ResultSet;
48
import java.sql.ResultSetMetaData;
49
import java.sql.SQLException;
50
import java.sql.Statement;
51
import java.sql.Types;
52

    
53
import com.hardcode.gdbms.engine.values.Value;
54
import com.hardcode.gdbms.engine.values.ValueFactory;
55
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.core.IFeature;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
59
import com.iver.cit.gvsig.fmap.drivers.WKBParser;
60

    
61
/**
62
 * @author FJP
63
 *
64
 * TODO To change the template for this generated type comment go to
65
 * Window - Preferences - Java - Code Generation - Code and Comments
66
 */
67
public class PostGisFeatureIterator implements IFeatureIterator {
68
    private WKBParser parser = new WKBParser();
69
    private ResultSetMetaData metaData = null;
70
    ResultSet rs;
71
    Statement st;
72
    String strAux;
73
    IGeometry geom;
74
    int numColumns;
75
    Value[] regAtt;
76
    
77
    /**
78
     * @throws SQLException
79
     * 
80
     */
81
    public PostGisFeatureIterator(ResultSet rs) {
82
        // Debe ser forward only
83
        this.rs = rs;
84
        try {
85
            numColumns = rs.getMetaData().getColumnCount();
86
            regAtt = new Value[numColumns-1];
87
            metaData = rs.getMetaData();
88
        } catch (SQLException e) {
89
            // TODO Auto-generated catch block
90
            e.printStackTrace();
91
        }
92
        
93
    }
94
    
95
    /* (non-Javadoc)
96
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
97
     */
98
    public boolean hasNext() throws SQLException {
99
        if (rs.next())
100
            return true;
101
        else
102
        {
103
            rs.close();
104
            return false;
105
        }
106
    }
107

    
108
    /* (non-Javadoc)
109
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
110
     */
111
    public IFeature next() throws SQLException {
112
        byte[] data = rs.getBytes(1);                
113
        geom = parser.parse(data);        
114
        for (int fieldId=2; fieldId <= numColumns; fieldId++ )
115
        {       
116
            data = rs.getBytes(fieldId);
117
            if (data == null)
118
                regAtt[fieldId-2] =  ValueFactory.createNullValue();
119
            else
120
            {
121
                ByteBuffer buf = ByteBuffer.wrap(data);
122
                if (metaData.getColumnType(fieldId) == Types.VARCHAR)
123
                    regAtt[fieldId-2] =  ValueFactory.createValue(rs.getString(fieldId));
124
                if (metaData.getColumnType(fieldId) == Types.FLOAT)
125
                    regAtt[fieldId-2] = ValueFactory.createValue(buf.getFloat());
126
                if (metaData.getColumnType(fieldId) == Types.DOUBLE)
127
                    regAtt[fieldId-2] = ValueFactory.createValue(buf.getDouble());
128
                if (metaData.getColumnType(fieldId) == Types.INTEGER)
129
                    regAtt[fieldId-2] = ValueFactory.createValue(buf.getInt());
130
                if (metaData.getColumnType(fieldId) == Types.BIGINT)
131
                    regAtt[fieldId-2] = ValueFactory.createValue(buf.getLong());
132
                if (metaData.getColumnType(fieldId) == Types.BIT)
133
                    // TODO
134
                    regAtt[fieldId-2] = ValueFactory.createValue(rs.getBoolean(fieldId));
135
                if (metaData.getColumnType(fieldId) == Types.DATE)
136
                    // TODO
137
                    regAtt[fieldId-2] = ValueFactory.createValue(rs.getDate(fieldId));
138
            }
139
        }
140
        
141
        // TODO: Aqu? habr?a que usar una Factor?a.
142
        IFeature feat = new DefaultFeature(geom, regAtt);
143
        
144
        return feat;
145

    
146
    }
147

    
148
}