Skip to content

Commit

Permalink
Merge pull request #831 from jeffgbutler/column-hierarchy-refactoring
Browse files Browse the repository at this point in the history
Allow Case Expressions in the Sum Function
  • Loading branch information
jeffgbutler authored Aug 7, 2024
2 parents e2d75aa + 406220c commit 8263897
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 56 deletions.
18 changes: 16 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are av

## Release 2.0.0 - Unreleased

Significant changes:
Release 2.0.0 is a significant milestone for the library. We have moved to Java 17 as the minimum version supported. If
you are unable to move to this version of Java then the releases in the 1.x line can be used with Java 8.

In addition, we have taken the opportunity to make changes to the library that may break existing code. We have
worked to make these changes as minimal as possible.

**Potentially Breaking Changes:**

- If you have implemented any custom functions, you will likely need to make changes. The supplied base classes now
hold an instance of `BasicColumn` rather than `BindableColumn`. This change was made to make the functions more
useful in variety of circumstances. If you follow the patterns shown on the
[Extending the Library](https://mybatis.org/mybatis-dynamic-sql/docs/extending.html) page, the change should be
limited to changing the private constructor to accept `BasicColumn` rather than `BindableColumn`.

Other important changes:

- The library now requires Java 17
- Deprecated code from prior releases is removed
- Allow CASE expressions in ORDER BY Clauses
- We now allow CASE expressions in ORDER BY Clauses

## Release 1.5.2 - June 3, 2024

Expand Down
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
<module.name>org.mybatis.dynamic.sql</module.name>

<kotlin.version>2.0.10</kotlin.version>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<kotlin.compiler.languageVersion>2.0</kotlin.compiler.languageVersion>
<kotlin.compiler.apiVersion>2.0</kotlin.compiler.apiVersion>

<sonar.sources>pom.xml,src/main/java,src/main/kotlin</sonar.sources>
<sonar.tests>src/test/java,src/test/kotlin</sonar.tests>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/BasicColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package org.mybatis.dynamic.sql;

import java.sql.JDBCType;
import java.util.Optional;

import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;

/**
Expand Down Expand Up @@ -58,6 +60,18 @@ public interface BasicColumn {
*/
FragmentAndParameters render(RenderingContext renderingContext);

default Optional<JDBCType> jdbcType() {
return Optional.empty();
}

default Optional<String> typeHandler() {
return Optional.empty();
}

default Optional<RenderingStrategy> renderingStrategy() {
return Optional.empty();
}

/**
* Utility method to make it easier to build column lists for methods that require an
* array rather than the varargs method.
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/org/mybatis/dynamic/sql/BindableColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
*/
package org.mybatis.dynamic.sql;

import java.sql.JDBCType;
import java.util.Optional;

import org.mybatis.dynamic.sql.render.RenderingStrategy;

/**
* Describes additional attributes of columns that are necessary for binding the column as a JDBC parameter.
* Columns in where clauses are typically bound.
* Describes a column with a known data type. The type is only used by the compiler to assure type safety
* when building clauses with conditions.
*
* @author Jeff Butler
*
Expand All @@ -37,18 +34,6 @@ public interface BindableColumn<T> extends BasicColumn {
@Override
BindableColumn<T> as(String alias);

default Optional<JDBCType> jdbcType() {
return Optional.empty();
}

default Optional<String> typeHandler() {
return Optional.empty();
}

default Optional<RenderingStrategy> renderingStrategy() {
return Optional.empty();
}

default Object convertParameterType(T value) {
return value;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ static <T> Sum<T> sum(BindableColumn<T> column) {
return Sum.of(column);
}

static Sum<Object> sum(BasicColumn column) {
return Sum.of(column);
}

static <T> Sum<T> sum(BindableColumn<T> column, VisitableCondition<T> condition) {
return Sum.of(column, condition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.mybatis.dynamic.sql.select.aggregate;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.select.function.AbstractUniTypeFunction;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;

public class Avg<T> extends AbstractUniTypeFunction<T, Avg<T>> {

private Avg(BindableColumn<T> column) {
private Avg(BasicColumn column) {
super(column);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.mybatis.dynamic.sql.select.aggregate;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.select.function.AbstractUniTypeFunction;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;

public class Max<T> extends AbstractUniTypeFunction<T, Max<T>> {

private Max(BindableColumn<T> column) {
private Max(BasicColumn column) {
super(column);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.mybatis.dynamic.sql.select.aggregate;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.select.function.AbstractUniTypeFunction;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;

public class Min<T> extends AbstractUniTypeFunction<T, Min<T>> {

private Min(BindableColumn<T> column) {
private Min(BasicColumn column) {
super(column);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.function.Function;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.VisitableCondition;
import org.mybatis.dynamic.sql.render.RenderingContext;
Expand All @@ -28,7 +29,7 @@
public class Sum<T> extends AbstractUniTypeFunction<T, Sum<T>> {
private final Function<RenderingContext, FragmentAndParameters> renderer;

private Sum(BindableColumn<T> column) {
private Sum(BasicColumn column) {
super(column);
renderer = rc -> column.render(rc).mapFragment(this::applyAggregate);
}
Expand All @@ -48,7 +49,7 @@ private Sum(BindableColumn<T> column, VisitableCondition<T> condition) {
};
}

private Sum(BindableColumn<T> column, Function<RenderingContext, FragmentAndParameters> renderer) {
private Sum(BasicColumn column, Function<RenderingContext, FragmentAndParameters> renderer) {
super(column);
this.renderer = renderer;
}
Expand All @@ -71,6 +72,10 @@ public static <T> Sum<T> of(BindableColumn<T> column) {
return new Sum<>(column);
}

public static Sum<Object> of(BasicColumn column) {
return new Sum<>(column);
}

public static <T> Sum<T> of(BindableColumn<T> column, VisitableCondition<T> condition) {
return new Sum<>(column, condition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;

/**
Expand All @@ -36,10 +37,10 @@
*/
public abstract class AbstractTypeConvertingFunction<T, R, U extends AbstractTypeConvertingFunction<T, R, U>>
implements BindableColumn<R> {
protected final BindableColumn<T> column;
protected final BasicColumn column;
protected String alias;

protected AbstractTypeConvertingFunction(BindableColumn<T> column) {
protected AbstractTypeConvertingFunction(BasicColumn column) {
this.column = Objects.requireNonNull(column);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.sql.JDBCType;
import java.util.Optional;

import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.BasicColumn;

/**
* Represents a function that does not change the underlying data type.
Expand All @@ -33,7 +33,7 @@
public abstract class AbstractUniTypeFunction<T, U extends AbstractUniTypeFunction<T, U>>
extends AbstractTypeConvertingFunction<T, T, U> {

protected AbstractUniTypeFunction(BindableColumn<T> column) {
protected AbstractUniTypeFunction(BasicColumn column) {
super(column);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class Add<T> extends OperatorFunction<T> {

private Add(BindableColumn<T> firstColumn, BasicColumn secondColumn,
private Add(BasicColumn firstColumn, BasicColumn secondColumn,
List<BasicColumn> subsequentColumns) {
super("+", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class Concat<T> extends AbstractUniTypeFunction<T, Concat<T>> {
private final List<BasicColumn> allColumns = new ArrayList<>();

protected Concat(BindableColumn<T> firstColumn, List<BasicColumn> subsequentColumns) {
protected Concat(BasicColumn firstColumn, List<BasicColumn> subsequentColumns) {
super(firstColumn);
allColumns.add(firstColumn);
this.allColumns.addAll(subsequentColumns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class Concatenate<T> extends OperatorFunction<T> {

protected Concatenate(BindableColumn<T> firstColumn, BasicColumn secondColumn,
protected Concatenate(BasicColumn firstColumn, BasicColumn secondColumn,
List<BasicColumn> subsequentColumns) {
super("||", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class Divide<T> extends OperatorFunction<T> {

private Divide(BindableColumn<T> firstColumn, BasicColumn secondColumn,
private Divide(BasicColumn firstColumn, BasicColumn secondColumn,
List<BasicColumn> subsequentColumns) {
super("/", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/
package org.mybatis.dynamic.sql.select.function;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;

public class Lower<T> extends AbstractUniTypeFunction<T, Lower<T>> {

private Lower(BindableColumn<T> column) {
private Lower(BasicColumn column) {
super(column);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class Multiply<T> extends OperatorFunction<T> {

private Multiply(BindableColumn<T> firstColumn, BasicColumn secondColumn,
private Multiply(BasicColumn firstColumn, BasicColumn secondColumn,
List<BasicColumn> subsequentColumns) {
super("*", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class OperatorFunction<T> extends AbstractUniTypeFunction<T, OperatorFunc
protected final List<BasicColumn> subsequentColumns = new ArrayList<>();
private final String operator;

protected OperatorFunction(String operator, BindableColumn<T> firstColumn, BasicColumn secondColumn,
protected OperatorFunction(String operator, BasicColumn firstColumn, BasicColumn secondColumn,
List<BasicColumn> subsequentColumns) {
super(firstColumn);
this.secondColumn = Objects.requireNonNull(secondColumn);
Expand All @@ -52,9 +52,7 @@ protected OperatorFunction<T> copy() {
public FragmentAndParameters render(RenderingContext renderingContext) {
String paddedOperator = " " + operator + " "; //$NON-NLS-1$ //$NON-NLS-2$

// note - the cast below is added for type inference issues in some compilers
return Stream.of(Stream.of((BasicColumn) column),
Stream.of(secondColumn), subsequentColumns.stream())
return Stream.of(Stream.of(column, secondColumn), subsequentColumns.stream())
.flatMap(Function.identity())
.map(column -> column.render(renderingContext))
.collect(FragmentCollector.collect())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.mybatis.dynamic.sql.select.function;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
Expand All @@ -24,7 +25,7 @@ public class Substring<T> extends AbstractUniTypeFunction<T, Substring<T>> {
private final int offset;
private final int length;

private Substring(BindableColumn<T> column, int offset, int length) {
private Substring(BasicColumn column, int offset, int length) {
super(column);
this.offset = offset;
this.length = length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class Subtract<T> extends OperatorFunction<T> {

private Subtract(BindableColumn<T> firstColumn, BasicColumn secondColumn,
private Subtract(BasicColumn firstColumn, BasicColumn secondColumn,
List<BasicColumn> subsequentColumns) {
super("-", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/
package org.mybatis.dynamic.sql.select.function;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;

public class Upper<T> extends AbstractUniTypeFunction<T, Upper<T>> {

private Upper(BindableColumn<T> column) {
private Upper(BasicColumn column) {
super(column);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ fun <T> avg(column: BindableColumn<T>): Avg<T> = SqlBuilder.avg(column)

fun <T> sum(column: BindableColumn<T>): Sum<T> = SqlBuilder.sum(column)

fun sum(column: BasicColumn): Sum<*> = SqlBuilder.sum(column)

fun <T> sum(column: BindableColumn<T>, condition: VisitableCondition<T>): Sum<T> = SqlBuilder.sum(column, condition)

// constants
Expand Down
Loading

0 comments on commit 8263897

Please sign in to comment.