Removing alterations to ContentQuery

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-12-12 17:57:43 -08:00
parent 682f38c1ee
commit 67fae9e19f
6 changed files with 29 additions and 361 deletions

View File

@@ -1,5 +1,5 @@
ec573e5476f7e8a5a61593d6393e9985e9484fcc src/Orchard.Web/Modules/Orchard.Forms
4e99d7e20caab07ddbdcf9c780525613bcf8a15b src/Orchard.Web/Modules/Orchard.Projections
09ddede21aceaf0f403aae2ed84f479818f7b3af src/Orchard.Web/Modules/Orchard.Projections
01b83c05050bb731d9f69256bbe8884d458ea1c9 src/Orchard.Web/Modules/Orchard.Rules
65057c6a5cd71f7994ba9bcbeece50dbb737620e src/Orchard.Web/Modules/Orchard.TaskLease
c2e3c396c1fc6c60b131bfc9f83564c6f8d18e58 src/Orchard.Web/Modules/Orchard.Tokens

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
@@ -31,36 +30,37 @@ namespace Orchard.ContentManagement {
return _session;
}
internal ICriteria BindCriteriaByPath(ICriteria criteria, string path, string alias = null) {
return criteria.GetCriteriaByPath(path) ?? (alias == null ? criteria.CreateCriteria(path) : criteria.CreateCriteria(path, alias));
ICriteria BindCriteriaByPath(ICriteria criteria, string path) {
return criteria.GetCriteriaByPath(path) ?? criteria.CreateCriteria(path);
}
internal ICriteria BindTypeCriteria() {
ICriteria BindTypeCriteria() {
// ([ContentItemVersionRecord] >join> [ContentItemRecord]) >join> [ContentType]
return BindCriteriaByPath(BindItemCriteria(), "ContentType");
}
internal ICriteria BindItemCriteria() {
ICriteria BindItemCriteria() {
// [ContentItemVersionRecord] >join> [ContentItemRecord]
return BindCriteriaByPath(BindItemVersionCriteria(), "ContentItemRecord", "ci");
return BindCriteriaByPath(BindItemVersionCriteria(), "ContentItemRecord");
}
internal ICriteria BindItemVersionCriteria() {
ICriteria BindItemVersionCriteria() {
if (_itemVersionCriteria == null) {
_itemVersionCriteria = BindSession().CreateCriteria<ContentItemVersionRecord>("civ");
_itemVersionCriteria = BindSession().CreateCriteria<ContentItemVersionRecord>();
}
return _itemVersionCriteria;
}
internal ICriteria BindPartCriteria<TRecord>() where TRecord : ContentPartRecord {
ICriteria BindPartCriteria<TRecord>() where TRecord : ContentPartRecord {
if (typeof(TRecord).IsSubclassOf(typeof(ContentPartVersionRecord))) {
return BindCriteriaByPath(BindItemVersionCriteria(), typeof(TRecord).Name);
}
return BindCriteriaByPath(BindItemCriteria(), typeof(TRecord).Name);
}
private void ForType(params string[] contentTypeNames) {
if (contentTypeNames != null && contentTypeNames.Length != 0)
BindTypeCriteria().Add(Restrictions.InG("Name", contentTypeNames));
@@ -75,44 +75,20 @@ namespace Orchard.ContentManagement {
BindPartCriteria<TRecord>();
}
private void Where<TRecord>(Expression<Func<TRecord, bool>> predicate, params Expression<Func<TRecord, bool>>[] orPredicates) where TRecord : ContentPartRecord {
private void Where<TRecord>(Expression<Func<TRecord, bool>> predicate) where TRecord : ContentPartRecord {
// build a linq to nhibernate expression
var options = new QueryOptions();
var queryProvider = new NHibernateQueryProvider(BindSession(), options);
var queryable = new Query<TRecord>(queryProvider, options).Where(predicate);
// translate it into the nhibernate ICriteria implementation
var criteria = (CriteriaImpl)queryProvider.TranslateExpression(queryable.Expression);
// attach the criterion from the predicate to this query's criteria for the record
var recordCriteria = BindPartCriteria<TRecord>();
var predicates = Enumerable
.Empty<Expression<Func<TRecord, bool>>>()
.Union(new []{predicate})
.Union(orPredicates);
var disjunction = Restrictions.Disjunction();
foreach (var p in predicates) {
// build a linq to nhibernate expression
var options = new QueryOptions();
var queryProvider = new NHibernateQueryProvider(BindSession(), options);
var queryable = new Query<TRecord>(queryProvider, options).Where(p);
var conjunction = Restrictions.Conjunction();
// translate it into the nhibernate ICriteria implementation
var criteria = (CriteriaImpl) queryProvider.TranslateExpression(queryable.Expression);
foreach (var expressionEntry in criteria.IterateExpressionEntries()) {
conjunction.Add(expressionEntry.Criterion);
}
disjunction.Add(conjunction);
}
recordCriteria.Add(disjunction);
}
private void Where(Action<IExpressionFactory> expression) {
var expressionFactory = new DefaultExpressionFactory(this);
expression(expressionFactory);
if (expressionFactory.Criterion != null) {
expressionFactory.Criteria.Add(expressionFactory.Criterion);
foreach (var expressionEntry in criteria.IterateExpressionEntries()) {
recordCriteria.Add(expressionEntry.Criterion);
}
}
@@ -132,15 +108,6 @@ namespace Orchard.ContentManagement {
}
}
private void OrderBy(Action<ISortFactory> expression) {
var sortFactory = new DefaultSortFactory(this);
expression(sortFactory);
if (sortFactory.Order != null) {
sortFactory.Criteria.AddOrder(sortFactory.Order);
}
}
private void OrderByDescending<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) where TRecord : ContentPartRecord {
// build a linq to nhibernate expression
var options = new QueryOptions();
@@ -159,7 +126,7 @@ namespace Orchard.ContentManagement {
private IEnumerable<ContentItem> Slice(int skip, int count) {
var criteria = BindItemVersionCriteria();
criteria.ApplyVersionOptionsRestrictions(_versionOptions);
// TODO: put 'removed false' filter in place
@@ -230,13 +197,8 @@ namespace Orchard.ContentManagement {
return new ContentQuery<T, TRecord>(_query);
}
IContentQuery<T> IContentQuery<T>.Where(Action<IExpressionFactory> predicate) {
IContentQuery<T, TRecord> IContentQuery<T>.Where<TRecord>(Expression<Func<TRecord, bool>> predicate) {
_query.Where(predicate);
return new ContentQuery<T>(_query);
}
IContentQuery<T, TRecord> IContentQuery<T>.Where<TRecord>(Expression<Func<TRecord, bool>> predicate, params Expression<Func<TRecord, bool>>[] orPredicates) {
_query.Where(predicate, orPredicates);
return new ContentQuery<T, TRecord>(_query);
}
@@ -249,13 +211,9 @@ namespace Orchard.ContentManagement {
_query.OrderByDescending(keySelector);
return new ContentQuery<T, TRecord>(_query);
}
IContentQuery<T> IContentQuery<T>.OrderBy(Action<ISortFactory> order) {
_query.OrderBy(order);
return new ContentQuery<T>(_query);
}
}
class ContentQuery<T, TR> : ContentQuery<T>, IContentQuery<T, TR>
where T : IContent
where TR : ContentPartRecord {
@@ -268,8 +226,8 @@ namespace Orchard.ContentManagement {
return this;
}
IContentQuery<T, TR> IContentQuery<T, TR>.Where(Expression<Func<TR, bool>> predicate, params Expression<Func<TR, bool>>[] orPredicates) {
_query.Where(predicate, orPredicates);
IContentQuery<T, TR> IContentQuery<T, TR>.Where(Expression<Func<TR, bool>> predicate) {
_query.Where(predicate);
return this;
}
@@ -284,230 +242,6 @@ namespace Orchard.ContentManagement {
}
}
public class DefaultExpressionFactory : IExpressionFactory {
private readonly DefaultContentQuery _query;
public ICriterion Criterion { get; private set; }
public ICriteria Criteria { get; private set; }
public DefaultExpressionFactory(DefaultContentQuery query) {
_query = query;
}
public IExpressionFactory WithRecord(string path) {
Criteria = _query.BindCriteriaByPath(_query.BindItemCriteria(), path);
return this;
}
public IExpressionFactory WithRelationship(string path) {
Criteria = _query.BindCriteriaByPath(Criteria, path);
return this;
}
public IExpressionFactory WithVersionRecord(string path) {
Criteria = _query.BindCriteriaByPath(_query.BindItemVersionCriteria(), path);
return this;
}
public IExpressionFactory WithIds(ICollection<int> ids) {
Criteria = _query.BindItemCriteria();
Criterion = Restrictions.InG("Id", ids);
return this;
}
public void Eq(string propertyName, object value) {
Criterion = Restrictions.Eq(propertyName, value);
}
public void Like(string propertyName, object value) {
Criterion = Restrictions.Like(propertyName, value);
}
public void Like(string propertyName, string value, MatchMode matchMode, char? escapeChar) {
Criterion = Restrictions.Like(propertyName, value, ToMatchMode(matchMode), escapeChar);
}
public void Like(string propertyName, string value, MatchMode matchMode) {
Criterion = Restrictions.Like(propertyName, value, ToMatchMode(matchMode));
}
public void InsensitiveLike(string propertyName, string value, MatchMode matchMode) {
Criterion = Restrictions.InsensitiveLike(propertyName, value, ToMatchMode(matchMode));
}
public void InsensitiveLike(string propertyName, object value) {
Criterion = Restrictions.InsensitiveLike(propertyName, value);
}
public void Gt(string propertyName, object value) {
Criterion = Restrictions.Gt(propertyName, value);
}
public void Lt(string propertyName, object value) {
Criterion = Restrictions.Lt(propertyName, value);
}
public void Le(string propertyName, object value) {
Criterion = Restrictions.Le(propertyName, value);
}
public void Ge(string propertyName, object value) {
Criterion = Restrictions.Ge(propertyName, value);
}
public void Between(string propertyName, object lo, object hi) {
Criterion = Restrictions.Between(propertyName, lo, hi);
}
public void In(string propertyName, object[] values) {
Criterion = Restrictions.In(propertyName, values);
}
public void In(string propertyName, ICollection values) {
Criterion = Restrictions.In(propertyName, values);
}
public void InG<T>(string propertyName, ICollection<T> values) {
Criterion = Restrictions.InG(propertyName, values);
}
public void IsNull(string propertyName) {
Criterion = Restrictions.IsNull(propertyName);
}
public void EqProperty(string propertyName, string otherPropertyName) {
Criterion = Restrictions.EqProperty(propertyName, otherPropertyName);
}
public void NotEqProperty(string propertyName, string otherPropertyName) {
Criterion = Restrictions.NotEqProperty(propertyName, otherPropertyName);
}
public void GtProperty(string propertyName, string otherPropertyName) {
Criterion = Restrictions.GtProperty(propertyName, otherPropertyName);
}
public void GeProperty(string propertyName, string otherPropertyName) {
Criterion = Restrictions.GeProperty(propertyName, otherPropertyName);
}
public void LtProperty(string propertyName, string otherPropertyName) {
Criterion = Restrictions.LtProperty(propertyName, otherPropertyName);
}
public void LeProperty(string propertyName, string otherPropertyName) {
Criterion = Restrictions.LeProperty(propertyName, otherPropertyName);
}
public void IsNotNull(string propertyName) {
Criterion = Restrictions.IsNotNull(propertyName);
}
public void IsNotEmpty(string propertyName) {
Criterion = Restrictions.IsNotEmpty(propertyName);
}
public void IsEmpty(string propertyName) {
Criterion = Restrictions.IsEmpty(propertyName);
}
public void And(Action<IExpressionFactory> lhs, Action<IExpressionFactory> rhs) {
lhs(this);
var a = Criterion;
rhs(this);
var b = Criterion;
Criterion = Restrictions.And(a, b);
}
public void Or(Action<IExpressionFactory> lhs, Action<IExpressionFactory> rhs) {
lhs(this);
var a = Criterion;
rhs(this);
var b = Criterion;
Criterion = Restrictions.Or(a, b);
}
public void Not(Action<IExpressionFactory> expression) {
expression(this);
var a = Criterion;
Criterion = Restrictions.Not(a);
}
public void Conjunction(Action<IExpressionFactory> expression, params Action<IExpressionFactory>[] otherExpressions) {
var junction = Restrictions.Conjunction();
foreach (var exp in Enumerable.Empty<Action<IExpressionFactory>>().Union(new[] { expression }).Union(otherExpressions)) {
exp(this);
junction.Add(Criterion);
}
Criterion = junction;
}
public void Disjunction(Action<IExpressionFactory> expression, params Action<IExpressionFactory>[] otherExpressions) {
var junction = Restrictions.Disjunction();
foreach (var exp in Enumerable.Empty<Action<IExpressionFactory>>().Union(new[] { expression }).Union(otherExpressions)) {
exp(this);
junction.Add(Criterion);
}
Criterion = junction;
}
public void AllEq(IDictionary propertyNameValues) {
Criterion = Restrictions.AllEq(propertyNameValues);
}
public void NaturalId() {
Criterion = Restrictions.NaturalId();
}
private static NHibernate.Criterion.MatchMode ToMatchMode(MatchMode matchMode) {
switch(matchMode) {
case MatchMode.Anywhere:
return NHibernate.Criterion.MatchMode.Anywhere;
case MatchMode.End:
return NHibernate.Criterion.MatchMode.End;
case MatchMode.Start:
return NHibernate.Criterion.MatchMode.Start;
case MatchMode.Exact:
return NHibernate.Criterion.MatchMode.Exact;
}
return NHibernate.Criterion.MatchMode.Anywhere;
}
}
public class DefaultSortFactory : ISortFactory {
private readonly DefaultContentQuery _query;
public Order Order { get; private set; }
public ICriteria Criteria { get; private set; }
public DefaultSortFactory(DefaultContentQuery query) {
_query = query;
}
public ISortFactory WithRecord(string path) {
Criteria = _query.BindCriteriaByPath(_query.BindItemCriteria(), path);
return this;
}
public ISortFactory WithVersionRecord(string path) {
Criteria = _query.BindCriteriaByPath(_query.BindItemVersionCriteria(), path);
return this;
}
public ISortFactory WithRelationship(string path) {
Criteria = _query.BindCriteriaByPath(Criteria, path);
return this;
}
public void Asc(string propertyName) {
Order = Order.Asc(propertyName);
}
public void Desc(string propertyName) {
Order = Order.Desc(propertyName);
}
}
}
internal static class CriteriaExtensions {

View File

@@ -5,7 +5,7 @@ using Orchard.ContentManagement.Records;
namespace Orchard.ContentManagement {
public interface IContentQuery{
public interface IContentQuery {
IContentManager ContentManager { get; }
IContentQuery<TPart> ForPart<TPart>() where TPart : IContent;
}
@@ -20,10 +20,7 @@ namespace Orchard.ContentManagement {
IContentQuery<TPart, TRecord> Join<TRecord>() where TRecord : ContentPartRecord;
IContentQuery<TPart> Where(Action<IExpressionFactory> predicate);
IContentQuery<TPart, TRecord> Where<TRecord>(Expression<Func<TRecord, bool>> predicate, params Expression<Func<TRecord, bool>>[] orPredicates) where TRecord : ContentPartRecord;
IContentQuery<TPart> OrderBy(Action<ISortFactory> order);
IContentQuery<TPart, TRecord> Where<TRecord>(Expression<Func<TRecord, bool>> predicate) where TRecord : ContentPartRecord;
IContentQuery<TPart, TRecord> OrderBy<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) where TRecord : ContentPartRecord;
IContentQuery<TPart, TRecord> OrderByDescending<TRecord, TKey>(Expression<Func<TRecord, TKey>> keySelector) where TRecord : ContentPartRecord;
}
@@ -31,7 +28,7 @@ namespace Orchard.ContentManagement {
public interface IContentQuery<TPart, TRecord> : IContentQuery<TPart> where TPart : IContent where TRecord : ContentPartRecord {
new IContentQuery<TPart, TRecord> ForVersion(VersionOptions options);
IContentQuery<TPart, TRecord> Where(Expression<Func<TRecord, bool>> predicate, params Expression<Func<TRecord, bool>>[] orPredicates);
IContentQuery<TPart, TRecord> Where(Expression<Func<TRecord, bool>> predicate);
IContentQuery<TPart, TRecord> OrderBy<TKey>(Expression<Func<TRecord, TKey>> keySelector);
IContentQuery<TPart, TRecord> OrderByDescending<TKey>(Expression<Func<TRecord, TKey>> keySelector);
}

View File

@@ -1,51 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace Orchard.ContentManagement {
public interface IExpressionFactory {
IExpressionFactory WithRecord(string recordName);
IExpressionFactory WithVersionRecord(string recordName);
IExpressionFactory WithRelationship(string propertyName);
IExpressionFactory WithIds(ICollection<int> ids);
void Eq(string propertyName, object value);
void Like(string propertyName, object value);
void Like(string propertyName, string value, MatchMode matchMode, char? escapeChar);
void Like(string propertyName, string value, MatchMode matchMode);
void InsensitiveLike(string propertyName, string value, MatchMode matchMode);
void InsensitiveLike(string propertyName, object value);
void Gt(string propertyName, object value);
void Lt(string propertyName, object value);
void Le(string propertyName, object value);
void Ge(string propertyName, object value);
void Between(string propertyName, object lo, object hi);
void In(string propertyName, object[] values);
void In(string propertyName, ICollection values);
void InG<T>(string propertyName, ICollection<T> values);
void IsNull(string propertyName);
void EqProperty(string propertyName, string otherPropertyName);
void NotEqProperty(string propertyName, string otherPropertyName);
void GtProperty(string propertyName, string otherPropertyName);
void GeProperty(string propertyName, string otherPropertyName);
void LtProperty(string propertyName, string otherPropertyName);
void LeProperty(string propertyName, string otherPropertyName);
void IsNotNull(string propertyName);
void IsNotEmpty(string propertyName);
void IsEmpty(string propertyName);
void And(Action<IExpressionFactory> lhs, Action<IExpressionFactory> rhs);
void Or(Action<IExpressionFactory> lhs, Action<IExpressionFactory> rhs);
void Not(Action<IExpressionFactory> expression);
void Conjunction(Action<IExpressionFactory> expression, params Action<IExpressionFactory>[] otherExpressions);
void Disjunction(Action<IExpressionFactory> expression, params Action<IExpressionFactory>[] otherExpressions);
void AllEq(IDictionary propertyNameValues);
void NaturalId();
}
public enum MatchMode {
Exact,
Start,
End,
Anywhere
}
}

View File

@@ -1,10 +0,0 @@
namespace Orchard.ContentManagement {
public interface ISortFactory {
ISortFactory WithRecord(string recordName);
ISortFactory WithVersionRecord(string recordName);
ISortFactory WithRelationship(string propertyName);
void Asc(string propertyName);
void Desc(string propertyName);
}
}

View File

@@ -174,7 +174,6 @@
<Compile Include="ContentManagement\IHqlQuery.cs" />
<Compile Include="ContentManagement\Handlers\DescribeMembersContext.cs" />
<Compile Include="ContentManagement\IHqlCriterion.cs" />
<Compile Include="ContentManagement\ISortFactory.cs" />
<Compile Include="ContentManagement\DefaultContentDisplay.cs" />
<Compile Include="ContentManagement\Drivers\ContentShapeResult.cs" />
<Compile Include="ContentManagement\FieldStorage\IFieldStorageEvents.cs" />
@@ -184,7 +183,6 @@
<Compile Include="ContentManagement\Handlers\ImportContentContext.cs" />
<Compile Include="ContentManagement\Handlers\TitleAspectHandler.cs" />
<Compile Include="ContentManagement\IContentBehavior.cs" />
<Compile Include="ContentManagement\IExpressionFactory.cs" />
<Compile Include="ContentManagement\ImportContentSession.cs" />
<Compile Include="ContentManagement\MetaData\Services\ISettingsFormatter.cs" />
<Compile Include="ContentManagement\QueryHints.cs" />