Skip to content

Commit 13c343a

Browse files
committed
Merge merge-impl
2 parents c6e9389 + af5a090 commit 13c343a

File tree

10 files changed

+439
-5
lines changed

10 files changed

+439
-5
lines changed

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.sf.jsqlparser.statement.drop.Drop;
3030
import net.sf.jsqlparser.statement.execute.Execute;
3131
import net.sf.jsqlparser.statement.insert.Insert;
32+
import net.sf.jsqlparser.statement.merge.Merge;
3233
import net.sf.jsqlparser.statement.replace.Replace;
3334
import net.sf.jsqlparser.statement.select.Select;
3435
import net.sf.jsqlparser.statement.truncate.Truncate;
@@ -63,4 +64,6 @@ public interface StatementVisitor {
6364
void visit(Execute execute);
6465

6566
void visit(SetStatement set);
67+
68+
void visit(Merge merge);
6669
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitorAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.sf.jsqlparser.statement.drop.Drop;
3030
import net.sf.jsqlparser.statement.execute.Execute;
3131
import net.sf.jsqlparser.statement.insert.Insert;
32+
import net.sf.jsqlparser.statement.merge.Merge;
3233
import net.sf.jsqlparser.statement.replace.Replace;
3334
import net.sf.jsqlparser.statement.select.Select;
3435
import net.sf.jsqlparser.statement.truncate.Truncate;
@@ -104,4 +105,9 @@ public void visit(Execute execute) {
104105
public void visit(SetStatement set) {
105106

106107
}
108+
109+
@Override
110+
public void visit(Merge merge) {
111+
112+
}
107113
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 net.sf.jsqlparser.expression.Alias;
25+
import net.sf.jsqlparser.expression.Expression;
26+
import net.sf.jsqlparser.schema.Table;
27+
import net.sf.jsqlparser.statement.Statement;
28+
import net.sf.jsqlparser.statement.StatementVisitor;
29+
import net.sf.jsqlparser.statement.select.SubSelect;
30+
31+
/**
32+
* Merge - statement
33+
*
34+
* @author tw
35+
*/
36+
public class Merge implements Statement {
37+
38+
private Table table;
39+
40+
public Table getTable() {
41+
return table;
42+
}
43+
44+
public void setTable(Table name) {
45+
table = name;
46+
}
47+
48+
private Table usingTable;
49+
50+
public Table getUsingTable() {
51+
return usingTable;
52+
}
53+
54+
public void setUsingTable(Table usingTable) {
55+
this.usingTable = usingTable;
56+
}
57+
58+
private SubSelect usingSelect;
59+
60+
public SubSelect getUsingSelect() {
61+
return usingSelect;
62+
}
63+
64+
public void setUsingSelect(SubSelect usingSelect) {
65+
this.usingSelect = usingSelect;
66+
if (this.usingSelect != null) {
67+
this.usingSelect.setUseBrackets(false);
68+
}
69+
}
70+
71+
private Alias usingAlias;
72+
73+
public Alias getUsingAlias() {
74+
return usingAlias;
75+
}
76+
77+
public void setUsingAlias(Alias usingAlias) {
78+
this.usingAlias = usingAlias;
79+
}
80+
81+
private Expression onCondition;
82+
83+
public Expression getOnCondition() {
84+
return onCondition;
85+
}
86+
87+
public void setOnCondition(Expression onCondition) {
88+
this.onCondition = onCondition;
89+
}
90+
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+
111+
@Override
112+
public void accept(StatementVisitor statementVisitor) {
113+
statementVisitor.visit(this);
114+
}
115+
116+
@Override
117+
public String toString() {
118+
StringBuilder b = new StringBuilder();
119+
b.append("MERGE INTO ");
120+
b.append(table);
121+
b.append(" USING (");
122+
if (usingTable != null) {
123+
b.append(usingTable.toString());
124+
} else if (usingSelect != null) {
125+
b.append(usingSelect.toString());
126+
}
127+
b.append(")");
128+
if (usingAlias != null) {
129+
b.append(usingAlias.toString());
130+
}
131+
b.append(" ON (");
132+
b.append(onCondition);
133+
b.append(")");
134+
135+
if (mergeUpdate != null) {
136+
b.append(mergeUpdate.toString());
137+
}
138+
139+
if (mergeInsert != null) {
140+
b.append(mergeInsert.toString());
141+
}
142+
143+
return b.toString();
144+
}
145+
}
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+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
28+
/**
29+
*
30+
* @author toben
31+
*/
32+
public class MergeUpdate {
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+
}
88+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import net.sf.jsqlparser.statement.create.view.CreateView;
4646
import net.sf.jsqlparser.statement.drop.Drop;
4747
import net.sf.jsqlparser.statement.execute.Execute;
48+
import net.sf.jsqlparser.statement.merge.Merge;
4849
import net.sf.jsqlparser.statement.truncate.Truncate;
4950

5051
/**
@@ -627,4 +628,9 @@ public void visit(RowConstructor rowConstructor) {
627628
public void visit(HexValue hexValue) {
628629

629630
}
631+
632+
@Override
633+
public void visit(Merge merge) {
634+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
635+
}
630636
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import net.sf.jsqlparser.statement.drop.Drop;
3535
import net.sf.jsqlparser.statement.execute.Execute;
3636
import net.sf.jsqlparser.statement.insert.Insert;
37+
import net.sf.jsqlparser.statement.merge.Merge;
3738
import net.sf.jsqlparser.statement.replace.Replace;
3839
import net.sf.jsqlparser.statement.select.Select;
3940
import net.sf.jsqlparser.statement.select.WithItem;
@@ -175,4 +176,10 @@ public void visit(SetStatement set) {
175176
selectDeParser.setExpressionVisitor(expressionDeParser);
176177
setStatementDeparser.deParse(set);
177178
}
179+
180+
@Override
181+
public void visit(Merge merge) {
182+
//TODO implementation of a deparser
183+
buffer.append(merge.toString());
184+
}
178185
}

0 commit comments

Comments
 (0)