Statistics
| Revision:

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

History | View | Annotate | Download (5.47 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.driver.exceptions.ReadDriverException;
52
import com.hardcode.gdbms.engine.values.Value;
53
import com.hardcode.gdbms.engine.values.ValueFactory;
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.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 ReadDriverException {
104
                try {
105
                        if (rs.isLast()) {
106
                                rs.close();
107
                                return false;
108
                        }
109
                        return true;
110
                } catch (SQLException e) {
111
//                        SqlDriveExceptionType type = new SqlDriveExceptionType();
112
//            type.setDriverName("HSQLDB Driver");
113
//            try {
114
//                                type.setSql(rs.getStatement().toString());
115
//                        } catch (SQLException e1) {
116
//                                e1.printStackTrace();
117
//                        }
118
            throw new ReadDriverException("HSQLDB Driver",e);
119
                }
120
        }
121

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

    
158
                        }
159

    
160
                        // TODO: Aqu? habr?a que usar una Factor?a.
161

    
162
                        IFeature feat = new DefaultFeature(geom, regAtt);
163

    
164
                        return feat;
165
                } catch (SQLException e) {
166
//                        SqlDriveExceptionType type = new SqlDriveExceptionType();
167
//            type.setDriverName("HSQLDB Driver");
168
//            try {
169
//                                type.setSql(rs.getStatement().toString());
170
//                        } catch (SQLException e1) {
171
//                                e1.printStackTrace();
172
//                        }
173
            throw new ReadDriverException("HSQLDB Driver",e);
174
                }
175

    
176
        }
177

    
178
        /*
179
         * (non-Javadoc)
180
         *
181
         * @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
182
         */
183
        public void closeIterator() throws ReadDriverException {
184
                try {
185
                        rs.close();
186
                } catch (SQLException e) {
187
//                        SqlDriveExceptionType type = new SqlDriveExceptionType();
188
//            type.setDriverName("HSQLDB Driver");
189
//            try {
190
//                                type.setSql(rs.getStatement().toString());
191
//                        } catch (SQLException e1) {
192
//                                e1.printStackTrace();
193
//                        }
194
            throw new ReadDriverException("HSQLDB Driver",e);
195
//                        throw new DriverException(e);
196
                }
197
        }
198

    
199
}