mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Adding a Count() to the DefaultContentQuery
Also pulling the version option restrictions being applied in Slice(int skip, int count) to an ICriteria extension method in the same file to be used by Count() --HG-- branch : dev
This commit is contained in:
@@ -124,32 +124,12 @@ namespace Orchard.ContentManagement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private IEnumerable<ContentItem> Slice(int skip, int count) {
|
private IEnumerable<ContentItem> Slice(int skip, int count) {
|
||||||
var criteria = BindItemVersionCriteria();
|
var criteria = BindItemVersionCriteria();
|
||||||
if (_versionOptions == null) {
|
|
||||||
criteria.Add(Restrictions.Eq("Published", true));
|
criteria.ApplyVersionOptionsRestrictions(_versionOptions);
|
||||||
}
|
|
||||||
else if (_versionOptions.IsPublished) {
|
|
||||||
criteria.Add(Restrictions.Eq("Published", true));
|
|
||||||
}
|
|
||||||
else if (_versionOptions.IsLatest) {
|
|
||||||
criteria.Add(Restrictions.Eq("Latest", true));
|
|
||||||
}
|
|
||||||
else if (_versionOptions.IsDraft) {
|
|
||||||
criteria.Add(Restrictions.And(
|
|
||||||
Restrictions.Eq("Latest", true),
|
|
||||||
Restrictions.Eq("Published", false)));
|
|
||||||
}
|
|
||||||
else if (_versionOptions.IsAllVersions) {
|
|
||||||
// no-op... all versions will be returned by default
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new ApplicationException("Invalid VersionOptions for content query");
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: put 'removed false' filter in place
|
// TODO: put 'removed false' filter in place
|
||||||
|
|
||||||
if (skip != 0) {
|
if (skip != 0) {
|
||||||
criteria = criteria.SetFirstResult(skip);
|
criteria = criteria.SetFirstResult(skip);
|
||||||
}
|
}
|
||||||
@@ -162,6 +142,15 @@ namespace Orchard.ContentManagement {
|
|||||||
.ToReadOnlyCollection();
|
.ToReadOnlyCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Count() {
|
||||||
|
var criteria = (ICriteria)BindItemVersionCriteria().Clone();
|
||||||
|
criteria.ClearOrders();
|
||||||
|
|
||||||
|
criteria.ApplyVersionOptionsRestrictions(_versionOptions);
|
||||||
|
|
||||||
|
return criteria.SetProjection(Projections.RowCount()).UniqueResult<Int32>();
|
||||||
|
}
|
||||||
|
|
||||||
IContentQuery<TPart> IContentQuery.ForPart<TPart>() {
|
IContentQuery<TPart> IContentQuery.ForPart<TPart>() {
|
||||||
return new ContentQuery<TPart>(this);
|
return new ContentQuery<TPart>(this);
|
||||||
}
|
}
|
||||||
@@ -195,16 +184,14 @@ namespace Orchard.ContentManagement {
|
|||||||
return _query.Slice(0, 0).AsPart<T>();
|
return _query.Slice(0, 0).AsPart<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
int IContentQuery<T>.Count() {
|
|
||||||
var criteria = (ICriteria)_query.BindItemVersionCriteria().Clone();
|
|
||||||
criteria.ClearOrders();
|
|
||||||
return criteria.SetProjection( Projections.RowCount() ).UniqueResult<Int32>();
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerable<T> IContentQuery<T>.Slice(int skip, int count) {
|
IEnumerable<T> IContentQuery<T>.Slice(int skip, int count) {
|
||||||
return _query.Slice(skip, count).AsPart<T>();
|
return _query.Slice(skip, count).AsPart<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int IContentQuery<T>.Count() {
|
||||||
|
return _query.Count();
|
||||||
|
}
|
||||||
|
|
||||||
IContentQuery<T, TRecord> IContentQuery<T>.Join<TRecord>() {
|
IContentQuery<T, TRecord> IContentQuery<T>.Join<TRecord>() {
|
||||||
_query.Where<TRecord>();
|
_query.Where<TRecord>();
|
||||||
return new ContentQuery<T, TRecord>(_query);
|
return new ContentQuery<T, TRecord>(_query);
|
||||||
@@ -256,4 +243,29 @@ namespace Orchard.ContentManagement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static class CriteriaExtensions {
|
||||||
|
internal static void ApplyVersionOptionsRestrictions(this ICriteria criteria, VersionOptions versionOptions) {
|
||||||
|
if (versionOptions == null) {
|
||||||
|
criteria.Add(Restrictions.Eq("Published", true));
|
||||||
|
}
|
||||||
|
else if (versionOptions.IsPublished) {
|
||||||
|
criteria.Add(Restrictions.Eq("Published", true));
|
||||||
|
}
|
||||||
|
else if (versionOptions.IsLatest) {
|
||||||
|
criteria.Add(Restrictions.Eq("Latest", true));
|
||||||
|
}
|
||||||
|
else if (versionOptions.IsDraft) {
|
||||||
|
criteria.Add(Restrictions.And(
|
||||||
|
Restrictions.Eq("Latest", true),
|
||||||
|
Restrictions.Eq("Published", false)));
|
||||||
|
}
|
||||||
|
else if (versionOptions.IsAllVersions) {
|
||||||
|
// no-op... all versions will be returned by default
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new ApplicationException("Invalid VersionOptions for content query");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user