Skip to content

Commit

Permalink
Merge pull request #3298 from ZLATAN628/fix-3287
Browse files Browse the repository at this point in the history
Update NClobTypeHandler to use methods for national character set
  • Loading branch information
harawata authored Nov 18, 2024
2 parents 6a2676a + 97e04b3 commit 372319a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
22 changes: 11 additions & 11 deletions src/main/java/org/apache/ibatis/type/NClobTypeHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@

import java.io.StringReader;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -31,29 +31,29 @@ public class NClobTypeHandler extends BaseTypeHandler<String> {
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
throws SQLException {
StringReader reader = new StringReader(parameter);
ps.setCharacterStream(i, reader, parameter.length());
ps.setNCharacterStream(i, reader, parameter.length());
}

@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
Clob clob = rs.getClob(columnName);
return toString(clob);
NClob nclob = rs.getNClob(columnName);
return toString(nclob);
}

@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Clob clob = rs.getClob(columnIndex);
return toString(clob);
NClob nclob = rs.getNClob(columnIndex);
return toString(nclob);
}

@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Clob clob = cs.getClob(columnIndex);
return toString(clob);
NClob nclob = cs.getNClob(columnIndex);
return toString(nclob);
}

private String toString(Clob clob) throws SQLException {
return clob == null ? null : clob.getSubString(1, (int) clob.length());
private String toString(NClob nclob) throws SQLException {
return nclob == null ? null : nclob.getSubString(1, (int) nclob.length());
}

}
32 changes: 16 additions & 16 deletions src/test/java/org/apache/ibatis/type/NClobTypeHandlerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
import static org.mockito.Mockito.when;

import java.io.Reader;
import java.sql.Clob;
import java.sql.NClob;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
Expand All @@ -32,60 +32,60 @@ class NClobTypeHandlerTest extends BaseTypeHandlerTest {
private static final TypeHandler<String> TYPE_HANDLER = new NClobTypeHandler();

@Mock
protected Clob clob;
protected NClob nclob;

@Override
@Test
public void shouldSetParameter() throws Exception {
TYPE_HANDLER.setParameter(ps, 1, "Hello", null);
verify(ps).setCharacterStream(ArgumentMatchers.eq(1), ArgumentMatchers.any(Reader.class), ArgumentMatchers.eq(5));
verify(ps).setNCharacterStream(ArgumentMatchers.eq(1), ArgumentMatchers.any(Reader.class), ArgumentMatchers.eq(5L));
}

@Override
@Test
public void shouldGetResultFromResultSetByName() throws Exception {
when(rs.getClob("column")).thenReturn(clob);
when(clob.length()).thenReturn(3L);
when(clob.getSubString(1, 3)).thenReturn("Hello");
when(rs.getNClob("column")).thenReturn(nclob);
when(nclob.length()).thenReturn(3L);
when(nclob.getSubString(1, 3)).thenReturn("Hello");
assertEquals("Hello", TYPE_HANDLER.getResult(rs, "column"));
}

@Override
@Test
public void shouldGetResultNullFromResultSetByName() throws Exception {
when(rs.getClob("column")).thenReturn(null);
when(rs.getNClob("column")).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, "column"));
}

@Override
@Test
public void shouldGetResultFromResultSetByPosition() throws Exception {
when(rs.getClob(1)).thenReturn(clob);
when(clob.length()).thenReturn(3L);
when(clob.getSubString(1, 3)).thenReturn("Hello");
when(rs.getNClob(1)).thenReturn(nclob);
when(nclob.length()).thenReturn(3L);
when(nclob.getSubString(1, 3)).thenReturn("Hello");
assertEquals("Hello", TYPE_HANDLER.getResult(rs, 1));
}

@Override
@Test
public void shouldGetResultNullFromResultSetByPosition() throws Exception {
when(rs.getClob(1)).thenReturn(null);
when(rs.getNClob(1)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, 1));
}

@Override
@Test
public void shouldGetResultFromCallableStatement() throws Exception {
when(cs.getClob(1)).thenReturn(clob);
when(clob.length()).thenReturn(3L);
when(clob.getSubString(1, 3)).thenReturn("Hello");
when(cs.getNClob(1)).thenReturn(nclob);
when(nclob.length()).thenReturn(3L);
when(nclob.getSubString(1, 3)).thenReturn("Hello");
assertEquals("Hello", TYPE_HANDLER.getResult(cs, 1));
}

@Override
@Test
public void shouldGetResultNullFromCallableStatement() throws Exception {
when(cs.getClob(1)).thenReturn(null);
when(cs.getNClob(1)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(cs, 1));
}

Expand Down

0 comments on commit 372319a

Please sign in to comment.