Skip to content

Commit

Permalink
fix: UpdateSet shall not have brackets with single element only
Browse files Browse the repository at this point in the history
- fixes #1910
  • Loading branch information
manticore-projects committed Dec 9, 2023
1 parent dd6cf23 commit 15b9aef
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public class MergeUpdate implements Serializable {
private Expression whereCondition;
private Expression deleteWhereCondition;

public MergeUpdate() {
}
public MergeUpdate() {}

public MergeUpdate(List<UpdateSet> updateSets) {
this.updateSets = updateSets;
Expand Down Expand Up @@ -98,7 +97,7 @@ public MergeUpdate withDeleteWhereCondition(Expression deleteWhereCondition) {

public <E extends Expression> E getAndPredicate(Class<E> type) {
return type.cast(getAndPredicate());
}
}

public <E extends Expression> E getWhereCondition(Class<E> type) {
return type.cast(getWhereCondition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void add(Column column, Expression value) {
* @param column
*/
public void add(Column column) {
if (columns.size() < 2 && !(columns instanceof ParenthesedExpressionList)) {
if (!columns.isEmpty() && !(columns instanceof ParenthesedExpressionList)) {
columns = new ParenthesedExpressionList<>(columns);
}
columns.add(column);
Expand All @@ -85,7 +85,7 @@ public void add(Column column) {
* @param expression
*/
public void add(Expression expression) {
if (values.size() < 2 && !(values instanceof ParenthesedExpressionList)) {
if (!values.isEmpty() && !(values instanceof ParenthesedExpressionList)) {
values = new ParenthesedExpressionList<>(values);
}
values.add(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ public void testSnowflakeMergeStatementWithMatchedAndPredicate() throws JSQLPars

@Test
void testSnowflakeMergeStatementWithNotMatchedAndPredicate() throws JSQLParserException {
String sql = "MERGE INTO target USING (select k, max(v) as v from src group by k) AS b ON target.k = b.k\n" +
" WHEN MATCHED THEN UPDATE SET target.v = b.v\n" +
" WHEN NOT MATCHED AND b.v != 11 THEN INSERT (k, v) VALUES (b.k, b.v)";
String sql =
"MERGE INTO target USING (select k, max(v) as v from src group by k) AS b ON target.k = b.k\n"
+
" WHEN MATCHED THEN UPDATE SET target.v = b.v\n" +
" WHEN NOT MATCHED AND b.v != 11 THEN INSERT (k, v) VALUES (b.k, b.v)";

assertSqlCanBeParsedAndDeparsed(sql, true);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/update/UpdateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

import java.io.StringReader;
Expand Down Expand Up @@ -362,4 +364,20 @@ void testArrayColumnsIssue1083() throws JSQLParserException {
sqlStr = "update utilisateur set listes[0:3] = (1,2,3,4)";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testIssue1910() throws JSQLParserException {
Update update = new Update();
update.setTable(new Table("sys_dept"));

UpdateSet updateSet = new UpdateSet(new Column("deleted"), new LongValue(1L));
update.addUpdateSet(updateSet);

TestUtils.assertStatementCanBeDeparsedAs(update, "UPDATE sys_dept SET deleted = 1", true);

updateSet.add(new Column("created"), new LongValue(2L));

TestUtils.assertStatementCanBeDeparsedAs(update,
"UPDATE sys_dept SET (deleted, created) = (1,2)", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,10 @@ public void testConnectedByRootOperator() throws JSQLParserException {
void testJoinSubSelect() throws JSQLParserException {
String sqlStr = "select * from A left join B on A.id=B.id and A.age = (select age from C)";
Set<String> tableNames = TablesNamesFinder.findTables(sqlStr);
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
assertThat(tableNames).containsExactlyInAnyOrder("A", "B", "C");

String exprStr = "A.id=B.id and A.age = (select age from C)";
tableNames = TablesNamesFinder.findTablesInExpression(exprStr);
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
assertThat(tableNames).containsExactlyInAnyOrder("A", "B", "C");
}
}

0 comments on commit 15b9aef

Please sign in to comment.