KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > ByteType


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

19
20 package org.apache.cayenne.access.types;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25
26 /**
27  * Handles <code>java.lang.Byte</code> type mapping. Can be configured to recast
28  * java.lang.Byte to java.lang.Integer when binding values to PreparedStatement. This is a
29  * workaround for bugs in certain drivers. Drivers that are proven to have issues with
30  * byte values are Sybase and Oracle (Mac OS X only).
31  *
32  * @author Andrus Adamchik
33  * @since 1.0.3
34  */

35 public class ByteType extends AbstractType {
36
37     protected boolean widenBytes;
38
39     public ByteType(boolean widenBytes) {
40         this.widenBytes = widenBytes;
41     }
42
43     public String JavaDoc getClassName() {
44         return Byte JavaDoc.class.getName();
45     }
46
47     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
48         byte b = rs.getByte(index);
49         return (rs.wasNull()) ? null : new Byte JavaDoc(b);
50     }
51
52     public Object JavaDoc materializeObject(CallableStatement JavaDoc st, int index, int type)
53             throws Exception JavaDoc {
54         byte b = st.getByte(index);
55         return (st.wasNull()) ? null : new Byte JavaDoc(b);
56     }
57
58     public void setJdbcObject(
59             PreparedStatement JavaDoc st,
60             Object JavaDoc val,
61             int pos,
62             int type,
63             int precision) throws Exception JavaDoc {
64
65         if (widenBytes && (val instanceof Byte JavaDoc)) {
66             val = new Integer JavaDoc(((Byte JavaDoc) val).intValue());
67         }
68
69         super.setJdbcObject(st, val, pos, type, precision);
70     }
71 }
72
Popular Tags