root / tags / v1_1_Build_1014 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / XTypes.java @ 13593
History | View | Annotate | Download (6.8 KB)
1 |
/*
|
---|---|
2 |
* Created on 26-oct-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; |
45 |
|
46 |
import java.math.BigDecimal; |
47 |
import java.nio.ByteBuffer; |
48 |
import java.sql.ResultSet; |
49 |
import java.sql.ResultSetMetaData; |
50 |
import java.sql.SQLException; |
51 |
import java.sql.Timestamp; |
52 |
import java.sql.Types; |
53 |
|
54 |
import com.hardcode.gdbms.engine.values.BooleanValue; |
55 |
import com.hardcode.gdbms.engine.values.DateValue; |
56 |
import com.hardcode.gdbms.engine.values.DoubleValue; |
57 |
import com.hardcode.gdbms.engine.values.FloatValue; |
58 |
import com.hardcode.gdbms.engine.values.IntValue; |
59 |
import com.hardcode.gdbms.engine.values.LongValue; |
60 |
import com.hardcode.gdbms.engine.values.NullValue; |
61 |
import com.hardcode.gdbms.engine.values.ShortValue; |
62 |
import com.hardcode.gdbms.engine.values.StringValue; |
63 |
import com.hardcode.gdbms.engine.values.TimeValue; |
64 |
import com.hardcode.gdbms.engine.values.TimestampValue; |
65 |
import com.hardcode.gdbms.engine.values.Value; |
66 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
67 |
|
68 |
public class XTypes { |
69 |
public final static double NUM_msSecs2000 = 9.466776E11; |
70 |
public final static int POINT2D = 100; |
71 |
|
72 |
public final static int LINE2D = 101; |
73 |
|
74 |
public final static int POLYGON2D = 102; |
75 |
|
76 |
public final static int MULTI2D = 103; |
77 |
|
78 |
public static String fieldTypeToString(int fieldType) { |
79 |
String aux = "text"; // Por defecto. |
80 |
switch (fieldType) {
|
81 |
case Types.SMALLINT: |
82 |
aux = "integer";
|
83 |
break;
|
84 |
case Types.INTEGER: |
85 |
aux = "integer";
|
86 |
break;
|
87 |
case Types.BIGINT: |
88 |
aux = "integer";
|
89 |
break;
|
90 |
case Types.BOOLEAN: |
91 |
aux = "boolean";
|
92 |
break;
|
93 |
case Types.DECIMAL: |
94 |
aux = "numeric";
|
95 |
break;
|
96 |
case Types.DOUBLE: |
97 |
aux = "float8";
|
98 |
break;
|
99 |
case Types.FLOAT: |
100 |
aux = "float";
|
101 |
break;
|
102 |
case Types.CHAR: |
103 |
aux = "char";
|
104 |
break;
|
105 |
case Types.VARCHAR: |
106 |
aux = "text";
|
107 |
break;
|
108 |
case Types.LONGVARCHAR: |
109 |
aux = "text";
|
110 |
break;
|
111 |
|
112 |
case POINT2D:
|
113 |
aux = "POINT";
|
114 |
break;
|
115 |
case LINE2D:
|
116 |
aux = "MULTILINESTRING";
|
117 |
break;
|
118 |
case POLYGON2D:
|
119 |
aux = "MULTIPOLYGON";
|
120 |
break;
|
121 |
case MULTI2D:
|
122 |
aux = "GEOMETRY";
|
123 |
break;
|
124 |
|
125 |
} |
126 |
|
127 |
return aux;
|
128 |
} |
129 |
|
130 |
/**
|
131 |
* @param fieldId
|
132 |
* @throws SQLException
|
133 |
*/
|
134 |
public static Value getValue(ResultSet rs, int fieldId) throws SQLException { |
135 |
byte[] data; |
136 |
data = rs.getBytes(fieldId); |
137 |
Value val = null;
|
138 |
ResultSetMetaData metaData = rs.getMetaData();
|
139 |
if (data == null) |
140 |
val = ValueFactory.createNullValue(); |
141 |
else {
|
142 |
ByteBuffer buf = ByteBuffer.wrap(data); |
143 |
if (metaData.getColumnType(fieldId) == Types.VARCHAR) |
144 |
val = ValueFactory.createValue(rs.getString(fieldId)); |
145 |
if (metaData.getColumnType(fieldId) == Types.FLOAT) |
146 |
val = ValueFactory.createValue(buf.getFloat()); |
147 |
if (metaData.getColumnType(fieldId) == Types.DOUBLE) |
148 |
val = ValueFactory.createValue(buf.getDouble()); |
149 |
if (metaData.getColumnType(fieldId) == Types.INTEGER) |
150 |
val = ValueFactory.createValue(buf.getInt()); |
151 |
if (metaData.getColumnType(fieldId) == Types.SMALLINT) |
152 |
val = ValueFactory.createValue(buf.getShort()); |
153 |
if (metaData.getColumnType(fieldId) == Types.TINYINT) |
154 |
val = ValueFactory.createValue(buf.getShort()); |
155 |
if (metaData.getColumnType(fieldId) == Types.BIGINT) |
156 |
val = ValueFactory.createValue(buf.getLong()); |
157 |
if (metaData.getColumnType(fieldId) == Types.BIT || metaData.getColumnType(fieldId) == Types.BOOLEAN ) |
158 |
val = ValueFactory.createValue(rs.getBoolean(fieldId)); |
159 |
if (metaData.getColumnType(fieldId) == Types.DATE) |
160 |
val = ValueFactory.createValue(rs.getDate(fieldId)); |
161 |
if (metaData.getColumnType(fieldId) == Types.TIME) |
162 |
val = ValueFactory.createValue(rs.getTime(fieldId)); |
163 |
if (metaData.getColumnType(fieldId) == Types.TIMESTAMP) |
164 |
val = ValueFactory.createValue(rs.getTimestamp(fieldId)); |
165 |
|
166 |
|
167 |
|
168 |
} |
169 |
return val;
|
170 |
} |
171 |
|
172 |
public static void updateValue(ResultSet rs, int fieldId_ceroBased, |
173 |
Value val) throws SQLException { |
174 |
if (val instanceof NullValue) |
175 |
return;
|
176 |
// byte[] data;
|
177 |
ResultSetMetaData metaData = rs.getMetaData();
|
178 |
int fieldId = fieldId_ceroBased + 1; |
179 |
// System.out.println("EScritrua: " + metaData.isDefinitelyWritable(1));
|
180 |
switch (val.getSQLType()) {
|
181 |
case Types.VARCHAR: |
182 |
case Types.LONGVARCHAR: |
183 |
// Para evitar escribir en el campo geometria:
|
184 |
if (metaData.getColumnType(fieldId) == Types.OTHER) |
185 |
return;
|
186 |
StringValue valStr = (StringValue) val; |
187 |
rs.updateString(fieldId, valStr.getValue()); |
188 |
// System.out.println("Field " + fieldId + " :" +
|
189 |
// metaData.getColumnTypeName(fieldId));
|
190 |
break;
|
191 |
case Types.FLOAT: |
192 |
FloatValue vFloat = (FloatValue) val; |
193 |
rs.updateFloat(fieldId, vFloat.getValue()); |
194 |
break;
|
195 |
case Types.DOUBLE: |
196 |
DoubleValue vDouble = (DoubleValue) val; |
197 |
rs.updateDouble(fieldId, vDouble.getValue()); |
198 |
break;
|
199 |
case Types.INTEGER: |
200 |
IntValue vInt = (IntValue) val; |
201 |
rs.updateInt(fieldId, vInt.getValue()); |
202 |
break;
|
203 |
case Types.SMALLINT: |
204 |
ShortValue vShort = (ShortValue) val; |
205 |
rs.updateShort(fieldId, vShort.shortValue()); |
206 |
break;
|
207 |
case Types.BIGINT: |
208 |
LongValue vLong = (LongValue) val; |
209 |
rs.updateLong(fieldId, vLong.getValue()); |
210 |
break;
|
211 |
case Types.BIT: |
212 |
case Types.BOOLEAN: |
213 |
BooleanValue vBool = (BooleanValue) val; |
214 |
rs.updateBoolean(fieldId, vBool.getValue()); |
215 |
break;
|
216 |
case Types.DATE: |
217 |
DateValue vDate = (DateValue) val; |
218 |
rs.updateDate(fieldId, vDate.getValue()); |
219 |
break;
|
220 |
case Types.TIME: |
221 |
TimeValue vTime = (TimeValue) val; |
222 |
rs.updateTime(fieldId, vTime.getValue()); |
223 |
break;
|
224 |
case Types.TIMESTAMP: |
225 |
TimestampValue vTimeStamp = (TimestampValue) val; |
226 |
rs.updateTimestamp(fieldId, vTimeStamp.getValue()); |
227 |
break;
|
228 |
|
229 |
default:
|
230 |
System.err.println("Tipo no soportado:" |
231 |
+ metaData.getColumnType(fieldId) + ". Field:" + fieldId
|
232 |
+ ": " + metaData.getColumnName(fieldId));
|
233 |
// throw new UnsupportedOperationException();
|
234 |
|
235 |
} |
236 |
} |
237 |
|
238 |
} |