Skip to content

Add missing fetch first methods #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
selectDSL.addQueryExpression(buildModel());
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.fetchFirst(fetchFirstRows);
}
}

public class GroupByFinisher implements Buildable<R> {
Expand All @@ -449,6 +455,10 @@ public SelectDSL<R>.LimitFinisher limit(long limit) {
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return selectDSL.fetchFirst(fetchFirstRows);
}
}

public class UnionBuilder {
Expand Down
26 changes: 8 additions & 18 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,38 +177,28 @@ public R build() {
}

public class FetchFirstFinisher {
private Long offset;
private Long fetchFirstRows;

public FetchFirstFinisher(long fetchFirstRows) {
this.fetchFirstRows = fetchFirstRows;
SelectDSL.this.pagingModel = new FetchFirstPagingModel.Builder()
.withFetchFirstRows(fetchFirstRows)
.build();
}

public FetchFirstFinisher(long offset, long fetchFirstRows) {
this.offset = offset;
this.fetchFirstRows = fetchFirstRows;
SelectDSL.this.pagingModel = new FetchFirstPagingModel.Builder()
.withOffset(offset)
.withFetchFirstRows(fetchFirstRows)
.build();
}

public RowsOnlyFinisher rowsOnly() {
return new RowsOnlyFinisher(offset, fetchFirstRows);
return new RowsOnlyFinisher();
}
}

public class RowsOnlyFinisher implements Buildable<R> {
private Long offset;
private Long fetchFirstRows;

public RowsOnlyFinisher(Long offset, Long fetchFirstRows) {
this.offset = offset;
this.fetchFirstRows = fetchFirstRows;
}

@Override
public R build() {
SelectDSL.this.pagingModel = new FetchFirstPagingModel.Builder()
.withOffset(offset)
.withFetchFirstRows(fetchFirstRows)
.build();
return SelectDSL.this.build();
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/examples/groupby/GroupByTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,51 @@ public void testOffsetOnlyAfterGroupBy() {
assertThat(row.get("COUNT")).isEqualTo(3L);
}
}

@Test
public void testOffsetAndFetchFirstAfterGroupBy() {
try (SqlSession session = sqlSessionFactory.openSession()) {
GroupByMapper mapper = session.getMapper(GroupByMapper.class);

SelectStatementProvider selectStatement = select(lastName, count().as("count"))
.from(person)
.groupBy(lastName)
.offset(1)
.fetchFirst(1).rowsOnly()
.build()
.render(RenderingStrategy.MYBATIS3);

String expected = "select last_name, count(*) as count from Person group by last_name offset #{parameters._offset} fetch first #{parameters._fetchFirstRows} rows only";
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);

List<Map<String, Object>> rows = mapper.generalSelect(selectStatement);
assertThat(rows.size()).isEqualTo(1);
Map<String, Object> row = rows.get(0);
assertThat(row.get("LAST_NAME")).isEqualTo("Rubble");
assertThat(row.get("COUNT")).isEqualTo(3L);
}
}

@Test
public void testFetchFirstOnlyAfterGroupBy() {
try (SqlSession session = sqlSessionFactory.openSession()) {
GroupByMapper mapper = session.getMapper(GroupByMapper.class);

SelectStatementProvider selectStatement = select(lastName, count().as("count"))
.from(person)
.groupBy(lastName)
.fetchFirst(1).rowsOnly()
.build()
.render(RenderingStrategy.MYBATIS3);

String expected = "select last_name, count(*) as count from Person group by last_name fetch first #{parameters._fetchFirstRows} rows only";
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);

List<Map<String, Object>> rows = mapper.generalSelect(selectStatement);
assertThat(rows.size()).isEqualTo(1);
Map<String, Object> row = rows.get(0);
assertThat(row.get("LAST_NAME")).isEqualTo("Flintstone");
assertThat(row.get("COUNT")).isEqualTo(4L);
}
}
}
69 changes: 69 additions & 0 deletions src/test/java/examples/joins/JoinMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,4 +614,73 @@ public void testOffsetOnlyAfterJoin() {
assertThat(row.get("ITEM_ID")).isEqualTo(44);
}
}

@Test
public void testOffsetAndFetchFirstAfterJoin() {
try (SqlSession session = sqlSessionFactory.openSession()) {
JoinMapper mapper = session.getMapper(JoinMapper.class);

SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description)
.from(itemMaster, "im")
.leftJoin(orderLine, "ol").on(orderLine.itemId, equalTo(itemMaster.itemId))
.offset(1)
.fetchFirst(2).rowsOnly()
.build()
.render(RenderingStrategy.MYBATIS3);

String expectedStatment = "select ol.order_id, ol.quantity, im.item_id, im.description"
+ " from ItemMaster im left join OrderLine ol on ol.item_id = im.item_id"
+ " offset #{parameters._offset} fetch first #{parameters._fetchFirstRows} rows only";
assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment);

List<Map<String, Object>> rows = mapper.generalSelect(selectStatement);

assertThat(rows.size()).isEqualTo(2);
Map<String, Object> row = rows.get(0);
assertThat(row.get("ORDER_ID")).isEqualTo(2);
assertThat(row.get("QUANTITY")).isEqualTo(1);
assertThat(row.get("DESCRIPTION")).isEqualTo("Helmet");
assertThat(row.get("ITEM_ID")).isEqualTo(22);

row = rows.get(1);
assertThat(row.get("ORDER_ID")).isEqualTo(1);
assertThat(row.get("QUANTITY")).isEqualTo(1);
assertThat(row.get("DESCRIPTION")).isEqualTo("First Base Glove");
assertThat(row.get("ITEM_ID")).isEqualTo(33);
}
}

@Test
public void testFetchFirstOnlyAfterJoin() {
try (SqlSession session = sqlSessionFactory.openSession()) {
JoinMapper mapper = session.getMapper(JoinMapper.class);

SelectStatementProvider selectStatement = select(orderLine.orderId, orderLine.quantity, itemMaster.itemId, itemMaster.description)
.from(itemMaster, "im")
.leftJoin(orderLine, "ol").on(orderLine.itemId, equalTo(itemMaster.itemId))
.fetchFirst(2).rowsOnly()
.build()
.render(RenderingStrategy.MYBATIS3);

String expectedStatment = "select ol.order_id, ol.quantity, im.item_id, im.description"
+ " from ItemMaster im left join OrderLine ol on ol.item_id = im.item_id"
+ " fetch first #{parameters._fetchFirstRows} rows only";
assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedStatment);

List<Map<String, Object>> rows = mapper.generalSelect(selectStatement);

assertThat(rows.size()).isEqualTo(2);
Map<String, Object> row = rows.get(0);
assertThat(row.get("ORDER_ID")).isEqualTo(1);
assertThat(row.get("QUANTITY")).isEqualTo(1);
assertThat(row.get("DESCRIPTION")).isEqualTo("Helmet");
assertThat(row.get("ITEM_ID")).isEqualTo(22);

row = rows.get(1);
assertThat(row.get("ORDER_ID")).isEqualTo(2);
assertThat(row.get("QUANTITY")).isEqualTo(1);
assertThat(row.get("DESCRIPTION")).isEqualTo("Helmet");
assertThat(row.get("ITEM_ID")).isEqualTo(22);
}
}
}