Skip to content

Commit 8ad1ca6

Browse files
committed
Coverage
1 parent c4a9b91 commit 8ad1ca6

File tree

11 files changed

+207
-33
lines changed

11 files changed

+207
-33
lines changed

src/main/java/org/mybatis/dynamic/sql/insert/InsertDSL.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.mybatis.dynamic.sql.util.NullMapping;
3131
import org.mybatis.dynamic.sql.util.PropertyMapping;
3232
import org.mybatis.dynamic.sql.util.PropertyWhenPresentMapping;
33+
import org.mybatis.dynamic.sql.util.RowMapping;
3334
import org.mybatis.dynamic.sql.util.StringConstantMapping;
3435

3536
public class InsertDSL<T> implements Buildable<InsertModel<T>> {
@@ -104,6 +105,11 @@ public InsertDSL<T> toStringConstant(String constant) {
104105
columnMappings.add(StringConstantMapping.of(column, constant));
105106
return InsertDSL.this;
106107
}
108+
109+
public InsertDSL<T> toRow() {
110+
columnMappings.add(RowMapping.of(column));
111+
return InsertDSL.this;
112+
}
107113
}
108114

109115
public static class Builder<T> {

src/main/java/org/mybatis/dynamic/sql/util/spring/NamedParameterJdbcTemplateExtensions.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
3737
import org.mybatis.dynamic.sql.util.Buildable;
3838
import org.springframework.dao.EmptyResultDataAccessException;
39-
import org.springframework.jdbc.core.JdbcOperations;
4039
import org.springframework.jdbc.core.RowMapper;
4140
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
4241
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
@@ -159,17 +158,4 @@ public int update(Buildable<UpdateModel> updateStatement) {
159158
public int update(UpdateStatementProvider updateStatement) {
160159
return template.update(updateStatement.getUpdateStatement(), updateStatement.getParameters());
161160
}
162-
163-
/**
164-
* Allow access to the underlying template for unusual circumstances.
165-
*
166-
* @return The underlying Spring JdbcOperations implementation
167-
*/
168-
public JdbcOperations getJdbcOperations() {
169-
return template.getJdbcOperations();
170-
}
171-
172-
public NamedParameterJdbcTemplate getTemplate() {
173-
return template;
174-
}
175161
}

src/test/java/examples/simple/CompoundKeyDynamicSqlSupport.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package examples.simple;
217

318
import org.mybatis.dynamic.sql.SqlColumn;

src/test/java/examples/simple/CompoundKeyMapper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package examples.simple;
217

318
import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper;

src/test/java/examples/simple/CompoundKeyRow.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package examples.simple;
217

318
public class CompoundKeyRow {

src/test/java/examples/simple/MyBatisMapToRowTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package examples.simple;
217

318
import java.io.InputStream;
@@ -22,6 +37,7 @@
2237
import org.junit.jupiter.api.BeforeEach;
2338
import org.junit.jupiter.api.Test;
2439
import org.mybatis.dynamic.sql.insert.render.BatchInsert;
40+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2541
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
2642
import org.mybatis.dynamic.sql.render.RenderingStrategies;
2743
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
@@ -30,11 +46,12 @@
3046
import static examples.simple.CompoundKeyDynamicSqlSupport.id1;
3147
import static examples.simple.CompoundKeyDynamicSqlSupport.id2;
3248
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.mybatis.dynamic.sql.SqlBuilder.insert;
3350
import static org.mybatis.dynamic.sql.SqlBuilder.insertBatch;
3451
import static org.mybatis.dynamic.sql.SqlBuilder.insertMultiple;
3552
import static org.mybatis.dynamic.sql.SqlBuilder.select;
3653

37-
public class MyBatisMapToRowTest {
54+
class MyBatisMapToRowTest {
3855
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
3956
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
4057
private SqlSessionFactory sqlSessionFactory;
@@ -57,6 +74,36 @@ void setup() throws Exception {
5774
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
5875
}
5976

77+
@Test
78+
void testInsertOne() {
79+
try (SqlSession session = sqlSessionFactory.openSession()) {
80+
CompoundKeyMapper mapper = session.getMapper(CompoundKeyMapper.class);
81+
82+
Integer i = 1;
83+
84+
InsertStatementProvider<Integer> insertStatement = insert(i)
85+
.into(compoundKey)
86+
.map(id1).toConstant("22")
87+
.map(id2).toRow()
88+
.build()
89+
.render(RenderingStrategies.MYBATIS3);
90+
91+
String expected = "insert into CompoundKey (id1, id2) values (22, #{row,jdbcType=INTEGER})";
92+
assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
93+
94+
int rows = mapper.insert(insertStatement);
95+
assertThat(rows).isEqualTo(1);
96+
97+
SelectStatementProvider selectStatement = select(id1, id2)
98+
.from(compoundKey)
99+
.orderBy(id1, id2)
100+
.build().render(RenderingStrategies.MYBATIS3);
101+
102+
List<CompoundKeyRow> records = mapper.selectMany(selectStatement, this::mapRow);
103+
assertThat(records).hasSize(1);
104+
}
105+
}
106+
60107
@Test
61108
void testInsertMultiple() {
62109
try (SqlSession session = sqlSessionFactory.openSession()) {

src/test/java/examples/spring/CompoundKeyDynamicSqlSupport.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package examples.spring;
217

318
import org.mybatis.dynamic.sql.SqlColumn;

src/test/java/examples/spring/CompoundKeyRow.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package examples.spring;
217

318
public class CompoundKeyRow {

src/test/java/examples/spring/SpringMapToRowTest.java

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,77 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package examples.spring;
217

318
import static examples.spring.CompoundKeyDynamicSqlSupport.compoundKey;
419
import static examples.spring.CompoundKeyDynamicSqlSupport.id1;
520
import static examples.spring.CompoundKeyDynamicSqlSupport.id2;
621
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.mybatis.dynamic.sql.SqlBuilder.insert;
723
import static org.mybatis.dynamic.sql.SqlBuilder.insertBatch;
824
import static org.mybatis.dynamic.sql.SqlBuilder.insertMultiple;
925
import static org.mybatis.dynamic.sql.SqlBuilder.select;
1026

11-
import java.sql.PreparedStatement;
12-
import java.sql.SQLException;
1327
import java.util.ArrayList;
1428
import java.util.List;
1529
import java.util.stream.IntStream;
1630

17-
import org.jetbrains.annotations.NotNull;
1831
import org.junit.jupiter.api.Test;
1932
import org.mybatis.dynamic.sql.insert.render.BatchInsert;
33+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2034
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
2135
import org.mybatis.dynamic.sql.render.RenderingStrategies;
2236
import org.mybatis.dynamic.sql.select.SelectModel;
2337
import org.mybatis.dynamic.sql.util.Buildable;
2438
import org.mybatis.dynamic.sql.util.spring.NamedParameterJdbcTemplateExtensions;
2539
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
2740
import org.springframework.jdbc.core.RowMapper;
2841
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2942
import org.springframework.transaction.annotation.Transactional;
3043

3144
@SpringJUnitConfig(classes = SpringConfiguration.class)
3245
@Transactional
33-
public class SpringMapToRowTest {
46+
class SpringMapToRowTest {
3447
@Autowired
3548
private NamedParameterJdbcTemplateExtensions template;
3649

50+
@Test
51+
void testInsertOne() {
52+
Integer i = 1;
53+
54+
InsertStatementProvider<Integer> insertStatement = insert(i)
55+
.into(compoundKey)
56+
.map(id1).toConstant("22")
57+
.map(id2).toRow()
58+
.build()
59+
.render(RenderingStrategies.SPRING_NAMED_PARAMETER);
60+
61+
String expected = "insert into CompoundKey (id1, id2) values (22, :row)";
62+
assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
63+
64+
int rows = template.insert(insertStatement);
65+
assertThat(rows).isEqualTo(1);
66+
67+
Buildable<SelectModel> selectStatement = select(id1, id2)
68+
.from(compoundKey)
69+
.orderBy(id1, id2);
70+
71+
List<CompoundKeyRow> records = template.selectList(selectStatement, rowMapper);
72+
assertThat(records).hasSize(1);
73+
}
74+
3775
@Test
3876
void testInsertMultiple() {
3977
List<Integer> integers = new ArrayList<>();
@@ -79,19 +117,6 @@ void testInsertBatch() {
79117
String expected = "insert into CompoundKey (id1, id2) values (22, :row)";
80118
assertThat(insertStatement.getInsertStatementSQL()).isEqualTo(expected);
81119

82-
// String sql = insertStatement.getInsertStatementSQL().replace(":row", "?");
83-
// int[] rowCounts = template.getJdbcOperations().batchUpdate(sql, new BatchPreparedStatementSetter() {
84-
// @Override
85-
// public void setValues(@NotNull PreparedStatement ps, int i) throws SQLException {
86-
// ps.setInt(1, insertStatement.getRecords().get(i));
87-
// }
88-
//
89-
// @Override
90-
// public int getBatchSize() {
91-
// return insertStatement.getRecords().size();
92-
// }
93-
// });
94-
95120
int[] rowCounts = template.insertBatch(insertStatement);
96121

97122
assertThat(IntStream.of(rowCounts).sum()).isEqualTo(3);

src/test/java/org/mybatis/dynamic/sql/insert/MapToRowTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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+
*/
116
package org.mybatis.dynamic.sql.insert;
217

318
import static org.assertj.core.api.Assertions.assertThat;

0 commit comments

Comments
 (0)