Skip to content

Commit ff950a9

Browse files
committed
Fixed the problem that when the jdbc type is Array and Array.free() is called, the log parameter printed by BaseJdbcLogger is null.
1 parent d10d266 commit ff950a9

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/main/java/org/apache/ibatis/logging/jdbc/BaseJdbcLogger.java

+19-20
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@
1515
*/
1616
package org.apache.ibatis.logging.jdbc;
1717

18+
import org.apache.ibatis.builder.SqlSourceBuilder;
19+
import org.apache.ibatis.logging.Log;
20+
import org.apache.ibatis.reflection.ArrayUtil;
21+
1822
import java.lang.reflect.Method;
1923
import java.sql.Array;
2024
import java.sql.PreparedStatement;
2125
import java.sql.SQLException;
22-
import java.util.ArrayList;
23-
import java.util.Arrays;
24-
import java.util.HashMap;
25-
import java.util.HashSet;
26-
import java.util.List;
27-
import java.util.Map;
28-
import java.util.Set;
26+
import java.util.*;
2927
import java.util.stream.Collectors;
3028

31-
import org.apache.ibatis.builder.SqlSourceBuilder;
32-
import org.apache.ibatis.logging.Log;
33-
import org.apache.ibatis.reflection.ArrayUtil;
34-
3529
/**
3630
* Base class for proxies to do logging.
3731
*
@@ -43,10 +37,10 @@ public abstract class BaseJdbcLogger {
4337
protected static final Set<String> SET_METHODS;
4438
protected static final Set<String> EXECUTE_METHODS = new HashSet<>();
4539

46-
private final Map<Object, Object> columnMap = new HashMap<>();
40+
private final Map<Object, String> columnMap = new HashMap<>();
4741

4842
private final List<Object> columnNames = new ArrayList<>();
49-
private final List<Object> columnValues = new ArrayList<>();
43+
private final List<String> columnValues = new ArrayList<>();
5044

5145
protected final Log statementLog;
5246
protected final int queryStack;
@@ -75,9 +69,18 @@ public BaseJdbcLogger(Log log, int queryStack) {
7569
}
7670

7771
protected void setColumn(Object key, Object value) {
78-
columnMap.put(key, value);
72+
String valueString = null;
73+
if (value!=null){
74+
valueString = objectValueString(value) +
75+
"(" + value.getClass().getSimpleName()+")";
76+
} else {
77+
valueString = "null";
78+
}
79+
80+
81+
columnMap.put(key, valueString);
7982
columnNames.add(key);
80-
columnValues.add(value);
83+
columnValues.add(valueString);
8184
}
8285

8386
protected Object getColumn(Object key) {
@@ -87,11 +90,7 @@ protected Object getColumn(Object key) {
8790
protected String getParameterValueString() {
8891
List<Object> typeList = new ArrayList<>(columnValues.size());
8992
for (Object value : columnValues) {
90-
if (value == null) {
91-
typeList.add("null");
92-
} else {
93-
typeList.add(objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");
94-
}
93+
typeList.add(value);
9594
}
9695
final String parameters = typeList.toString();
9796
return parameters.substring(1, parameters.length() - 1);

src/test/java/org/apache/ibatis/logging/jdbc/BaseJdbcLoggerTest.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@
1515
*/
1616
package org.apache.ibatis.logging.jdbc;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.mockito.Mockito.when;
20-
21-
import java.sql.Array;
22-
2318
import org.apache.ibatis.logging.Log;
2419
import org.junit.jupiter.api.BeforeEach;
2520
import org.junit.jupiter.api.Test;
2621
import org.junit.jupiter.api.extension.ExtendWith;
2722
import org.mockito.Mock;
2823
import org.mockito.junit.jupiter.MockitoExtension;
2924

25+
import java.sql.Array;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.mockito.Mockito.doAnswer;
29+
import static org.mockito.Mockito.when;
30+
3031
@ExtendWith(MockitoExtension.class)
3132
class BaseJdbcLoggerTest {
3233

3334
@Mock
3435
Log log;
35-
@Mock
36+
@Mock(strictness = Mock.Strictness.LENIENT)
3637
Array array;
3738
private BaseJdbcLogger logger;
3839

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

4546
@Test
4647
void shouldDescribePrimitiveArrayParameter() throws Exception {
47-
logger.setColumn("1", array);
4848
when(array.getArray()).thenReturn(new int[] { 1, 2, 3 });
49+
logger.setColumn("1", array);
4950
assertThat(logger.getParameterValueString()).startsWith("[1, 2, 3]");
5051
}
5152

5253
@Test
5354
void shouldDescribeObjectArrayParameter() throws Exception {
55+
when(array.getArray()).thenReturn(new String[] { "one", "two", "three" });
5456
logger.setColumn("1", array);
57+
assertThat(logger.getParameterValueString()).startsWith("[one, two, three]");
58+
}
59+
60+
@Test
61+
void shouldDescribeObjectArrayCallFreeParameter() throws Exception {
62+
5563
when(array.getArray()).thenReturn(new String[] { "one", "two", "three" });
64+
logger.setColumn("1", array);
65+
doAnswer(e->{
66+
when(array.getArray()).thenReturn(null);
67+
return null;
68+
}).when(array).free();
69+
array.free();
5670
assertThat(logger.getParameterValueString()).startsWith("[one, two, three]");
71+
5772
}
5873
}

0 commit comments

Comments
 (0)