Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
package net.sf.jsqlparser.statement.insert;

public enum ConflictActionType {
DO_NOTHING, DO_UPDATE;
NOTHING,
DO_NOTHING,
DO_UPDATE;

public static ConflictActionType from(String type) {
return Enum.valueOf(ConflictActionType.class, type.toUpperCase());
Expand Down
43 changes: 26 additions & 17 deletions src/main/java/net/sf/jsqlparser/statement/insert/Insert.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
import net.sf.jsqlparser.statement.ReturningClause;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.select.PlainSelect;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid wildcard imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I will adjust the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have adjusted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ':publishMavenJavaPublicationToOssrhRepository'.
Failed to publish publication 'mavenJava' to repository 'ossrh'
Could not PUT 'https://central.sonatype.com/repository/maven-snapshots/com/github/jsqlparser/jsqlparser/5.4-SNAPSHOT/jsqlparser-5.4-20250828.114640-16.jar'. Received status code 401 from server: Unauthorized

There seems to be a problem with the username or password of the Maven repository

import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.select.Values;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.update.UpdateSet;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.*;

@SuppressWarnings({"PMD.CyclomaticComplexity"})
public class Insert implements Statement {
Expand All @@ -52,8 +44,12 @@ public class Insert implements Statement {
private OutputClause outputClause;
private InsertConflictTarget conflictTarget;
private InsertConflictAction conflictAction;
private InsertDuplicateAction duplicateAction;

public List<UpdateSet> getDuplicateUpdateSets() {
if (duplicateAction != null) {
return duplicateAction.getUpdateSets();
}
return duplicateUpdateSets;
}

Expand All @@ -62,7 +58,13 @@ public List<UpdateSet> getSetUpdateSets() {
}

public Insert withDuplicateUpdateSets(List<UpdateSet> duplicateUpdateSets) {
this.duplicateUpdateSets = duplicateUpdateSets;
if (duplicateAction != null) {
duplicateAction.setConflictActionType(ConflictActionType.DO_UPDATE);
duplicateAction.setUpdateSets(duplicateUpdateSets);
} else {
duplicateAction = new InsertDuplicateAction(ConflictActionType.DO_UPDATE);
duplicateAction.setUpdateSets(duplicateUpdateSets);
}
return this;
}

Expand Down Expand Up @@ -157,7 +159,7 @@ public boolean isUseSelectBrackets() {

@Deprecated
public boolean isUseDuplicate() {
return duplicateUpdateSets != null && !duplicateUpdateSets.isEmpty();
return duplicateAction != null && duplicateAction.getUpdateSets() != null && !duplicateAction.getUpdateSets().isEmpty();
}

public InsertModifierPriority getModifierPriority() {
Expand Down Expand Up @@ -263,7 +265,7 @@ public String toString() {
StringBuilder sql = new StringBuilder();
if (withItemsList != null && !withItemsList.isEmpty()) {
sql.append("WITH ");
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext();) {
for (Iterator<WithItem<?>> iter = withItemsList.iterator(); iter.hasNext(); ) {
WithItem<?> withItem = iter.next();
sql.append(withItem);
if (iter.hasNext()) {
Expand Down Expand Up @@ -331,9 +333,9 @@ public String toString() {
sql = UpdateSet.appendUpdateSetsTo(sql, setUpdateSets);
}

if (duplicateUpdateSets != null && !duplicateUpdateSets.isEmpty()) {
if (duplicateAction != null) {
sql.append(" ON DUPLICATE KEY UPDATE ");
sql = UpdateSet.appendUpdateSetsTo(sql, duplicateUpdateSets);
duplicateAction.appendTo(sql);
}

if (conflictAction != null) {
Expand Down Expand Up @@ -387,9 +389,16 @@ public Insert addColumns(Column... columns) {
}

public Insert addColumns(Collection<Column> columns) {
ExpressionList<Column> collection =
Optional.ofNullable(getColumns()).orElseGet(ExpressionList::new);
ExpressionList<Column> collection = Optional.ofNullable(getColumns()).orElseGet(ExpressionList::new);
collection.addAll(columns);
return this.withColumns(collection);
}

public InsertDuplicateAction getDuplicateAction() {
return duplicateAction;
}

public void setDuplicateAction(InsertDuplicateAction duplicateAction) {
this.duplicateAction = duplicateAction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2025 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.insert;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.update.UpdateSet;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* on duplicate key is one of:
*
* ON DUPLICATE KEY UPDATE NOTHING ON DUPLICATE KEY UPDATE { column_name = { expression | DEFAULT } | ( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT
* } [, ...] ) | ( column_name [, ...] ) = ( sub-SELECT ) } [, ...] [ WHERE condition ]
*
* @author zhangconan
*/
public class InsertDuplicateAction implements Serializable {

ConflictActionType conflictActionType;
Expression whereExpression;
private List<UpdateSet> updateSets;

public InsertDuplicateAction(ConflictActionType conflictActionType) {
this.conflictActionType = Objects.requireNonNull(conflictActionType, "The Conflict Action Type is mandatory and must not be Null.");
}

public List<UpdateSet> getUpdateSets() {
return updateSets;
}

public void setUpdateSets(List<UpdateSet> updateSets) {
this.updateSets = updateSets;
}

public InsertDuplicateAction withUpdateSets(List<UpdateSet> updateSets) {
this.setUpdateSets(updateSets);
return this;
}

public ConflictActionType getConflictActionType() {
return conflictActionType;
}

public void setConflictActionType(ConflictActionType conflictActionType) {
this.conflictActionType = Objects.requireNonNull(conflictActionType, "The Conflict Action Type is mandatory and must not be Null.");
}

public InsertDuplicateAction withConflictActionType(ConflictActionType conflictActionType) {
setConflictActionType(conflictActionType);
return this;
}

public InsertDuplicateAction addUpdateSet(Column column, Expression expression) {
return this.addUpdateSet(new UpdateSet());
}

public InsertDuplicateAction addUpdateSet(UpdateSet updateSet) {
if (updateSets == null) {
updateSets = new ArrayList<>();
}
this.updateSets.add(updateSet);
return this;
}

public InsertDuplicateAction withUpdateSets(Collection<UpdateSet> updateSets) {
this.setUpdateSets(new ArrayList<>(updateSets));
return this;
}

public Expression getWhereExpression() {
return whereExpression;
}

public void setWhereExpression(Expression whereExpression) {
this.whereExpression = whereExpression;
}

public InsertDuplicateAction withWhereExpression(Expression whereExpression) {
setWhereExpression(whereExpression);
return this;
}

@SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault")
public StringBuilder appendTo(StringBuilder builder) {
switch (conflictActionType) {
case NOTHING:
builder.append(" NOTHING ");
break;
default:
UpdateSet.appendUpdateSetsTo(builder, updateSets);

if (whereExpression != null) {
builder.append(" WHERE ").append(whereExpression);
}
break;
}
return builder;
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
}
29 changes: 24 additions & 5 deletions src/main/java/net/sf/jsqlparser/statement/upsert/Upsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.insert.ConflictActionType;
import net.sf.jsqlparser.statement.insert.InsertDuplicateAction;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SetOperationList;
Expand All @@ -35,6 +37,7 @@ public class Upsert implements Statement {
private List<UpdateSet> duplicateUpdateSets;
private UpsertType upsertType = UpsertType.UPSERT;
private boolean isUsingInto;
private InsertDuplicateAction duplicateAction;

public List<UpdateSet> getUpdateSets() {
return updateSets;
Expand All @@ -46,11 +49,20 @@ public Upsert setUpdateSets(List<UpdateSet> updateSets) {
}

public List<UpdateSet> getDuplicateUpdateSets() {
if (duplicateAction != null) {
return duplicateAction.getUpdateSets();
}
return duplicateUpdateSets;
}

public Upsert setDuplicateUpdateSets(List<UpdateSet> duplicateUpdateSets) {
this.duplicateUpdateSets = duplicateUpdateSets;
if (duplicateAction != null) {
duplicateAction.setConflictActionType(ConflictActionType.DO_UPDATE);
duplicateAction.setUpdateSets(duplicateUpdateSets);
} else {
duplicateAction = new InsertDuplicateAction(ConflictActionType.DO_UPDATE);
duplicateAction.setUpdateSets(duplicateUpdateSets);
}
return this;
}

Expand Down Expand Up @@ -181,9 +193,9 @@ public String toString() {
}
}

if (duplicateUpdateSets != null) {
if (duplicateAction != null) {
sb.append(" ON DUPLICATE KEY UPDATE ");
UpdateSet.appendUpdateSetsTo(sb, duplicateUpdateSets);
duplicateAction.appendTo(sb);
}

return sb.toString();
Expand Down Expand Up @@ -214,9 +226,16 @@ public Upsert addColumns(Column... columns) {
}

public Upsert addColumns(Collection<? extends Column> columns) {
ExpressionList<Column> collection =
Optional.ofNullable(getColumns()).orElseGet(ExpressionList::new);
ExpressionList<Column> collection = Optional.ofNullable(getColumns()).orElseGet(ExpressionList::new);
collection.addAll(columns);
return this.withColumns(collection);
}

public InsertDuplicateAction getDuplicateAction() {
return duplicateAction;
}

public void setDuplicateAction(InsertDuplicateAction duplicateAction) {
this.duplicateAction = duplicateAction;
}
}
21 changes: 11 additions & 10 deletions src/main/java/net/sf/jsqlparser/util/deparser/InsertDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Partition;
import net.sf.jsqlparser.statement.insert.ConflictActionType;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitor;
Expand All @@ -28,22 +29,18 @@ public InsertDeParser() {
super(new StringBuilder());
}

public InsertDeParser(ExpressionVisitor<StringBuilder> expressionVisitor,
SelectVisitor<StringBuilder> selectVisitor,
StringBuilder buffer) {
public InsertDeParser(ExpressionVisitor<StringBuilder> expressionVisitor, SelectVisitor<StringBuilder> selectVisitor, StringBuilder buffer) {
super(buffer);
this.expressionVisitor = expressionVisitor;
this.selectVisitor = selectVisitor;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.ExcessiveMethodLength",
"PMD.NPathComplexity"})
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.ExcessiveMethodLength", "PMD.NPathComplexity"})
public void deParse(Insert insert) {
if (insert.getWithItemsList() != null && !insert.getWithItemsList().isEmpty()) {
builder.append("WITH ");
for (Iterator<WithItem<?>> iter = insert.getWithItemsList().iterator(); iter
.hasNext();) {
for (Iterator<WithItem<?>> iter = insert.getWithItemsList().iterator(); iter.hasNext(); ) {
WithItem<?> withItem = iter.next();
withItem.accept(this.selectVisitor, null);
if (iter.hasNext()) {
Expand Down Expand Up @@ -80,7 +77,7 @@ public void deParse(Insert insert) {

if (insert.getColumns() != null) {
builder.append(" (");
for (Iterator<Column> iter = insert.getColumns().iterator(); iter.hasNext();) {
for (Iterator<Column> iter = insert.getColumns().iterator(); iter.hasNext(); ) {
Column column = iter.next();
builder.append(column.getColumnName());
if (iter.hasNext()) {
Expand Down Expand Up @@ -115,9 +112,13 @@ public void deParse(Insert insert) {
deparseUpdateSets(insert.getSetUpdateSets(), builder, expressionVisitor);
}

if (insert.getDuplicateUpdateSets() != null) {
if (insert.getDuplicateAction() != null) {
builder.append(" ON DUPLICATE KEY UPDATE ");
deparseUpdateSets(insert.getDuplicateUpdateSets(), builder, expressionVisitor);
if (ConflictActionType.DO_UPDATE.equals(insert.getDuplicateAction().getConflictActionType())) {
deparseUpdateSets(insert.getDuplicateUpdateSets(), builder, expressionVisitor);
} else {
insert.getDuplicateAction().appendTo(builder);
}
}

// @todo: Accept some Visitors for the involved Expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package net.sf.jsqlparser.util.deparser;

import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.statement.insert.ConflictActionType;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.upsert.Upsert;

Expand All @@ -19,8 +20,7 @@ public class UpsertDeParser extends AbstractDeParser<Upsert> {
private ExpressionDeParser expressionVisitor;
private SelectDeParser selectVisitor;

public UpsertDeParser(ExpressionDeParser expressionVisitor, SelectDeParser selectVisitor,
StringBuilder buffer) {
public UpsertDeParser(ExpressionDeParser expressionVisitor, SelectDeParser selectVisitor, StringBuilder buffer) {
super(buffer);
this.expressionVisitor = expressionVisitor;
this.expressionVisitor.setSelectVisitor(selectVisitor);
Expand Down Expand Up @@ -78,9 +78,13 @@ public void deParse(Upsert upsert) {
upsert.getSelect().accept((SelectVisitor<StringBuilder>) selectVisitor, null);
}

if (upsert.getDuplicateUpdateSets() != null) {
if (upsert.getDuplicateAction() != null) {
builder.append(" ON DUPLICATE KEY UPDATE ");
deparseUpdateSets(upsert.getDuplicateUpdateSets(), builder, expressionVisitor);
if (ConflictActionType.DO_UPDATE.equals(upsert.getDuplicateAction().getConflictActionType())) {
deparseUpdateSets(upsert.getDuplicateUpdateSets(), builder, expressionVisitor);
} else {
upsert.getDuplicateAction().appendTo(builder);
}
}
}
}
Expand Down
Loading