|
9 | 9 | */ |
10 | 10 | package net.sf.jsqlparser.statement; |
11 | 11 |
|
| 12 | +import java.util.List; |
12 | 13 | import java.util.function.Consumer; |
| 14 | +import net.sf.jsqlparser.expression.Expression; |
13 | 15 | import net.sf.jsqlparser.expression.StringValue; |
14 | 16 | import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 17 | +import net.sf.jsqlparser.statement.select.SelectItem; |
15 | 18 |
|
16 | | -/** A PostgreSQL anonymous routine. The language-specific body remains a string literal. */ |
| 19 | +/** |
| 20 | + * A {@code DO} statement: a PostgreSQL anonymous routine, whose language-specific body remains a |
| 21 | + * string literal, or MySQL's {@code DO expr [, expr] ...}, which evaluates expressions without |
| 22 | + * returning a result. MySQL reads the expressions as a select list, so they can carry aliases. |
| 23 | + * |
| 24 | + * @see <a href="https://www.postgresql.org/docs/18/sql-do.html">PostgreSQL DO</a> |
| 25 | + * @see <a href="https://dev.mysql.com/doc/refman/8.4/en/do.html">MySQL DO</a> |
| 26 | + */ |
17 | 27 | public class DoStatement extends ASTNodeAccessImpl implements Statement { |
18 | 28 | private StringValue code; |
19 | 29 | private String language; |
20 | 30 | private boolean languageBeforeCode; |
| 31 | + private List<SelectItem<?>> selectItems; |
21 | 32 |
|
22 | 33 | public StringValue getCode() { |
23 | 34 | return code; |
@@ -54,8 +65,48 @@ public void setLanguageBeforeCode(boolean languageBeforeCode) { |
54 | 65 | this.languageBeforeCode = languageBeforeCode; |
55 | 66 | } |
56 | 67 |
|
| 68 | + /** The expressions of MySQL's form, or null for a PostgreSQL code block. */ |
| 69 | + public List<SelectItem<?>> getSelectItems() { |
| 70 | + return selectItems; |
| 71 | + } |
| 72 | + |
| 73 | + public void setSelectItems(List<SelectItem<?>> selectItems) { |
| 74 | + this.selectItems = selectItems; |
| 75 | + } |
| 76 | + |
| 77 | + public DoStatement withSelectItems(List<SelectItem<?>> selectItems) { |
| 78 | + setSelectItems(selectItems); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + /** Visits the code block, or the expression of every select item. */ |
| 83 | + public void visitExpressions(Consumer<Expression> visitor) { |
| 84 | + if (selectItems != null) { |
| 85 | + for (SelectItem<?> item : selectItems) { |
| 86 | + visitor.accept(item.getExpression()); |
| 87 | + } |
| 88 | + } else if (code != null) { |
| 89 | + visitor.accept(code); |
| 90 | + } |
| 91 | + } |
| 92 | + |
57 | 93 | public StringBuilder appendTo(StringBuilder builder, Consumer<StringValue> codePrinter) { |
| 94 | + return appendTo(builder, codePrinter, builder::append); |
| 95 | + } |
| 96 | + |
| 97 | + /** Renders the code block or the select items through the caller's writers. */ |
| 98 | + public StringBuilder appendTo(StringBuilder builder, Consumer<StringValue> codePrinter, |
| 99 | + Consumer<SelectItem<?>> itemPrinter) { |
58 | 100 | builder.append("DO "); |
| 101 | + if (selectItems != null) { |
| 102 | + for (int i = 0; i < selectItems.size(); i++) { |
| 103 | + if (i > 0) { |
| 104 | + builder.append(", "); |
| 105 | + } |
| 106 | + itemPrinter.accept(selectItems.get(i)); |
| 107 | + } |
| 108 | + return builder; |
| 109 | + } |
59 | 110 | if (languageBeforeCode && language != null) { |
60 | 111 | builder.append("LANGUAGE ").append(language).append(' '); |
61 | 112 | } |
|
0 commit comments