Skip to content

Commit 3747f1c

Browse files
committed
xmlserialize support patch for optional order by part
1 parent 8c4e0ca commit 3747f1c

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/main/java/net/sf/jsqlparser/expression/XMLSerializeExpr.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public void setDataType(ColDataType dataType) {
5252

5353
@Override
5454
public String toString() {
55-
return "xmlserialize(xmlagg(xmltext(" + expression + ") ORDER BY " +
56-
orderByElements.stream().map(item -> item.toString()).collect(joining(", ")) +
57-
") AS " + dataType + ")";
55+
return "xmlserialize(xmlagg(xmltext(" + expression + ")"
56+
+ (orderByElements != null ? " ORDER BY " + orderByElements.stream().map(item -> item.toString()).collect(joining(", ")) : "")
57+
+ ") AS " + dataType + ")";
5858
}
5959
}

src/main/java/net/sf/jsqlparser/util/deparser/ExpressionDeParser.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,14 @@ public void visit(XMLSerializeExpr expr) {
908908
//xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) as varchar(1024))
909909
buffer.append("xmlserialize(xmlagg(xmltext(");
910910
expr.getExpression().accept(this);
911-
buffer.append(") ORDER BY ");
912-
for (Iterator<OrderByElement> i = expr.getOrderByElements().iterator(); i.hasNext();) {
913-
buffer.append(i.next().toString());
914-
if (i.hasNext()) {
915-
buffer.append(", ");
911+
buffer.append(")");
912+
if (expr.getOrderByElements() != null){
913+
buffer.append(" ORDER BY ");
914+
for (Iterator<OrderByElement> i = expr.getOrderByElements().iterator(); i.hasNext();) {
915+
buffer.append(i.next().toString());
916+
if (i.hasNext()) {
917+
buffer.append(", ");
918+
}
916919
}
917920
}
918921
buffer.append(") AS ").append(expr.getDataType()).append(")");

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,16 +3791,17 @@ Function InternalFunction(Function retval) :
37913791
XMLSerializeExpr XMLSerializeExpr(): {
37923792
XMLSerializeExpr result;
37933793
Expression expression;
3794-
List<OrderByElement> orderByElements;
3794+
List<OrderByElement> orderByElements = null;
37953795
ColDataType dataType;
37963796
}
37973797
{
37983798
<K_XMLSERIALIZE>
37993799
"(" <K_XMLAGG>
38003800
"(" <K_XMLTEXT>
38013801
"(" expression=SimpleExpression() ")"
3802-
orderByElements=OrderByElements() ")"
3803-
<K_AS> dataType=ColDataType() ")"
3802+
[ orderByElements=OrderByElements() ]
3803+
")"
3804+
<K_AS> dataType=ColDataType() ")"
38043805
{
38053806
result = new XMLSerializeExpr();
38063807
result.setExpression(expression);

src/test/java/net/sf/jsqlparser/statement/select/SelectXMLSerializeTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
* @author tobens
1919
*/
2020
public class SelectXMLSerializeTest {
21+
2122
@Test
2223
public void testXmlAgg1() throws JSQLParserException {
2324
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024)) FROM mytable GROUP BY COMMENT_NUMBER");
2425
}
25-
26+
2627
@Test
2728
public void testXmlAgg2() throws JSQLParserException {
2829
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE, COMMENT_LINE) AS varchar (1024)) FROM mytable GROUP BY COMMENT_NUMBER");
2930
}
30-
31+
3132
@Test
3233
public void testXmlAgg3() throws JSQLParserException {
3334
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024))");
3435
}
35-
36+
3637
@Test
3738
public void testXmlAgg4() throws JSQLParserException {
3839
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE_PREFIX || COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024))");
@@ -42,4 +43,9 @@ public void testXmlAgg4() throws JSQLParserException {
4243
public void testXmlAgg5() throws JSQLParserException {
4344
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(CONCAT(', ', TRIM(SOME_COLUMN))) ORDER BY MY_SEQUENCE) AS varchar (1024))");
4445
}
46+
47+
@Test
48+
public void testXmlAgg6() throws JSQLParserException {
49+
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE)) AS varchar (1024))");
50+
}
4551
}

0 commit comments

Comments
 (0)