-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fixing searchAndCount searchAndDistinctCount when sc is null #4374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -344,23 +344,13 @@ public T lockOneRandomRow(final SearchCriteria<T> sc, final boolean exclusive) { | |
|
|
||
| @DB() | ||
| protected List<T> search(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache) { | ||
| if (_removed != null) { | ||
| if (sc == null) { | ||
| sc = createSearchCriteria(); | ||
| } | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| } | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| return searchIncludingRemoved(sc, filter, lock, cache); | ||
| } | ||
|
|
||
| @DB() | ||
| protected List<T> search(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache, final boolean enableQueryCache) { | ||
| if (_removed != null) { | ||
| if (sc == null) { | ||
| sc = createSearchCriteria(); | ||
| } | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| } | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| return searchIncludingRemoved(sc, filter, lock, cache, enableQueryCache); | ||
| } | ||
|
|
||
|
|
@@ -519,7 +509,6 @@ public <M> List<M> customSearch(SearchCriteria<M> sc, final Filter filter) { | |
| if (_removed != null) { | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| } | ||
|
|
||
| return customSearchIncludingRemoved(sc, filter); | ||
| } | ||
|
|
||
|
|
@@ -911,26 +900,20 @@ protected T findOneIncludingRemovedBy(final SearchCriteria<T> sc) { | |
|
|
||
| @Override | ||
| @DB() | ||
| public T findOneBy(final SearchCriteria<T> sc) { | ||
| if (_removed != null) { | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| } | ||
| public T findOneBy(SearchCriteria<T> sc) { | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| return findOneIncludingRemovedBy(sc); | ||
| } | ||
|
|
||
| @DB() | ||
| protected List<T> listBy(final SearchCriteria<T> sc, final Filter filter) { | ||
| if (_removed != null) { | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| } | ||
| protected List<T> listBy(SearchCriteria<T> sc, final Filter filter) { | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| return listIncludingRemovedBy(sc, filter); | ||
| } | ||
|
|
||
| @DB() | ||
| protected List<T> listBy(final SearchCriteria<T> sc, final Filter filter, final boolean enableQueryCache) { | ||
| if (_removed != null) { | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| } | ||
| protected List<T> listBy(SearchCriteria<T> sc, final Filter filter, final boolean enableQueryCache) { | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| return listIncludingRemovedBy(sc, filter, enableQueryCache); | ||
| } | ||
|
|
||
|
|
@@ -1329,7 +1312,7 @@ public List<T> search(final SearchCriteria<T> sc, final Filter filter) { | |
| @DB() | ||
| public Pair<List<T>, Integer> searchAndCount(final SearchCriteria<T> sc, final Filter filter) { | ||
| List<T> objects = search(sc, filter, null, false); | ||
| Integer count = getCount(sc); | ||
| Integer count = getCount(sc, false); | ||
| // Count cannot be less than the result set but can be higher due to pagination, see CLOUDSTACK-10320 | ||
| if (count < objects.size()) { | ||
| count = objects.size(); | ||
|
|
@@ -1341,7 +1324,7 @@ public Pair<List<T>, Integer> searchAndCount(final SearchCriteria<T> sc, final F | |
| @DB() | ||
| public Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc, final Filter filter) { | ||
| List<T> objects = search(sc, filter, null, false); | ||
| Integer count = getDistinctCount(sc); | ||
| Integer count = getDistinctCount(sc, false); | ||
| // Count cannot be 0 if there is at least a result in the list, see CLOUDSTACK-10320 | ||
| if (count == 0 && !objects.isEmpty()) { | ||
| // Cannot assume if it's more than one since the count is distinct vs search | ||
|
|
@@ -1354,7 +1337,7 @@ public Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc, | |
| @DB() | ||
| public Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc, final Filter filter, final String[] distinctColumns) { | ||
| List<T> objects = search(sc, filter, null, false); | ||
| Integer count = getDistinctCount(sc, distinctColumns); | ||
| Integer count = getDistinctCount(sc, distinctColumns, false); | ||
| // Count cannot be 0 if there is at least a result in the list, see CLOUDSTACK-10320 | ||
| if (count == 0 && !objects.isEmpty()) { | ||
| // Cannot assume if it's more than one since the count is distinct vs search | ||
|
|
@@ -1935,6 +1918,23 @@ public SearchCriteria<T> createSearchCriteria() { | |
| return builder.create(); | ||
| } | ||
|
|
||
| private SearchCriteria<T> checkAndSetRemovedIsNull(SearchCriteria<T> sc) { | ||
| if (_removed != null) { | ||
| if (sc == null) { | ||
| sc = createSearchCriteria(); | ||
| } | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| } | ||
| return sc; | ||
| } | ||
|
|
||
| public Integer getDistinctCount(SearchCriteria<T> sc, boolean removed) { | ||
| if (!removed) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of now
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getDistinctCountExcludingRemoved is not in line with other methods, so I do not recommend.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default method should not include removed records, explicitly mentioned methods "*IncludingRemovedBy()" should. so, may be you can change current method to "*IncludingRemovedBy()" and add other method with current name.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added! Thanks @sureshanaparti
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect, changes LGTM. |
||
| sc = checkAndSetRemovedIsNull(sc); | ||
| } | ||
| return getDistinctCount(sc); | ||
| } | ||
|
|
||
| public Integer getDistinctCount(SearchCriteria<T> sc) { | ||
| String clause = sc != null ? sc.getWhereClause() : null; | ||
| if (clause != null && clause.length() == 0) { | ||
|
|
@@ -1993,6 +1993,13 @@ public Integer getDistinctCount(SearchCriteria<T> sc) { | |
| } | ||
| } | ||
|
|
||
| public Integer getDistinctCount(SearchCriteria<T> sc, String[] distinctColumns, boolean removed) { | ||
| if (!removed) { | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| } | ||
| return getDistinctCount(sc, distinctColumns); | ||
| } | ||
|
|
||
| public Integer getDistinctCount(SearchCriteria<T> sc, String[] distinctColumns) { | ||
| String clause = sc != null ? sc.getWhereClause() : null; | ||
| if (Strings.isNullOrEmpty(clause)) { | ||
|
|
@@ -2041,9 +2048,13 @@ public Integer getDistinctCount(SearchCriteria<T> sc, String[] distinctColumns) | |
|
|
||
| public Integer countAll() { | ||
| SearchCriteria<T> sc = null; | ||
| if (_removed != null) { | ||
| sc = createSearchCriteria(); | ||
| sc.addAnd(_removed.second().field.getName(), SearchCriteria.Op.NULL); | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| return getCount(sc); | ||
| } | ||
|
|
||
| public Integer getCount(SearchCriteria<T> sc, boolean removed) { | ||
| if (!removed) { | ||
| sc = checkAndSetRemovedIsNull(sc); | ||
| } | ||
| return getCount(sc); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use checkAndSetRemovedIsNull() here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes it a bit difficult since it is of a different type M not T
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok @davidjumani