View Javadoc
1   /*
2    * Copyright 2004-2025 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.math.BigDecimal;
19  import java.sql.CallableStatement;
20  import java.sql.PreparedStatement;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  
24  /**
25   * BigDecimal implementation of TypeHandler.
26   */
27  public class BigDecimalTypeHandler extends BaseTypeHandler implements TypeHandler {
28  
29    @Override
30    public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) throws SQLException {
31      ps.setBigDecimal(i, (BigDecimal) parameter);
32    }
33  
34    @Override
35    public Object getResult(ResultSet rs, String columnName) throws SQLException {
36      Object bigdec = rs.getBigDecimal(columnName);
37      if (rs.wasNull()) {
38        return null;
39      }
40      return bigdec;
41    }
42  
43    @Override
44    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
45      Object bigdec = rs.getBigDecimal(columnIndex);
46      if (rs.wasNull()) {
47        return null;
48      }
49      return bigdec;
50    }
51  
52    @Override
53    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
54      Object bigdec = cs.getBigDecimal(columnIndex);
55      if (cs.wasNull()) {
56        return null;
57      }
58      return bigdec;
59    }
60  
61    @Override
62    public Object valueOf(String s) {
63      return java.math.BigDecimal.valueOf(Long.parseLong(s));
64    }
65  
66  }