Skip to content

Commit

Permalink
Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffgbutler committed Oct 23, 2024
1 parent 3ade4fe commit d4005a7
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 23 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<properties>
<java.version>17</java.version>
<java.release.version>17</java.release.version>
<java.test.version>17</java.test.version>
<java.test.release.version>17</java.test.release.version>
<junit.jupiter.version>5.11.1</junit.jupiter.version>
<spring.batch.version>5.1.2</spring.batch.version>

Expand Down
19 changes: 8 additions & 11 deletions src/main/java/org/mybatis/dynamic/sql/render/RenderingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,27 @@ private String nextMapKey() {
return renderingStrategy.formatParameterMapKey(sequence);
}

private String renderedPlaceHolder(String mapKey) {
return renderingStrategy.getFormattedJdbcPlaceholder(calculatedParameterName, mapKey);
}

private <T> String renderedPlaceHolder(String mapKey, BindableColumn<T> column) {
return column.renderingStrategy().orElse(renderingStrategy)
.getFormattedJdbcPlaceholder(column, calculatedParameterName, mapKey);
}

public RenderedParameterInfo calculateFetchFirstRowsParameterInfo() {
String mapKey = renderingStrategy.formatParameterMapKeyForFetchFirstRows(sequence);
return new RenderedParameterInfo(mapKey,
renderingStrategy.getFormattedJdbcPlaceholderForPagingParameters(calculatedParameterName, mapKey));
}

public RenderedParameterInfo calculateLimitParameterInfo() {
String mapKey = renderingStrategy.formatParameterMapKeyForLimit(sequence);
return new RenderedParameterInfo(mapKey,
renderingStrategy.getFormattedJdbcPlaceholderForLimitOrOffset(calculatedParameterName, mapKey));
renderingStrategy.getFormattedJdbcPlaceholderForPagingParameters(calculatedParameterName, mapKey));
}

public RenderedParameterInfo calculateOffsetParameterInfo() {
String mapKey = renderingStrategy.formatParameterMapKeyForOffset(sequence);
return new RenderedParameterInfo(mapKey,
renderingStrategy.getFormattedJdbcPlaceholderForLimitOrOffset(calculatedParameterName, mapKey));
}

public RenderedParameterInfo calculateParameterInfo() {
String mapKey = nextMapKey();
return new RenderedParameterInfo(mapKey, renderedPlaceHolder(mapKey));
renderingStrategy.getFormattedJdbcPlaceholderForPagingParameters(calculatedParameterName, mapKey));
}

public <T> RenderedParameterInfo calculateParameterInfo(BindableColumn<T> column) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class RenderingStrategy {
public static final String DEFAULT_PARAMETER_PREFIX = "parameters"; //$NON-NLS-1$

/**
* Return a unique key that can be used to place a parameter value in the parameter map
* Generate a unique key that can be used to place a parameter value in the parameter map
*
* @param sequence a sequence for calculating a unique value
* @return a key used to place the parameter value in the parameter map
Expand All @@ -47,7 +47,20 @@ public String formatParameterMapKey(AtomicInteger sequence) {
}

/**
* Return a parameter map key intended as a parameter for a limit or fetch first query.
* Return a parameter map key intended as a parameter for a fetch first query.
*
* <p>By default, this parameter is treated the same as any other. This method is a hook to support
* MyBatis Spring Batch.
*
* @param sequence a sequence for calculating a unique value
* @return a key used to place the parameter value in the parameter map
*/
public String formatParameterMapKeyForFetchFirstRows(AtomicInteger sequence) {
return formatParameterMapKeyForLimit(sequence);
}

/**
* Return a parameter map key intended as a parameter for a limit query.
*
* <p>By default, this parameter is treated the same as any other. This method is a hook to support
* MyBatis Spring Batch.
Expand Down Expand Up @@ -123,7 +136,7 @@ public String formatParameterMapKeyForOffset(AtomicInteger sequence) {
* {@link RenderingStrategy#formatParameterMapKey(AtomicInteger)}
* @return the generated binding
*/
public String getFormattedJdbcPlaceholderForLimitOrOffset(String prefix, String parameterName) {
public String getFormattedJdbcPlaceholderForPagingParameters(String prefix, String parameterName) {
return getFormattedJdbcPlaceholder(prefix, parameterName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ private FragmentAndParameters renderFetchFirstRowsOnly() {
}

private FragmentAndParameters renderFetchFirstRowsOnly(Long fetchFirstRows) {
RenderedParameterInfo limitParameterInfo = renderingContext.calculateLimitParameterInfo();
RenderedParameterInfo fetchFirstParameterInfo = renderingContext.calculateFetchFirstRowsParameterInfo();
return FragmentAndParameters
.withFragment("fetch first " + limitParameterInfo.renderedPlaceHolder() //$NON-NLS-1$
.withFragment("fetch first " + fetchFirstParameterInfo.renderedPlaceHolder() //$NON-NLS-1$
+ " rows only") //$NON-NLS-1$
.withParameter(limitParameterInfo.parameterMapKey(), fetchFirstRows)
.withParameter(fetchFirstParameterInfo.parameterMapKey(), fetchFirstRows)
.build();
}

Expand All @@ -69,12 +69,12 @@ private FragmentAndParameters renderOffsetOnly(Long offset) {

private FragmentAndParameters renderOffsetAndFetchFirstRows(Long offset, Long fetchFirstRows) {
RenderedParameterInfo offsetParameterInfo = renderingContext.calculateOffsetParameterInfo();
RenderedParameterInfo limitParameterInfo = renderingContext.calculateParameterInfo();
RenderedParameterInfo fetchFirstParameterInfo = renderingContext.calculateFetchFirstRowsParameterInfo();
return FragmentAndParameters.withFragment("offset " + offsetParameterInfo.renderedPlaceHolder() //$NON-NLS-1$
+ " rows fetch first " + limitParameterInfo.renderedPlaceHolder() //$NON-NLS-1$
+ " rows fetch first " + fetchFirstParameterInfo.renderedPlaceHolder() //$NON-NLS-1$
+ " rows only") //$NON-NLS-1$
.withParameter(offsetParameterInfo.parameterMapKey(), offset)
.withParameter(limitParameterInfo.parameterMapKey(), fetchFirstRows)
.withParameter(fetchFirstParameterInfo.parameterMapKey(), fetchFirstRows)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class SpringBatchPagingItemReaderRenderingStrategy extends MyBatis3RenderingStrategy {

@Override
public String getFormattedJdbcPlaceholderForLimitOrOffset(String prefix, String parameterName) {
public String getFormattedJdbcPlaceholderForPagingParameters(String prefix, String parameterName) {
return "#{" //$NON-NLS-1$
+ parameterName
+ "}"; //$NON-NLS-1$
Expand Down
2 changes: 1 addition & 1 deletion src/site/markdown/docs/springBatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ to respond to these parameter values that are supplied at runtime. As with the o
must also be taken to prepare the parameter values for use with this reader. See the following example:

```java
@Bean
@Bean
public MyBatisPagingItemReader<PersonRecord> reader(SqlSessionFactory sqlSessionFactory) {
SelectStatementProvider selectStatement = select(person.allColumns())
.from(person)
Expand Down
1 change: 0 additions & 1 deletion src/test/java/examples/simple/MyBatisMapToRowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/examples/springbatch/SpringBatchRenderingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2016-2024 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
*
* https://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 examples.springbatch;

import static examples.springbatch.mapper.PersonDynamicSqlSupport.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mybatis.dynamic.sql.SqlBuilder.isLike;
import static org.mybatis.dynamic.sql.SqlBuilder.select;

import org.junit.jupiter.api.Test;
import org.mybatis.dynamic.sql.util.springbatch.SpringBatchUtility;

class SpringBatchRenderingTest {

@Test
void renderLimit() {
var selectStatement = select(person.allColumns())
.from(person)
.where(firstName, isLike("%f%"))
.limit(SpringBatchUtility.MYBATIS_SPRING_BATCH_PAGESIZE)
.offset(SpringBatchUtility.MYBATIS_SPRING_BATCH_SKIPROWS)
.build()
.render(SpringBatchUtility.SPRING_BATCH_PAGING_ITEM_READER_RENDERING_STRATEGY);

assertThat(selectStatement.getSelectStatement())
.isEqualTo("""
select * \
from person \
where first_name like #{parameters.p1,jdbcType=VARCHAR} \
limit #{_pagesize} \
offset #{_skiprows}""");
}

@Test
void renderFetchFirst() {
var selectStatement = select(person.allColumns())
.from(person)
.where(firstName, isLike("%f%"))
.offset(SpringBatchUtility.MYBATIS_SPRING_BATCH_SKIPROWS)
.fetchFirst(SpringBatchUtility.MYBATIS_SPRING_BATCH_PAGESIZE).rowsOnly()
.build()
.render(SpringBatchUtility.SPRING_BATCH_PAGING_ITEM_READER_RENDERING_STRATEGY);

assertThat(selectStatement.getSelectStatement())
.isEqualTo("""
select * \
from person \
where first_name like #{parameters.p1,jdbcType=VARCHAR} \
offset #{_skiprows} rows \
fetch first #{_pagesize} rows only""");
}
}

0 comments on commit d4005a7

Please sign in to comment.