View Javadoc
1   /*
2    * Copyright 2004-2022 the original author or authors.
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    *    https://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;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  
23  /**
24   * Implementation of TypeHandler for dealing with unknown types.
25   */
26  public class UnknownTypeHandler extends BaseTypeHandler implements TypeHandler {
27  
28    /** The factory. */
29    private TypeHandlerFactory factory;
30  
31    /**
32     * Constructor to create via a factory.
33     *
34     * @param factory
35     *          - the factory to associate this with
36     */
37    public UnknownTypeHandler(TypeHandlerFactory factory) {
38      this.factory = factory;
39    }
40  
41    public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) throws SQLException {
42      Class searchClass = parameter.getClass();
43      if (searchClass == null) {
44        searchClass = parameter.getClass();
45      }
46      TypeHandler handler = factory.getTypeHandler(searchClass, jdbcType);
47      handler.setParameter(ps, i, parameter, jdbcType);
48    }
49  
50    public Object getResult(ResultSet rs, String columnName) throws SQLException {
51      Object object = rs.getObject(columnName);
52      if (rs.wasNull()) {
53        return null;
54      } else {
55        return object;
56      }
57    }
58  
59    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
60      Object object = rs.getObject(columnIndex);
61      if (rs.wasNull()) {
62        return null;
63      } else {
64        return object;
65      }
66    }
67  
68    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
69      Object object = cs.getObject(columnIndex);
70      if (cs.wasNull()) {
71        return null;
72      } else {
73        return object;
74      }
75    }
76  
77    public Object valueOf(String s) {
78      return s;
79    }
80  
81    @Override
82    public boolean equals(Object object, String string) {
83      if (object == null || string == null) {
84        return object == string;
85      } else {
86        TypeHandler handler = factory.getTypeHandler(object.getClass());
87        Object castedObject = handler.valueOf(string);
88        return object.equals(castedObject);
89      }
90    }
91  
92  }