Skip to content

Commit

Permalink
Duplicated Conditions (#127)
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr PG Gajek <[email protected]>
  • Loading branch information
pgajek2 authored Oct 3, 2024
1 parent 32645c1 commit 1e2a8f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ public virtual inherited sharing class SOQL implements Queryable {
String orderWithSpecialCharacters = getConditionsLogic();

for (Integer i = 0; i < queryConditions.size(); i++) {
orderWithSpecialCharacters = orderWithSpecialCharacters.replaceFirst(String.valueOf(i + 1), '{' + i + '}');
orderWithSpecialCharacters = orderWithSpecialCharacters.replaceAll('\\b' + String.valueOf(i + 1) + '\\b', '{' + i + '}');
}

return orderWithSpecialCharacters; // e.g ({0} AND ({1} AND {2}))
Expand Down
38 changes: 38 additions & 0 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,44 @@ private class SOQL_Test {
Assert.areEqual('%11%', binding.get('v11'));
}

@IsTest
static void duplicatedConditionsInConditionOrder() {
// Test
SOQL builder = SOQL.of(Account.SObjectType)
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('1')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('2')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('3')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('4')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('5')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('6')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('7')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('8')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('9')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('10')))
.whereAre(SOQL.FilterGroup.add(SOQL.Filter.with(Account.Name).contains('11')))
.conditionLogic('(1 AND 2 AND 3 AND 4 AND 5 AND 5 AND 7 AND 8 AND 9 AND 10 AND 11) OR (11 AND 1 AND 10)');

// Verify
String soql = builder.toString();
Assert.areEqual(
'SELECT Id FROM Account WHERE ((Name LIKE :v1) AND (Name LIKE :v2) AND (Name LIKE :v3) AND (Name LIKE :v4) AND (Name LIKE :v5) AND (Name LIKE :v5) AND (Name LIKE :v7) AND (Name LIKE :v8) AND (Name LIKE :v9) AND (Name LIKE :v10) AND (Name LIKE :v11)) OR ((Name LIKE :v11) AND (Name LIKE :v1) AND (Name LIKE :v10))',
soql
);

Map<String, Object> binding = builder.binding();
Assert.areEqual('%1%', binding.get('v1'));
Assert.areEqual('%2%', binding.get('v2'));
Assert.areEqual('%3%', binding.get('v3'));
Assert.areEqual('%4%', binding.get('v4'));
Assert.areEqual('%5%', binding.get('v5'));
Assert.areEqual('%6%', binding.get('v6'));
Assert.areEqual('%7%', binding.get('v7'));
Assert.areEqual('%8%', binding.get('v8'));
Assert.areEqual('%9%', binding.get('v9'));
Assert.areEqual('%10%', binding.get('v10'));
Assert.areEqual('%11%', binding.get('v11'));
}

@IsTest
static void anyConditionMatchingForInnerGroup() {
// Test
Expand Down

0 comments on commit 1e2a8f5

Please sign in to comment.