/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.MvcHelper;
namespace Everest.CmsServices.LINQ{
public class SchemaContentQueryable : ContentQueryable<ContentContainer>
{
public SchemaContentQueryable(string schemaName)
: base()
{
this.SchemaName = schemaName;
}
internal SchemaContentQueryable(string schemaName, Expression expression)
: base(expression)
{
this.SchemaName = schemaName;
}
public string SchemaName { get; private set; }
public override IQueryProvider Provider
{
get { return new SchemaContentQueryProvider(this); }
}
}
internal class SchemaContentQueryProvider : ContentQueryProvider
{
private SchemaContentQueryable queryable;
public SchemaContentQueryProvider(SchemaContentQueryable queryable)
{
this.queryable = queryable;
}
public override IQueryable<TElement> CreateQuery<TElement>(System.Linq.Expressions.Expression expression)
{
return (IQueryable<TElement>)new SchemaContentQueryable(queryable.SchemaName, expression);
}
public override IQueryable CreateQuery(System.Linq.Expressions.Expression expression)
{
Type elementType = TypeSystem.GetElementType(expression.Type);
try
{
return (IQueryable)Activator.CreateInstance(typeof(SchemaContentQueryable), new object[] { this, queryable.SchemaName, expression });
}
catch (TargetInvocationException tie)
{
throw tie.InnerException;
}
}
private object countValue;
protected override object Count(StatementExpression exp)
{
if (countValue == null)
{
countValue = ContentService.GetContentCountBySchema(queryable.SchemaName, exp.QueryStatement, exp.Parameters.ToNameValueCollection(), null);
}
return countValue;
}
private object firstValue;
protected override object First(StatementExpression exp)
{
if (firstValue == null)
{
firstValue = new ContentContainer(ContentService.GetContentsBySchema(queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).First());
}
return firstValue;
}
private object firstOrDefaultValue;
protected override object FirstOrDefault(StatementExpression exp)
{
if (firstOrDefaultValue == null)
{
var retValue = ContentService.GetContentsBySchema(queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).FirstOrDefault();
if (retValue != null)
{
firstOrDefaultValue = new ContentContainer(retValue);
}
else
{
firstOrDefaultValue = retValue;
}
}
return firstOrDefaultValue;
}
private object lastValue;
protected override object Last(StatementExpression exp)
{
if (lastValue == null)
{
var startIndex = exp.SkipCount;
if (exp.TakeCount == 0)
{
startIndex = startIndex + (int)Count(exp);
}
else
{
startIndex = startIndex + exp.TakeCount;
}
lastValue = new ContentContainer(ContentService.GetContentsBySchema(queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), startIndex, 1, null).First());
}
return lastValue;
}
private object lastOrDefaultValue;
protected override object LastOrDefault(StatementExpression exp)
{
if (lastValue == null)
{
var startIndex = exp.SkipCount;
if (exp.TakeCount == 0)
{
startIndex = startIndex + (int)Count(exp);
}
else
{
startIndex = startIndex + exp.TakeCount;
}
var retValue = ContentService.GetContentsBySchema(queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), startIndex, 1, null).FirstOrDefault();
if (retValue != null)
{
lastOrDefaultValue = new ContentContainer(retValue);
}
else
{
lastOrDefaultValue = retValue;
}
}
return lastValue;
}
private object value;
protected override object ExecuteCore(StatementExpression exp)
{
if (value == null)
{
var pageSize = exp.TakeCount;
if (pageSize == 0)
{
pageSize = int.MaxValue;
}
IList<ContentContainer> list = new List<ContentContainer>();
foreach (var item in ContentService.GetContentsBySchema(queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, pageSize, null))
{
list.Add(new ContentContainer(item));
}
value = list;
}
return value;
}
}
}
|