Skip to content

Commit

Permalink
xmlserialize support patch for optional order by part
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Sep 10, 2020
1 parent 8c4e0ca commit 3747f1c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void setDataType(ColDataType dataType) {

@Override
public String toString() {
return "xmlserialize(xmlagg(xmltext(" + expression + ") ORDER BY " +
orderByElements.stream().map(item -> item.toString()).collect(joining(", ")) +
") AS " + dataType + ")";
return "xmlserialize(xmlagg(xmltext(" + expression + ")"
+ (orderByElements != null ? " ORDER BY " + orderByElements.stream().map(item -> item.toString()).collect(joining(", ")) : "")
+ ") AS " + dataType + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,14 @@ public void visit(XMLSerializeExpr expr) {
//xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) as varchar(1024))
buffer.append("xmlserialize(xmlagg(xmltext(");
expr.getExpression().accept(this);
buffer.append(") ORDER BY ");
for (Iterator<OrderByElement> i = expr.getOrderByElements().iterator(); i.hasNext();) {
buffer.append(i.next().toString());
if (i.hasNext()) {
buffer.append(", ");
buffer.append(")");
if (expr.getOrderByElements() != null){
buffer.append(" ORDER BY ");
for (Iterator<OrderByElement> i = expr.getOrderByElements().iterator(); i.hasNext();) {
buffer.append(i.next().toString());
if (i.hasNext()) {
buffer.append(", ");
}
}
}
buffer.append(") AS ").append(expr.getDataType()).append(")");
Expand Down
7 changes: 4 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -3791,16 +3791,17 @@ Function InternalFunction(Function retval) :
XMLSerializeExpr XMLSerializeExpr(): {
XMLSerializeExpr result;
Expression expression;
List<OrderByElement> orderByElements;
List<OrderByElement> orderByElements = null;
ColDataType dataType;
}
{
<K_XMLSERIALIZE>
"(" <K_XMLAGG>
"(" <K_XMLTEXT>
"(" expression=SimpleExpression() ")"
orderByElements=OrderByElements() ")"
<K_AS> dataType=ColDataType() ")"
[ orderByElements=OrderByElements() ]
")"
<K_AS> dataType=ColDataType() ")"
{
result = new XMLSerializeExpr();
result.setExpression(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@
* @author tobens
*/
public class SelectXMLSerializeTest {

@Test
public void testXmlAgg1() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024)) FROM mytable GROUP BY COMMENT_NUMBER");
}

@Test
public void testXmlAgg2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE, COMMENT_LINE) AS varchar (1024)) FROM mytable GROUP BY COMMENT_NUMBER");
}

@Test
public void testXmlAgg3() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024))");
}

@Test
public void testXmlAgg4() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE_PREFIX || COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024))");
Expand All @@ -42,4 +43,9 @@ public void testXmlAgg4() throws JSQLParserException {
public void testXmlAgg5() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(CONCAT(', ', TRIM(SOME_COLUMN))) ORDER BY MY_SEQUENCE) AS varchar (1024))");
}

@Test
public void testXmlAgg6() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE)) AS varchar (1024))");
}
}

0 comments on commit 3747f1c

Please sign in to comment.