Skip to content

support functions/expression in condition #655

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

Closed
mmm8955405 opened this issue Aug 7, 2023 · 3 comments
Closed

support functions/expression in condition #655

mmm8955405 opened this issue Aug 7, 2023 · 3 comments

Comments

@mmm8955405
Copy link

mmm8955405 commented Aug 7, 2023

SELECT name, SUM ( id>1)
FROM foo
WHERE ..
public class Sum<T> extends AbstractUniTypeFunction<T, Sum<T>> {

    private final VisitableCondition<T> condition;

    private Sum(BindableColumn<T> column, VisitableCondition<T> condition) {
        super(column);
        this.condition = condition;
    }

    @Override
    public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
    	ColumnAndConditionCriterion<T>  criteion = ColumnAndConditionCriterion
    			.withColumn(column)
    			.withCondition(condition).build();
        String s = criteion.toString();
    	
        return "sum(" //$NON-NLS-1$
                + column.renderWithTableAlias(tableAliasCalculator)
                + s
                + ")"; //$NON-NLS-1$
    }

    @Override
    protected Sum<T> copy() {
        return new Sum<>(column, condition);
    }

    public static <T> Sum<T> of(BindableColumn<T> column, VisitableCondition<T> condition) {
        return new Sum<>(column, condition);
    }
}

How to render VisitableCondition ?

I see that this method is outdated

CriterionRenderer.withCriterion(subCriterion).withSequence(sequence)
  .withRenderingStrategy(renderingStrategy)
  .withTableAliasCalculator(tableAliasCalculator)
  .build()
  .render();
@jeffgbutler
Copy link
Member

criterion.toString() won't work - you will need to use the CriterionRenderer. But you won't have access to the required parameters for the builder. I'll need to make a change to support rendering for cases like this. Shouldn't be too difficult.

@mmm8955405
Copy link
Author

If that's the case, it's really great

@jeffgbutler
Copy link
Member

I've updated the rendering to support this. I also went ahead and implemented the change to the existing sum function to support the original request. You can see how it is implemented here: https://github.com/mybatis/mybatis-dynamic-sql/blob/master/src/main/java/org/mybatis/dynamic/sql/select/aggregate/Sum.java

Thanks for the idea - it caused a good refactoring in the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants