Skip to content

Commit b580a09

Browse files
committed
xmlserialize support
1 parent 536fb03 commit b580a09

File tree

8 files changed

+160
-6
lines changed

8 files changed

+160
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Also I would like to know about needed examples or documentation stuff.
6767

6868
## Extensions in the latest SNAPSHOT version 4.0
6969

70+
* support for **xmlserialize(xmlagg(xmltext( <column> ) ORDER BY <list of columns>) as <column data type> )** expression
7071
* first try to support conditions as select items: **SELECT a < b**
7172
* support for **DROP SEQUENCE**
7273
* heavy updating of many class files to support fluent like class building and setting of attributes (thx to @gitmotte)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,5 @@ public interface ExpressionVisitor {
160160

161161
public void visit(VariableAssignment aThis);
162162

163+
public void visit(XMLSerializeExpr aThis);
163164
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,12 @@ public void visit(VariableAssignment var) {
547547
var.getVariable().accept(this);
548548
var.getExpression().accept(this);
549549
}
550+
551+
@Override
552+
public void visit(XMLSerializeExpr expr) {
553+
expr.getColumn().accept(this);
554+
for (OrderByElement elm : expr.getOrderByElements()) {
555+
elm.getExpression().accept(this);
556+
}
557+
}
550558
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 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 java.util.List;
13+
import static java.util.stream.Collectors.joining;
14+
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
15+
import net.sf.jsqlparser.schema.Column;
16+
import net.sf.jsqlparser.statement.create.table.ColDataType;
17+
import net.sf.jsqlparser.statement.select.OrderByElement;
18+
19+
public class XMLSerializeExpr extends ASTNodeAccessImpl implements Expression {
20+
21+
private Column column;
22+
private List<OrderByElement> orderByElements;
23+
private ColDataType dataType;
24+
25+
@Override
26+
public void accept(ExpressionVisitor expressionVisitor) {
27+
expressionVisitor.visit(this);
28+
}
29+
30+
public Column getColumn() {
31+
return column;
32+
}
33+
34+
public void setColumn(Column column) {
35+
this.column = column;
36+
}
37+
38+
public List<OrderByElement> getOrderByElements() {
39+
return orderByElements;
40+
}
41+
42+
public void setOrderByElements(List<OrderByElement> orderByElements) {
43+
this.orderByElements = orderByElements;
44+
}
45+
46+
public ColDataType getDataType() {
47+
return dataType;
48+
}
49+
50+
public void setDataType(ColDataType dataType) {
51+
this.dataType = dataType;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "xmlserialize(xmlagg(xmltext(" + column + ") ORDER BY " +
57+
orderByElements.stream().map(item -> item.toString()).collect(joining(", ")) +
58+
") AS " + dataType + ")";
59+
}
60+
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import net.sf.jsqlparser.expression.ValueListExpression;
5252
import net.sf.jsqlparser.expression.VariableAssignment;
5353
import net.sf.jsqlparser.expression.WhenClause;
54+
import net.sf.jsqlparser.expression.XMLSerializeExpr;
5455
import net.sf.jsqlparser.expression.operators.arithmetic.*;
5556
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
5657
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
@@ -898,4 +899,8 @@ public void visit(VariableAssignment var) {
898899
var.getVariable().accept(this);
899900
var.getExpression().accept(this);
900901
}
902+
903+
@Override
904+
public void visit(XMLSerializeExpr aThis) {
905+
}
901906
}

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import net.sf.jsqlparser.expression.VariableAssignment;
5454
import net.sf.jsqlparser.expression.WhenClause;
5555
import net.sf.jsqlparser.expression.WindowElement;
56+
import net.sf.jsqlparser.expression.XMLSerializeExpr;
5657
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
5758
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
5859
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseLeftShift;
@@ -709,11 +710,11 @@ public void visit(AnalyticExpression aexpr) {
709710
}
710711

711712
switch (aexpr.getType()) {
712-
case WITHIN_GROUP:
713-
buffer.append("WITHIN GROUP");
714-
break;
715-
default:
716-
buffer.append("OVER");
713+
case WITHIN_GROUP:
714+
buffer.append("WITHIN GROUP");
715+
break;
716+
default:
717+
buffer.append("OVER");
717718
}
718719
buffer.append(" (");
719720

@@ -894,11 +895,26 @@ public void visit(ArrayExpression array) {
894895
void deParse(Expression statement) {
895896
statement.accept(this);
896897
}
897-
898+
898899
@Override
899900
public void visit(VariableAssignment var) {
900901
var.getVariable().accept(this);
901902
buffer.append(" ").append(var.getOperation()).append(" ");
902903
var.getExpression().accept(this);
903904
}
905+
906+
@Override
907+
public void visit(XMLSerializeExpr expr) {
908+
//xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) as varchar(1024))
909+
buffer.append("xmlserialize(xmlagg(xmltext(");
910+
expr.getColumn().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(", ");
916+
}
917+
}
918+
buffer.append(") AS ").append(expr.getDataType()).append(")");
919+
}
904920
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
355355
| <K_WITHIN:"WITHIN">
356356
| <K_WITHOUT:"WITHOUT">
357357
| <K_XML:"XML">
358+
| <K_XMLSERIALIZE:"XMLSERIALIZE">
359+
| <K_XMLAGG:"XMLAGG">
360+
| <K_XMLTEXT:"XMLTEXT">
358361
| <K_YAML:"YAML">
359362
| <K_ZONE:"ZONE">
360363
}
@@ -3227,6 +3230,8 @@ Expression PrimaryExpression() #PrimaryExpression:
32273230

32283231
| retval=MySQLGroupConcat()
32293232

3233+
| retval=XMLSerializeExpr()
3234+
32303235
| LOOKAHEAD(JsonExpression()) retval=JsonExpression()
32313236

32323237
| LOOKAHEAD(FunctionWithCondParams()) retval = FunctionWithCondParams()
@@ -3783,6 +3788,29 @@ Function InternalFunction(Function retval) :
37833788
}
37843789
}
37853790

3791+
XMLSerializeExpr XMLSerializeExpr(): {
3792+
XMLSerializeExpr result;
3793+
Column column;
3794+
List<OrderByElement> orderByElements;
3795+
ColDataType dataType;
3796+
}
3797+
{
3798+
<K_XMLSERIALIZE>
3799+
"(" <K_XMLAGG>
3800+
"(" <K_XMLTEXT>
3801+
"(" column=Column() ")"
3802+
orderByElements=OrderByElements() ")"
3803+
<K_AS> dataType=ColDataType() ")"
3804+
{
3805+
result = new XMLSerializeExpr();
3806+
result.setColumn(column);
3807+
result.setOrderByElements(orderByElements);
3808+
result.setDataType(dataType);
3809+
return result;
3810+
}
3811+
}
3812+
3813+
37863814
MySQLGroupConcat MySQLGroupConcat():{
37873815
MySQLGroupConcat retval = new MySQLGroupConcat();
37883816
ExpressionList expressionList = null;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2020 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.select;
11+
12+
import net.sf.jsqlparser.JSQLParserException;
13+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
14+
import org.junit.Test;
15+
16+
/**
17+
*
18+
* @author tobens
19+
*/
20+
public class SelectXMLSerializeTest {
21+
@Test
22+
public void testXmlAgg1() throws JSQLParserException {
23+
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024)) FROM mytable GROUP BY COMMENT_NUMBER");
24+
}
25+
26+
@Test
27+
public void testXmlAgg2() throws JSQLParserException {
28+
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE, COMMENT_LINE) AS varchar (1024)) FROM mytable GROUP BY COMMENT_NUMBER");
29+
}
30+
31+
@Test
32+
public void testXmlAgg3() throws JSQLParserException {
33+
assertSqlCanBeParsedAndDeparsed("SELECT xmlserialize(xmlagg(xmltext(COMMENT_LINE) ORDER BY COMMENT_SEQUENCE) AS varchar (1024))");
34+
}
35+
}

0 commit comments

Comments
 (0)