Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySqlFeatureIterator.java @ 3476

History | View | Annotate | Download (5.84 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.mysql;
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.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.IFeature;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
57
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
58
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser;
59

    
60
/**
61
 * @author FJP
62
 *
63
 * TODO To change the template for this generated type comment go to
64
 * Window - Preferences - Java - Code Generation - Code and Comments
65
 */
66
public class MySqlFeatureIterator implements IFeatureIterator {
67
    private WKBParser parser = new WKBParser();
68
    ResultSet rs;
69
    String strAux;
70
    IGeometry geom;
71
    int numColumns;
72
    int idFieldID = -1;
73
    
74
    int[] relIds;
75
    private DBLayerDefinition lyrDef;
76

    
77
    private ResultSetMetaData metaData = null;
78
    Value[] regAtt;
79
    /**
80
     * @throws SQLException
81
     * 
82
     */
83
    public MySqlFeatureIterator(ResultSet rs) {
84
        // Debe ser forward only
85
        this.rs = rs;
86
        try {
87
            numColumns = rs.getMetaData().getColumnCount();
88
            regAtt = new Value[numColumns-1];
89
            metaData = rs.getMetaData();
90
        } catch (SQLException e) {
91
            // TODO Auto-generated catch block
92
            e.printStackTrace();
93
        }
94
    }
95
    
96
    /* (non-Javadoc)
97
     * @see com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator#hasNext()
98
     */
99
    public boolean hasNext() throws SQLException {
100
        if (rs.next())
101
            return true;
102
        else
103
        {
104
            closeIterator();
105
            return false;
106
        }
107
    }
108

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

    
152
        
153
        return feat;
154
    }
155

    
156
    /* (non-Javadoc)
157
     * @see com.iver.cit.gvsig.fmap.drivers.IFeatureIterator#closeIterator()
158
     */
159
    public void closeIterator() throws SQLException {
160
        rs.close();
161
    }
162
    
163
    public void setLyrDef(DBLayerDefinition lyrDef)
164
    {
165
        this.lyrDef  =lyrDef;
166
        // Aunque no nos hayan pedido todos los campos, devolveremos
167
        // tantos atributos como la capa tiene. Eso s?, puestos a null
168
        regAtt = new Value[lyrDef.getFieldNames().length];
169
        relIds = new int[numColumns-1];
170
        
171
        try {
172
            for (int i=2; i<= metaData.getColumnCount(); i++)
173
            {
174
                int idRel = lyrDef.getFieldIdByName(metaData.getColumnName(i));
175
                relIds[i-2] = idRel;
176
                if (lyrDef.getFieldID().equals(metaData.getColumnName(i)))
177
                {
178
                    idFieldID = i;
179
                    break;
180
                }
181
            }
182
        } catch (SQLException e) {
183
            // Si no est?, no pasa nada
184
        }
185
        
186
    }
187

    
188

    
189
}