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