1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivetranse.transaction.ibatis; |
16 | |
17 | import java.sql.SQLException; |
18 | import java.sql.Types; |
19 | |
20 | import com.ibatis.sqlmap.client.extensions.ParameterSetter; |
21 | import com.ibatis.sqlmap.client.extensions.ResultGetter; |
22 | import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback; |
23 | |
24 | public abstract class AbstractEnumTypeHandler implements TypeHandlerCallback |
25 | { |
26 | protected AbstractEnumTypeHandler(Class enumClass) |
27 | { |
28 | _enums = enumClass.getEnumConstants(); |
29 | } |
30 | |
31 | public Object getResult(ResultGetter getter) |
32 | throws SQLException |
33 | { |
34 | int value = getter.getInt(); |
35 | if (getter.wasNull()) |
36 | { |
37 | return null; |
38 | } |
39 | return _enums[value]; |
40 | } |
41 | |
42 | public void setParameter(ParameterSetter setter, Object parameter) |
43 | throws SQLException |
44 | { |
45 | if (parameter == null) |
46 | { |
47 | setter.setNull(Types.INTEGER); |
48 | } |
49 | else |
50 | { |
51 | Enum value = (Enum) parameter; |
52 | setter.setInt(value.ordinal()); |
53 | } |
54 | } |
55 | |
56 | public Object valueOf(String s) |
57 | { |
58 | return s; |
59 | } |
60 | |
61 | final private Object[] _enums; |
62 | } |