1 /* 2 * Copyright 2009-2021 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 * 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 org.apache.ibatis.type; 17 18 import java.io.Reader; 19 import java.sql.CallableStatement; 20 import java.sql.Clob; 21 import java.sql.PreparedStatement; 22 import java.sql.ResultSet; 23 import java.sql.SQLException; 24 25 /** 26 * The {@link TypeHandler} for {@link Clob}/{@link Reader} using method supported at JDBC 4.0. 27 * @since 3.4.0 28 * @author Kazuki Shimizu 29 */ 30 public class ClobReaderTypeHandler extends BaseTypeHandler<Reader> { 31 32 /** 33 * Set a {@link Reader} into {@link PreparedStatement}. 34 * @see PreparedStatement#setClob(int, Reader) 35 */ 36 @Override 37 public void setNonNullParameter(PreparedStatement ps, int i, Reader parameter, JdbcType jdbcType) 38 throws SQLException { 39 ps.setClob(i, parameter); 40 } 41 42 /** 43 * Get a {@link Reader} that corresponds to a specified column name from {@link ResultSet}. 44 * @see ResultSet#getClob(String) 45 */ 46 @Override 47 public Reader getNullableResult(ResultSet rs, String columnName) 48 throws SQLException { 49 return toReader(rs.getClob(columnName)); 50 } 51 52 /** 53 * Get a {@link Reader} that corresponds to a specified column index from {@link ResultSet}. 54 * @see ResultSet#getClob(int) 55 */ 56 @Override 57 public Reader getNullableResult(ResultSet rs, int columnIndex) 58 throws SQLException { 59 return toReader(rs.getClob(columnIndex)); 60 } 61 62 /** 63 * Get a {@link Reader} that corresponds to a specified column index from {@link CallableStatement}. 64 * @see CallableStatement#getClob(int) 65 */ 66 @Override 67 public Reader getNullableResult(CallableStatement cs, int columnIndex) 68 throws SQLException { 69 return toReader(cs.getClob(columnIndex)); 70 } 71 72 private Reader toReader(Clob clob) throws SQLException { 73 if (clob == null) { 74 return null; 75 } else { 76 return clob.getCharacterStream(); 77 } 78 } 79 80 }