Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/insert/Insert.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.statement.insert;

import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.schema.Column;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class Insert implements Statement {
private InsertConflictTarget conflictTarget;
private InsertConflictAction conflictAction;
private InsertDuplicateAction duplicateAction;
private Alias rowAlias;

public List<UpdateSet> getDuplicateUpdateSets() {
if (duplicateAction != null) {
Expand Down Expand Up @@ -340,6 +342,9 @@ public String toString() {
if (setUpdateSets != null && !setUpdateSets.isEmpty()) {
sql.append("SET ");
sql = UpdateSet.appendUpdateSetsTo(sql, setUpdateSets);
if (rowAlias != null) {
sql.append(" ").append(rowAlias);
}
}

if (duplicateAction != null) {
Expand Down Expand Up @@ -411,4 +416,12 @@ public InsertDuplicateAction getDuplicateAction() {
public void setDuplicateAction(InsertDuplicateAction duplicateAction) {
this.duplicateAction = duplicateAction;
}

public Alias getRowAlias() {
return rowAlias;
}

public void setRowAlias(Alias rowAlias) {
this.rowAlias = rowAlias;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public void deParse(Insert insert) {
if (insert.getSetUpdateSets() != null) {
builder.append(" SET ");
deparseUpdateSets(insert.getSetUpdateSets(), builder, expressionVisitor);
if (insert.getRowAlias() != null) {
builder.append(" ").append(insert.getRowAlias());
}
}

if (insert.getDuplicateAction() != null) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2809,6 +2809,8 @@ Insert Insert():

String name = null;
boolean useAs = false;
boolean useSet = false;
Alias rowAlias = null;
OutputClause outputClause = null;

InsertConflictTarget conflictTarget = null;
Expand Down Expand Up @@ -2846,12 +2848,20 @@ Insert Insert():
<K_DEFAULT> <K_VALUES> { insert.setOnlyDefaultValues(true); }
|
(
<K_SET> updateSets = UpdateSets() { insert.withSetUpdateSets(updateSets); }
<K_SET> updateSets = UpdateSets() { insert.withSetUpdateSets(updateSets); useSet = true; }
)
|
select = Select()
)

[ LOOKAHEAD(2, { select instanceof Values || useSet }) rowAlias = Alias() {
if (select instanceof Values) {
select.setAlias(rowAlias);
} else {
insert.setRowAlias(rowAlias);
}
} ]

[ LOOKAHEAD(2) <K_ON> <K_DUPLICATE> <K_KEY> <K_UPDATE>
duplicateAction = InsertDuplicateAction() { insert.setDuplicateAction(duplicateAction); }
]
Expand Down
42 changes: 27 additions & 15 deletions src/test/java/net/sf/jsqlparser/statement/insert/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
*/
package net.sf.jsqlparser.statement.insert;

import static net.sf.jsqlparser.test.TestUtils.assertDeparse;
import static net.sf.jsqlparser.test.TestUtils.assertOracleHintExists;
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static net.sf.jsqlparser.test.TestUtils.assertStatementCanBeDeparsedAs;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.StringReader;
import java.util.List;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.DoubleValue;
Expand All @@ -34,21 +48,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.StringReader;
import java.util.List;

import static net.sf.jsqlparser.test.TestUtils.assertDeparse;
import static net.sf.jsqlparser.test.TestUtils.assertOracleHintExists;
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static net.sf.jsqlparser.test.TestUtils.assertStatementCanBeDeparsedAs;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class InsertTest {

private final CCJSqlParserManager parserManager = new CCJSqlParserManager();
Expand Down Expand Up @@ -395,10 +394,23 @@ public void testInsertValuesWithDuplicateEliminationInDeparsing() throws JSQLPar
+ "ON DUPLICATE KEY UPDATE COUNTER = COUNTER + 1");
}

@Test
public void testInsertValuesAliasWithDuplicateEliminationIssue() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS new"
+ " ON DUPLICATE KEY UPDATE c = new.a+new.b;");

assertSqlCanBeParsedAndDeparsed(
"INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS new(m,n,p) "
+ " ON DUPLICATE KEY UPDATE c = m+n;");
}

@Test
public void testInsertSetWithDuplicateEliminationInDeparsing() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("INSERT INTO mytable SET col1 = 122 "
+ "ON DUPLICATE KEY UPDATE col2 = col2 + 1, col3 = 'saint'");

assertSqlCanBeParsedAndDeparsed("INSERT INTO t1 SET a=1,b=2,c=3 AS new"
+ " ON DUPLICATE KEY UPDATE c = new.a+new.b;");
}

@Test
Expand Down