JoiningQuery.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using Ant.Core;
  2. using Ant.DbExpressions;
  3. using Ant.Infrastructure;
  4. using Ant.ORM;
  5. using Ant.ORM.Query;
  6. using Ant.Query.QueryExpressions;
  7. using Ant.Query.QueryState;
  8. using Ant.Query.Visitors;
  9. using Ant.Utility;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Linq.Expressions;
  14. using System.Text;
  15. namespace Ant.Query
  16. {
  17. class JoiningQuery<T1, T2> : IJoiningQuery<T1, T2>
  18. {
  19. AntORM _dbContext;
  20. QueryBase _rootQuery;
  21. List<JoiningQueryInfo> _joinedQueries;
  22. List<LambdaExpression> _filterPredicates;
  23. public AntORM DbContext { get { return this._dbContext; } }
  24. public QueryBase RootQuery { get { return this._rootQuery; } }
  25. public List<JoiningQueryInfo> JoinedQueries { get { return this._joinedQueries; } }
  26. public List<LambdaExpression> FilterPredicates { get { return this._filterPredicates; } }
  27. public JoiningQuery(Queryable<T1> q1, Queryable<T2> q2, JoinType joinType, Expression<Func<T1, T2, bool>> on)
  28. {
  29. this._dbContext = q1.DbContext;
  30. this._rootQuery = q1;
  31. this._joinedQueries = new List<JoiningQueryInfo>(1);
  32. JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q2, joinType, on);
  33. this._joinedQueries.Add(joiningQueryInfo);
  34. this._filterPredicates = new List<LambdaExpression>();
  35. }
  36. public JoiningQuery(JoiningQuery<T1, T2> jq, LambdaExpression filterPredicate)
  37. {
  38. this._dbContext = jq._dbContext;
  39. this._rootQuery = jq._rootQuery;
  40. this._joinedQueries = OrmUtils.Clone(jq._joinedQueries);
  41. this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate);
  42. }
  43. public IJoiningQuery<T1, T2> Where(Expression<Func<T1, T2, bool>> predicate)
  44. {
  45. return new JoiningQuery<T1, T2>(this, predicate);
  46. }
  47. public IJoiningQuery<T1, T2, T3> Join<T3>(JoinType joinType, Expression<Func<T1, T2, T3, bool>> on)
  48. {
  49. return this.Join<T3>(new Queryable<T3>(this._dbContext), joinType, on);
  50. }
  51. public IJoiningQuery<T1, T2, T3> Join<T3>(IQuery<T3> q, JoinType joinType, Expression<Func<T1, T2, T3, bool>> on)
  52. {
  53. return new JoiningQuery<T1, T2, T3>(this, (Queryable<T3>)q, joinType, on);
  54. }
  55. public IJoiningQuery<T1, T2, T3> InnerJoin<T3>(IQuery<T3> q, Expression<Func<T1, T2, T3, bool>> on)
  56. {
  57. return new JoiningQuery<T1, T2, T3>(this, (Queryable<T3>)q, JoinType.InnerJoin, on);
  58. }
  59. public IJoiningQuery<T1, T2, T3> LeftJoin<T3>(IQuery<T3> q, Expression<Func<T1, T2, T3, bool>> on)
  60. {
  61. return new JoiningQuery<T1, T2, T3>(this, (Queryable<T3>)q, JoinType.LeftJoin, on);
  62. }
  63. public IJoiningQuery<T1, T2, T3> RightJoin<T3>(IQuery<T3> q, Expression<Func<T1, T2, T3, bool>> on)
  64. {
  65. return new JoiningQuery<T1, T2, T3>(this, (Queryable<T3>)q, JoinType.RightJoin, on);
  66. }
  67. public IJoiningQuery<T1, T2, T3> FullJoin<T3>(IQuery<T3> q, Expression<Func<T1, T2, T3, bool>> on)
  68. {
  69. return new JoiningQuery<T1, T2, T3>(this, (Queryable<T3>)q, JoinType.FullJoin, on);
  70. }
  71. public IJoiningQuery<T1, T2, T3> InnerJoin<T3>(Expression<Func<T1, T2, T3, bool>> on)
  72. {
  73. return this.InnerJoin<T3>(new Queryable<T3>(this._dbContext), on);
  74. }
  75. public IJoiningQuery<T1, T2, T3> LeftJoin<T3>(Expression<Func<T1, T2, T3, bool>> on)
  76. {
  77. return this.LeftJoin<T3>(new Queryable<T3>(this._dbContext), on);
  78. }
  79. public IJoiningQuery<T1, T2, T3> RightJoin<T3>(Expression<Func<T1, T2, T3, bool>> on)
  80. {
  81. return this.RightJoin<T3>(new Queryable<T3>(this._dbContext), on);
  82. }
  83. public IJoiningQuery<T1, T2, T3> FullJoin<T3>(Expression<Func<T1, T2, T3, bool>> on)
  84. {
  85. return this.FullJoin<T3>(new Queryable<T3>(this._dbContext), on);
  86. }
  87. public IQuery<TResult> Select<TResult>(Expression<Func<T1, T2, TResult>> selector)
  88. {
  89. JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector);
  90. return new Queryable<TResult>(this.DbContext, e, this._rootQuery.TrackEntity);
  91. }
  92. }
  93. class JoiningQuery<T1, T2, T3> : IJoiningQuery<T1, T2, T3>
  94. {
  95. AntORM _dbContext;
  96. QueryBase _rootQuery;
  97. List<JoiningQueryInfo> _joinedQueries;
  98. List<LambdaExpression> _filterPredicates;
  99. public AntORM DbContext { get { return this._dbContext; } }
  100. public QueryBase RootQuery { get { return this._rootQuery; } }
  101. public List<JoiningQueryInfo> JoinedQueries { get { return this._joinedQueries; } }
  102. public List<LambdaExpression> FilterPredicates { get { return this._filterPredicates; } }
  103. public JoiningQuery(JoiningQuery<T1, T2> joiningQuery, Queryable<T3> q, JoinType joinType, Expression<Func<T1, T2, T3, bool>> on)
  104. {
  105. //this._dbContext = joiningQuery.DbContext;
  106. //this._rootQuery = joiningQuery.RootQuery;
  107. //this._joinedQueries = new List<JoiningQueryInfo>(joiningQuery.JoinedQueries.Count);
  108. //this._joinedQueries.AddRange(joiningQuery.JoinedQueries);
  109. //JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q, joinType, on);
  110. //this._joinedQueries.Add(joiningQueryInfo);
  111. this._dbContext = joiningQuery.DbContext;
  112. this._rootQuery = joiningQuery.RootQuery;
  113. this._joinedQueries = OrmUtils.CloneAndAppendOne(joiningQuery.JoinedQueries, new JoiningQueryInfo(q, joinType, on));
  114. this._filterPredicates = OrmUtils.Clone(joiningQuery.FilterPredicates);
  115. }
  116. public JoiningQuery(JoiningQuery<T1, T2, T3> jq, LambdaExpression filterPredicate)
  117. {
  118. this._dbContext = jq._dbContext;
  119. this._rootQuery = jq._rootQuery;
  120. this._joinedQueries = OrmUtils.Clone(jq._joinedQueries);
  121. this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate);
  122. }
  123. public IJoiningQuery<T1, T2, T3> Where(Expression<Func<T1, T2, T3, bool>> predicate)
  124. {
  125. return new JoiningQuery<T1, T2, T3>(this, predicate);
  126. }
  127. public IJoiningQuery<T1, T2, T3, T4> Join<T4>(JoinType joinType, Expression<Func<T1, T2, T3, T4, bool>> on)
  128. {
  129. return this.Join<T4>(new Queryable<T4>(this._dbContext), joinType, on);
  130. }
  131. public IJoiningQuery<T1, T2, T3, T4> Join<T4>(IQuery<T4> q, JoinType joinType, Expression<Func<T1, T2, T3, T4, bool>> on)
  132. {
  133. return new JoiningQuery<T1, T2, T3, T4>(this, (Queryable<T4>)q, joinType, on);
  134. }
  135. public IJoiningQuery<T1, T2, T3, T4> InnerJoin<T4>(IQuery<T4> q, Expression<Func<T1, T2, T3, T4, bool>> on)
  136. {
  137. return new JoiningQuery<T1, T2, T3, T4>(this, (Queryable<T4>)q, JoinType.InnerJoin, on);
  138. }
  139. public IJoiningQuery<T1, T2, T3, T4> LeftJoin<T4>(IQuery<T4> q, Expression<Func<T1, T2, T3, T4, bool>> on)
  140. {
  141. return new JoiningQuery<T1, T2, T3, T4>(this, (Queryable<T4>)q, JoinType.LeftJoin, on);
  142. }
  143. public IJoiningQuery<T1, T2, T3, T4> RightJoin<T4>(IQuery<T4> q, Expression<Func<T1, T2, T3, T4, bool>> on)
  144. {
  145. return new JoiningQuery<T1, T2, T3, T4>(this, (Queryable<T4>)q, JoinType.RightJoin, on);
  146. }
  147. public IJoiningQuery<T1, T2, T3, T4> FullJoin<T4>(IQuery<T4> q, Expression<Func<T1, T2, T3, T4, bool>> on)
  148. {
  149. return new JoiningQuery<T1, T2, T3, T4>(this, (Queryable<T4>)q, JoinType.FullJoin, on);
  150. }
  151. public IJoiningQuery<T1, T2, T3, T4> InnerJoin<T4>(Expression<Func<T1, T2, T3, T4, bool>> on)
  152. {
  153. return this.InnerJoin<T4>(new Queryable<T4>(this._dbContext), on);
  154. }
  155. public IJoiningQuery<T1, T2, T3, T4> LeftJoin<T4>(Expression<Func<T1, T2, T3, T4, bool>> on)
  156. {
  157. return this.LeftJoin<T4>(new Queryable<T4>(this._dbContext), on);
  158. }
  159. public IJoiningQuery<T1, T2, T3, T4> RightJoin<T4>(Expression<Func<T1, T2, T3, T4, bool>> on)
  160. {
  161. return this.RightJoin<T4>(new Queryable<T4>(this._dbContext), on);
  162. }
  163. public IJoiningQuery<T1, T2, T3, T4> FullJoin<T4>(Expression<Func<T1, T2, T3, T4, bool>> on)
  164. {
  165. return this.FullJoin<T4>(new Queryable<T4>(this._dbContext), on);
  166. }
  167. public IQuery<TResult> Select<TResult>(Expression<Func<T1, T2, T3, TResult>> selector)
  168. {
  169. JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector);
  170. return new Queryable<TResult>(this.DbContext, e, this._rootQuery.TrackEntity);
  171. }
  172. }
  173. class JoiningQuery<T1, T2, T3, T4> : IJoiningQuery<T1, T2, T3, T4>
  174. {
  175. AntORM _dbContext;
  176. QueryBase _rootQuery;
  177. List<JoiningQueryInfo> _joinedQueries;
  178. List<LambdaExpression> _filterPredicates;
  179. public AntORM DbContext { get { return this._dbContext; } }
  180. public QueryBase RootQuery { get { return this._rootQuery; } }
  181. public List<JoiningQueryInfo> JoinedQueries { get { return this._joinedQueries; } }
  182. public List<LambdaExpression> FilterPredicates { get { return this._filterPredicates; } }
  183. //public JoiningQuery(JoiningQuery<T1, T2, T3> joiningQuery, Queryable<T4> q, JoinType joinType, Expression<Func<T1, T2, T3, T4, bool>> on)
  184. //{
  185. // this._dbContext = joiningQuery.DbContext;
  186. // this._rootQuery = joiningQuery.RootQuery;
  187. // this._joinedQueries = new List<JoiningQueryInfo>(joiningQuery.JoinedQueries.Count);
  188. // this._joinedQueries.AddRange(joiningQuery.JoinedQueries);
  189. // JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q, joinType, on);
  190. // this._joinedQueries.Add(joiningQueryInfo);
  191. //}
  192. public JoiningQuery(JoiningQuery<T1, T2, T3> joiningQuery, Queryable<T4> q, JoinType joinType, Expression<Func<T1, T2, T3, T4, bool>> on)
  193. {
  194. this._dbContext = joiningQuery.DbContext;
  195. this._rootQuery = joiningQuery.RootQuery;
  196. this._joinedQueries = OrmUtils.CloneAndAppendOne(joiningQuery.JoinedQueries, new JoiningQueryInfo(q, joinType, on));
  197. this._filterPredicates = OrmUtils.Clone(joiningQuery.FilterPredicates);
  198. }
  199. public JoiningQuery(JoiningQuery<T1, T2, T3, T4> jq, LambdaExpression filterPredicate)
  200. {
  201. this._dbContext = jq._dbContext;
  202. this._rootQuery = jq._rootQuery;
  203. this._joinedQueries = OrmUtils.Clone(jq._joinedQueries);
  204. this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate);
  205. }
  206. public IJoiningQuery<T1, T2, T3, T4> Where(Expression<Func<T1, T2, T3, T4, bool>> predicate)
  207. {
  208. return new JoiningQuery<T1, T2, T3, T4>(this, predicate);
  209. }
  210. public IJoiningQuery<T1, T2, T3, T4, T5> Join<T5>(JoinType joinType, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  211. {
  212. return this.Join<T5>(new Queryable<T5>(this._dbContext), joinType, on);
  213. }
  214. public IJoiningQuery<T1, T2, T3, T4, T5> Join<T5>(IQuery<T5> q, JoinType joinType, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  215. {
  216. return new JoiningQuery<T1, T2, T3, T4, T5>(this, (Queryable<T5>)q, joinType, on);
  217. }
  218. public IJoiningQuery<T1, T2, T3, T4, T5> InnerJoin<T5>(IQuery<T5> q, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  219. {
  220. return new JoiningQuery<T1, T2, T3, T4, T5>(this, (Queryable<T5>)q, JoinType.InnerJoin, on);
  221. }
  222. public IJoiningQuery<T1, T2, T3, T4, T5> LeftJoin<T5>(IQuery<T5> q, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  223. {
  224. return new JoiningQuery<T1, T2, T3, T4, T5>(this, (Queryable<T5>)q, JoinType.LeftJoin, on);
  225. }
  226. public IJoiningQuery<T1, T2, T3, T4, T5> RightJoin<T5>(IQuery<T5> q, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  227. {
  228. return new JoiningQuery<T1, T2, T3, T4, T5>(this, (Queryable<T5>)q, JoinType.RightJoin, on);
  229. }
  230. public IJoiningQuery<T1, T2, T3, T4, T5> FullJoin<T5>(IQuery<T5> q, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  231. {
  232. return new JoiningQuery<T1, T2, T3, T4, T5>(this, (Queryable<T5>)q, JoinType.FullJoin, on);
  233. }
  234. public IJoiningQuery<T1, T2, T3, T4, T5> InnerJoin<T5>(Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  235. {
  236. return this.InnerJoin<T5>(new Queryable<T5>(this._dbContext), on);
  237. }
  238. public IJoiningQuery<T1, T2, T3, T4, T5> LeftJoin<T5>(Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  239. {
  240. return this.LeftJoin<T5>(new Queryable<T5>(this._dbContext), on);
  241. }
  242. public IJoiningQuery<T1, T2, T3, T4, T5> RightJoin<T5>(Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  243. {
  244. return this.RightJoin<T5>(new Queryable<T5>(this._dbContext), on);
  245. }
  246. public IJoiningQuery<T1, T2, T3, T4, T5> FullJoin<T5>(Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  247. {
  248. return this.FullJoin<T5>(new Queryable<T5>(this._dbContext), on);
  249. }
  250. public IQuery<TResult> Select<TResult>(Expression<Func<T1, T2, T3, T4, TResult>> selector)
  251. {
  252. JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector);
  253. return new Queryable<TResult>(this.DbContext, e, this._rootQuery.TrackEntity);
  254. }
  255. }
  256. class JoiningQuery<T1, T2, T3, T4, T5> : IJoiningQuery<T1, T2, T3, T4, T5>
  257. {
  258. AntORM _dbContext;
  259. QueryBase _rootQuery;
  260. List<JoiningQueryInfo> _joinedQueries;
  261. List<LambdaExpression> _filterPredicates;
  262. public AntORM DbContext { get { return this._dbContext; } }
  263. public QueryBase RootQuery { get { return this._rootQuery; } }
  264. public List<JoiningQueryInfo> JoinedQueries { get { return this._joinedQueries; } }
  265. public List<LambdaExpression> FilterPredicates { get { return this._filterPredicates; } }
  266. //老版
  267. //public JoiningQuery(JoiningQuery<T1, T2, T3, T4> joiningQuery, Queryable<T5> q, JoinType joinType, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  268. //{
  269. // this._dbContext = joiningQuery.DbContext;
  270. // this._rootQuery = joiningQuery.RootQuery;
  271. // this._joinedQueries = new List<JoiningQueryInfo>(joiningQuery.JoinedQueries.Count);
  272. // this._joinedQueries.AddRange(joiningQuery.JoinedQueries);
  273. // JoiningQueryInfo joiningQueryInfo = new JoiningQueryInfo(q, joinType, on);
  274. // this._joinedQueries.Add(joiningQueryInfo);
  275. //}
  276. //public IQuery<TResult> Select<TResult>(Expression<Func<T1, T2, T3, T4, T5, TResult>> selector)
  277. //{
  278. // JoinQueryExpression e = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector);
  279. // return new Queryable<TResult>(this.DbContext, e, this._rootQuery.TrackEntity);
  280. //}
  281. public JoiningQuery(JoiningQuery<T1, T2, T3, T4> joiningQuery, Queryable<T5> q, JoinType joinType, Expression<Func<T1, T2, T3, T4, T5, bool>> on)
  282. {
  283. this._dbContext = joiningQuery.DbContext;
  284. this._rootQuery = joiningQuery.RootQuery;
  285. this._joinedQueries = OrmUtils.CloneAndAppendOne(joiningQuery.JoinedQueries, new JoiningQueryInfo(q, joinType, on));
  286. this._filterPredicates = OrmUtils.Clone(joiningQuery.FilterPredicates);
  287. }
  288. public JoiningQuery(JoiningQuery<T1, T2, T3, T4, T5> jq, LambdaExpression filterPredicate)
  289. {
  290. this._dbContext = jq._dbContext;
  291. this._rootQuery = jq._rootQuery;
  292. this._joinedQueries = OrmUtils.Clone(jq._joinedQueries);
  293. this._filterPredicates = OrmUtils.CloneAndAppendOne(jq._filterPredicates, filterPredicate);
  294. }
  295. public IJoiningQuery<T1, T2, T3, T4, T5> Where(Expression<Func<T1, T2, T3, T4, T5, bool>> predicate)
  296. {
  297. return new JoiningQuery<T1, T2, T3, T4, T5>(this, predicate);
  298. }
  299. public IQuery<TResult> Select<TResult>(Expression<Func<T1, T2, T3, T4, T5, TResult>> selector)
  300. {
  301. if (this._filterPredicates.Count == 0)
  302. {
  303. JoinQueryExpression e1 = new JoinQueryExpression(typeof(TResult), this._rootQuery.QueryExpression, this._joinedQueries, selector);
  304. return new Queryable<TResult>(this.DbContext, e1, this._rootQuery.TrackEntity);
  305. }
  306. Type joinResultType = typeof(JoinResult<T1, T2, T3, T4, T5>);
  307. Expression<Func<T1, T2, T3, T4, T5, JoinResult<T1, T2, T3, T4, T5>>> joinResultSelector = (t1, t2, t3, t4, t5) => new JoinResult<T1, T2, T3, T4, T5>() { Item1 = t1, Item2 = t2, Item3 = t3, Item4 = t4, Item5 = t5 };
  308. JoinQueryExpression e = new JoinQueryExpression(joinResultType, this._rootQuery.QueryExpression, this._joinedQueries, joinResultSelector);
  309. IQuery<JoinResult<T1, T2, T3, T4, T5>> q = new Queryable<JoinResult<T1, T2, T3, T4, T5>>(this.DbContext, e, this._rootQuery.TrackEntity);
  310. ParameterExpression parameter = Expression.Parameter(joinResultType, "a");
  311. Expression[] expressionSubstitutes = QueryHelper.MakeExpressionSubstitutes(joinResultType, parameter);
  312. Expression<Func<JoinResult<T1, T2, T3, T4, T5>, bool>> predicate = QueryHelper.ComposePredicate<Func<JoinResult<T1, T2, T3, T4, T5>, bool>>(this._filterPredicates, expressionSubstitutes, parameter);
  313. var q1 = q.Where(predicate);
  314. Expression<Func<JoinResult<T1, T2, T3, T4, T5>, TResult>> selector2 = JoinQueryParameterExpressionReplacer.Replace(selector, expressionSubstitutes, parameter) as Expression<Func<JoinResult<T1, T2, T3, T4, T5>, TResult>>;
  315. var ret = q1.Select(selector2);
  316. return ret;
  317. }
  318. }
  319. }