/*
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.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using Everest.CmsServices.MvcHelper;
namespace Everest.CmsServices.LINQ{
internal abstract class ContentQueryProvider : IQueryProvider
{
public ContentQueryProvider()
{
Translator = new ContentQueryTranslator();
}
#region IQueryProvider Members
public abstract IQueryable<TElement> CreateQuery<TElement>(Expression expression);
//{
// return (IQueryable<TElement>)new ContentQueryable<TElement>(expression);
//}
public abstract IQueryable CreateQuery(Expression expression);
//{
// Type elementType = TypeSystem.GetElementType(expression.Type);
// try
// {
// return (IQueryable)Activator.CreateInstance(typeof(ContentQueryable<>).MakeGenericType(elementType), new object[] { this, expression });
// }
// catch (TargetInvocationException tie)
// {
// throw tie.InnerException;
// }
//}
public virtual TResult Execute<TResult>(Expression expression)
{
return (TResult)this.Execute(expression);
}
public virtual object Execute(Expression expression)
{
return ExecuteCall(expression);
}
#endregion
protected virtual object ExecuteCall(Expression expression)
{
var statementExpression = Translator.Translate(expression);
switch (statementExpression.QueryMethodType)
{
case SupportedQueryType.Count:
return Count(statementExpression);
case SupportedQueryType.First:
return First(statementExpression);
case SupportedQueryType.FirstOrDefault:
return FirstOrDefault(statementExpression);
case SupportedQueryType.Last:
return Last(statementExpression);
case SupportedQueryType.LastOrDefault:
return LastOrDefault(statementExpression);
default:
return ExecuteCore(statementExpression);
}
}
protected abstract object Count(StatementExpression exp);
protected abstract object First(StatementExpression exp);
protected abstract object FirstOrDefault(StatementExpression exp);
protected abstract object Last(StatementExpression exp);
protected abstract object LastOrDefault(StatementExpression exp);
protected abstract object ExecuteCore(StatementExpression exp);
public virtual ContentQueryTranslator Translator { get; private set; }
protected virtual ContentService ContentService
{
get
{
return HttpContext.Current.GetCmsContext().ContentService;
}
}
}
}
|