Statistics
| Revision:

root / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / hsqldb / HSQLDBFeatureIterator.java @ 5007

History | View | Annotate | Download (4.61 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.hsqldb;
45

    
46
import java.sql.ResultSet;
47
import java.sql.ResultSetMetaData;
48
import java.sql.SQLException;
49
import java.sql.Types;
50

    
51
import com.hardcode.gdbms.engine.values.Value;
52
import com.hardcode.gdbms.engine.values.ValueFactory;
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
55
import com.iver.cit.gvsig.fmap.core.IFeature;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
58
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser2;
59

    
60
/**
61
 * @author FJP
62
 * 
63
 * TODO To change the template for this generated type comment go to Window -
64
 * Preferences - Java - Code Generation - Code and Comments
65
 */
66
public class HSQLDBFeatureIterator implements IFeatureIterator {
67
        private WKBParser2 parser = new WKBParser2();
68

    
69
        ResultSet rs;
70

    
71
        String strAux;
72

    
73
        IGeometry geom;
74

    
75
        int numColumns;
76

    
77
        private ResultSetMetaData metaData = null;
78

    
79
        Value[] regAtt;
80

    
81
        /**
82
         * @throws SQLException
83
         * 
84
         */
85
        public HSQLDBFeatureIterator(ResultSet rs) {
86
                // Debe ser forward only
87
                this.rs = rs;
88
                try {
89
                        numColumns = rs.getMetaData().getColumnCount();
90
                        regAtt = new Value[numColumns - 1];
91
                        metaData = rs.getMetaData();
92
                } catch (SQLException e) {
93
                        // TODO Auto-generated catch block
94
                        e.printStackTrace();
95
                }
96
        }
97

    
98
        /*
99
         * (non-Javadoc)
100
         * 
101
         * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
102
         */
103
        public boolean hasNext() throws DriverException {
104
                try {
105
                        if (rs.isLast()) {
106
                                rs.close();
107
                                return false;
108
                        } else
109
                                return true;
110
                } catch (SQLException e) {
111
                        throw new DriverException(e);
112
                }
113
        }
114

    
115
        /*
116
         * (non-Javadoc)
117
         * 
118
         * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
119
         */
120
        public IFeature next() throws DriverException {
121
                try {
122
                        rs.next();
123
                        byte[] data = rs.getBytes(1);
124
                        geom = parser.parse(data);
125
                        for (int fieldId = 2; fieldId <= numColumns; fieldId++) {
126
                                if (metaData.getColumnType(fieldId) == Types.VARCHAR) {
127
                                        String strAux = rs.getString(fieldId);
128
                                        if (strAux == null)
129
                                                strAux = "";
130
                                        regAtt[fieldId - 2] = ValueFactory.createValue(strAux);
131
                                }
132
                                if (metaData.getColumnType(fieldId) == Types.FLOAT)
133
                                        regAtt[fieldId - 2] = ValueFactory.createValue(rs
134
                                                        .getFloat(fieldId));
135
                                if (metaData.getColumnType(fieldId) == Types.DOUBLE)
136
                                        regAtt[fieldId - 2] = ValueFactory.createValue(rs
137
                                                        .getDouble(fieldId));
138
                                if (metaData.getColumnType(fieldId) == Types.INTEGER)
139
                                        regAtt[fieldId - 2] = ValueFactory.createValue(rs
140
                                                        .getInt(fieldId));
141
                                if (metaData.getColumnType(fieldId) == Types.BIGINT)
142
                                        regAtt[fieldId - 2] = ValueFactory.createValue(rs
143
                                                        .getLong(fieldId));
144
                                if (metaData.getColumnType(fieldId) == Types.BIT)
145
                                        regAtt[fieldId - 2] = ValueFactory.createValue(rs
146
                                                        .getBoolean(fieldId));
147
                                if (metaData.getColumnType(fieldId) == Types.DATE)
148
                                        regAtt[fieldId - 2] = ValueFactory.createValue(rs
149
                                                        .getDate(fieldId));
150

    
151
                        }
152

    
153
                        // TODO: Aqu? habr?a que usar una Factor?a.
154

    
155
                        IFeature feat = new DefaultFeature(geom, regAtt);
156

    
157
                        return feat;
158
                } catch (SQLException e) {
159
                        throw new DriverException(e);
160
                }
161

    
162
        }
163

    
164
        /*
165
         * (non-Javadoc)
166
         * 
167
         * @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
168
         */
169
        public void closeIterator() throws DriverException {
170
                try {
171
                        rs.close();
172
                } catch (SQLException e) {
173
                        throw new DriverException(e);
174
                }
175
        }
176

    
177
}