Skip to content

Commit

Permalink
Fixed the problem that when the jdbc type is Array and Array.free() i…
Browse files Browse the repository at this point in the history
…s called, the log parameter printed by BaseJdbcLogger is null.
  • Loading branch information
smagical committed May 21, 2024
1 parent d10d266 commit ff950a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
39 changes: 19 additions & 20 deletions src/main/java/org/apache/ibatis/logging/jdbc/BaseJdbcLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,17 @@
*/
package org.apache.ibatis.logging.jdbc;

import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.reflection.ArrayUtil;

import java.lang.reflect.Method;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.reflection.ArrayUtil;

/**
* Base class for proxies to do logging.
*
Expand All @@ -43,10 +37,10 @@ public abstract class BaseJdbcLogger {
protected static final Set<String> SET_METHODS;
protected static final Set<String> EXECUTE_METHODS = new HashSet<>();

private final Map<Object, Object> columnMap = new HashMap<>();
private final Map<Object, String> columnMap = new HashMap<>();

private final List<Object> columnNames = new ArrayList<>();
private final List<Object> columnValues = new ArrayList<>();
private final List<String> columnValues = new ArrayList<>();

protected final Log statementLog;
protected final int queryStack;
Expand Down Expand Up @@ -75,9 +69,18 @@ public BaseJdbcLogger(Log log, int queryStack) {
}

protected void setColumn(Object key, Object value) {
columnMap.put(key, value);
String valueString = null;
if (value!=null){
valueString = objectValueString(value) +
"(" + value.getClass().getSimpleName()+")";
} else {
valueString = "null";
}


columnMap.put(key, valueString);
columnNames.add(key);
columnValues.add(value);
columnValues.add(valueString);
}

protected Object getColumn(Object key) {
Expand All @@ -87,11 +90,7 @@ protected Object getColumn(Object key) {
protected String getParameterValueString() {
List<Object> typeList = new ArrayList<>(columnValues.size());
for (Object value : columnValues) {
if (value == null) {
typeList.add("null");
} else {
typeList.add(objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");
}
typeList.add(value);
}
final String parameters = typeList.toString();
return parameters.substring(1, parameters.length() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@
*/
package org.apache.ibatis.logging.jdbc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.sql.Array;

import org.apache.ibatis.logging.Log;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.sql.Array;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class BaseJdbcLoggerTest {

@Mock
Log log;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
Array array;
private BaseJdbcLogger logger;

Expand All @@ -44,15 +45,29 @@ void setUp() {

@Test
void shouldDescribePrimitiveArrayParameter() throws Exception {
logger.setColumn("1", array);
when(array.getArray()).thenReturn(new int[] { 1, 2, 3 });
logger.setColumn("1", array);
assertThat(logger.getParameterValueString()).startsWith("[1, 2, 3]");
}

@Test
void shouldDescribeObjectArrayParameter() throws Exception {
when(array.getArray()).thenReturn(new String[] { "one", "two", "three" });
logger.setColumn("1", array);
assertThat(logger.getParameterValueString()).startsWith("[one, two, three]");
}

@Test
void shouldDescribeObjectArrayCallFreeParameter() throws Exception {

when(array.getArray()).thenReturn(new String[] { "one", "two", "three" });
logger.setColumn("1", array);
doAnswer(e->{
when(array.getArray()).thenReturn(null);
return null;
}).when(array).free();
array.free();
assertThat(logger.getParameterValueString()).startsWith("[one, two, three]");

}
}

0 comments on commit ff950a9

Please sign in to comment.