KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > MysqlDefs


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

19 package com.mysql.jdbc;
20
21 import java.sql.Types JavaDoc;
22
23
24 /**
25  * MysqlDefs contains many values that are needed for communication
26  * with the MySQL server.
27  *
28  * @author Mark Matthews
29  * @version $Id: MysqlDefs.java,v 1.6.2.6 2003/11/20 19:43:45 mmatthew Exp $
30  */

31 final class MysqlDefs {
32     //
33
// Constants defined from mysql
34
//
35
// DB Operations
36
static final int SLEEP = 0;
37     static final int QUIT = 1;
38     static final int INIT_DB = 2;
39     static final int QUERY = 3;
40     static final int FIELD_LIST = 4;
41     static final int CREATE_DB = 5;
42     static final int DROP_DB = 6;
43     static final int RELOAD = 7;
44     static final int SHUTDOWN = 8;
45     static final int STATISTICS = 9;
46     static final int PROCESS_INFO = 10;
47     static final int CONNECT = 11;
48     static final int PROCESS_KILL = 12;
49     static final int DEBUG = 13;
50     static final int PING = 14;
51     static final int TIME = 15;
52     static final int DELAYED_INSERT = 16;
53     static final int CHANGE_USER = 17;
54     static final int COM_BINLOG_DUMP = 18;
55     static final int COM_TABLE_DUMP = 19;
56     static final int COM_CONNECT_OUT = 20;
57     static final int COM_REGISTER_SLAVE = 21;
58     static final int COM_PREPARE = 22;
59     static final int COM_EXECUTE = 23;
60     static final int COM_LONG_DATA = 24;
61
62     // Data Types
63
static final int FIELD_TYPE_DECIMAL = 0;
64     static final int FIELD_TYPE_TINY = 1;
65     static final int FIELD_TYPE_SHORT = 2;
66     static final int FIELD_TYPE_LONG = 3;
67     static final int FIELD_TYPE_FLOAT = 4;
68     static final int FIELD_TYPE_DOUBLE = 5;
69     static final int FIELD_TYPE_NULL = 6;
70     static final int FIELD_TYPE_TIMESTAMP = 7;
71     static final int FIELD_TYPE_LONGLONG = 8;
72     static final int FIELD_TYPE_INT24 = 9;
73     static final int FIELD_TYPE_DATE = 10;
74     static final int FIELD_TYPE_TIME = 11;
75     static final int FIELD_TYPE_DATETIME = 12;
76
77     // Newer data types
78
static final int FIELD_TYPE_YEAR = 13;
79     static final int FIELD_TYPE_NEWDATE = 14;
80     static final int FIELD_TYPE_ENUM = 247;
81     static final int FIELD_TYPE_SET = 248;
82
83     // Older data types
84
static final int FIELD_TYPE_TINY_BLOB = 249;
85     static final int FIELD_TYPE_MEDIUM_BLOB = 250;
86     static final int FIELD_TYPE_LONG_BLOB = 251;
87     static final int FIELD_TYPE_BLOB = 252;
88     static final int FIELD_TYPE_VAR_STRING = 253;
89     static final int FIELD_TYPE_STRING = 254;
90     static final int ER_WARNING_NOT_COMPLETE_ROLLBACK = 1196;
91
92     // Limitations
93
static final int MAX_ROWS = 50000000; // From the MySQL FAQ
94

95     static final String JavaDoc MIN_DOUBLE_VAL_STRING = "-1.7976931348623157E+308";
96     static final String JavaDoc MAX_DOUBLE_VAL_STRING = "1.7976931348623157E+308";
97     static final String JavaDoc MIN_FLOAT_VAL_STRING = "-3.402823466E+38";
98     static final String JavaDoc MAX_FLOAT_VAL_STRING = "3.402823466E+38";
99     static final String JavaDoc NAN_VAL_STRING = "0";
100             
101
102     /**
103      * Maps the given MySQL type to the correct JDBC type.
104      */

105     static int mysqlToJavaType(int mysqlType) {
106         int jdbcType;
107
108         switch (mysqlType) {
109         case MysqlDefs.FIELD_TYPE_DECIMAL:
110             jdbcType = Types.DECIMAL;
111
112             break;
113
114         case MysqlDefs.FIELD_TYPE_TINY:
115             jdbcType = Types.TINYINT;
116
117             break;
118
119         case MysqlDefs.FIELD_TYPE_SHORT:
120             jdbcType = Types.SMALLINT;
121
122             break;
123
124         case MysqlDefs.FIELD_TYPE_LONG:
125             jdbcType = Types.INTEGER;
126
127             break;
128
129         case MysqlDefs.FIELD_TYPE_FLOAT:
130             jdbcType = Types.REAL;
131
132             break;
133
134         case MysqlDefs.FIELD_TYPE_DOUBLE:
135             jdbcType = Types.DOUBLE;
136
137             break;
138
139         case MysqlDefs.FIELD_TYPE_NULL:
140             jdbcType = Types.NULL;
141
142             break;
143
144         case MysqlDefs.FIELD_TYPE_TIMESTAMP:
145             jdbcType = Types.TIMESTAMP;
146
147             break;
148
149         case MysqlDefs.FIELD_TYPE_LONGLONG:
150             jdbcType = Types.BIGINT;
151
152             break;
153
154         case MysqlDefs.FIELD_TYPE_INT24:
155             jdbcType = Types.INTEGER;
156
157             break;
158
159         case MysqlDefs.FIELD_TYPE_DATE:
160             jdbcType = Types.DATE;
161
162             break;
163
164         case MysqlDefs.FIELD_TYPE_TIME:
165             jdbcType = Types.TIME;
166
167             break;
168
169         case MysqlDefs.FIELD_TYPE_DATETIME:
170             jdbcType = Types.TIMESTAMP;
171
172             break;
173
174         case MysqlDefs.FIELD_TYPE_YEAR:
175             jdbcType = Types.DATE;
176
177             break;
178
179         case MysqlDefs.FIELD_TYPE_NEWDATE:
180             jdbcType = Types.DATE;
181
182             break;
183
184         case MysqlDefs.FIELD_TYPE_ENUM:
185             jdbcType = Types.CHAR;
186
187             break;
188
189         case MysqlDefs.FIELD_TYPE_SET:
190             jdbcType = Types.CHAR;
191
192             break;
193
194         case MysqlDefs.FIELD_TYPE_TINY_BLOB:
195             jdbcType = Types.VARBINARY;
196
197             break;
198
199         case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:
200             jdbcType = Types.LONGVARBINARY;
201
202             break;
203
204         case MysqlDefs.FIELD_TYPE_LONG_BLOB:
205             jdbcType = Types.LONGVARBINARY;
206
207             break;
208
209         case MysqlDefs.FIELD_TYPE_BLOB:
210             jdbcType = Types.LONGVARBINARY;
211
212             break;
213
214         case MysqlDefs.FIELD_TYPE_VAR_STRING:
215             jdbcType = Types.VARCHAR;
216
217             break;
218
219         case MysqlDefs.FIELD_TYPE_STRING:
220             jdbcType = Types.CHAR;
221
222             break;
223
224         default:
225             jdbcType = Types.VARCHAR;
226         }
227
228         return jdbcType;
229     }
230
231     /**
232      * Maps the given MySQL type to the correct JDBC type.
233      */

234     static int mysqlToJavaType(String JavaDoc mysqlType) {
235         if (mysqlType.equalsIgnoreCase("TINYINT")) {
236             return java.sql.Types.TINYINT;
237         } else if (mysqlType.equalsIgnoreCase("SMALLINT")) {
238             return java.sql.Types.SMALLINT;
239         } else if (mysqlType.equalsIgnoreCase("MEDIUMINT")) {
240             return java.sql.Types.SMALLINT;
241         } else if (mysqlType.equalsIgnoreCase("INT")) {
242             return java.sql.Types.INTEGER;
243         } else if (mysqlType.equalsIgnoreCase("INTEGER")) {
244             return java.sql.Types.INTEGER;
245         } else if (mysqlType.equalsIgnoreCase("BIGINT")) {
246             return java.sql.Types.BIGINT;
247         } else if (mysqlType.equalsIgnoreCase("INT24")) {
248             return java.sql.Types.BIGINT;
249         } else if (mysqlType.equalsIgnoreCase("REAL")) {
250             return java.sql.Types.REAL;
251         } else if (mysqlType.equalsIgnoreCase("FLOAT")) {
252             return java.sql.Types.FLOAT;
253         } else if (mysqlType.equalsIgnoreCase("DECIMAL")) {
254             return java.sql.Types.DECIMAL;
255         } else if (mysqlType.equalsIgnoreCase("NUMERIC")) {
256             return java.sql.Types.NUMERIC;
257         } else if (mysqlType.equalsIgnoreCase("DOUBLE")) {
258             return java.sql.Types.DOUBLE;
259         } else if (mysqlType.equalsIgnoreCase("CHAR")) {
260             return java.sql.Types.CHAR;
261         } else if (mysqlType.equalsIgnoreCase("VARCHAR")) {
262             return java.sql.Types.VARCHAR;
263         } else if (mysqlType.equalsIgnoreCase("DATE")) {
264             return java.sql.Types.DATE;
265         } else if (mysqlType.equalsIgnoreCase("TIME")) {
266             return java.sql.Types.TIME;
267         } else if (mysqlType.equalsIgnoreCase("YEAR")) {
268             return java.sql.Types.DATE;
269         } else if (mysqlType.equalsIgnoreCase("TIMESTAMP")) {
270             return java.sql.Types.TIMESTAMP;
271         } else if (mysqlType.equalsIgnoreCase("DATETIME")) {
272             return java.sql.Types.TIMESTAMP;
273         } else if (mysqlType.equalsIgnoreCase("TINYBLOB")) {
274             return java.sql.Types.BINARY;
275         } else if (mysqlType.equalsIgnoreCase("BLOB")) {
276             return java.sql.Types.VARBINARY;
277         } else if (mysqlType.equalsIgnoreCase("MEDIUMBLOB")) {
278             return java.sql.Types.VARBINARY;
279         } else if (mysqlType.equalsIgnoreCase("LONGBLOB")) {
280             return java.sql.Types.LONGVARBINARY;
281         } else if (mysqlType.equalsIgnoreCase("TINYTEXT")) {
282             return java.sql.Types.VARCHAR;
283         } else if (mysqlType.equalsIgnoreCase("TEXT")) {
284             return java.sql.Types.LONGVARCHAR;
285         } else if (mysqlType.equalsIgnoreCase("MEDIUMTEXT")) {
286             return java.sql.Types.LONGVARCHAR;
287         } else if (mysqlType.equalsIgnoreCase("LONGTEXT")) {
288             return java.sql.Types.LONGVARCHAR;
289         } else if (mysqlType.equalsIgnoreCase("ENUM")) {
290             return java.sql.Types.CHAR;
291         } else if (mysqlType.equalsIgnoreCase("SET")) {
292             return java.sql.Types.CHAR;
293         }
294
295         // Punt
296
return java.sql.Types.OTHER;
297     }
298 }
299
Popular Tags