-
Couldn't load subscription status.
- Fork 377
Add support for CASE statements #1844
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
Add support for CASE statements #1844
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm asking for a few minor changes.
| */ | ||
| public class CaseExpression extends AbstractSegment implements Expression { | ||
| private final List<When> whenList = new ArrayList<>(); | ||
| private final Literal other; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other is a undescriptive as it gets. I guess we can't use else or default since those are Java keywords.
But elseBranch would be a better name, or maybe whenElse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also other should be an Expression, not constraint to a Literal
| */ | ||
| public CaseExpression when(When condition) { | ||
| this.whenList.add(condition); | ||
| return new CaseExpression(whenList, other); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it uses CaseExpression as an immutable, but it actually modifies the whenList. We should avoid that modification.
| */ | ||
| public class When extends AbstractSegment { | ||
| private final Expression condition; | ||
| private final Literal value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To answer your question: Yes, please make value an expression.
| * @since 3.4 | ||
| */ | ||
| public class When extends AbstractSegment { | ||
| private final Expression condition; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
condition should be an actual Condition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: there are other forms of CASE expressions, where you have an expression to start with and in each branch an expression to compare it to. See https://modern-sql.com/feature/case
But that is not what we are doing here.
| Thanks for the review @schauder. I think I addressed all your comments. |
Minor formatting. Adding author tag in Javadoc. Fixing warnings. Original pull request #1844
| Thanks. That is polished and merged. |
This PR add supports for CASE statements. Added 4 new types:
CaseExpressionandWhenplus their respective visitors.Also I changed the order of visitors in
TypedSingleConditionRenderSupport. BecauseConditionextendsExpressiontheExpressionVisitorwas used forConditions.Question: Should I add support for any
Expressionin the THEN and OTHER clause (versusLiteralnow)?