KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > type > UnknownTypeHandler


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.type;
17
18 import java.sql.CallableStatement JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 /**
24  * Implementation of TypeHandler for dealing with unknown types
25  */

26 public class UnknownTypeHandler extends BaseTypeHandler implements TypeHandler {
27
28   private TypeHandlerFactory factory;
29
30   /**
31    * Constructor to create via a factory
32    *
33    * @param factory - the factory to associate this with
34    */

35   public UnknownTypeHandler(TypeHandlerFactory factory) {
36     this.factory = factory;
37   }
38
39   public void setParameter(PreparedStatement JavaDoc ps, int i, Object JavaDoc parameter, String JavaDoc jdbcType)
40       throws SQLException JavaDoc {
41     TypeHandler handler = factory.getTypeHandler(parameter.getClass(), jdbcType);
42     handler.setParameter(ps, i, parameter, jdbcType);
43   }
44
45   public Object JavaDoc getResult(ResultSet JavaDoc rs, String JavaDoc columnName)
46       throws SQLException JavaDoc {
47     Object JavaDoc object = rs.getObject(columnName);
48     if (rs.wasNull()) {
49       return null;
50     } else {
51       return object;
52     }
53   }
54
55   public Object JavaDoc getResult(ResultSet JavaDoc rs, int columnIndex)
56       throws SQLException JavaDoc {
57     Object JavaDoc object = rs.getObject(columnIndex);
58     if (rs.wasNull()) {
59       return null;
60     } else {
61       return object;
62     }
63   }
64
65   public Object JavaDoc getResult(CallableStatement JavaDoc cs, int columnIndex)
66       throws SQLException JavaDoc {
67     Object JavaDoc object = cs.getObject(columnIndex);
68     if (cs.wasNull()) {
69       return null;
70     } else {
71       return object;
72     }
73   }
74
75   public Object JavaDoc valueOf(String JavaDoc s) {
76     return s;
77   }
78
79   public boolean equals(Object JavaDoc object, String JavaDoc string) {
80     if (object == null || string == null) {
81       return object == string;
82     } else {
83       TypeHandler handler = factory.getTypeHandler(object.getClass());
84       Object JavaDoc castedObject = handler.valueOf(string);
85       return object.equals(castedObject);
86     }
87   }
88
89 }
90
Popular Tags