Skip to content

Commit 7e03b29

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 7e03b29

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

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

+20-14
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
*
@@ -75,9 +69,16 @@ public BaseJdbcLogger(Log log, int queryStack) {
7569
}
7670

7771
protected void setColumn(Object key, Object value) {
78-
columnMap.put(key, value);
72+
Object tempValue = value;
73+
if (tempValue!=null && tempValue instanceof Array){
74+
try {
75+
tempValue = ((Array)value).getArray();
76+
} catch (SQLException e) {
77+
}
78+
}
79+
columnMap.put(key, tempValue);
7980
columnNames.add(key);
80-
columnValues.add(value);
81+
columnValues.add(tempValue);
8182
}
8283

8384
protected Object getColumn(Object key) {
@@ -90,7 +91,10 @@ protected String getParameterValueString() {
9091
if (value == null) {
9192
typeList.add("null");
9293
} else {
93-
typeList.add(objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");
94+
typeList.add(objectValueString(value) +
95+
"(" + (value.getClass().isArray() ?
96+
Array.class.getSimpleName():value.getClass().getSimpleName()) + ")"
97+
);
9498
}
9599
}
96100
final String parameters = typeList.toString();
@@ -104,6 +108,8 @@ protected String objectValueString(Object value) {
104108
} catch (SQLException e) {
105109
// Intentialy fall through to return value.toString()
106110
}
111+
}else if (value.getClass().isArray()){
112+
return ArrayUtil.toString(value);
107113
}
108114
return value.toString();
109115
}

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

+21-6
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

@@ -55,4 +56,18 @@ void shouldDescribeObjectArrayParameter() throws Exception {
5556
when(array.getArray()).thenReturn(new String[] { "one", "two", "three" });
5657
assertThat(logger.getParameterValueString()).startsWith("[one, two, three]");
5758
}
59+
60+
@Test
61+
void shouldDescribeObjectArrayCallFreeParameter() throws Exception {
62+
63+
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();
70+
assertThat(logger.getParameterValueString()).startsWith("[one, two, three]");
71+
72+
}
5873
}

0 commit comments

Comments
 (0)