KickJava   Java API By Example, From Geeks To Geeks.

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


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.Short</code> type mapping. Can be configured to recast
28  * java.lang.Short to java.lang.Integer when binding values to PreparedStatement. This is
29  * a workaround for bugs in certain drivers. Drivers that are proven to have issues with
30  * short values are Sybase and Oracle (Mac OS X only).
31  *
32  * @author Andrus Adamchik
33  * @since 1.0.2
34  */

35 public class ShortType extends AbstractType {
36
37     protected boolean widenShorts;
38
39     public ShortType(boolean widenShorts) {
40         this.widenShorts = widenShorts;
41     }
42
43     public String JavaDoc getClassName() {
44         return Short JavaDoc.class.getName();
45     }
46
47     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
48         short s = rs.getShort(index);
49         return (rs.wasNull()) ? null : new Short JavaDoc(s);
50     }
51
52     public Object JavaDoc materializeObject(CallableStatement JavaDoc st, int index, int type)
53             throws Exception JavaDoc {
54         short s = st.getShort(index);
55         return (st.wasNull()) ? null : new Short JavaDoc(s);
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 (widenShorts && (val instanceof Short JavaDoc)) {
66             val = new Integer JavaDoc(((Short JavaDoc) val).intValue());
67         }
68
69         super.setJdbcObject(st, val, pos, type, precision);
70     }
71 }
72
Popular Tags