Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1548 from dirk-olmes/ArrayTypeHandlerImprovements
Improve ArrayTypeHandler
- Loading branch information
Showing
8 changed files
with
363 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/java/org/apache/ibatis/submitted/array_type_handler/ArrayTypeHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright 2009-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.array_type_handler; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.io.Reader; | ||
|
||
import org.apache.ibatis.BaseDataTest; | ||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ArrayTypeHandlerTest { | ||
|
||
private SqlSessionFactory sqlSessionFactory; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
try (Reader reader = Resources | ||
.getResourceAsReader("org/apache/ibatis/submitted/array_type_handler/mybatis-config.xml")) { | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||
} | ||
|
||
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), | ||
"org/apache/ibatis/submitted/array_type_handler/CreateDB.sql"); | ||
} | ||
|
||
@Test | ||
public void shouldInsertArrayValue() throws Exception { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
User user = new User(); | ||
user.setId(1); | ||
user.setName("User 1"); | ||
user.setNicknames(new String[] { "User", "one" }); | ||
|
||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
mapper.insert(user); | ||
sqlSession.commit(); | ||
|
||
int usersInDatabase = mapper.getUserCount(); | ||
assertEquals(1, usersInDatabase); | ||
|
||
Integer nicknameCount = mapper.getNicknameCount(); | ||
assertEquals(2, nicknameCount); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldInsertNullValue() throws Exception { | ||
try (SqlSession sqlSession = sqlSessionFactory.openSession()) { | ||
User user = new User(); | ||
user.setId(1); | ||
user.setName("User 1"); | ||
// note how the user does not have nicknames | ||
|
||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||
mapper.insert(user); | ||
sqlSession.commit(); | ||
|
||
int usersInDatabase = mapper.getUserCount(); | ||
assertEquals(1, usersInDatabase); | ||
|
||
Integer nicknameCount = mapper.getNicknameCount(); | ||
assertNull(nicknameCount); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/org/apache/ibatis/submitted/array_type_handler/CreateDB.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- | ||
-- Copyright 2009-2019 the original author or authors. | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
|
||
drop table users if exists; | ||
|
||
create table users ( | ||
id int, | ||
name varchar(20), | ||
nicknames varchar(20) array | ||
); |
28 changes: 28 additions & 0 deletions
28
src/test/java/org/apache/ibatis/submitted/array_type_handler/Mapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright 2009-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.array_type_handler; | ||
|
||
public interface Mapper { | ||
|
||
void insert(User user); | ||
|
||
int getUserCount(); | ||
|
||
/** | ||
* HSQL returns NULL when asked for the cardinality of an array column with NULL value :-( | ||
*/ | ||
Integer getNicknameCount(); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/org/apache/ibatis/submitted/array_type_handler/Mapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2009-2019 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="org.apache.ibatis.submitted.array_type_handler.Mapper"> | ||
|
||
<insert id="insert" | ||
parameterType="org.apache.ibatis.submitted.array_type_handler.User"> | ||
insert into users | ||
(id, name, nicknames) | ||
values | ||
(#{id}, #{name}, #{nicknames,typeHandler=org.apache.ibatis.type.ArrayTypeHandler}) | ||
</insert> | ||
|
||
<select id="getUserCount" resultType="int"> | ||
select count(*) from users | ||
</select> | ||
|
||
<select id="getNicknameCount" resultType="int"> | ||
select cardinality(nicknames) from users where id = 1 | ||
</select> | ||
|
||
</mapper> |
47 changes: 47 additions & 0 deletions
47
src/test/java/org/apache/ibatis/submitted/array_type_handler/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2009-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.submitted.array_type_handler; | ||
|
||
public class User { | ||
|
||
private Integer id; | ||
private String name; | ||
private String[] nicknames; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String[] getNicknames() { | ||
return nicknames; | ||
} | ||
|
||
public void setNicknames(String[] nicknames) { | ||
this.nicknames = nicknames; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/org/apache/ibatis/submitted/array_type_handler/mybatis-config.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
Copyright 2009-2019 the original author or authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!DOCTYPE configuration | ||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
|
||
<configuration> | ||
|
||
<environments default="development"> | ||
<environment id="development"> | ||
<transactionManager type="JDBC"></transactionManager> | ||
<dataSource type="UNPOOLED"> | ||
<property name="driver" value="org.hsqldb.jdbcDriver" /> | ||
<property name="url" value="jdbc:hsqldb:mem:arrayrtypehandler" /> | ||
<property name="username" value="sa" /> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
|
||
<mappers> | ||
<mapper class="org.apache.ibatis.submitted.array_type_handler.Mapper" /> | ||
</mappers> | ||
|
||
</configuration> |
Oops, something went wrong.