|
9 | 9 | */ |
10 | 10 | package net.sf.jsqlparser.expression; |
11 | 11 |
|
| 12 | +import java.util.ArrayList; |
12 | 13 | import java.util.List; |
| 14 | +import java.util.function.Consumer; |
13 | 15 |
|
14 | 16 | import static java.util.stream.Collectors.joining; |
15 | 17 |
|
|
19 | 21 |
|
20 | 22 | public class XMLSerializeExpr extends ASTNodeAccessImpl implements Expression { |
21 | 23 |
|
| 24 | + public enum SerializationMode { |
| 25 | + CONTENT, DOCUMENT |
| 26 | + } |
| 27 | + |
22 | 28 | private Expression expression; |
23 | 29 | private List<OrderByElement> orderByElements; |
24 | 30 | private ColDataType dataType; |
| 31 | + private SerializationMode serializationMode; |
| 32 | + private StringValue encoding; |
| 33 | + private StringValue version; |
| 34 | + private Boolean indent; |
| 35 | + private LongValue indentSize; |
| 36 | + private Boolean showDefaults; |
25 | 37 |
|
26 | 38 | @Override |
27 | 39 | public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) { |
@@ -52,11 +64,141 @@ public void setDataType(ColDataType dataType) { |
52 | 64 | this.dataType = dataType; |
53 | 65 | } |
54 | 66 |
|
| 67 | + /** Null retains the legacy XMLAGG(XMLTEXT(...)) form and its existing expression getter. */ |
| 68 | + public SerializationMode getSerializationMode() { |
| 69 | + return serializationMode; |
| 70 | + } |
| 71 | + |
| 72 | + public void setSerializationMode(SerializationMode serializationMode) { |
| 73 | + this.serializationMode = serializationMode; |
| 74 | + } |
| 75 | + |
| 76 | + public StringValue getEncoding() { |
| 77 | + return encoding; |
| 78 | + } |
| 79 | + |
| 80 | + public void setEncoding(StringValue encoding) { |
| 81 | + this.encoding = encoding; |
| 82 | + } |
| 83 | + |
| 84 | + public StringValue getVersion() { |
| 85 | + return version; |
| 86 | + } |
| 87 | + |
| 88 | + public void setVersion(StringValue version) { |
| 89 | + this.version = version; |
| 90 | + } |
| 91 | + |
| 92 | + /** Null preserves omission, true is INDENT, and false is NO INDENT. */ |
| 93 | + public Boolean getIndent() { |
| 94 | + return indent; |
| 95 | + } |
| 96 | + |
| 97 | + public void setIndent(Boolean indent) { |
| 98 | + this.indent = indent; |
| 99 | + } |
| 100 | + |
| 101 | + public LongValue getIndentSize() { |
| 102 | + return indentSize; |
| 103 | + } |
| 104 | + |
| 105 | + public void setIndentSize(LongValue indentSize) { |
| 106 | + this.indentSize = indentSize; |
| 107 | + } |
| 108 | + |
| 109 | + /** Null preserves omission, true is SHOW DEFAULTS, and false is HIDE DEFAULTS. */ |
| 110 | + public Boolean getShowDefaults() { |
| 111 | + return showDefaults; |
| 112 | + } |
| 113 | + |
| 114 | + public void setShowDefaults(Boolean showDefaults) { |
| 115 | + this.showDefaults = showDefaults; |
| 116 | + } |
| 117 | + |
| 118 | + /** Shared child discovery for expression, table-name and validation visitors. */ |
| 119 | + public List<Expression> getExpressions() { |
| 120 | + List<Expression> result = new ArrayList<>(); |
| 121 | + if (expression != null) { |
| 122 | + result.add(expression); |
| 123 | + } |
| 124 | + if (orderByElements != null) { |
| 125 | + for (OrderByElement orderBy : orderByElements) { |
| 126 | + result.add(orderBy.getExpression()); |
| 127 | + } |
| 128 | + } |
| 129 | + if (encoding != null) { |
| 130 | + result.add(encoding); |
| 131 | + } |
| 132 | + if (version != null) { |
| 133 | + result.add(version); |
| 134 | + } |
| 135 | + if (indentSize != null) { |
| 136 | + result.add(indentSize); |
| 137 | + } |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + /** Render both forms without bypassing custom expression or ORDER BY deparsers. */ |
| 142 | + public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> expressionWriter, |
| 143 | + Consumer<List<OrderByElement>> orderByWriter) { |
| 144 | + validateOptions(); |
| 145 | + sql.append("xmlserialize("); |
| 146 | + if (serializationMode == null) { |
| 147 | + sql.append("xmlagg(xmltext("); |
| 148 | + expressionWriter.accept(expression); |
| 149 | + sql.append(")"); |
| 150 | + if (orderByElements != null) { |
| 151 | + orderByWriter.accept(orderByElements); |
| 152 | + } |
| 153 | + sql.append(") AS ").append(dataType); |
| 154 | + } else { |
| 155 | + sql.append(serializationMode).append(" "); |
| 156 | + expressionWriter.accept(expression); |
| 157 | + if (dataType != null) { |
| 158 | + sql.append(" AS ").append(dataType); |
| 159 | + } |
| 160 | + if (encoding != null) { |
| 161 | + sql.append(" ENCODING "); |
| 162 | + expressionWriter.accept(encoding); |
| 163 | + } |
| 164 | + if (version != null) { |
| 165 | + sql.append(" VERSION "); |
| 166 | + expressionWriter.accept(version); |
| 167 | + } |
| 168 | + if (indent != null) { |
| 169 | + sql.append(indent ? " INDENT" : " NO INDENT"); |
| 170 | + if (indent && indentSize != null) { |
| 171 | + sql.append(" SIZE = "); |
| 172 | + expressionWriter.accept(indentSize); |
| 173 | + } |
| 174 | + } |
| 175 | + if (showDefaults != null) { |
| 176 | + sql.append(showDefaults ? " SHOW DEFAULTS" : " HIDE DEFAULTS"); |
| 177 | + } |
| 178 | + } |
| 179 | + return sql.append(")"); |
| 180 | + } |
| 181 | + |
| 182 | + public void validateOptions() { |
| 183 | + if (indentSize != null && (!Boolean.TRUE.equals(indent) || indentSize.getValue() < 0)) { |
| 184 | + throw new IllegalArgumentException( |
| 185 | + "An indentation size requires INDENT and must be nonnegative"); |
| 186 | + } |
| 187 | + if (serializationMode == null && (encoding != null || version != null || indent != null |
| 188 | + || indentSize != null || showDefaults != null)) { |
| 189 | + throw new IllegalArgumentException("Serialization options require CONTENT or DOCUMENT"); |
| 190 | + } |
| 191 | + if (serializationMode != null && orderByElements != null && !orderByElements.isEmpty()) { |
| 192 | + throw new IllegalArgumentException( |
| 193 | + "ORDER BY belongs inside the serialized XMLAGG expression"); |
| 194 | + } |
| 195 | + } |
| 196 | + |
55 | 197 | @Override |
56 | 198 | public String toString() { |
57 | | - return "xmlserialize(xmlagg(xmltext(" + expression + ")" |
58 | | - + (orderByElements != null ? " ORDER BY " + orderByElements.stream() |
59 | | - .map(OrderByElement::toString).collect(joining(", ")) : "") |
60 | | - + ") AS " + dataType + ")"; |
| 199 | + StringBuilder sql = new StringBuilder(); |
| 200 | + return appendTo(sql, sql::append, orderBy -> sql.append(" ORDER BY ") |
| 201 | + .append(orderBy.stream().map(OrderByElement::toString).collect(joining(", ")))) |
| 202 | + .toString(); |
61 | 203 | } |
62 | 204 | } |
0 commit comments