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
24 changes: 20 additions & 4 deletions src/main/java/net/sf/jsqlparser/statement/select/SetOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,35 @@ public String getModifier() {
}

public boolean isAll() {
return modifier != null && modifier.contains("ALL");
return leadsWith("ALL");
}

public void setAll(boolean all) {
this.modifier = "ALL";
this.modifier = all ? "ALL" : "";
}

public boolean isDistinct() {
return modifier != null && modifier.contains("DISTINCT");
return leadsWith("DISTINCT");
}

public void setDistinct(boolean distinct) {
this.modifier = "DISTINCT";
this.modifier = distinct ? "DISTINCT" : "";
}

/**
* Whether the modifier opens with {@code keyword}, which the grammar records verbatim from the
* source and so may be written in any case. Anchored at the start and required to end on a word
* boundary, so a {@code CORRESPONDING BY NAME MATCHING(all)} column list cannot be mistaken for
* the ALL quantifier.
*/
private boolean leadsWith(String keyword) {
if (modifier == null) {
return false;
}
String trimmed = modifier.trim();
return trimmed.regionMatches(true, 0, keyword, 0, keyword.length())
&& (trimmed.length() == keyword.length()
|| !Character.isLetterOrDigit(trimmed.charAt(keyword.length())));
}

private final SetOperationType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -50,6 +51,72 @@ void testSetOperationModifierRoundTrip(String sql) throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(sql);
}

/**
* The grammar records the modifier verbatim from the source, so the quantifier survives in
* whatever case it was written. Every case spells the same set operation.
*
* @see <a href="https://github.com/JSQLParser/JSqlParser/issues/2419">#2419</a>
*/
@ParameterizedTest
@ValueSource(strings = {"ALL", "all", "All", "aLL"})
void testAllModifierRecognisedInAnyCase(String quantifier) throws JSQLParserException {
SetOperation operation = firstOperation(
"SELECT a FROM t1 UNION " + quantifier + " SELECT a FROM t2");
assertTrue(operation.isAll(), "UNION " + quantifier + " should be an ALL union");
assertFalse(operation.isDistinct(), "UNION " + quantifier + " is not DISTINCT");
}

@ParameterizedTest
@ValueSource(strings = {"DISTINCT", "distinct", "Distinct"})
void testDistinctModifierRecognisedInAnyCase(String quantifier) throws JSQLParserException {
SetOperation operation = firstOperation(
"SELECT a FROM t1 UNION " + quantifier + " SELECT a FROM t2");
assertTrue(operation.isDistinct(), "UNION " + quantifier + " should be a DISTINCT union");
assertFalse(operation.isAll(), "UNION " + quantifier + " is not ALL");
}

@ParameterizedTest
@ValueSource(strings = {"EXCEPT", "INTERSECT", "MINUS"})
void testAllModifierRecognisedInAnyCaseForEverySetOperation(String setOperation)
throws JSQLParserException {
assertTrue(firstOperation("SELECT a FROM t1 " + setOperation + " all SELECT a FROM t2")
.isAll(), setOperation + " all should be an ALL operation");
}

/**
* A plain set operation carries no quantifier, and neither predicate may claim one.
*/
@Test
void testUnqualifiedSetOperationIsNeitherAllNorDistinct() throws JSQLParserException {
SetOperation operation = firstOperation("SELECT a FROM t1 UNION SELECT a FROM t2");
assertFalse(operation.isAll());
assertFalse(operation.isDistinct());
}

/**
* The setters record what they are told rather than the keyword they are named after.
*/
@Test
void testSettersHonourTheirArgument() {
UnionOp union = new UnionOp();

union.setAll(true);
assertTrue(union.isAll());
union.setAll(false);
assertFalse(union.isAll(), "setAll(false) must not leave an ALL modifier behind");

union.setDistinct(true);
assertTrue(union.isDistinct());
union.setDistinct(false);
assertFalse(union.isDistinct(), "setDistinct(false) must not leave a DISTINCT modifier");
}

private static SetOperation firstOperation(String sql) throws JSQLParserException {
Statement statement = CCJSqlParserUtil.parse(sql);
assertInstanceOf(SetOperationList.class, statement);
return ((SetOperationList) statement).getOperations().get(0);
}

@ParameterizedTest
@MethodSource("provideModifierLeakCases")
void testModifierDoesNotLeakBetweenOperators(String sql, String forbidden)
Expand Down
Loading