Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jdbf/engine/sql/Types.java


1   /*
2   * 05/18/2002 - 13:01:11
3   *
4   *
5   * Types.java - JDBF Object Relational mapping system
6   * Copyright (C) 2002 Giovanni Martone
7   * giovannimartone@hotmail.com
8   * http://jdbf.sourceforge.net
9   *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24  package org.jdbf.engine.sql;
25  
26  import java.util.HashMap;
27  
28  import org.jdbf.engine.mapping.MappingException;
29  
30  /**
31   * <code> Types </code> is the class that maps the type of the property
32   * with SQLType (e.g. int with java.sql.Types.INTEGER).
33   * 
34   * <pre>
35   *
36   *  |    type      |        SQL Type          | 
37   *  |------------- |--------------------------|
38   *  |   int        |  java.sql.Type.INTEGER   |
39   *  |   long       |  java.sql.Type.BIGINT    |
40   *  |   binary     |  java.sql.Type.BINARY    |
41   *  |   boolean    |  java.sql.Type.BIT       |
42   *  |   date       |  java.sql.Type.DATE      |
43   *  |   decimal    |  java.sql.Type.DECIMAL   |
44   *  |   double     |  java.sql.Type.DOUBLE    |
45   *  |   float      |  java.sql.Type.FLOAT     |
46   *  |   real       |  java.sql.Type.REAL      |
47   *  |   smallint   |  java.sql.Type.INTEGER   |
48   *  |   time       |  java.sql.Type.TIME      |
49   *  |   timestamp  |  java.sql.Type.TIMESTAMP |
50   *  |   tinyint    |  java.sql.Type.TINYINT   |
51   *  |   varbinary  |  java.sql.Type.VARBINARY |
52   *  |   char       |  java.sql.Type.VARCHAR   |
53   *  |   string     |  java.sql.Type.VARCHAR   |
54   *  |   numeric    |  java.sql.Type.NUMERIC   |
55   *  |   longstring |  java.sql.Type.VARCHAR   |
56   *  |   lognbinary |  java.sql.Type.BINARY    |
57   *
58   * </pre>
59   *
60   * @author Giovanni Martone
61   * @version $id$
62   *
63   */
64  public class Types{
65  
66    /** 
67     * Map type of property - sql type 
68     */
69    protected static final HashMap TYPES = loadTypes();
70  
71  
72    /** 
73     * Load the table that maps the type of property with SQL type.
74     * 
75     * @return map loaded.
76     */
77    private static HashMap loadTypes(){
78      HashMap types = new HashMap();
79      types.put("string",new Integer(java.sql.Types.VARCHAR));
80      types.put("long",new Integer(java.sql.Types.BIGINT));
81      types.put("binary",new Integer(java.sql.Types.BINARY));
82      types.put("boolean",new Integer(java.sql.Types.BIT));
83      types.put("date",new Integer(java.sql.Types.DATE));
84      types.put("decimal",new Integer(java.sql.Types.DECIMAL));
85      types.put("double",new Integer(java.sql.Types.DOUBLE));
86      types.put("float",new Integer(java.sql.Types.FLOAT));
87      types.put("int",new Integer(java.sql.Types.INTEGER));
88      types.put("real",new Integer(java.sql.Types.REAL));
89      types.put("smallint",new Integer(java.sql.Types.INTEGER));
90      types.put("time",new Integer(java.sql.Types.TIME));
91      types.put("timestamp",new Integer(java.sql.Types.TIMESTAMP));
92      types.put("tinyint",new Integer(java.sql.Types.TINYINT));
93      types.put("varbinary",new Integer(java.sql.Types.VARBINARY));
94      types.put("char",new Integer(java.sql.Types.CHAR));
95      types.put("numeric", new Integer(java.sql.Types.NUMERIC));
96      types.put("longstring", new Integer(java.sql.Types.VARCHAR));
97      types.put("longbinary", new Integer(java.sql.Types.BINARY));
98      return types;
99    }
100 
101 
102   /**
103    * Return the sql type for type of property specified in type.
104    * If type is invalid type MappigException if thrown
105    *
106    * @param type of property
107    * @return sql type
108    * @throws MappingException if type is invalid
109    */
110   public static int getSQLType(String type) throws MappingException{
111     Integer sqlType = (Integer) TYPES.get(type);  
112     if( sqlType == null)
113       throw new MappingException("mapping.invalidType",type);
114     else
115       return sqlType.intValue();
116   }
117 }