01: package net.sourceforge.jaxor.generator;
02:
03: import java.util.Map;
04: import java.util.HashMap;
05: import java.sql.Types;
06:
07: /**
08: * Created By: fede
09: * Date: 2-feb-2004
10: * Time: 11.52.30
11: *
12: * Last Checkin: $Author: fspinazzi $
13: * Date: $Date: 2004/02/10 08:50:55 $
14: * Revision: $Revision: 1.4 $
15: *
16: * See
17: * http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec/jdbc-spec.frame8.html
18: */
19: public class Sql2JavaMapping {
20:
21: private final Map _baseRegistry = new HashMap();
22: private final Map _extendedRegistry = new HashMap();
23: public final static String UNKNOW_DATA_TYPE = "Unknown";
24:
25: public Sql2JavaMapping() {
26: _baseRegistry.put(new Integer(Types.CHAR), "String"); // todo: threats char(x) as varchar: correct?
27: _baseRegistry.put(new Integer(Types.VARCHAR), "String");
28: _baseRegistry.put(new Integer(Types.BIT), "Boolean");
29: _baseRegistry.put(new Integer(Types.INTEGER), "Integer");
30: _baseRegistry.put(new Integer(Types.BIGINT), "Long");
31: _baseRegistry.put(new Integer(Types.SMALLINT), "Short");
32: _baseRegistry.put(new Integer(Types.NUMERIC),
33: java.math.BigDecimal.class.getName());
34: _baseRegistry.put(new Integer(Types.REAL), "Float");
35: _baseRegistry.put(new Integer(Types.DOUBLE), "Double");
36: _baseRegistry.put(new Integer(Types.DECIMAL), "Double");
37: _baseRegistry.put(new Integer(Types.TINYINT), "Byte");
38: _baseRegistry.put(new Integer(Types.DATE), "java.sql.Date");
39: _baseRegistry.put(new Integer(Types.TIMESTAMP),
40: "java.sql.Timestamp");
41: }
42:
43: public String getByTypeCode(int sqlType) {
44: final String fromBaseRegistry = (String) _baseRegistry
45: .get(new Integer(sqlType));
46: if (fromBaseRegistry != null)
47: return fromBaseRegistry;
48: return UNKNOW_DATA_TYPE;
49: }
50:
51: public void register(String typeToMap, String typeMappedTo) {
52: _extendedRegistry.put(typeToMap, typeMappedTo);
53: }
54:
55: public String getByTypeName(String typeName) {
56: final String s = (String) _extendedRegistry.get(typeName);
57: if (s != null)
58: return s;
59: return UNKNOW_DATA_TYPE;
60: }
61:
62: public String getByTypeNameOrCode(String typeName, int typeCode) {
63: final String byTypeName = getByTypeName(typeName);
64: if (!UNKNOW_DATA_TYPE.equals(byTypeName))
65: return byTypeName;
66:
67: final String byTypeCode = getByTypeCode(typeCode);
68: if (!UNKNOW_DATA_TYPE.equals(byTypeCode))
69: return byTypeCode;
70:
71: return UNKNOW_DATA_TYPE;
72: }
73: }
|