Statistics
| Revision:

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

History | View | Annotate | Download (7.51 KB)

1 2269 fjp
/*
2
 * Created on 11-mar-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5 10626 caballero
 *
6 2269 fjp
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7 10626 caballero
 *
8 2269 fjp
 * 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 10626 caballero
 *
13 2269 fjp
 * 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 10626 caballero
 *
18 2269 fjp
 * 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 10626 caballero
 *
22 2269 fjp
 * 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 10626 caballero
 *
34 2269 fjp
 *    or
35 10626 caballero
 *
36 2269 fjp
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40 10626 caballero
 *
41 2269 fjp
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql;
45
46
import java.sql.ResultSet;
47
import java.sql.ResultSetMetaData;
48
import java.sql.SQLException;
49
import java.sql.Types;
50
51 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
52 2269 fjp
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 3347 fjp
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
58 2269 fjp
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
59 5714 fjp
import com.iver.cit.gvsig.fmap.drivers.WKBParser2;
60 2269 fjp
61
/**
62 11339 azabala
 * Iterator over the features of a MySQL driver.
63
 *
64 2269 fjp
 *
65 11339 azabala
 *
66
 * @author FJP
67 2269 fjp
 */
68
public class MySqlFeatureIterator implements IFeatureIterator {
69 5007 fjp
    private WKBParser2 parser = new WKBParser2();
70 2269 fjp
    ResultSet rs;
71
    String strAux;
72
    IGeometry geom;
73
    int numColumns;
74 3268 fjp
    int idFieldID = -1;
75 10626 caballero
76 3347 fjp
    int[] relIds;
77
    private DBLayerDefinition lyrDef;
78
79 2269 fjp
    private ResultSetMetaData metaData = null;
80
    Value[] regAtt;
81
    /**
82
     * @throws SQLException
83 10626 caballero
     *
84 2269 fjp
     */
85
    public MySqlFeatureIterator(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 10626 caballero
98 2269 fjp
    /* (non-Javadoc)
99
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
100
     */
101 10626 caballero
    public boolean hasNext() throws ReadDriverException {
102 4089 fjp
        try {
103
                        if (rs.next())
104
                            return true;
105 10626 caballero
                        closeIterator();
106
                        return false;
107 4089 fjp
                } catch (SQLException e) {
108 10626 caballero
//                        SqlDriveExceptionType type = new SqlDriveExceptionType();
109
//            type.setDriverName("MySQL Driver");
110
//            try {
111
//                                type.setSql(rs.getStatement().toString());
112
//                        } catch (SQLException e1) {
113
//                                e1.printStackTrace();
114
//                        }
115
            throw new ReadDriverException("MySQL Driver",e);
116 7470 azabala
//                        throw new DriverException(e);
117 4089 fjp
                }
118 2269 fjp
    }
119
120
    /* (non-Javadoc)
121
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#next()
122
     */
123 10626 caballero
    public IFeature next() throws ReadDriverException {
124 4089 fjp
        byte[] data;
125 11339 azabala
126 4089 fjp
                try {
127
                        data = rs.getBytes(1);
128
                        geom = parser.parse(data);
129 10626 caballero
130 4089 fjp
                for (int fieldId=2; fieldId <= numColumns; fieldId++ )
131
                {
132
                    Value val = null;
133
                    if (metaData.getColumnType(fieldId) == Types.VARCHAR)
134
                    {
135
                        String strAux = rs.getString(fieldId);
136
                        if (strAux == null) strAux = "";
137
                        val =  ValueFactory.createValue(strAux);
138 11339 azabala
                    }else if (metaData.getColumnType(fieldId) == Types.FLOAT)
139 4089 fjp
                        val = ValueFactory.createValue(rs.getFloat(fieldId));
140 11339 azabala
                    else if (metaData.getColumnType(fieldId) == Types.DOUBLE)
141 4089 fjp
                        val = ValueFactory.createValue(rs.getDouble(fieldId));
142 11339 azabala
                    else if (metaData.getColumnType(fieldId) == Types.INTEGER)
143 4089 fjp
                        val = ValueFactory.createValue(rs.getInt(fieldId));
144 11339 azabala
                    else if (metaData.getColumnType(fieldId) == Types.BIGINT)
145 4089 fjp
                        val = ValueFactory.createValue(rs.getLong(fieldId));
146 11339 azabala
                    else if (metaData.getColumnType(fieldId) == Types.BIT)
147 4089 fjp
                        val = ValueFactory.createValue(rs.getBoolean(fieldId));
148 11339 azabala
                    else if (metaData.getColumnType(fieldId) == Types.DATE)
149 4089 fjp
                        val = ValueFactory.createValue(rs.getDate(fieldId));
150 10626 caballero
151 11339 azabala
                    regAtt[relIds[fieldId-2]] = val;
152 4089 fjp
                }
153
                IFeature feat = null;
154
                if (idFieldID != -1)
155 11339 azabala
                {//TODO Review if we could find problems when the table has only geom and gid
156
                        int fieldId = lyrDef.getIdFieldID();
157
                        Value idValue = regAtt[fieldId];
158
                        String theID = "";
159
                        if(idValue != null)//azabala: sometimes we found problems with gid (null pointer exceptions)
160
                                theID = idValue.toString();
161 4089 fjp
                    feat = new DefaultFeature(geom, regAtt, theID);
162
                }
163
                else
164 4181 fjp
                        {
165 10626 caballero
//                                // feat = new DefaultFeature(geom, regAtt);
166
//                        FeatureWithoutIdExceptionType  type = new FeatureWithoutIdExceptionType();
167
//                        type.setSchema(lyrDef);
168
                                throw new ReadDriverException("MySQL Driver",null);
169 4181 fjp
                        }
170 11339 azabala
171 4181 fjp
172 10626 caballero
173
174 4089 fjp
                return feat;
175
                } catch (SQLException e) {
176 10626 caballero
//                        SqlDriveExceptionType type = new SqlDriveExceptionType();
177
//            type.setDriverName("MySQL Driver");
178
//            try {
179
//                                type.setSql(rs.getStatement().toString());
180
//                        } catch (SQLException e1) {
181
//                                e1.printStackTrace();
182
//                        }
183
            throw new ReadDriverException("MySQL Driver",e);
184 7470 azabala
//                        throw new DriverException(e);
185 10626 caballero
                }
186 2269 fjp
    }
187
188 3259 fjp
    /* (non-Javadoc)
189
     * @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
190
     */
191 10626 caballero
    public void closeIterator() throws ReadDriverException {
192 4089 fjp
        try {
193
                        rs.close();
194
                } catch (SQLException e) {
195 10626 caballero
//                        SqlDriveExceptionType type = new SqlDriveExceptionType();
196
//            type.setDriverName("MySQL Driver");
197
//            try {
198
//                                type.setSql(rs.getStatement().toString());
199
//                        } catch (SQLException e1) {
200
//                                e1.printStackTrace();
201
//                        }
202
            throw new ReadDriverException("MySQL Driver",e);
203 7470 azabala
//                        throw new DriverException(e);
204 4089 fjp
                }
205 3259 fjp
    }
206 10626 caballero
207 3347 fjp
    public void setLyrDef(DBLayerDefinition lyrDef)
208 3268 fjp
    {
209 3347 fjp
        this.lyrDef  =lyrDef;
210
        // Aunque no nos hayan pedido todos los campos, devolveremos
211
        // tantos atributos como la capa tiene. Eso s?, puestos a null
212
        regAtt = new Value[lyrDef.getFieldNames().length];
213 9735 azabala
        //no deber?a ser numColums - 2 ??
214 3347 fjp
        relIds = new int[numColumns-1];
215 10626 caballero
216 3268 fjp
        try {
217 9735 azabala
//            for (int i=2; i<= metaData.getColumnCount(); i++)
218
                for (int i=2; i <= numColumns; i++)
219 3268 fjp
            {
220 3347 fjp
                int idRel = lyrDef.getFieldIdByName(metaData.getColumnName(i));
221 6034 fjp
                                if (idRel == -1)
222
                                {
223
                                        throw new RuntimeException("No se ha encontrado el nombre de campo " + metaData.getColumnName(i));
224
                                }
225
226 3347 fjp
                relIds[i-2] = idRel;
227
                if (lyrDef.getFieldID().equals(metaData.getColumnName(i)))
228 3268 fjp
                {
229
                    idFieldID = i;
230
                    break;
231
                }
232
            }
233
        } catch (SQLException e) {
234
            // Si no est?, no pasa nada
235
        }
236 10626 caballero
237 3268 fjp
    }
238 3259 fjp
239 3268 fjp
240 2269 fjp
}