KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > SQLTypes


1 /*
2
3    Derby - Class org.apache.derby.impl.drda.SQLTypes
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
22 package org.apache.derby.impl.drda;
23
24 import java.sql.Types JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import org.apache.derby.iapi.reference.DRDAConstants;
27 import org.apache.derby.iapi.reference.JDBC30Translation;
28
29
30
31 class SQLTypes {
32
33   // define final statics for the fdoca type codes here!!!
34

35   // hide the default constructor
36
private SQLTypes() {}
37
38
39   /**
40    * Map DB2 SQL Type to JDBC Type
41    *
42    * @param sqlType SQL Type to convert
43    * @param length storage length of type
44    * @param ccsid ccsid of type
45    *
46    * @return Corresponding JDBC Type
47    */

48
49   static protected int mapDB2SqlTypeToJdbcType (int sqlType, long length, int ccsid)
50   {
51     switch (getNonNullableSqlType (sqlType)) { // mask the isNullable bit
52
case DRDAConstants.DB2_SQLTYPE_SMALL:
53       return java.sql.Types.SMALLINT;
54     case DRDAConstants.DB2_SQLTYPE_INTEGER:
55       return java.sql.Types.INTEGER;
56     case DRDAConstants.DB2_SQLTYPE_BIGINT:
57       return java.sql.Types.BIGINT;
58     case DRDAConstants.DB2_SQLTYPE_FLOAT:
59       if (length == 16) // can map to either NUMERIC or DECIMAL!!! @sxg
60
return java.sql.Types.DECIMAL;
61       else if (length == 8) // can map to either DOUBLE or FLOAT!!! @sxg
62
return java.sql.Types.DOUBLE;
63       else if (length == 4)
64         return java.sql.Types.REAL;
65       else
66         return 0;
67         //throw new BugCheckException ("Encountered unexpected float length");
68
case DRDAConstants.DB2_SQLTYPE_DECIMAL: // can map to either NUMERIC or DECIMAL!!! @sxg
69
case DRDAConstants.DB2_SQLTYPE_ZONED: // can map to either NUMERIC or DECIMAL!!! @sxg
70
case DRDAConstants.DB2_SQLTYPE_NUMERIC: // can map to either NUMERIC or DECIMAL!!! @sxg
71
return java.sql.Types.DECIMAL;
72     case DRDAConstants.DB2_SQLTYPE_CHAR: // mixed and single byte
73
if (ccsid == 0xffff || ccsid == 0) // we think UW returns 0, and 390 returns 0xffff, doublecheck !!!
74
return java.sql.Types.BINARY;
75       else
76         return java.sql.Types.CHAR;
77     case DRDAConstants.DB2_SQLTYPE_CSTR: // SBCS null terminated
78
case DRDAConstants.DB2_SQLTYPE_GRAPHIC: // fixed character DBCS
79
return java.sql.Types.CHAR;
80     // use ccsid to distinguish between BINARY and CHAR, VARBINARY and VARCHAR, LONG... !!! -j/p/s
81
case DRDAConstants.DB2_SQLTYPE_VARGRAPH: // variable character DBCS
82
case DRDAConstants.DB2_SQLTYPE_VARCHAR: // variable character SBCS/Mixed
83
if (ccsid == 0xffff || ccsid == 0) // we think UW returns 0, and 390 returns 0xffff, doublecheck !!!
84
return java.sql.Types.VARBINARY;
85       else
86         return java.sql.Types.VARCHAR;
87     case DRDAConstants.DB2_SQLTYPE_LSTR: // pascal string SBCS/Mixed
88
return java.sql.Types.VARCHAR;
89     case DRDAConstants.DB2_SQLTYPE_LONGRAPH: // long varchar DBCS
90
case DRDAConstants.DB2_SQLTYPE_LONG: // long varchar SBCS/Mixed
91
if (ccsid == 0xffff || ccsid == 0) // we think UW returns 0, and 390 returns 0xffff, doublecheck !!!
92
return java.sql.Types.LONGVARBINARY;
93       else
94         return java.sql.Types.LONGVARCHAR;
95     case DRDAConstants.DB2_SQLTYPE_DATE:
96       return java.sql.Types.DATE;
97     case DRDAConstants.DB2_SQLTYPE_TIME:
98       return java.sql.Types.TIME;
99     case DRDAConstants.DB2_SQLTYPE_TIMESTAMP:
100       return java.sql.Types.TIMESTAMP;
101     case DRDAConstants.DB2_SQLTYPE_CLOB: // large object character SBCS/Mixed
102
case DRDAConstants.DB2_SQLTYPE_DBCLOB: // large object character DBCS
103
return java.sql.Types.CLOB;
104     case DRDAConstants.DB2_SQLTYPE_BLOB: // large object bytes
105
case DRDAConstants.DB2_SQLTYPE_BLOB_LOCATOR:
106         case DRDAConstants.DB2_SQLTYPE_CLOB_LOCATOR:
107         case DRDAConstants.DB2_SQLTYPE_DBCLOB_LOCATOR:
108       return java.sql.Types.BLOB;
109     default:
110       //throw new BugCheckException ("Encountered unexpected type code");
111
return 0;
112     }
113   }
114
115
116     /**
117      * Map jdbc type to the DB2 DRDA SQL Types expected by jcc.
118      *@param jdbctype - jdbc Type to convert
119      *@param nullable - whether the type is nullable
120      **/

121
122     
123  /** Map JDBC Type to DB2 SqlType
124   * @param jdbctype JDBC Type from java.sql.Types
125   * @param nullable true if this is a nullable type
126   * @param outlen output parameter with type length
127   *
128   * @return Corresponding DB2 SQL Type (See DRDA Manual FD:OCA Meta
129   * Data Summary, page 245)
130   *
131   * @exception SQLException thrown for unrecognized SQLType
132   */

133
134  static protected int mapJdbcTypeToDB2SqlType (int jdbctype, boolean nullable,
135                                                int[] outlen)
136      throws SQLException JavaDoc
137   {
138       int nullAddVal =0;
139
140       if (nullable)
141           nullAddVal =1;
142       
143       // Call FdocaConstants just to get the length
144
FdocaConstants.mapJdbcTypeToDrdaType(jdbctype,nullable,outlen);
145
146       switch(jdbctype)
147       {
148           case JDBC30Translation.BOOLEAN:
149           case java.sql.Types.BIT:
150           case java.sql.Types.TINYINT:
151           case java.sql.Types.SMALLINT:
152               return DRDAConstants.DB2_SQLTYPE_SMALL + nullAddVal;
153           case java.sql.Types.INTEGER:
154               return DRDAConstants.DB2_SQLTYPE_INTEGER + nullAddVal;
155           case java.sql.Types.BIGINT:
156               return DRDAConstants.DB2_SQLTYPE_BIGINT + nullAddVal;
157           case java.sql.Types.DOUBLE:
158           case java.sql.Types.REAL:
159               return DRDAConstants.DB2_SQLTYPE_FLOAT + nullAddVal;
160           case java.sql.Types.DECIMAL:
161           case java.sql.Types.NUMERIC:
162               return DRDAConstants.DB2_SQLTYPE_DECIMAL + nullAddVal;
163           case java.sql.Types.DATE:
164               return DRDAConstants.DB2_SQLTYPE_DATE + nullAddVal;
165           case java.sql.Types.TIME:
166               return DRDAConstants.DB2_SQLTYPE_TIME + nullAddVal;
167           case java.sql.Types.TIMESTAMP:
168               return DRDAConstants.DB2_SQLTYPE_TIMESTAMP + nullAddVal;
169           case java.sql.Types.CHAR:
170               return DRDAConstants.DB2_SQLTYPE_CHAR + nullAddVal; // null terminated SBCS/Mixed
171
case java.sql.Types.BINARY:
172               return DRDAConstants.DB2_SQLTYPE_CHAR + nullAddVal;
173
174           case java.sql.Types.VARCHAR:
175           case java.sql.Types.VARBINARY:
176               return DRDAConstants.DB2_SQLTYPE_VARCHAR + nullAddVal;
177           case java.sql.Types.LONGVARBINARY:
178               return DRDAConstants.DB2_SQLTYPE_LONG + nullAddVal;
179           case java.sql.Types.JAVA_OBJECT:
180               return DRDAConstants.DB2_SQLTYPE_LONG + nullAddVal;
181           case java.sql.Types.BLOB:
182               return DRDAConstants.DB2_SQLTYPE_BLOB + nullAddVal;
183           case java.sql.Types.CLOB:
184               return DRDAConstants.DB2_SQLTYPE_CLOB + nullAddVal;
185           case java.sql.Types.LONGVARCHAR:
186               return DRDAConstants.DB2_SQLTYPE_LONG + nullAddVal;
187           case java.sql.Types.ARRAY:
188           case java.sql.Types.DISTINCT:
189           case java.sql.Types.NULL:
190           case java.sql.Types.OTHER:
191           case java.sql.Types.REF:
192           case java.sql.Types.STRUCT:
193               throw new SQLException JavaDoc("Jdbc type" + jdbctype + "not Supported yet");
194             default:
195                 throw new SQLException JavaDoc ("unrecognized sql type: " + jdbctype);
196               //throw new BugCheckException ("Encountered unexpected type code");
197

198       }
199   }
200
201     /**
202      * Translate DB2 SQL Type to the non-nullable type.
203      * @param sqlType DB2 SQL Type
204      *
205      * @return The Non-Nullable DB2 SQL Type.
206      */

207    protected static int getNonNullableSqlType (int sqlType)
208   {
209     return sqlType & ~1;
210   }
211
212
213 }
214
Popular Tags