KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > MySQLTypeInfo


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: MySQLTypeInfo.java,v 1.3 2004/03/22 04:58:13 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.sql.ResultSet JavaDoc;
14 import java.sql.Types JavaDoc;
15
16
17 /**
18  * Represents the metadata of a MySQL data type.
19  *
20  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
21  * @version $Revision: 1.3 $
22  */

23
24 class MySQLTypeInfo extends TypeInfo
25 {
26     /**
27      * Constructs a type information object from the current row of the given
28      * result set. The {@link ResultSet} object passed must have been obtained
29      * from a call to DatabaseMetaData.getTypeInfo().
30      *
31      * <p>This method only retrieves the values from the current row; the caller
32      * is required to advance to the next row with {@link ResultSet#next}.
33      *
34      * @param rs The result set returned from DatabaseMetaData.getTypeInfo().
35      */

36
37     public MySQLTypeInfo(ResultSet JavaDoc rs)
38     {
39         super(rs);
40
41         /*
42          * Use the BINARY qualifier to specify case-sensitive treatment of CHAR
43          * and VARCHAR types. The M in (M) will get replaced by the actual
44          * precision in Column.getSQLDefinition().
45          */

46         if (typeName.equalsIgnoreCase("CHAR"))
47         {
48             typeName = "CHAR(M) BINARY";
49             createParams = "";
50         }
51         else if (typeName.equalsIgnoreCase("VARCHAR"))
52         {
53             typeName = "VARCHAR(M) BINARY";
54             createParams = "";
55         }
56     }
57
58
59     public boolean isCompatibleWith(ColumnInfo colInfo)
60     {
61         if (super.isCompatibleWith(colInfo))
62             return true;
63
64         if (isStringType(dataType) && isStringType(colInfo.dataType))
65             return true;
66
67         /* Columns created as BIT come back as TINYINT(1) */
68         if (dataType == Types.BIT)
69             return colInfo.dataType == Types.TINYINT && colInfo.columnSize == 1;
70
71         return false;
72     }
73
74
75     /**
76      * Tests whether or not the given JDBC type is a MySQL "string" type.
77      * <p>
78      * MySQL likes to interchange CHAR and VARCHAR at its own discretion, and
79      * automatically upgrades types to bigger types if necessary.
80      * In addition, we use the BINARY qualifier on CHAR and VARCHAR to get
81      * case-sensitive treatment.
82      * Taken together it means we really can't distinguish one string/binary
83      * type from another so we treat them all as one big happy string family.
84      */

85
86     private static boolean isStringType(int type)
87     {
88         switch (type)
89         {
90             case Types.CHAR:
91             case Types.VARCHAR:
92             case Types.LONGVARCHAR:
93             case Types.BINARY:
94             case Types.VARBINARY:
95             case Types.LONGVARBINARY:
96                 return true;
97
98             default:
99                 return false;
100         }
101     }
102 }
103
Popular Tags