Skip to content

Commit

Permalink
merge impl started
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Sep 24, 2015
1 parent 6ea74fd commit 8d8c0e4
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.replace.Replace;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.truncate.Truncate;
Expand Down Expand Up @@ -63,4 +64,6 @@ public interface StatementVisitor {
void visit(Execute execute);

void visit(SetStatement set);

void visit(Merge merge);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.replace.Replace;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.truncate.Truncate;
Expand Down Expand Up @@ -104,4 +105,9 @@ public void visit(Execute execute) {
public void visit(SetStatement set) {

}

@Override
public void visit(Merge merge) {

}
}
117 changes: 117 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/merge/Merge.java
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();
}
}
6 changes: 6 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.sf.jsqlparser.statement.create.view.CreateView;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.truncate.Truncate;

/**
Expand Down Expand Up @@ -627,4 +628,9 @@ public void visit(RowConstructor rowConstructor) {
public void visit(HexValue hexValue) {

}

@Override
public void visit(Merge merge) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.replace.Replace;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.WithItem;
Expand Down Expand Up @@ -175,4 +176,9 @@ public void visit(SetStatement set) {
selectDeParser.setExpressionVisitor(expressionDeParser);
setStatementDeparser.deParse(set);
}

@Override
public void visit(Merge merge) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
43 changes: 43 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import net.sf.jsqlparser.statement.execute.*;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.truncate.*;
import net.sf.jsqlparser.statement.update.*;
import net.sf.jsqlparser.statement.merge.*;

import java.util.*;

Expand Down Expand Up @@ -222,6 +223,8 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_GROUP_CONCAT:"GROUP_CONCAT">
| <K_SEPARATOR:"SEPARATOR">
| <K_SKIP: "SKIP">
| <K_MERGE: "MERGE">
| <K_MATCHED: "MATCHED">
}

TOKEN : /* Numeric Constants */
Expand Down Expand Up @@ -279,6 +282,8 @@ Statement SingleStatement() :
stm = Replace()
|
stm = Alter()
|
stm = Merge()
|
LOOKAHEAD(3)
stm = CreateIndex()
Expand Down Expand Up @@ -516,6 +521,44 @@ Delete Delete():
}
}

Statement Merge() : {
Merge merge = new Merge();
Table table;
SubSelect select;
Alias alias;
Expression condition;
}
{
<K_MERGE> <K_INTO> table=TableWithAlias() { merge.setTable(table); }
<K_USING> "("
( table=Table() { merge.setUsingTable(table); }
| select=SubSelect() { merge.setUsingSelect(select); } )
")" [ alias = Alias() { merge.setUsingAlias(alias); } ] <K_ON>
"(" condition = Condition() { merge.setOnCondition(condition); } ")"

[ LOOKAHEAD(2) MergeUpdateClause() ]

[ MergeInsertClause() ]

{ return merge; }
}

void MergeUpdateClause() : {}
{
<K_WHEN> <K_MATCHED> <K_THEN> <K_UPDATE>
<K_SET>
Column() "=" SimpleExpression() ("," Column() "=" SimpleExpression() )*
[ <K_WHERE> Condition() ]
[ <K_DELETE> <K_WHERE> Condition() ]
}

void MergeInsertClause() : {}
{
<K_WHEN> <K_NOT> <K_MATCHED> <K_THEN>
<K_INSERT> "(" Column() ("," Column() )* ")" <K_VALUES>
"(" SimpleExpression() ("," SimpleExpression() )* ")"
}

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

Column Column() #Column :
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/net/sf/jsqlparser/test/insert/InsertTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.sf.jsqlparser.test.insert;

import java.io.StringReader;
import static junit.framework.Assert.assertEquals;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.DoubleValue;
Expand All @@ -15,10 +14,8 @@
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.PlainSelect;
import static net.sf.jsqlparser.test.TestUtils.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;

import org.junit.Test;

public class InsertTest {
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/net/sf/jsqlparser/test/merge/MergeTest.java
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());
}
}

0 comments on commit 8d8c0e4

Please sign in to comment.