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