using Ant.Core; using Ant.DbExpressions; using Ant.Infrastructure; using Ant.ORM; using Ant.ORM.Query; using Ant.Query.QueryExpressions; using Ant.Query.QueryState; using Ant.Query.Visitors; using Ant.Utility; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace Ant.Query { class JoiningQuery : IJoiningQuery { AntORM _dbContext; QueryBase _rootQuery; List _joinedQueries; List _filterPredicates; public AntORM DbContext { get { return this._dbContext; } } public QueryBase RootQuery { get { return this._rootQuery; } } public List JoinedQueries { get { return this._joinedQueries; } } public List FilterPredicates { get { return this._filterPredicates; } } public JoiningQuery(Queryable q1, Queryable q2, JoinType joinType, Expression> on) { this._dbContext = q1.DbContext; this._rootQuery = q1; this._joinedQueries = new List(1); JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q2, joinType, on); this._joinedQueries.Add(joiningQueryInfo); this._filterPredicates = new List(); } public JoiningQuery(JoiningQuery jq, LambdaExpression filterPredicate) { this._dbContext = jq._dbContext; this._rootQuery = jq._rootQuery; this._joinedQueries = OrmUtils.Clone(jq._joinedQueries); this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate); } public IJoiningQuery Where(Expression> predicate) { return new JoiningQuery(this, predicate); } public IJoiningQuery Join(JoinType joinType, Expression> on) { return this.Join(new Queryable(this._dbContext), joinType, on); } public IJoiningQuery Join(IQuery q, JoinType joinType, Expression> on) { return new JoiningQuery(this, (Queryable)q, joinType, on); } public IJoiningQuery InnerJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.InnerJoin, on); } public IJoiningQuery LeftJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.LeftJoin, on); } public IJoiningQuery RightJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.RightJoin, on); } public IJoiningQuery FullJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.FullJoin, on); } public IJoiningQuery InnerJoin(Expression> on) { return this.InnerJoin(new Queryable(this._dbContext), on); } public IJoiningQuery LeftJoin(Expression> on) { return this.LeftJoin(new Queryable(this._dbContext), on); } public IJoiningQuery RightJoin(Expression> on) { return this.RightJoin(new Queryable(this._dbContext), on); } public IJoiningQuery FullJoin(Expression> on) { return this.FullJoin(new Queryable(this._dbContext), on); } public IQuery Select(Expression> selector) { JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector); return new Queryable(this.DbContext, e, this._rootQuery.TrackEntity); } } class JoiningQuery : IJoiningQuery { AntORM _dbContext; QueryBase _rootQuery; List _joinedQueries; List _filterPredicates; public AntORM DbContext { get { return this._dbContext; } } public QueryBase RootQuery { get { return this._rootQuery; } } public List JoinedQueries { get { return this._joinedQueries; } } public List FilterPredicates { get { return this._filterPredicates; } } public JoiningQuery(JoiningQuery joiningQuery, Queryable q, JoinType joinType, Expression> on) { //this._dbContext = joiningQuery.DbContext; //this._rootQuery = joiningQuery.RootQuery; //this._joinedQueries = new List(joiningQuery.JoinedQueries.Count); //this._joinedQueries.AddRange(joiningQuery.JoinedQueries); //JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q, joinType, on); //this._joinedQueries.Add(joiningQueryInfo); this._dbContext = joiningQuery.DbContext; this._rootQuery = joiningQuery.RootQuery; this._joinedQueries = OrmUtils.CloneAndAppendOne(joiningQuery.JoinedQueries, new JoiningQueryInfo(q, joinType, on)); this._filterPredicates = OrmUtils.Clone(joiningQuery.FilterPredicates); } public JoiningQuery(JoiningQuery jq, LambdaExpression filterPredicate) { this._dbContext = jq._dbContext; this._rootQuery = jq._rootQuery; this._joinedQueries = OrmUtils.Clone(jq._joinedQueries); this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate); } public IJoiningQuery Where(Expression> predicate) { return new JoiningQuery(this, predicate); } public IJoiningQuery Join(JoinType joinType, Expression> on) { return this.Join(new Queryable(this._dbContext), joinType, on); } public IJoiningQuery Join(IQuery q, JoinType joinType, Expression> on) { return new JoiningQuery(this, (Queryable)q, joinType, on); } public IJoiningQuery InnerJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.InnerJoin, on); } public IJoiningQuery LeftJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.LeftJoin, on); } public IJoiningQuery RightJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.RightJoin, on); } public IJoiningQuery FullJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.FullJoin, on); } public IJoiningQuery InnerJoin(Expression> on) { return this.InnerJoin(new Queryable(this._dbContext), on); } public IJoiningQuery LeftJoin(Expression> on) { return this.LeftJoin(new Queryable(this._dbContext), on); } public IJoiningQuery RightJoin(Expression> on) { return this.RightJoin(new Queryable(this._dbContext), on); } public IJoiningQuery FullJoin(Expression> on) { return this.FullJoin(new Queryable(this._dbContext), on); } public IQuery Select(Expression> selector) { JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector); return new Queryable(this.DbContext, e, this._rootQuery.TrackEntity); } } class JoiningQuery : IJoiningQuery { AntORM _dbContext; QueryBase _rootQuery; List _joinedQueries; List _filterPredicates; public AntORM DbContext { get { return this._dbContext; } } public QueryBase RootQuery { get { return this._rootQuery; } } public List JoinedQueries { get { return this._joinedQueries; } } public List FilterPredicates { get { return this._filterPredicates; } } //public JoiningQuery(JoiningQuery joiningQuery, Queryable q, JoinType joinType, Expression> on) //{ // this._dbContext = joiningQuery.DbContext; // this._rootQuery = joiningQuery.RootQuery; // this._joinedQueries = new List(joiningQuery.JoinedQueries.Count); // this._joinedQueries.AddRange(joiningQuery.JoinedQueries); // JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q, joinType, on); // this._joinedQueries.Add(joiningQueryInfo); //} public JoiningQuery(JoiningQuery joiningQuery, Queryable q, JoinType joinType, Expression> on) { this._dbContext = joiningQuery.DbContext; this._rootQuery = joiningQuery.RootQuery; this._joinedQueries = OrmUtils.CloneAndAppendOne(joiningQuery.JoinedQueries, new JoiningQueryInfo(q, joinType, on)); this._filterPredicates = OrmUtils.Clone(joiningQuery.FilterPredicates); } public JoiningQuery(JoiningQuery jq, LambdaExpression filterPredicate) { this._dbContext = jq._dbContext; this._rootQuery = jq._rootQuery; this._joinedQueries = OrmUtils.Clone(jq._joinedQueries); this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate); } public IJoiningQuery Where(Expression> predicate) { return new JoiningQuery(this, predicate); } public IJoiningQuery Join(JoinType joinType, Expression> on) { return this.Join(new Queryable(this._dbContext), joinType, on); } public IJoiningQuery Join(IQuery q, JoinType joinType, Expression> on) { return new JoiningQuery(this, (Queryable)q, joinType, on); } public IJoiningQuery InnerJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.InnerJoin, on); } public IJoiningQuery LeftJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.LeftJoin, on); } public IJoiningQuery RightJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.RightJoin, on); } public IJoiningQuery FullJoin(IQuery q, Expression> on) { return new JoiningQuery(this, (Queryable)q, JoinType.FullJoin, on); } public IJoiningQuery InnerJoin(Expression> on) { return this.InnerJoin(new Queryable(this._dbContext), on); } public IJoiningQuery LeftJoin(Expression> on) { return this.LeftJoin(new Queryable(this._dbContext), on); } public IJoiningQuery RightJoin(Expression> on) { return this.RightJoin(new Queryable(this._dbContext), on); } public IJoiningQuery FullJoin(Expression> on) { return this.FullJoin(new Queryable(this._dbContext), on); } public IQuery Select(Expression> selector) { JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector); return new Queryable(this.DbContext, e, this._rootQuery.TrackEntity); } } class JoiningQuery : IJoiningQuery { AntORM _dbContext; QueryBase _rootQuery; List _joinedQueries; List _filterPredicates; public AntORM DbContext { get { return this._dbContext; } } public QueryBase RootQuery { get { return this._rootQuery; } } public List JoinedQueries { get { return this._joinedQueries; } } public List FilterPredicates { get { return this._filterPredicates; } } //老版 //public JoiningQuery(JoiningQuery joiningQuery, Queryable q, JoinType joinType, Expression> on) //{ // this._dbContext = joiningQuery.DbContext; // this._rootQuery = joiningQuery.RootQuery; // this._joinedQueries = new List(joiningQuery.JoinedQueries.Count); // this._joinedQueries.AddRange(joiningQuery.JoinedQueries); // JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q, joinType, on); // this._joinedQueries.Add(joiningQueryInfo); //} //public IQuery Select(Expression> selector) //{ // JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector); // return new Queryable(this.DbContext, e, this._rootQuery.TrackEntity); //} public JoiningQuery(JoiningQuery joiningQuery, Queryable q, JoinType joinType, Expression> on) { this._dbContext = joiningQuery.DbContext; this._rootQuery = joiningQuery.RootQuery; this._joinedQueries = OrmUtils.CloneAndAppendOne(joiningQuery.JoinedQueries, new JoiningQueryInfo(q, joinType, on)); this._filterPredicates = OrmUtils.Clone(joiningQuery.FilterPredicates); } public JoiningQuery(JoiningQuery jq, LambdaExpression filterPredicate) { this._dbContext = jq._dbContext; this._rootQuery = jq._rootQuery; this._joinedQueries = OrmUtils.Clone(jq._joinedQueries); this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate); } public IJoiningQuery Where(Expression> predicate) { return new JoiningQuery(this, predicate); } public IQuery Select(Expression> selector) { if (this._filterPredicates.Count == 0) { JoinQueryExpression e1 = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector); return new Queryable(this.DbContext, e1, this._rootQuery.TrackEntity); } Type joinResultType = typeof(JoinResult); Expression>> joinResultSelector = (t1, t2, t3, t4, t5) => new JoinResult() { Item1 = t1, Item2 = t2, Item3 = t3, Item4 = t4, Item5 = t5 }; JoinQueryExpression e = new JoinQueryExpression(joinResultType, this._rootQuery.QueryExpression, this._joinedQueries, joinResultSelector); IQuery> q = new Queryable>(this.DbContext, e, this._rootQuery.TrackEntity); ParameterExpression parameter = Expression.Parameter(joinResultType, "a"); Expression[] expressionSubstitutes = QueryHelper.MakeExpressionSubstitutes(joinResultType, parameter); Expression, bool>> predicate = QueryHelper.ComposePredicate, bool>>(this._filterPredicates, expressionSubstitutes, parameter); var q1 = q.Where(predicate); Expression, TResult>> selector2 = JoinQueryParameterExpressionReplacer.Replace(selector, expressionSubstitutes, parameter) as Expression, TResult>>; var ret = q1.Select(selector2); return ret; } } }