Source code: org/enableit/db/SqlType.java
1 package org.enableit.db;
2
3 /*
4 * PROJECT : database integration library
5 *
6 * COPYRIGHT : Copyright (C) 2001 Tim Stephenson
7 *
8 * Change History:
9 * -------------------------------------------------------------------
10 * Version Date Author Description
11 * 1.0 28Apr01 Tim Stephenson Created.
12 * $Log: SqlType.java,v $
13 * Revision 1.3 2002/09/14 04:03:01 default
14 * added more types
15 *
16 * Revision 1.2 2002/08/21 09:04:45 default
17 * bundle up all changes preparatory to 'mavenizing'
18 *
19 * Revision 1.1 2002/02/17 07:51:13 tstephen
20 * initial version under the enableit package name , added GPL licence and a few other things
21 *
22 * Revision 1.1 2001/06/01 08:37:19 tstephen
23 * lots of untracked changes leading to v1.0
24 *
25 */
26
27 import java.io.Serializable;
28 import java.sql.Types;
29
30 /**
31 * <p>This class has a primary purpose of representing a null value to be
32 * inserted into some SQL call handled by the proxy objects</p>
33 *
34 * <p>This is necessary to allow the proxies to receive a null object
35 * yet still provide the JDBC driver with a known object type
36 * as required by <CODE>PreparedStatement.setNull</CODE></p>
37 *
38 * <p>The integer used in the constructor should be one of the static
39 * final members of <CODE>Types</CODE></p>
40 *
41 * @version __VERSION__
42 *
43 * @author tim@thestephensons.org
44 *
45 * @see java.sql.Types java.sql.Types <br/>
46 * @see org.enableit.db.DatabaseProxy DatabaseProxy <br/>
47 * @see org.enableit.db.GenericDBProxy GenericDBProxy <br/>
48 */
49 public class SqlType implements Serializable {
50
51 /**
52 * Represents a Boolean
53 */
54 public static final int BOOLEAN = Types.BIT;
55 /**
56 * Represents a Character
57 */
58 public static final int CHARACTER = Types.CHAR;
59 /**
60 * Represents a Double
61 */
62 public static final int DOUBLE = Types.DOUBLE;
63 /**
64 * Represents a Float
65 */
66 public static final int FLOAT = Types.FLOAT;
67 /**
68 * Represents an Integer
69 * @deprecated
70 */
71 public static final int INTEGER = Types.INTEGER;
72 /**
73 * Represents a Long
74 */
75 public static final int LONG = Types.NUMERIC;
76 /**
77 * Represents a Short
78 */
79 public static final int SHORT = Types.SMALLINT;
80 /**
81 * Represents a String
82 */
83 public static final int STRING = Types.VARCHAR;
84 /**
85 * Represents a BigDecimal
86 */
87 public static final int BIGDECIMAL = Types.NUMERIC;
88 /**
89 * Represents a java.util.Date
90 */
91 public static final int DATE = Types.DATE;
92 /**
93 * Represents a Byte[]
94 */
95 public static final int BYTEARRAY = Types.BLOB;
96 /**
97 * Represents a SQL Date
98 */
99 public static final int SQLDATE = Types.DATE;
100 /**
101 * Represents a SQL Time
102 */
103 public static final int SQLTIME = Types.TIME;
104 /*
105 * Represents a SQL Timestamp.
106 * @deprecated
107 */
108 public static final int SQLTIMESTAMP = Types.TIMESTAMP;
109
110 /**
111 * Returns a string name of the datatype represented by this object
112 */
113 public String toString() {
114 String dataTypeName ;
115
116 switch (dataType) {
117 case Types.BINARY:
118 case Types.BLOB:
119 dataTypeName = "blob" ;
120 break ;
121 case Types.LONGVARCHAR:
122 case Types.CLOB:
123 dataTypeName = "text" ;
124 break ;
125 case Types.BIT:
126 case Types.CHAR:
127 dataTypeName = "char" ;
128 dataTypeName += "(" ;
129 dataTypeName += String.valueOf(scale) ;
130 dataTypeName += ")" ;
131 break ;
132 case Types.DATE:
133 dataTypeName = "datetime" ;
134 break ;
135 case Types.DECIMAL:
136 dataTypeName = "decimal" ;
137 dataTypeName += "(" ;
138 dataTypeName += String.valueOf(scale) ;
139 dataTypeName += "," ;
140 dataTypeName += String.valueOf(precision) ;
141 dataTypeName += ")" ;
142 break ;
143 case Types.DOUBLE:
144 dataTypeName = "double" ;
145 break ;
146 case Types.FLOAT:
147 dataTypeName = "float" ;
148 break ;
149 case Types.INTEGER:
150 dataTypeName = "int" ;
151 break ;
152 case Types.JAVA_OBJECT:
153 dataTypeName = "java.lang.Object" ;
154 break ;
155 case Types.LONGVARBINARY:
156 dataTypeName = "long varbinary" ;
157 break ;
158 case Types.NUMERIC:
159 dataTypeName = "numeric" ;
160 dataTypeName += "(" ;
161 dataTypeName += String.valueOf(scale) ;
162 dataTypeName += "," ;
163 dataTypeName += String.valueOf(precision) ;
164 dataTypeName += ")" ;
165 break ;
166 case Types.OTHER:
167 dataTypeName = "java.lang.Object" ;
168 break ;
169 case Types.REAL:
170 dataTypeName = "real" ;
171 break ;
172 case Types.SMALLINT:
173 dataTypeName = "smallint" ;
174 break ;
175 case Types.TINYINT:
176 dataTypeName = "tinyint" ;
177 break ;
178 case Types.TIME:
179 dataTypeName = "datetime" ;
180 break ;
181 case Types.TIMESTAMP:
182 dataTypeName = "datetime" ;
183 break ;
184 case Types.VARCHAR:
185 dataTypeName = "varchar" ;
186 dataTypeName += "(" ;
187 dataTypeName += String.valueOf(scale) ;
188 dataTypeName += ")" ;
189 break ;
190 default :
191 dataTypeName = "NotImplemented, enumerated type=" + dataType ;
192 }
193
194 return dataTypeName ;
195 }
196
197 /**
198 * Constructs a new instance of this class initialised with the integer constant of its type.
199 * @param dataType the int that represents the datatype that this class is being created for.
200 */
201 public SqlType(int dataType) {
202 this.dataType = dataType;
203 }
204
205 /**
206 * Constructs a new instance of this class initialised with the integer constant of its type.
207 * @param dataType the int that represents the datatype that this class is being created for.
208 * @param scale The scale of this data type.
209 * @param precision The precision of this data type.
210 */
211 public SqlType(int dataType, int scale, int precision) {
212 this.dataType = dataType;
213 this.scale = scale;
214 this.precision = precision;
215 }
216
217 /**
218 * Returns the datatype that this class represents
219 * @returns int the datatype that this class is representative of.
220 */
221 public int getDataType() {
222 return dataType;
223 }
224
225 /** the internal storage for this dataType. */
226 private int dataType = 0;
227 /** the internal storage for this data type's scale */
228 private int scale = 0;
229 /** the internal storage for this data type's precision */
230 private int precision = 0;
231
232 /**
233 * Information on the exact CVS version accessible after compilation
234 */
235 public static final String about = "$Revision: 1.3 $";
236
237 }
238