Skip to content

Commit

Permalink
Merge pull request #904 from jeffgbutler/select-enhancements
Browse files Browse the repository at this point in the history
Add Support for Various Locking Functions in Select Statements
  • Loading branch information
jeffgbutler authored Jan 24, 2025
2 parents a645046 + 2a1b8da commit 173ad6b
Show file tree
Hide file tree
Showing 22 changed files with 817 additions and 216 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ Runtime behavior changes:
rendering if a null value is passed in
- The JOIN syntax is updated and now allows full boolean expressions like a WHERE clause. The prior JOIN syntax
is deprecated and will be removed in a future release.
- Add support for locking options in select statements (for update, for share, etc.) This is not an abstraction of
these concepts for different databases it simply adds known clauses to a generated SQL statement. You should always
test to make sure these functions work in your target database. Currently, we support, and test, the options
supported by PostgreSQL.

## Release 1.5.2 - June 3, 2024

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.VisitableCondition;

public abstract class AbstractHavingStarter<F extends AbstractHavingFinisher<?>> {
public interface AbstractHavingStarter<F extends AbstractHavingFinisher<?>> {

public <T> F having(BindableColumn<T> column, VisitableCondition<T> condition,
default <T> F having(BindableColumn<T> column, VisitableCondition<T> condition,
AndOrCriteriaGroup... subCriteria) {
return having(column, condition, Arrays.asList(subCriteria));
}

public <T> F having(BindableColumn<T> column, VisitableCondition<T> condition,
default <T> F having(BindableColumn<T> column, VisitableCondition<T> condition,
List<AndOrCriteriaGroup> subCriteria) {
SqlCriterion sqlCriterion = ColumnAndConditionCriterion.withColumn(column)
.withCondition(condition)
Expand All @@ -42,11 +42,11 @@ public <T> F having(BindableColumn<T> column, VisitableCondition<T> condition,
return initialize(sqlCriterion);
}

public F having(SqlCriterion initialCriterion, AndOrCriteriaGroup... subCriteria) {
default F having(SqlCriterion initialCriterion, AndOrCriteriaGroup... subCriteria) {
return having(initialCriterion, Arrays.asList(subCriteria));
}

public F having(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
default F having(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
SqlCriterion sqlCriterion = new CriteriaGroup.Builder()
.withInitialCriterion(initialCriterion)
.withSubCriteria(subCriteria)
Expand All @@ -55,9 +55,9 @@ public F having(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCrite
return initialize(sqlCriterion);
}

protected abstract F having();
F having();

public F applyHaving(HavingApplier havingApplier) {
default F applyHaving(HavingApplier havingApplier) {
F finisher = having();
havingApplier.accept(finisher);
return finisher;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/mybatis/dynamic/sql/select/HavingDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

import org.mybatis.dynamic.sql.util.Buildable;

public class HavingDSL extends AbstractHavingStarter<HavingDSL.StandaloneHavingFinisher> {
public class HavingDSL implements AbstractHavingStarter<HavingDSL.StandaloneHavingFinisher> {
private final StandaloneHavingFinisher havingFinisher = new StandaloneHavingFinisher();

@Override
protected StandaloneHavingFinisher having() {
public StandaloneHavingFinisher having() {
return havingFinisher;
}

Expand Down
63 changes: 43 additions & 20 deletions src/main/java/org/mybatis/dynamic/sql/select/MultiSelectDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.ConfigurableStatement;

public class MultiSelectDSL implements Buildable<MultiSelectModel>, ConfigurableStatement<MultiSelectDSL>,
PagingDSL<MultiSelectModel> {
public class MultiSelectDSL implements Buildable<MultiSelectModel>, ConfigurableStatement<MultiSelectDSL> {
private final List<UnionQuery> unionQueries = new ArrayList<>();
private final SelectModel initialSelect;
private @Nullable OrderByModel orderByModel;
Expand Down Expand Up @@ -62,22 +61,31 @@ public MultiSelectDSL orderBy(Collection<? extends SortSpecification> columns) {
return this;
}

@Override
public LimitFinisher<MultiSelectModel> limitWhenPresent(@Nullable Long limit) {
public LimitFinisher limit(long limit) {
return limitWhenPresent(limit);
}

public LimitFinisher limitWhenPresent(@Nullable Long limit) {
this.limit = limit;
return new LocalLimitFinisher();
return new LimitFinisher();
}

@Override
public OffsetFirstFinisher<MultiSelectModel> offsetWhenPresent(@Nullable Long offset) {
public OffsetFirstFinisher offset(long offset) {
return offsetWhenPresent(offset);
}

public OffsetFirstFinisher offsetWhenPresent(@Nullable Long offset) {
this.offset = offset;
return new LocalOffsetFirstFinisher();
return new OffsetFirstFinisher();
}

@Override
public FetchFirstFinisher<MultiSelectModel> fetchFirstWhenPresent(@Nullable Long fetchFirstRows) {
public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return fetchFirstWhenPresent(fetchFirstRows);
}

public FetchFirstFinisher fetchFirstWhenPresent(@Nullable Long fetchFirstRows) {
this.fetchFirstRows = fetchFirstRows;
return () -> this;
return new FetchFirstFinisher();
}

@Override
Expand Down Expand Up @@ -105,25 +113,40 @@ public MultiSelectDSL configureStatement(Consumer<StatementConfiguration> consum
return this;
}

abstract class BaseBuildable implements Buildable<MultiSelectModel> {
public class OffsetFirstFinisher implements Buildable<MultiSelectModel> {
public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return fetchFirstWhenPresent(fetchFirstRows);
}

public FetchFirstFinisher fetchFirstWhenPresent(@Nullable Long fetchFirstRows) {
MultiSelectDSL.this.fetchFirstRows = fetchFirstRows;
return new FetchFirstFinisher();
}

@Override
public MultiSelectModel build() {
return MultiSelectDSL.this.build();
}
}

class LocalOffsetFirstFinisher extends BaseBuildable implements OffsetFirstFinisher<MultiSelectModel> {
public class LimitFinisher implements Buildable<MultiSelectModel> {
public MultiSelectDSL offset(long offset) {
return offsetWhenPresent(offset);
}

public MultiSelectDSL offsetWhenPresent(@Nullable Long offset) {
MultiSelectDSL.this.offset = offset;
return MultiSelectDSL.this;
}

@Override
public FetchFirstFinisher<MultiSelectModel> fetchFirstWhenPresent(Long fetchFirstRows) {
MultiSelectDSL.this.fetchFirstRows = fetchFirstRows;
return () -> MultiSelectDSL.this;
public MultiSelectModel build() {
return MultiSelectDSL.this.build();
}
}

class LocalLimitFinisher extends BaseBuildable implements LimitFinisher<MultiSelectModel> {
@Override
public Buildable<MultiSelectModel> offsetWhenPresent(Long offset) {
MultiSelectDSL.this.offset = offset;
public class FetchFirstFinisher {
public MultiSelectDSL rowsOnly() {
return MultiSelectDSL.this;
}
}
Expand Down
59 changes: 0 additions & 59 deletions src/main/java/org/mybatis/dynamic/sql/select/PagingDSL.java

This file was deleted.

Loading

0 comments on commit 173ad6b

Please sign in to comment.