|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.expression; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.function.Consumer; |
| 16 | + |
| 17 | +/** |
| 18 | + * DuckDB's list comprehension {@code [expression FOR x[, i] IN list [IF condition]]}, where |
| 19 | + * {@code i} is the 1-based index of {@code x}. |
| 20 | + * |
| 21 | + * @see <a href="https://duckdb.org/docs/stable/sql/functions/list#list-comprehension">List |
| 22 | + * comprehension</a> |
| 23 | + */ |
| 24 | +public class ListComprehension extends ASTNodeAccessImpl implements Expression { |
| 25 | + private Expression expression; |
| 26 | + private List<String> variables; |
| 27 | + private Expression list; |
| 28 | + private Expression condition; |
| 29 | + |
| 30 | + public ListComprehension(Expression expression, List<String> variables, Expression list, |
| 31 | + Expression condition) { |
| 32 | + this.expression = expression; |
| 33 | + this.variables = variables; |
| 34 | + this.list = list; |
| 35 | + this.condition = condition; |
| 36 | + } |
| 37 | + |
| 38 | + public Expression getExpression() { |
| 39 | + return expression; |
| 40 | + } |
| 41 | + |
| 42 | + public ListComprehension setExpression(Expression expression) { |
| 43 | + this.expression = expression; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + /** The element variable, followed by the index variable if any. */ |
| 48 | + public List<String> getVariables() { |
| 49 | + return variables; |
| 50 | + } |
| 51 | + |
| 52 | + public ListComprehension setVariables(List<String> variables) { |
| 53 | + this.variables = variables; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public Expression getList() { |
| 58 | + return list; |
| 59 | + } |
| 60 | + |
| 61 | + public ListComprehension setList(Expression list) { |
| 62 | + this.list = list; |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /** The {@code IF} filter, or {@code null}. */ |
| 67 | + public Expression getCondition() { |
| 68 | + return condition; |
| 69 | + } |
| 70 | + |
| 71 | + public ListComprehension setCondition(Expression condition) { |
| 72 | + this.condition = condition; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressionRenderer) { |
| 77 | + builder.append("["); |
| 78 | + expressionRenderer.accept(expression); |
| 79 | + builder.append(" FOR ").append(String.join(", ", variables)).append(" IN "); |
| 80 | + expressionRenderer.accept(list); |
| 81 | + if (condition != null) { |
| 82 | + builder.append(" IF "); |
| 83 | + expressionRenderer.accept(condition); |
| 84 | + } |
| 85 | + return builder.append("]"); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String toString() { |
| 90 | + StringBuilder builder = new StringBuilder(); |
| 91 | + return appendTo(builder, builder::append).toString(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) { |
| 96 | + return expressionVisitor.visit(this, context); |
| 97 | + } |
| 98 | +} |
0 commit comments