KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > DataType


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.datatype;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Types JavaDoc;
28
29
30 /**
31  * @author Manuel Laflamme
32  * @version $Revision: 1.24 $
33  */

34 public abstract class DataType
35 {
36     public static final DataType UNKNOWN = new UnknownDataType();
37
38     public static final DataType CHAR = new StringDataType(
39             "CHAR", Types.CHAR);
40     public static final DataType VARCHAR = new StringDataType(
41             "VARCHAR", Types.VARCHAR);
42     public static final DataType LONGVARCHAR = new StringDataType(
43             "LONGVARCHAR", Types.LONGVARCHAR);
44     public static final DataType CLOB = new ClobDataType();
45
46     public static final DataType NUMERIC = new NumberDataType(
47             "NUMERIC", Types.NUMERIC);
48     public static final DataType DECIMAL = new NumberDataType(
49             "DECIMAL", Types.DECIMAL);
50
51     public static final DataType BOOLEAN = new BooleanDataType();
52
53     public static final DataType TINYINT = new IntegerDataType(
54             "TINYINT", Types.TINYINT);
55     public static final DataType SMALLINT = new IntegerDataType(
56             "SMALLINT", Types.SMALLINT);
57     public static final DataType INTEGER = new IntegerDataType(
58             "INTEGER", Types.INTEGER);
59
60     public static final DataType BIGINT = new LongDataType();
61
62     public static final DataType REAL = new FloatDataType();
63
64     public static final DataType FLOAT = new DoubleDataType(
65             "FLOAT", Types.FLOAT);
66     public static final DataType DOUBLE = new DoubleDataType(
67             "DOUBLE", Types.DOUBLE);
68
69     public static final DataType DATE = new DateDataType();
70     public static final DataType TIME = new TimeDataType();
71     public static final DataType TIMESTAMP = new TimestampDataType();
72
73     public static final DataType BINARY = new BytesDataType(
74             "BINARY", Types.BINARY);
75     public static final DataType VARBINARY = new BytesDataType(
76             "VARBINARY", Types.VARBINARY);
77     public static final DataType LONGVARBINARY = new BytesDataType(
78             "LONGVARBINARY", Types.LONGVARBINARY);
79     public static final DataType BLOB = new BlobDataType();
80
81     private static final DataType[] TYPES = {
82         VARCHAR, CHAR, LONGVARCHAR, CLOB, NUMERIC, DECIMAL, BOOLEAN, INTEGER,
83         TINYINT, SMALLINT, BIGINT, REAL, DOUBLE, FLOAT, DATE, TIME, TIMESTAMP,
84         VARBINARY, BINARY, LONGVARBINARY, BLOB,
85     };
86
87     /**
88      * Returns the specified value typecasted to this <code>DataType</code>
89      */

90     public abstract Object JavaDoc typeCast(Object JavaDoc value) throws TypeCastException;
91
92     /**
93      * Returns a negative integer, zero, or a positive integer as the first
94      * argument is less than, equal to, or greater than the second.
95      * <p>
96      * The two values are typecast to this DataType before being compared.
97      *
98      * @throws TypeCastException if the arguments' types prevent them from
99      * being compared by this Comparator.
100      */

101     public abstract int compare(Object JavaDoc o1, Object JavaDoc o2) throws TypeCastException;
102
103     /**
104      * Returns the coresponding {@link java.sql.Types}.
105      */

106     public abstract int getSqlType();
107
108     /**
109      * Returns the runtime class of the typecast result.
110      */

111     public abstract Class JavaDoc getTypeClass();
112
113     /**
114      * Returns <code>true</code> if this <code>DataType</code> represents a
115      * number.
116      */

117     public abstract boolean isNumber();
118
119     /**
120      * Returns <code>true</code> if this <code>DataType</code> represents a
121      * date and/or time.
122      */

123     public abstract boolean isDateTime();
124
125     /**
126      * Returns the specified column value from the specified resultset object.
127      */

128     public abstract Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
129             throws SQLException JavaDoc, TypeCastException;
130
131     /**
132      * Set the specified value to the specified prepared statement object.
133      */

134     public abstract void setSqlValue(Object JavaDoc value, int column,
135             PreparedStatement JavaDoc statement) throws SQLException JavaDoc, TypeCastException;
136
137     /**
138      * Typecast the specified value to string.
139      */

140     public static String JavaDoc asString(Object JavaDoc value) throws TypeCastException
141     {
142         return (String JavaDoc)DataType.VARCHAR.typeCast(value);
143     }
144
145     /**
146      * Returns the <code>DataType</code> corresponding to the specified Sql
147      * type. See {@link java.sql.Types}.
148      *
149      */

150     public static DataType forSqlType(int sqlType) throws DataTypeException
151     {
152         for (int i = 0; i < TYPES.length; i++)
153         {
154             if (sqlType == TYPES[i].getSqlType())
155             {
156                 return TYPES[i];
157             }
158         }
159
160         return UNKNOWN;
161     }
162
163     /**
164      * Returns the <code>DataType</code> corresponding to the specified Sql
165      * type name.
166      *
167      * @deprecated Should not be used anymore
168      */

169     public static DataType forSqlTypeName(String JavaDoc sqlTypeName) throws DataTypeException
170     {
171         for (int i = 0; i < TYPES.length; i++)
172         {
173             if (sqlTypeName.equals(TYPES[i].toString()))
174             {
175                 return TYPES[i];
176             }
177         }
178
179         return UNKNOWN;
180     }
181
182     /**
183      * Returns the <code>DataType</code> corresponding to the specified value
184      * runtime class. This method returns <code>DataType.UNKNOWN</code>
185      * if the value is <code>null</code> or runtime class not recognized.
186      */

187     public static DataType forObject(Object JavaDoc value)
188     {
189         if (value == null)
190         {
191             return UNKNOWN;
192         }
193
194         for (int i = 0; i < TYPES.length; i++)
195         {
196             Class JavaDoc typeClass = TYPES[i].getTypeClass();
197             if (typeClass.isInstance(value))
198             {
199                 return TYPES[i];
200             }
201         }
202
203         return UNKNOWN;
204     }
205 }
206
207
208
209
210
211
212
213
214
215
216
Popular Tags