Skip to content

Commit af5a090

Browse files
committed
simple merge implementation
1 parent 1fb426e commit af5a090

File tree

6 files changed

+168
-27
lines changed

6 files changed

+168
-27
lines changed

src/main/java/net/sf/jsqlparser/statement/merge/Merge.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ public void setOnCondition(Expression onCondition) {
8888
this.onCondition = onCondition;
8989
}
9090

91+
private MergeInsert mergeInsert;
92+
93+
public MergeInsert getMergeInsert() {
94+
return mergeInsert;
95+
}
96+
97+
public void setMergeInsert(MergeInsert insert) {
98+
this.mergeInsert = insert;
99+
}
100+
101+
private MergeUpdate mergeUpdate;
102+
103+
public MergeUpdate getMergeUpdate() {
104+
return mergeUpdate;
105+
}
106+
107+
public void setMergeUpdate(MergeUpdate mergeUpdate) {
108+
this.mergeUpdate = mergeUpdate;
109+
}
110+
91111
@Override
92112
public void accept(StatementVisitor statementVisitor) {
93113
statementVisitor.visit(this);
@@ -112,6 +132,14 @@ public String toString() {
112132
b.append(onCondition);
113133
b.append(")");
114134

135+
if (mergeUpdate != null) {
136+
b.append(mergeUpdate.toString());
137+
}
138+
139+
if (mergeInsert != null) {
140+
b.append(mergeInsert.toString());
141+
}
142+
115143
return b.toString();
116144
}
117145
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2015 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.statement.merge;
23+
24+
import java.util.List;
25+
import net.sf.jsqlparser.expression.Expression;
26+
import net.sf.jsqlparser.schema.Column;
27+
import net.sf.jsqlparser.statement.select.PlainSelect;
28+
29+
/**
30+
*
31+
* @author toben
32+
*/
33+
public class MergeInsert {
34+
35+
private List<Column> columns = null;
36+
private List<Expression> values = null;
37+
38+
public List<Column> getColumns() {
39+
return columns;
40+
}
41+
42+
public void setColumns(List<Column> columns) {
43+
this.columns = columns;
44+
}
45+
46+
public List<Expression> getValues() {
47+
return values;
48+
}
49+
50+
public void setValues(List<Expression> values) {
51+
this.values = values;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return " WHEN NOT MATCHED THEN INSERT " + PlainSelect.getStringList(columns, true, true)
57+
+ " VALUES " + PlainSelect.getStringList(values, true, true);
58+
}
59+
}

src/main/java/net/sf/jsqlparser/statement/merge/MergeUpdate.java

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,70 @@
1919
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
2020
* #L%
2121
*/
22-
/*
23-
* Copyright (C) 2015 JSQLParser.
24-
*
25-
* This library is free software; you can redistribute it and/or
26-
* modify it under the terms of the GNU Lesser General Public
27-
* License as published by the Free Software Foundation; either
28-
* version 2.1 of the License, or (at your option) any later version.
29-
*
30-
* This library is distributed in the hope that it will be useful,
31-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33-
* Lesser General Public License for more details.
34-
*
35-
* You should have received a copy of the GNU Lesser General Public
36-
* License along with this library; if not, write to the Free Software
37-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
38-
* MA 02110-1301 USA
39-
*/
4022
package net.sf.jsqlparser.statement.merge;
4123

24+
import java.util.List;
25+
import net.sf.jsqlparser.expression.Expression;
26+
import net.sf.jsqlparser.schema.Column;
27+
4228
/**
4329
*
4430
* @author toben
4531
*/
4632
public class MergeUpdate {
47-
33+
34+
private List<Column> columns = null;
35+
private List<Expression> values = null;
36+
37+
public List<Column> getColumns() {
38+
return columns;
39+
}
40+
41+
public void setColumns(List<Column> columns) {
42+
this.columns = columns;
43+
}
44+
45+
public List<Expression> getValues() {
46+
return values;
47+
}
48+
49+
public void setValues(List<Expression> values) {
50+
this.values = values;
51+
}
52+
53+
private Expression whereCondition;
54+
55+
public Expression getWhereCondition() {
56+
return whereCondition;
57+
}
58+
59+
public void setWhereCondition(Expression whereCondition) {
60+
this.whereCondition = whereCondition;
61+
}
62+
63+
private Expression deleteWhereCondition;
64+
65+
public Expression getDeleteWhereCondition() {
66+
return deleteWhereCondition;
67+
}
68+
69+
public void setDeleteWhereCondition(Expression deleteWhereCondition) {
70+
this.deleteWhereCondition = deleteWhereCondition;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
StringBuilder b = new StringBuilder();
76+
b.append(" WHEN MATCHED THEN UPDATE SET ");
77+
for (int i = 0; i < columns.size(); i++) {
78+
b.append(columns.get(i).toString()).append(" = ").append(values.get(i).toString());
79+
}
80+
if (whereCondition != null) {
81+
b.append(" WHERE ").append(whereCondition.toString());
82+
}
83+
if (deleteWhereCondition != null) {
84+
b.append(" DELETE WHERE ").append(deleteWhereCondition.toString());
85+
}
86+
return b.toString();
87+
}
4888
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public void visit(SetStatement set) {
179179

180180
@Override
181181
public void visit(Merge merge) {
182-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
182+
//TODO implementation of a deparser
183+
buffer.append(merge.toString());
183184
}
184185
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ Statement Merge() : {
527527
SubSelect select;
528528
Alias alias;
529529
Expression condition;
530+
MergeUpdate update;
531+
MergeInsert insert;
530532
}
531533
{
532534
<K_MERGE> <K_INTO> table=TableWithAlias() { merge.setTable(table); }
@@ -536,9 +538,9 @@ Statement Merge() : {
536538
")" [ alias = Alias() { merge.setUsingAlias(alias); } ] <K_ON>
537539
"(" condition = Condition() { merge.setOnCondition(condition); } ")"
538540

539-
[ LOOKAHEAD(2) MergeUpdateClause() ]
541+
[ LOOKAHEAD(2) update = MergeUpdateClause() { merge.setMergeUpdate(update); } ]
540542

541-
[ MergeInsertClause() ]
543+
[ insert = MergeInsertClause() { merge.setMergeInsert(insert); } ]
542544

543545
{ return merge; }
544546
}
@@ -547,7 +549,7 @@ MergeUpdate MergeUpdateClause() : {
547549
MergeUpdate mu = new MergeUpdate();
548550
List<Column> columns = new ArrayList<Column>();
549551
List<Expression> expList = new ArrayList<Expression>();
550-
Columns col;
552+
Column col;
551553
Expression exp;
552554
Expression condition;
553555
}
@@ -558,22 +560,30 @@ MergeUpdate MergeUpdateClause() : {
558560
{ columns.add(col); expList.add(exp); }
559561
("," col = Column() "=" exp = SimpleExpression() { columns.add(col); expList.add(exp); } )*
560562

561-
{ mu.setColumns(columns); mu.setExpressions(expList); }
563+
{ mu.setColumns(columns); mu.setValues(expList); }
562564

563565
[ <K_WHERE> condition = Condition() { mu.setWhereCondition(condition); }]
564566
[ <K_DELETE> <K_WHERE> condition = Condition() { mu.setDeleteWhereCondition(condition); } ]
565567

566568
{ return mu; }
567569
}
568570

569-
void MergeInsertClause() : {
571+
MergeInsert MergeInsertClause() : {
572+
MergeInsert mi = new MergeInsert();
570573
List<Column> columns = new ArrayList<Column>();
571574
List<Expression> expList = new ArrayList<Expression>();
575+
Column col;
576+
Expression exp;
572577
}
573578
{
574579
<K_WHEN> <K_NOT> <K_MATCHED> <K_THEN>
575-
<K_INSERT> "(" Column() ("," Column() )* ")" <K_VALUES>
576-
"(" SimpleExpression() ("," SimpleExpression() )* ")"
580+
<K_INSERT> "(" col=Column() { columns.add(col); } ("," col=Column() { columns.add(col); } )* ")" <K_VALUES>
581+
"(" exp=SimpleExpression() { expList.add(exp); } ("," exp=SimpleExpression() { expList.add(exp); } )* ")"
582+
{
583+
mi.setColumns(columns);
584+
mi.setValues(expList);
585+
return mi;
586+
}
577587
}
578588

579589
// See: http://technet.microsoft.com/en-us/library/ms187879%28v=sql.105%29.aspx

src/test/java/net/sf/jsqlparser/test/merge/MergeTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
2323
import net.sf.jsqlparser.statement.Statement;
2424
import org.junit.Test;
25+
import static net.sf.jsqlparser.test.TestUtils.*;
2526

2627
/**
2728
*
@@ -46,5 +47,7 @@ public void testOracleMergeIntoStatement() throws JSQLParserException {
4647
Statement statement = CCJSqlParserUtil.parse(sql);
4748

4849
System.out.println(statement.toString());
50+
51+
assertSqlCanBeParsedAndDeparsed(sql, true);
4952
}
5053
}

0 commit comments

Comments
 (0)