forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertDuplicateAction.java
More file actions
117 lines (97 loc) · 3.49 KB
/
InsertDuplicateAction.java
File metadata and controls
117 lines (97 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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();
}
}