Skip to content

Commit 85b3bc4

Browse files
refactor: UpdateSets for Update and InsertConflictTarget
- remove redundant code - add license headers - register `function06.sql` success
1 parent 905ef65 commit 85b3bc4

18 files changed

Lines changed: 211 additions & 98 deletions

File tree

src/main/java/net/sf/jsqlparser/expression/TranscodingFunction.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.expression;
211

312
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

src/main/java/net/sf/jsqlparser/expression/TrimFunction.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.expression;
211

312
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

src/main/java/net/sf/jsqlparser/expression/operators/relational/ParenthesedExpressionList.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.expression.operators.relational;
211

312
import net.sf.jsqlparser.expression.Expression;

src/main/java/net/sf/jsqlparser/statement/insert/InsertConflictAction.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
import java.io.Serializable;
1717
import java.util.ArrayList;
1818
import java.util.Collection;
19+
import java.util.List;
1920
import java.util.Objects;
2021

2122
/**
2223
* https://www.postgresql.org/docs/current/sql-insert.html
24+
*
2325
* <pre>
2426
* conflict_action is one of:
2527
*
@@ -35,23 +37,35 @@
3537
public class InsertConflictAction implements Serializable {
3638
ConflictActionType conflictActionType;
3739

38-
private final ArrayList<UpdateSet> updateSets = new ArrayList<>();
40+
private List<UpdateSet> updateSets;
3941

4042
Expression whereExpression;
43+
4144
public InsertConflictAction(ConflictActionType conflictActionType) {
42-
this.conflictActionType = Objects.requireNonNull(conflictActionType, "The Conflict Action Type is mandatory and must not be Null.");
45+
this.conflictActionType = Objects.requireNonNull(conflictActionType,
46+
"The Conflict Action Type is mandatory and must not be Null.");
4347
}
4448

45-
public ArrayList<UpdateSet> getUpdateSets() {
49+
public List<UpdateSet> getUpdateSets() {
4650
return updateSets;
4751
}
4852

53+
public void setUpdateSets(List<UpdateSet> updateSets) {
54+
this.updateSets = updateSets;
55+
}
56+
57+
public InsertConflictAction withUpdateSets(List<UpdateSet> updateSets) {
58+
this.setUpdateSets(updateSets);
59+
return this;
60+
}
61+
4962
public ConflictActionType getConflictActionType() {
5063
return conflictActionType;
5164
}
5265

5366
public void setConflictActionType(ConflictActionType conflictActionType) {
54-
this.conflictActionType = Objects.requireNonNull(conflictActionType, "The Conflict Action Type is mandatory and must not be Null.");
67+
this.conflictActionType = Objects.requireNonNull(conflictActionType,
68+
"The Conflict Action Type is mandatory and must not be Null.");
5569
}
5670

5771
public InsertConflictAction withConflictActionType(ConflictActionType conflictActionType) {
@@ -60,18 +74,19 @@ public InsertConflictAction withConflictActionType(ConflictActionType conflictAc
6074
}
6175

6276
public InsertConflictAction addUpdateSet(Column column, Expression expression) {
63-
this.updateSets.add(new UpdateSet(column, expression));
64-
return this;
77+
return this.addUpdateSet(new UpdateSet());
6578
}
6679

6780
public InsertConflictAction addUpdateSet(UpdateSet updateSet) {
81+
if (updateSets == null) {
82+
updateSets = new ArrayList<>();
83+
}
6884
this.updateSets.add(updateSet);
6985
return this;
7086
}
7187

7288
public InsertConflictAction withUpdateSets(Collection<UpdateSet> updateSets) {
73-
this.updateSets.clear();
74-
this.updateSets.addAll(updateSets);
89+
this.setUpdateSets(new ArrayList<>(updateSets));
7590
return this;
7691
}
7792

@@ -98,7 +113,7 @@ public StringBuilder appendTo(StringBuilder builder) {
98113
builder.append(" DO UPDATE ");
99114
UpdateSet.appendUpdateSetsTo(builder, updateSets);
100115

101-
if (whereExpression!=null) {
116+
if (whereExpression != null) {
102117
builder.append(" WHERE ").append(whereExpression);
103118
}
104119
break;

src/main/java/net/sf/jsqlparser/statement/select/KSQLWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class KSQLWindow extends ASTNodeAccessImpl {
1515

16-
public static enum TimeUnit {
16+
public enum TimeUnit {
1717
DAY("DAY"), HOUR("HOUR"), MINUTE("MINUTE"), SECOND("SECOND"), MILLISECOND(
1818
"MILLISECOND"), DAYS("DAYS"), HOURS("HOURS"), MINUTES(
1919
"MINUTES"), SECONDS("SECONDS"), MILLISECONDS("MILLISECONDS");

src/main/java/net/sf/jsqlparser/statement/select/LateralView.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.statement.select;
211

312
import net.sf.jsqlparser.expression.Alias;

src/main/java/net/sf/jsqlparser/statement/select/ParenthesedSelect.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.statement.select;
211

312
import net.sf.jsqlparser.expression.Alias;

src/main/java/net/sf/jsqlparser/statement/update/Update.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Update implements Statement {
3939
private List<WithItem> withItemsList;
4040
private Table table;
4141
private Expression where;
42-
private final ArrayList<UpdateSet> updateSets = new ArrayList<>();
42+
private List<UpdateSet> updateSets;
4343
private FromItem fromItem;
4444
private List<Join> joins;
4545
private List<Join> startJoins;
@@ -60,10 +60,19 @@ public void setOutputClause(OutputClause outputClause) {
6060
this.outputClause = outputClause;
6161
}
6262

63-
public ArrayList<UpdateSet> getUpdateSets() {
63+
public List<UpdateSet> getUpdateSets() {
6464
return updateSets;
6565
}
6666

67+
public void setUpdateSets(List<UpdateSet> updateSets) {
68+
this.updateSets = updateSets;
69+
}
70+
71+
public Update withUpdateSets(List<UpdateSet> updateSets) {
72+
this.setUpdateSets(updateSets);
73+
return this;
74+
}
75+
6776
@Override
6877
public void accept(StatementVisitor statementVisitor) {
6978
statementVisitor.visit(this);
@@ -120,12 +129,16 @@ public void setOracleHint(OracleHint oracleHint) {
120129
this.oracleHint = oracleHint;
121130
}
122131

123-
public void addUpdateSet(Column column, Expression expression) {
124-
updateSets.add(new UpdateSet(column, expression));
132+
public Update addUpdateSet(Column column, Expression expression) {
133+
return this.addUpdateSet(new UpdateSet(column, expression));
125134
}
126135

127-
public void addUpdateSet(UpdateSet updateSet) {
128-
updateSets.add(updateSet);
136+
public Update addUpdateSet(UpdateSet updateSet) {
137+
if (this.updateSets == null) {
138+
this.updateSets = new ArrayList<>();
139+
}
140+
this.updateSets.add(updateSet);
141+
return this;
129142
}
130143

131144
@Deprecated

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 47 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,11 +1246,7 @@ Update Update( List<WithItem> with ):
12461246
Table table = null;
12471247
List<Join> startJoins = null;
12481248

1249-
UpdateSet updateSet;
1250-
Column tableColumn;
1251-
Expression valueExpression;
1252-
ExpressionList columns;
1253-
ExpressionList values;
1249+
List<UpdateSet> updateSets;
12541250
Expression where = null;
12551251
FromItem fromItem = null;
12561252
List<Join> joins = null;
@@ -1269,17 +1265,53 @@ Update Update( List<WithItem> with ):
12691265
[ LOOKAHEAD(2) <K_LOW_PRIORITY> { modifierPriority = UpdateModifierPriority.LOW_PRIORITY; }]
12701266
[ LOOKAHEAD(2) <K_IGNORE> { modifierIgnore = true; }]
12711267
table=TableWithAlias() [ startJoins=JoinsList() ]
1272-
<K_SET>
1268+
<K_SET> updateSets = UpdateSets() { update.setUpdateSets(updateSets); }
1269+
1270+
[ outputClause = OutputClause() {update.setOutputClause(outputClause); } ]
1271+
1272+
[ <K_FROM>
1273+
fromItem=FromItem()
1274+
[ joins=JoinsList() ] ]
1275+
1276+
[ where=WhereClause() { update.setWhere(where); } ]
1277+
1278+
[ orderByElements = OrderByElements() { update.setOrderByElements(orderByElements); } ]
1279+
[ limit = PlainLimit() { update.setLimit(limit); } ]
1280+
[ <K_RETURNING> returning=SelectItemsList() ]
1281+
1282+
{
1283+
return update.withWithItemsList(with)
1284+
.withTable(table)
1285+
.withStartJoins(startJoins)
1286+
.withFromItem(fromItem)
1287+
.withJoins(joins)
1288+
.withModifierPriority(modifierPriority)
1289+
.withModifierIgnore(modifierIgnore)
1290+
.withReturningExpressionList(returning);
1291+
}
1292+
}
1293+
1294+
List<UpdateSet> UpdateSets():
1295+
{
1296+
ArrayList<UpdateSet> updateSets = new ArrayList<UpdateSet>();
1297+
UpdateSet updateSet;
1298+
Column tableColumn;
1299+
Expression valueExpression;
1300+
1301+
ExpressionList<Column> columns;
1302+
ExpressionList<Expression>values;
1303+
}
1304+
{
12731305
(
12741306
(
1275-
tableColumn=Column() "=" valueExpression=SimpleExpression() { update.addUpdateSet(tableColumn, valueExpression); }
1307+
tableColumn=Column() "=" valueExpression=SimpleExpression()
1308+
{ updateSets.add( new UpdateSet (tableColumn, valueExpression)); }
12761309
)
12771310
|
12781311
(
1279-
{ updateSet = new UpdateSet(); update.addUpdateSet(updateSet); }
1312+
{ updateSet = new UpdateSet(); updateSets.add(updateSet); }
12801313
columns = ParenthesedExpressionList() { updateSet.setColumns(columns); }
12811314
"="
1282-
12831315
(
12841316
LOOKAHEAD(3) valueExpression = ParenthesedSelect() { updateSet.setValues( new ExpressionList(valueExpression)); }
12851317
|
@@ -1291,12 +1323,12 @@ Update Update( List<WithItem> with ):
12911323
(
12921324
","
12931325
(
1294-
tableColumn=Column() "=" valueExpression=SimpleExpression() { update.addUpdateSet(tableColumn, valueExpression); }
1326+
tableColumn=Column() "=" valueExpression=SimpleExpression()
1327+
{ updateSets.add( new UpdateSet (tableColumn, valueExpression)); }
12951328
)
12961329
|
12971330
(
1298-
{ updateSet = new UpdateSet(); update.addUpdateSet(updateSet); }
1299-
1331+
{ updateSet = new UpdateSet(); updateSets.add(updateSet); }
13001332
columns = ParenthesedExpressionList() { updateSet.setColumns(columns); }
13011333
"="
13021334
(
@@ -1307,27 +1339,8 @@ Update Update( List<WithItem> with ):
13071339
)
13081340
)*
13091341

1310-
[ outputClause = OutputClause() {update.setOutputClause(outputClause); } ]
1311-
1312-
[ <K_FROM>
1313-
fromItem=FromItem()
1314-
[ joins=JoinsList() ] ]
1315-
1316-
[ where=WhereClause() { update.setWhere(where); } ]
1317-
1318-
[ orderByElements = OrderByElements() { update.setOrderByElements(orderByElements); } ]
1319-
[ limit = PlainLimit() { update.setLimit(limit); } ]
1320-
[ <K_RETURNING> returning=SelectItemsList() ]
1321-
13221342
{
1323-
return update.withWithItemsList(with)
1324-
.withTable(table)
1325-
.withStartJoins(startJoins)
1326-
.withFromItem(fromItem)
1327-
.withJoins(joins)
1328-
.withModifierPriority(modifierPriority)
1329-
.withModifierIgnore(modifierIgnore)
1330-
.withReturningExpressionList(returning);
1343+
return updateSets;
13311344
}
13321345
}
13331346

@@ -1464,16 +1477,8 @@ InsertConflictTarget InsertConflictTarget():
14641477
InsertConflictAction InsertConflictAction():
14651478
{
14661479
InsertConflictAction conflictAction;
1467-
ArrayList<UpdateSet> updateSets = new ArrayList<UpdateSet>();
1468-
UpdateSet updateSet = null;
14691480
Expression whereExpression = null;
1470-
1471-
Column tableColumn = null;
1472-
ParenthesedSelect subSelect;
1473-
Expression valueExpression = null;
1474-
1475-
ExpressionList<Column> columns;
1476-
ExpressionList values;
1481+
List<UpdateSet> updateSets;
14771482
}
14781483
{
14791484
(
@@ -1483,47 +1488,12 @@ InsertConflictAction InsertConflictAction():
14831488
|
14841489
(
14851490
<K_DO> <K_UPDATE> <K_SET> { conflictAction = new InsertConflictAction( ConflictActionType.DO_UPDATE ); }
1486-
(
1487-
LOOKAHEAD(3) tableColumn=Column()
1488-
"=" valueExpression=SimpleExpression() {
1489-
updateSet = new UpdateSet();
1490-
updateSet.add(tableColumn);
1491-
updateSet.add(valueExpression);
1492-
updateSets.add(updateSet);
1493-
}
1494-
(
1495-
","
1496-
tableColumn=Column()
1497-
"=" valueExpression=SimpleExpression() {
1498-
updateSet = new UpdateSet();
1499-
updateSet.add(tableColumn);
1500-
updateSet.add(valueExpression);
1501-
updateSets.add(updateSet);
1502-
}
1503-
)*
1504-
|
1505-
(
1506-
{ updateSet = new UpdateSet(); updateSets.add(updateSet); }
1507-
1508-
columns = ExpressionList() { updateSet.setColumns(columns); }
1509-
"="
1510-
values = ExpressionList() { updateSet.setValues(values); }
1511-
1512-
(
1513-
"," { updateSet = new UpdateSet(); updateSets.add(updateSet); }
1514-
columns = ExpressionList() { updateSet.setColumns(columns); }
1515-
"="
1516-
values = ExpressionList() { updateSet.setValues(values); }
1517-
)*
1518-
)
1519-
)
1520-
1491+
updateSets = UpdateSets() { conflictAction.setUpdateSets(updateSets); }
15211492
[ whereExpression = WhereClause() ]
15221493
)
15231494
)
15241495

15251496
{ return conflictAction
1526-
.withUpdateSets(updateSets)
15271497
.withWhereExpression(whereExpression); }
15281498
}
15291499

0 commit comments

Comments
 (0)