KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > Types


1 /*
2
3    Derby - Class org.apache.derby.client.am.Types
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21 package org.apache.derby.client.am;
22
23 import org.apache.derby.iapi.reference.DRDAConstants;
24 import org.apache.derby.iapi.reference.JDBC30Translation;
25 import org.apache.derby.iapi.reference.JDBC40Translation;
26
27 // This enumeration of types represents the typing scheme used by our jdbc driver.
28
// Once this is finished, we need to review our switches to make sure they are exhaustive
29

30 public class Types {
31     // -------------------------------- Driver types -------------------------------------------------
32

33     // Not currently supported as a DERBY column type. Mapped to SMALLINT.
34
// public final static int BIT = java.sql.Types.BIT; // -7;
35

36     // Not currently supported as a DERBY column type. Mapped to SMALLINT.
37
//public final static int TINYINT = java.sql.Types.TINYINT; // -6;
38

39     public final static int BOOLEAN = JDBC30Translation.BOOLEAN; // 16;
40

41     public final static int SMALLINT = java.sql.Types.SMALLINT; // 5;
42

43     public final static int INTEGER = java.sql.Types.INTEGER; // 4;
44

45     public final static int BIGINT = java.sql.Types.BIGINT; // -5;
46

47     // We type using DOUBLE
48
//public final static int FLOAT = java.sql.Types.FLOAT; // 6;
49

50     public final static int REAL = java.sql.Types.REAL; // 7;
51

52     public final static int DOUBLE = java.sql.Types.DOUBLE; // 8;
53

54     // We type using DECIMAL
55
//public final static int NUMERIC = java.sql.Types.NUMERIC; // 2;
56

57     public final static int DECIMAL = java.sql.Types.DECIMAL; // 3;
58

59     public final static int CHAR = java.sql.Types.CHAR; // 1;
60

61     public final static int VARCHAR = java.sql.Types.VARCHAR; // 12;
62

63     public final static int LONGVARCHAR = java.sql.Types.LONGVARCHAR; // -1;
64

65     public final static int DATE = java.sql.Types.DATE; // 91;
66

67     public final static int TIME = java.sql.Types.TIME; // 92;
68

69     public final static int TIMESTAMP = java.sql.Types.TIMESTAMP; // 93;
70

71     public final static int BINARY = java.sql.Types.BINARY; // -2;
72

73     public final static int VARBINARY = java.sql.Types.VARBINARY; // -3;
74

75     public final static int LONGVARBINARY = java.sql.Types.LONGVARBINARY; // -4;
76

77     public final static int BLOB = java.sql.Types.BLOB; // 2004;
78

79     public final static int CLOB = java.sql.Types.CLOB; // 2005;
80

81     // hide the default constructor
82
private Types() {
83    }
84     
85     static public String JavaDoc getTypeString(int type)
86     {
87         switch (type )
88         {
89             case BIGINT: return "BIGINT";
90             case BINARY: return "BINARY";
91             case BLOB: return "BLOB";
92             case BOOLEAN: return "BOOLEAN";
93             case CHAR: return "CHAR";
94             case CLOB: return "CLOB";
95             case DATE: return "DATE";
96             case DECIMAL: return "DECIMAL";
97             case DOUBLE: return "DOUBLE";
98             case INTEGER: return "INTEGER";
99             case LONGVARBINARY: return "LONGVARBINARY";
100             case LONGVARCHAR: return "LONGVARCHAR";
101             case REAL: return "REAL";
102             case SMALLINT: return "SMALLINT";
103             case TIME: return "TIME";
104             case TIMESTAMP: return "TIMESTAMP";
105             case VARBINARY: return "VARBINARY";
106             case VARCHAR: return "VARCHAR";
107             // Types we don't support:
108
case java.sql.Types.ARRAY: return "ARRAY";
109             case java.sql.Types.DATALINK: return "DATALINK";
110             case JDBC40Translation.NCHAR: return "NATIONAL CHAR";
111             case JDBC40Translation.NCLOB: return "NCLOB";
112             case JDBC40Translation.NVARCHAR: return "NATIONAL CHAR VARYING";
113             case JDBC40Translation.LONGNVARCHAR: return "LONG NVARCHAR";
114             case java.sql.Types.REF: return "REF";
115             case JDBC40Translation.ROWID: return "ROWID";
116             case JDBC40Translation.SQLXML: return "SQLXML";
117             case java.sql.Types.STRUCT: return "STRUCT";
118             // Unknown type:
119
default: return "<UNKNOWN>";
120         }
121     }
122
123     static public int mapDERBYTypeToDriverType(boolean isDescribed, int sqlType, long length, int ccsid) {
124         switch (Utils.getNonNullableSqlType(sqlType)) { // mask the isNullable bit
125
case DRDAConstants.DB2_SQLTYPE_SMALL:
126             return SMALLINT;
127         case DRDAConstants.DB2_SQLTYPE_INTEGER:
128             return INTEGER;
129         case DRDAConstants.DB2_SQLTYPE_BIGINT:
130             return BIGINT;
131         case DRDAConstants.DB2_SQLTYPE_FLOAT:
132             if (length == 16) // can map to either NUMERIC or DECIMAL
133
{
134                 return DECIMAL;
135             } else if (length == 8) // can map to either DOUBLE or FLOAT
136
{
137                 return DOUBLE;
138             } else if (length == 4) {
139                 return REAL;
140             } else {
141                 return 0;
142             }
143         case DRDAConstants.DB2_SQLTYPE_DECIMAL: // can map to either NUMERIC or DECIMAL
144
case DRDAConstants.DB2_SQLTYPE_NUMERIC: // can map to either NUMERIC or DECIMAL
145
return DECIMAL;
146         case DRDAConstants.DB2_SQLTYPE_CHAR: // mixed and single byte
147
if (isDescribed && (ccsid == 0xffff || ccsid == 0)) {
148                 return BINARY;
149             } else {
150                 return CHAR;
151             }
152         case DRDAConstants.DB2_SQLTYPE_CSTR: // null terminated SBCS/Mixed
153
return CHAR;
154             // use ccsid to distinguish between BINARY and CHAR, VARBINARY and VARCHAR, LONG...
155
case DRDAConstants.DB2_SQLTYPE_VARCHAR: // variable character SBCS/Mixed
156
if (isDescribed && (ccsid == 0xffff || ccsid == 0)) {
157                 return VARBINARY;
158             } else {
159                 return VARCHAR;
160             }
161         case DRDAConstants.DB2_SQLTYPE_LONG: // long varchar SBCS/Mixed
162
if (isDescribed && (ccsid == 0xffff || ccsid == 0)) {
163                 return LONGVARBINARY;
164             } else {
165                 return LONGVARCHAR;
166             }
167         case DRDAConstants.DB2_SQLTYPE_DATE:
168             return DATE;
169         case DRDAConstants.DB2_SQLTYPE_TIME:
170             return TIME;
171         case DRDAConstants.DB2_SQLTYPE_TIMESTAMP:
172             return TIMESTAMP;
173         case DRDAConstants.DB2_SQLTYPE_CLOB: // large object character SBCS/Mixed
174
return Types.CLOB;
175         case DRDAConstants.DB2_SQLTYPE_BLOB: // large object bytes
176
return java.sql.Types.BLOB;
177         default:
178             return 0;
179         }
180     }
181 }
182
Popular Tags