Skip to content

Commit

Permalink
[ANDROSDK-804] Wrap ordery by where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarciabnz committed May 3, 2019
1 parent 27496a3 commit 2f51430
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static void addSortingClauses(WhereClauseBuilder whereClauseBuilder,
.direction(RepositoryScope.OrderByDirection.ASC).build());
}

WhereClauseBuilder wrapperClause = new WhereClauseBuilder();
do {
List<RepositoryScopeOrderByItem> nextIterationItems = new ArrayList<>();
WhereClauseBuilder subWhereClause = new WhereClauseBuilder();
Expand All @@ -91,10 +92,12 @@ public static void addSortingClauses(WhereClauseBuilder whereClauseBuilder,
nextIterationItems.add(item);
}
}
whereClauseBuilder.appendOrComplexQuery(subWhereClause.build());
wrapperClause.appendOrComplexQuery(subWhereClause.build());
items = nextIterationItems;
}
while (!items.isEmpty());

whereClauseBuilder.appendComplexQuery(wrapperClause.build());
}

private static void addItemOperator(WhereClauseBuilder whereClauseBuilder, RepositoryScopeOrderByItem item,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.hisp.dhis.android.core.arch.repositories.paging.RepositoryDataSource;
import org.hisp.dhis.android.core.arch.repositories.paging.RepositoryPagingConfig;
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScope;
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScopeFilterItem;
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScopeHelper;
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScopeOrderByItem;
import org.hisp.dhis.android.core.category.CategoryOption;
Expand Down Expand Up @@ -137,22 +138,26 @@ public void get_initial_page_objects_with_two_order_by() {
}

@Test
public void get_after_page_objects_with_order_by() {
public void get_after_page_objects_with_order_by_and_filter() {
when(key.toContentValues()).thenReturn(keyContentValues);
when(keyContentValues.getAsString("_id")).thenReturn("5");
when(keyContentValues.getAsString("code")).thenReturn("key-code");
when(keyContentValues.getAsString("name")).thenReturn("key-name");

RepositoryScope updatedScope = RepositoryScopeHelper.withOrderBy(emptyScope,
RepositoryScope filterScope = RepositoryScopeHelper.withFilterItem(emptyScope,
RepositoryScopeFilterItem.builder().key("program").operator("=").value("'uid'").build());

RepositoryScope updatedScope = RepositoryScopeHelper.withOrderBy(filterScope,
RepositoryScopeOrderByItem.builder().column("code").direction(RepositoryScope.OrderByDirection.DESC).build());
RepositoryScope updatedScope2 = RepositoryScopeHelper.withOrderBy(updatedScope,
RepositoryScopeOrderByItem.builder().column("name").direction(RepositoryScope.OrderByDirection.ASC).build());
RepositoryDataSource<CategoryOption> dataSource = new RepositoryDataSource<>(store, updatedScope2, childrenAppenders);
dataSource.loadAfter(new ItemKeyedDataSource.LoadParams<>(key, 3), initialCallback);
verify(store).selectWhere(
"(code = 'key-code' AND name = 'key-name' AND _id > '5') OR " +
"((code = 'key-code' AND name = 'key-name' AND _id > '5') OR " +
"(code = 'key-code' AND name > 'key-name') OR " +
"(code < 'key-code')",
"(code < 'key-code')) " +
"AND program = 'uid'",
"code DESC, name ASC, _id ASC",
3);
verify(initialCallback).onResult(objects);
Expand Down

0 comments on commit 2f51430

Please sign in to comment.