@Override public Node visitWhenClause(final SqlBaseParser.WhenClauseContext context) { return new WhenClause( getLocation(context), (Expression) visit(context.condition), (Expression) visit(context.result) ); }
protected Node visitWhenClause(final WhenClause node, final Object context) { // use an if/else block here (instead of isPresent.map(...).orElse(...)) so only one object // gets instantiated (issue #1784) if (node.getLocation().isPresent()) { return new WhenClause(node.getLocation().get(), (Expression) process(node.getOperand(), context), (Expression) process(node.getResult(), context)); } else { return new WhenClause((Expression) process(node.getOperand(), context), (Expression) process(node.getResult(), context)); } }
@Test public void shouldFormatSearchedCaseExpression() { final SearchedCaseExpression expression = new SearchedCaseExpression( Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.empty()); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE WHEN 'foo' THEN 1 END)")); }
@Test public void shouldFormatSearchedCaseExpressionWithDefaultValue() { final SearchedCaseExpression expression = new SearchedCaseExpression( Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.of(new LongLiteral(2))); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE WHEN 'foo' THEN 1 ELSE 2 END)")); }
@Test public void shouldFormatSimpleCaseExpression() { final SimpleCaseExpression expression = new SimpleCaseExpression( new StringLiteral("operand"), Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.empty()); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE 'operand' WHEN 'foo' THEN 1 END)")); }
@Test public void shouldFormatSimpleCaseExpressionWithDefaultValue() { final SimpleCaseExpression expression = new SimpleCaseExpression( new StringLiteral("operand"), Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.of(new LongLiteral(2))); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE 'operand' WHEN 'foo' THEN 1 ELSE 2 END)")); }
@Test public void shouldFormatWhen() { assertThat(ExpressionFormatter.formatExpression(new WhenClause(new LongLiteral(1), new LongLiteral(2))), equalTo("WHEN 1 THEN 2")); }