diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java index 9c8bbe21ac1..ed3375037d0 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/jdbc/UniqueIDJDBCUserStoreManager.java @@ -3733,12 +3733,14 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername (isGroupFiltering && totalMultiGroupFilters > 1)) { // Handle multi attribute filtering. StringBuilder brackets = new StringBuilder(") AS R"); - // x = 2 --> Any DB2 query will begin with 2 opening brackets - // (totalFilters * 2) --> totalFilters are multiplied by 2 as 2 new opening - // brackets are created for every new filter. - // (totalFilters * 2) - 1 --> Deducted by 1, as there is 1 closing brackets in the setTail - // section below. - for (int x = 2; x <= (totalFilters * 2) - 1; x++) { + /* + * x is used to count the number of brackets + * (totalFilters * 2) --> totalFilters are multiplied by 2 as 2 new opening + * brackets are created for every new filter, which needs to be closed at the right position + * (totalMultiClaimFilters * 2) - 3 is deducted as there are 2 opening brackets in the SQL + * query and 1 closing brackets in the setTail section below. + */ + for (int x = 0; x <= (totalFilters * 2) - 3; x++) { brackets.append(" ) AS R"); } sqlBuilder.setTail(brackets.toString().concat(") AS p WHERE p.rn BETWEEN 1 AND ?"), @@ -3751,8 +3753,9 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername (isGroupFiltering && totalMultiGroupFilters > 1)) { // Handle multi attribute filtering. StringBuilder brackets = new StringBuilder(") AS R"); - // - 1 as the remainder of the query within setTail() includes a single ) - for (int x = 2; x <= (totalFilters * 2) - 1; x++) { + // - 3 due to 2 opening brackets and the remainder of the query within setTail() includes + // a single ) + for (int x = 0; x <= (totalFilters * 2) - 3; x++) { brackets.append(" ) AS R"); } sqlBuilder.setTail(brackets.toString().concat(") AS p WHERE p.rn BETWEEN 1 AND ? " + @@ -3768,8 +3771,9 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername (isGroupFiltering && totalMultiGroupFilters > 1)) { // Handle multi attribute filtering. StringBuilder brackets = new StringBuilder(") AS R"); - // - 1 as the remainder of the query within setTail() includes a single ) - for (int x = 2; x <= (totalFilters * 2) - 1; x++) { + // - 3 due to 2 opening brackets and the remainder of the query within setTail() includes + // a single ) + for (int x = 0; x <= (totalFilters * 2) - 3; x++) { brackets.append(" ) AS R"); } sqlBuilder.setTail(brackets.toString().concat(") AS P WHERE P.RowNum BETWEEN 1 AND ?"), @@ -3780,9 +3784,10 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername } else if (UserCoreConstants.PaginationDirection.PREVIOUS == direction) { if ((isClaimFiltering && totalMultiClaimFilters > 1) || (isGroupFiltering && totalMultiGroupFilters > 1)) { - // Handle multi attribute filtering. + // - 3 due to 2 opening brackets and the remainder of the query within setTail() includes + // a single ) StringBuilder brackets = new StringBuilder(") AS R"); - for (int x = 2; x <= (totalFilters * 2) - 1; x++) { + for (int x = 0; x <= (totalFilters * 2) - 3; x++) { brackets.append(" ) AS R"); } sqlBuilder.setTail(brackets.toString().concat(") AS P WHERE P.RowNum " + @@ -3797,8 +3802,9 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername if ((isClaimFiltering && totalMultiClaimFilters > 1) || (isGroupFiltering && totalMultiGroupFilters > 1)) { StringBuilder brackets = new StringBuilder(")"); - // - 2 as the remainder of the query within setTail() includes two ) - for (int x = 2; x <= (totalFilters * 2) - 2; x++) { + // - 3 due to 2 opening brackets and the remainder of the query within setTail() includes + // 2 closing brackets. + for (int x = 0; x <= (totalFilters * 2) - 4; x++) { brackets.append(" )"); } // Handle multi attribute filtering. @@ -3811,9 +3817,9 @@ protected SqlBuilder getQueryString(boolean isGroupFiltering, boolean isUsername if ((isClaimFiltering && totalMultiClaimFilters > 1) || (isGroupFiltering && totalMultiGroupFilters > 1)) { StringBuilder brackets = new StringBuilder(")"); - // Still - 2 because this is a query to get results in the previous direction which has - // an additional opening ( in the SQL - for (int x = 2; x <= (totalFilters * 2) - 2; x++) { + // Still - 4 because this is a query to get results in the previous direction which has + // an additional opening ( in the SQL and an additional ) in the setTail section. + for (int x = 0; x <= (totalFilters * 2) - 4; x++) { brackets.append(" )"); } // Handle multi attribute filtering.