Skip to content

Commit 9e85477

Browse files
committed
Minor adjustments
1 parent 3812082 commit 9e85477

File tree

8 files changed

+65
-72
lines changed

8 files changed

+65
-72
lines changed

src/main/java/org/apache/ibatis/type/ArrayTypeHandler.java

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.sql.SQLException;
2626
import java.sql.Time;
2727
import java.sql.Timestamp;
28-
import java.sql.Types;
2928
import java.time.LocalDate;
3029
import java.time.LocalDateTime;
3130
import java.time.LocalTime;
@@ -42,67 +41,64 @@ public class ArrayTypeHandler extends BaseTypeHandler<Object> {
4241
private static final ConcurrentHashMap<Class<?>, String> STANDARD_MAPPING;
4342
static {
4443
STANDARD_MAPPING = new ConcurrentHashMap<>();
45-
STANDARD_MAPPING.put(BigDecimal.class, "NUMERIC");
46-
STANDARD_MAPPING.put(BigInteger.class, "BIGINT");
47-
STANDARD_MAPPING.put(boolean.class, "BOOLEAN");
48-
STANDARD_MAPPING.put(Boolean.class, "BOOLEAN");
49-
STANDARD_MAPPING.put(byte[].class, "VARBINARY");
50-
STANDARD_MAPPING.put(byte.class, "TINYINT");
51-
STANDARD_MAPPING.put(Byte.class, "TINYINT");
52-
STANDARD_MAPPING.put(Calendar.class, "TIMESTAMP");
53-
STANDARD_MAPPING.put(java.sql.Date.class, "DATE");
54-
STANDARD_MAPPING.put(java.util.Date.class, "TIMESTAMP");
55-
STANDARD_MAPPING.put(double.class, "DOUBLE");
56-
STANDARD_MAPPING.put(Double.class, "DOUBLE");
57-
STANDARD_MAPPING.put(float.class, "REAL");
58-
STANDARD_MAPPING.put(Float.class, "REAL");
59-
STANDARD_MAPPING.put(int.class, "INTEGER");
60-
STANDARD_MAPPING.put(Integer.class, "INTEGER");
61-
STANDARD_MAPPING.put(LocalDate.class, "DATE");
62-
STANDARD_MAPPING.put(LocalDateTime.class, "TIMESTAMP");
63-
STANDARD_MAPPING.put(LocalTime.class, "TIME");
64-
STANDARD_MAPPING.put(long.class, "BIGINT");
65-
STANDARD_MAPPING.put(Long.class, "BIGINT");
66-
STANDARD_MAPPING.put(OffsetDateTime.class, "TIMESTAMP_WITH_TIMEZONE");
67-
STANDARD_MAPPING.put(OffsetTime.class, "TIME_WITH_TIMEZONE");
68-
STANDARD_MAPPING.put(Short.class, "SMALLINT");
69-
STANDARD_MAPPING.put(String.class, "VARCHAR");
70-
STANDARD_MAPPING.put(Time.class, "TIME");
71-
STANDARD_MAPPING.put(Timestamp.class, "TIMESTAMP");
72-
STANDARD_MAPPING.put(URL.class, "DATALINK");
73-
};
74-
44+
STANDARD_MAPPING.put(BigDecimal.class, JdbcType.NUMERIC.name());
45+
STANDARD_MAPPING.put(BigInteger.class, JdbcType.BIGINT.name());
46+
STANDARD_MAPPING.put(boolean.class, JdbcType.BOOLEAN.name());
47+
STANDARD_MAPPING.put(Boolean.class, JdbcType.BOOLEAN.name());
48+
STANDARD_MAPPING.put(byte[].class, JdbcType.VARBINARY.name());
49+
STANDARD_MAPPING.put(byte.class, JdbcType.TINYINT.name());
50+
STANDARD_MAPPING.put(Byte.class, JdbcType.TINYINT.name());
51+
STANDARD_MAPPING.put(Calendar.class, JdbcType.TIMESTAMP.name());
52+
STANDARD_MAPPING.put(java.sql.Date.class, JdbcType.DATE.name());
53+
STANDARD_MAPPING.put(java.util.Date.class, JdbcType.TIMESTAMP.name());
54+
STANDARD_MAPPING.put(double.class, JdbcType.DOUBLE.name());
55+
STANDARD_MAPPING.put(Double.class, JdbcType.DOUBLE.name());
56+
STANDARD_MAPPING.put(float.class, JdbcType.REAL.name());
57+
STANDARD_MAPPING.put(Float.class, JdbcType.REAL.name());
58+
STANDARD_MAPPING.put(int.class, JdbcType.INTEGER.name());
59+
STANDARD_MAPPING.put(Integer.class, JdbcType.INTEGER.name());
60+
STANDARD_MAPPING.put(LocalDate.class, JdbcType.DATE.name());
61+
STANDARD_MAPPING.put(LocalDateTime.class, JdbcType.TIMESTAMP.name());
62+
STANDARD_MAPPING.put(LocalTime.class, JdbcType.TIME.name());
63+
STANDARD_MAPPING.put(long.class, JdbcType.BIGINT.name());
64+
STANDARD_MAPPING.put(Long.class, JdbcType.BIGINT.name());
65+
STANDARD_MAPPING.put(OffsetDateTime.class, JdbcType.TIMESTAMP_WITH_TIMEZONE.name());
66+
STANDARD_MAPPING.put(OffsetTime.class, JdbcType.TIME_WITH_TIMEZONE.name());
67+
STANDARD_MAPPING.put(Short.class, JdbcType.SMALLINT.name());
68+
STANDARD_MAPPING.put(String.class, JdbcType.VARCHAR.name());
69+
STANDARD_MAPPING.put(Time.class, JdbcType.TIME.name());
70+
STANDARD_MAPPING.put(Timestamp.class, JdbcType.TIMESTAMP.name());
71+
STANDARD_MAPPING.put(URL.class, JdbcType.DATALINK.name());
72+
}
73+
7574
public ArrayTypeHandler() {
7675
super();
7776
}
7877

7978
@Override
80-
protected void setNullParameter(PreparedStatement ps, int i, JdbcType jdbcType) throws SQLException {
81-
ps.setNull(i, Types.ARRAY);
82-
}
83-
84-
@Override
85-
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
79+
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)
80+
throws SQLException {
8681
if (parameter instanceof Array) {
8782
// it's the user's responsibility to properly free() the Array instance
88-
ps.setArray(i, (Array)parameter);
89-
}
90-
else {
83+
ps.setArray(i, (Array) parameter);
84+
} else {
9185
if (!parameter.getClass().isArray()) {
92-
throw new TypeException("ArrayType Handler requires SQL array or java array parameter and does not support type " + parameter.getClass());
86+
throw new TypeException(
87+
"ArrayType Handler requires SQL array or java array parameter and does not support type "
88+
+ parameter.getClass());
9389
}
9490
Class<?> componentType = parameter.getClass().getComponentType();
9591
String arrayTypeName = resolveTypeName(componentType);
96-
Array array = ps.getConnection().createArrayOf(arrayTypeName, (Object[])parameter);
92+
Array array = ps.getConnection().createArrayOf(arrayTypeName, (Object[]) parameter);
9793
ps.setArray(i, array);
9894
array.free();
9995
}
10096
}
10197

10298
protected String resolveTypeName(Class<?> type) {
103-
return STANDARD_MAPPING.getOrDefault(type, "JAVA_OBJECT");
99+
return STANDARD_MAPPING.getOrDefault(type, JdbcType.JAVA_OBJECT.name());
104100
}
105-
101+
106102
@Override
107103
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
108104
return extractArray(rs.getArray(columnName));

src/main/java/org/apache/ibatis/type/BaseTypeHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbc
5858
throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
5959
}
6060
try {
61-
setNullParameter(ps, i, jdbcType);
61+
ps.setNull(i, jdbcType.TYPE_CODE);
6262
} catch (SQLException e) {
6363
throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
6464
+ "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
@@ -75,10 +75,6 @@ public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbc
7575
}
7676
}
7777

78-
protected void setNullParameter(PreparedStatement ps, int i, JdbcType jdbcType) throws SQLException {
79-
ps.setNull(i, jdbcType.TYPE_CODE);
80-
}
81-
8278
@Override
8379
public T getResult(ResultSet rs, String columnName) throws SQLException {
8480
try {

src/test/java/org/apache/ibatis/submitted/array_type_handler/BaseTest.java renamed to src/test/java/org/apache/ibatis/submitted/array_type_handler/ArrayTypeHandlerTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,48 +28,49 @@
2828
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Test;
3030

31-
public class BaseTest {
31+
public class ArrayTypeHandlerTest {
3232

3333
private SqlSessionFactory sqlSessionFactory;
3434

3535
@BeforeEach
3636
public void setUp() throws Exception {
37-
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/array_type_handler/mybatis-config.xml")) {
37+
try (Reader reader = Resources
38+
.getResourceAsReader("org/apache/ibatis/submitted/array_type_handler/mybatis-config.xml")) {
3839
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
3940
}
40-
41+
4142
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
4243
"org/apache/ibatis/submitted/array_type_handler/CreateDB.sql");
4344
}
44-
45+
4546
@Test
4647
public void shouldInsertArrayValue() throws Exception {
4748
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
4849
User user = new User();
4950
user.setId(1);
5051
user.setName("User 1");
5152
user.setNicknames(new String[] { "User", "one" });
52-
53+
5354
Mapper mapper = sqlSession.getMapper(Mapper.class);
5455
mapper.insert(user);
5556
sqlSession.commit();
5657

5758
int usersInDatabase = mapper.getUserCount();
5859
assertEquals(1, usersInDatabase);
59-
60+
6061
Integer nicknameCount = mapper.getNicknameCount();
6162
assertEquals(2, nicknameCount);
6263
}
6364
}
64-
65+
6566
@Test
6667
public void shouldInsertNullValue() throws Exception {
6768
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
6869
User user = new User();
6970
user.setId(1);
7071
user.setName("User 1");
7172
// note how the user does not have nicknames
72-
73+
7374
Mapper mapper = sqlSession.getMapper(Mapper.class);
7475
mapper.insert(user);
7576
sqlSession.commit();

src/test/java/org/apache/ibatis/submitted/array_type_handler/CreateDB.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2019 the original author or authors.
2+
-- Copyright 2009-2019 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.

src/test/java/org/apache/ibatis/submitted/array_type_handler/Mapper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,9 +21,8 @@ public interface Mapper {
2121

2222
int getUserCount();
2323

24-
/**
25-
* HSQL returns NULL when asked for the cardinality of an array column
26-
* with NULL value :-(
24+
/**
25+
* HSQL returns NULL when asked for the cardinality of an array column with NULL value :-(
2726
*/
2827
Integer getNicknameCount();
2928
}

src/test/java/org/apache/ibatis/submitted/array_type_handler/Mapper.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2019 the original author or authors.
4+
Copyright 2009-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727
insert into users
2828
(id, name, nicknames)
2929
values
30-
(#{id}, #{name}, #{nicknames,jdbcType=VARCHAR,typeHandler=org.apache.ibatis.type.ArrayTypeHandler})
30+
(#{id}, #{name}, #{nicknames,typeHandler=org.apache.ibatis.type.ArrayTypeHandler})
3131
</insert>
3232

3333
<select id="getUserCount" resultType="int">

src/test/java/org/apache/ibatis/submitted/array_type_handler/User.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2009-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,26 +20,27 @@ public class User {
2020
private Integer id;
2121
private String name;
2222
private String[] nicknames;
23-
23+
2424
public Integer getId() {
2525
return id;
2626
}
27+
2728
public void setId(Integer id) {
2829
this.id = id;
2930
}
30-
31+
3132
public String getName() {
3233
return name;
3334
}
34-
35+
3536
public void setName(String name) {
3637
this.name = name;
3738
}
38-
39+
3940
public String[] getNicknames() {
4041
return nicknames;
4142
}
42-
43+
4344
public void setNicknames(String[] nicknames) {
4445
this.nicknames = nicknames;
4546
}

src/test/java/org/apache/ibatis/submitted/array_type_handler/mybatis-config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
4-
Copyright 2019 the original author or authors.
4+
Copyright 2009-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)