Skip to content

Commit e7721be

Browse files
authored
Merge pull request #258 from jeffgbutler/array-examples
Add tests for JDBC Arrays
2 parents 03b560c + 7218373 commit e7721be

File tree

6 files changed

+328
-0
lines changed

6 files changed

+328
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2016-2020 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 examples.array;
17+
18+
import static examples.array.NamesTableDynamicSqlSupport.*;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mybatis.dynamic.sql.SqlBuilder.*;
21+
22+
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
23+
import org.apache.ibatis.jdbc.ScriptRunner;
24+
import org.apache.ibatis.mapping.Environment;
25+
import org.apache.ibatis.session.Configuration;
26+
import org.apache.ibatis.session.SqlSession;
27+
import org.apache.ibatis.session.SqlSessionFactory;
28+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
32+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
33+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
34+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
35+
36+
import java.io.InputStream;
37+
import java.io.InputStreamReader;
38+
import java.sql.Connection;
39+
import java.sql.DriverManager;
40+
import java.util.Optional;
41+
42+
class ArrayTest {
43+
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
44+
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
45+
46+
private SqlSessionFactory sqlSessionFactory;
47+
48+
@BeforeEach
49+
void setup() throws Exception {
50+
Class.forName(JDBC_DRIVER);
51+
InputStream is = getClass().getResourceAsStream("/examples/array/CreateDB.sql");
52+
try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
53+
ScriptRunner sr = new ScriptRunner(connection);
54+
sr.setLogWriter(null);
55+
sr.runScript(new InputStreamReader(is));
56+
}
57+
58+
UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
59+
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
60+
Configuration config = new Configuration(environment);
61+
config.addMapper(NamesTableMapper.class);
62+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
63+
}
64+
65+
@Test
66+
void testInsertSelectById() {
67+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68+
NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class);
69+
70+
String[] someNames = {"Fred", "Wilma", "Pebbles"};
71+
72+
GeneralInsertStatementProvider insertStatement = insertInto(namesTable)
73+
.set(id).toValue(1)
74+
.set(names).toValue(someNames)
75+
.build()
76+
.render(RenderingStrategies.MYBATIS3);
77+
int rows = mapper.generalInsert(insertStatement);
78+
assertThat(rows).isEqualTo(1);
79+
80+
SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names)
81+
.from(namesTable)
82+
.where(id, isEqualTo(1))
83+
.build()
84+
.render(RenderingStrategies.MYBATIS3);
85+
86+
Optional<NamesRecord> record = mapper.selectOne(selectStatement);
87+
assertThat(record).hasValueSatisfying( r -> {
88+
assertThat(r.getId()).isEqualTo(1);
89+
assertThat(r.getNames()).isEqualTo(someNames);
90+
});
91+
}
92+
}
93+
94+
@Test
95+
void testInsertSelectByArray() {
96+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
97+
NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class);
98+
99+
String[] someNames = {"Fred", "Wilma", "Pebbles"};
100+
101+
GeneralInsertStatementProvider insertStatement = insertInto(namesTable)
102+
.set(id).toValue(1)
103+
.set(names).toValue(someNames)
104+
.build()
105+
.render(RenderingStrategies.MYBATIS3);
106+
int rows = mapper.generalInsert(insertStatement);
107+
assertThat(rows).isEqualTo(1);
108+
109+
SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names)
110+
.from(namesTable)
111+
.where(names, isEqualTo(someNames))
112+
.build()
113+
.render(RenderingStrategies.MYBATIS3);
114+
115+
Optional<NamesRecord> record = mapper.selectOne(selectStatement);
116+
assertThat(record).hasValueSatisfying( r -> {
117+
assertThat(r.getId()).isEqualTo(1);
118+
assertThat(r.getNames()).isEqualTo(someNames);
119+
});
120+
}
121+
}
122+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016-2020 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 examples.array;
17+
18+
public class NamesRecord {
19+
private Integer id;
20+
private String[] names;
21+
22+
public Integer getId() {
23+
return id;
24+
}
25+
26+
public void setId(Integer id) {
27+
this.id = id;
28+
}
29+
30+
public String[] getNames() {
31+
return names;
32+
}
33+
34+
public void setNames(String[] names) {
35+
this.names = names;
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016-2020 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 examples.array;
17+
18+
import org.mybatis.dynamic.sql.SqlColumn;
19+
import org.mybatis.dynamic.sql.SqlTable;
20+
21+
import java.sql.JDBCType;
22+
23+
public class NamesTableDynamicSqlSupport {
24+
public static final NamesTable namesTable = new NamesTable();
25+
public static final SqlColumn<Integer> id = namesTable.id;
26+
public static final SqlColumn<String[]> names = namesTable.names;
27+
28+
public static final class NamesTable extends SqlTable {
29+
public NamesTable() {
30+
super("NamesTable");
31+
}
32+
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
33+
public final SqlColumn<String[]> names = column("names", JDBCType.ARRAY);
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2016-2020 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 examples.array;
17+
18+
import org.apache.ibatis.annotations.InsertProvider;
19+
import org.apache.ibatis.annotations.Result;
20+
import org.apache.ibatis.annotations.ResultMap;
21+
import org.apache.ibatis.annotations.Results;
22+
import org.apache.ibatis.annotations.SelectProvider;
23+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
24+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
25+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
26+
27+
import java.util.List;
28+
import java.util.Optional;
29+
30+
public interface NamesTableMapper {
31+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
32+
@Results(id="NamesTableResult", value={
33+
@Result(column="id", property="id", id=true),
34+
@Result(column="names", property="names", typeHandler = StringArrayTypeHandler.class)
35+
})
36+
List<NamesRecord> selectMany(SelectStatementProvider selectStatement);
37+
38+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
39+
@ResultMap("NamesTableResult")
40+
Optional<NamesRecord> selectOne(SelectStatementProvider selectStatement);
41+
42+
@InsertProvider(type= SqlProviderAdapter.class, method="generalInsert")
43+
int generalInsert(GeneralInsertStatementProvider insertStatement);
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016-2020 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 examples.array;
17+
18+
import org.apache.ibatis.type.BaseTypeHandler;
19+
import org.apache.ibatis.type.JdbcType;
20+
21+
import java.sql.Array;
22+
import java.sql.CallableStatement;
23+
import java.sql.PreparedStatement;
24+
import java.sql.ResultSet;
25+
import java.sql.SQLException;
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.stream.Collectors;
29+
30+
public class StringArrayTypeHandler extends BaseTypeHandler<String[]> {
31+
@Override
32+
public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
33+
Array array = ps.getConnection().createArrayOf("CHAR", parameter);
34+
ps.setArray(i, array);
35+
array.free();
36+
}
37+
38+
@Override
39+
public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
40+
Array array = rs.getArray(columnName);
41+
return extractArray(array);
42+
}
43+
44+
@Override
45+
public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
46+
Array array = rs.getArray(columnIndex);
47+
return extractArray(array);
48+
}
49+
50+
@Override
51+
public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
52+
Array array = cs.getArray(columnIndex);
53+
return extractArray(array);
54+
}
55+
56+
private String[] extractArray(Array array) throws SQLException {
57+
Object[] objArray = (Object[]) array.getArray();
58+
array.free();
59+
60+
List<String> stringList = Arrays.stream(objArray)
61+
.map(Object::toString)
62+
.collect(Collectors.toList());
63+
64+
String[] stringArray = new String[stringList.size()];
65+
return stringList.toArray(stringArray);
66+
}
67+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--
2+
-- Copyright 2016-2020 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 NamesTable if exists;
18+
19+
create table NamesTable (
20+
id int not null,
21+
names varchar(32) array,
22+
primary key(id)
23+
);

0 commit comments

Comments
 (0)