Skip to content

Commit bb1db02

Browse files
committed
Improve ArrayTypeHandler
ArrayTypeHandler unwraps the contents of the java.sql.Array when fetching data. Upon insertion it expects an instance of java.sql.Array as the parameter. This changeset improves ArrayTypeHandler so that it accepts an Object[] parameter for insert, constructing the java.sql.Array object if necessary. This aproach relies on the jdbcType of the column - it must reflect the array element's type. For setting NULL values on ARRAY columns I had to refactor BaseTypeHandler a bit - but I'm quite sure that this won't break anything.
1 parent 385155b commit bb1db02

File tree

9 files changed

+290
-2
lines changed

9 files changed

+290
-2
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.sql.PreparedStatement;
2121
import java.sql.ResultSet;
2222
import java.sql.SQLException;
23+
import java.sql.Types;
2324

2425
/**
2526
* @author Clinton Begin
@@ -30,9 +31,22 @@ public ArrayTypeHandler() {
3031
super();
3132
}
3233

34+
@Override
35+
protected void setNullParameter(PreparedStatement ps, int i, JdbcType jdbcType) throws SQLException {
36+
ps.setNull(i, Types.ARRAY);
37+
}
38+
3339
@Override
3440
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
35-
ps.setArray(i, (Array) parameter);
41+
Array array = null;
42+
if (parameter instanceof Array) {
43+
array = (Array)parameter;
44+
}
45+
else {
46+
Object[] values = (Object[])parameter;
47+
array = ps.getConnection().createArrayOf(jdbcType.name(), values);
48+
}
49+
ps.setArray(i, array);
3650
}
3751

3852
@Override

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

Lines changed: 5 additions & 1 deletion
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-
ps.setNull(i, jdbcType.TYPE_CODE);
61+
setNullParameter(ps, i, jdbcType);
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,6 +75,10 @@ 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+
7882
@Override
7983
public T getResult(ResultSet rs, String columnName) throws SQLException {
8084
try {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.array_type_handler;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
20+
21+
import java.io.Reader;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
public class BaseTest {
32+
33+
private SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeEach
36+
public void setUp() throws Exception {
37+
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/array_type_handler/mybatis-config.xml")) {
38+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39+
}
40+
41+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42+
"org/apache/ibatis/submitted/array_type_handler/CreateDB.sql");
43+
}
44+
45+
@Test
46+
public void shouldInsertArrayValue() throws Exception {
47+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
48+
User user = new User();
49+
user.setId(1);
50+
user.setName("User 1");
51+
user.setNicknames(new String[] { "User", "one" });
52+
53+
Mapper mapper = sqlSession.getMapper(Mapper.class);
54+
mapper.insert(user);
55+
sqlSession.commit();
56+
57+
int usersInDatabase = mapper.getUserCount();
58+
assertEquals(1, usersInDatabase);
59+
60+
Integer nicknameCount = mapper.getNicknameCount();
61+
assertEquals(2, nicknameCount);
62+
}
63+
}
64+
65+
@Test
66+
public void shouldInsertNullValue() throws Exception {
67+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68+
User user = new User();
69+
user.setId(1);
70+
user.setName("User 1");
71+
// note how the user does not have nicknames
72+
73+
Mapper mapper = sqlSession.getMapper(Mapper.class);
74+
mapper.insert(user);
75+
sqlSession.commit();
76+
77+
int usersInDatabase = mapper.getUserCount();
78+
assertEquals(1, usersInDatabase);
79+
80+
Integer nicknameCount = mapper.getNicknameCount();
81+
assertNull(nicknameCount);
82+
}
83+
}
84+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--
2+
-- Copyright 2019 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20),
22+
nicknames varchar(20) array
23+
);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.array_type_handler;
17+
18+
public interface Mapper {
19+
20+
void insert(User user);
21+
22+
int getUserCount();
23+
24+
/**
25+
* HSQL returns NULL when asked for the cardinality of an array column
26+
* with NULL value :-(
27+
*/
28+
Integer getNicknameCount();
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
23+
<mapper namespace="org.apache.ibatis.submitted.array_type_handler.Mapper">
24+
25+
<insert id="insert"
26+
parameterType="org.apache.ibatis.submitted.array_type_handler.User">
27+
insert into users
28+
(id, name, nicknames)
29+
values
30+
(#{id}, #{name}, #{nicknames,jdbcType=VARCHAR,typeHandler=org.apache.ibatis.type.ArrayTypeHandler})
31+
</insert>
32+
33+
<select id="getUserCount" resultType="int">
34+
select count(*) from users
35+
</select>
36+
37+
<select id="getNicknameCount" resultType="int">
38+
select cardinality(nicknames) from users where id = 1
39+
</select>
40+
41+
</mapper>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.array_type_handler;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
private String[] nicknames;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public String[] getNicknames() {
40+
return nicknames;
41+
}
42+
43+
public void setNicknames(String[] nicknames) {
44+
this.nicknames = nicknames;
45+
}
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC"></transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:arrayrtypehandler" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper class="org.apache.ibatis.submitted.array_type_handler.Mapper" />
38+
</mappers>
39+
40+
</configuration>

src/test/java/org/apache/ibatis/type/ArrayTypeHandlerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.mockito.Mock;
2020

2121
import java.sql.Array;
22+
import java.sql.Types;
2223

2324
import static org.junit.jupiter.api.Assertions.assertEquals;
2425
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -38,6 +39,12 @@ public void shouldSetParameter() throws Exception {
3839
TYPE_HANDLER.setParameter(ps, 1, mockArray, null);
3940
verify(ps).setArray(1, mockArray);
4041
}
42+
43+
@Test
44+
public void shouldSetNullParameter() throws Exception {
45+
TYPE_HANDLER.setParameter(ps, 1, null, JdbcType.ARRAY);
46+
verify(ps).setNull(1, Types.ARRAY);
47+
}
4148

4249
@Override
4350
@Test

0 commit comments

Comments
 (0)