-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/net/sf/jsqlparser/statement/merge/Merge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* #%L | ||
* JSQLParser library | ||
* %% | ||
* Copyright (C) 2004 - 2015 JSQLParser | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 2.1 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Lesser Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Lesser Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
* #L% | ||
*/ | ||
package net.sf.jsqlparser.statement.merge; | ||
|
||
import net.sf.jsqlparser.expression.Alias; | ||
import net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.schema.Table; | ||
import net.sf.jsqlparser.statement.Statement; | ||
import net.sf.jsqlparser.statement.StatementVisitor; | ||
import net.sf.jsqlparser.statement.select.SubSelect; | ||
|
||
/** | ||
* Merge - statement | ||
* | ||
* @author tw | ||
*/ | ||
public class Merge implements Statement { | ||
|
||
private Table table; | ||
|
||
public Table getTable() { | ||
return table; | ||
} | ||
|
||
public void setTable(Table name) { | ||
table = name; | ||
} | ||
|
||
private Table usingTable; | ||
|
||
public Table getUsingTable() { | ||
return usingTable; | ||
} | ||
|
||
public void setUsingTable(Table usingTable) { | ||
this.usingTable = usingTable; | ||
} | ||
|
||
private SubSelect usingSelect; | ||
|
||
public SubSelect getUsingSelect() { | ||
return usingSelect; | ||
} | ||
|
||
public void setUsingSelect(SubSelect usingSelect) { | ||
this.usingSelect = usingSelect; | ||
if (this.usingSelect != null) { | ||
this.usingSelect.setUseBrackets(false); | ||
} | ||
} | ||
|
||
private Alias usingAlias; | ||
|
||
public Alias getUsingAlias() { | ||
return usingAlias; | ||
} | ||
|
||
public void setUsingAlias(Alias usingAlias) { | ||
this.usingAlias = usingAlias; | ||
} | ||
|
||
private Expression onCondition; | ||
|
||
public Expression getOnCondition() { | ||
return onCondition; | ||
} | ||
|
||
public void setOnCondition(Expression onCondition) { | ||
this.onCondition = onCondition; | ||
} | ||
|
||
@Override | ||
public void accept(StatementVisitor statementVisitor) { | ||
statementVisitor.visit(this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder b = new StringBuilder(); | ||
b.append("MERGE INTO "); | ||
b.append(table); | ||
b.append(" USING ("); | ||
if (usingTable != null) { | ||
b.append(usingTable.toString()); | ||
} else if (usingSelect != null) { | ||
b.append(usingSelect.toString()); | ||
} | ||
b.append(")"); | ||
if (usingAlias != null) { | ||
b.append(usingAlias.toString()); | ||
} | ||
b.append(" ON ("); | ||
b.append(onCondition); | ||
b.append(")"); | ||
|
||
return b.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (C) 2015 JSQLParser. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301 USA | ||
*/ | ||
package net.sf.jsqlparser.test.merge; | ||
|
||
import net.sf.jsqlparser.JSQLParserException; | ||
import net.sf.jsqlparser.parser.CCJSqlParserUtil; | ||
import net.sf.jsqlparser.statement.Statement; | ||
import org.junit.Test; | ||
|
||
/** | ||
* | ||
* @author toben | ||
*/ | ||
public class MergeTest { | ||
|
||
@Test | ||
public void testOracleMergeIntoStatement() throws JSQLParserException { | ||
String sql = "MERGE INTO bonuses B\n" | ||
+ "USING (\n" | ||
+ " SELECT employee_id, salary\n" | ||
+ " FROM employee\n" | ||
+ " WHERE dept_no =20) E\n" | ||
+ "ON (B.employee_id = E.employee_id)\n" | ||
+ "WHEN MATCHED THEN\n" | ||
+ " UPDATE SET B.bonus = E.salary * 0.1\n" | ||
+ "WHEN NOT MATCHED THEN\n" | ||
+ " INSERT (B.employee_id, B.bonus)\n" | ||
+ " VALUES (E.employee_id, E.salary * 0.05) "; | ||
|
||
Statement statement = CCJSqlParserUtil.parse(sql); | ||
|
||
System.out.println(statement.toString()); | ||
} | ||
} |