PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/YUI/YUI.Core/Dao/Query/FromSection.cs

http://yuicore.googlecode.com/
C# | 789 lines | 491 code | 123 blank | 175 comment | 36 complexity | b5616ba5b672325e6cacafb6c416c656 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.Data;
  7. using System.Data.Common;
  8. using YUI.Core.Design;
  9. using YUI.Core.Dao.Session;
  10. using YUI.Core.Common;
  11. using YUI.Core.Dao.Expression;
  12. using YUI.Core.Dao.Source;
  13. namespace YUI.Core.Dao.Query
  14. {
  15. public class FromSection<T> : IQuerySection<T>
  16. where T : Entity
  17. {
  18. private Entity fromEntity;
  19. private Table fromTable;
  20. private QuerySection<T> query;
  21. private List<Entity> entityList = new List<Entity>();
  22. internal List<Entity> EntityList
  23. {
  24. get { return entityList; }
  25. set { entityList = value; }
  26. }
  27. #region ???FromSection
  28. internal FromSection()
  29. {
  30. this.fromEntity = DataUtils.CreateInstance<T>();
  31. this.fromTable = this.fromEntity.GetTable();
  32. }
  33. internal FromSection(DbProvider dbProvider, DbTrans dbTran, Table table)
  34. : this()
  35. {
  36. this.entityList.Add(this.fromEntity);
  37. //????????????
  38. this.tableName = table == null ? fromTable.Name : table.Name;
  39. fromTable.AliasName = this.tableName;
  40. Field pagingField = fromEntity.PagingField;
  41. this.query = new QuerySection<T>(this, dbProvider, dbTran, pagingField);
  42. }
  43. internal FromSection(DbProvider dbProvider, DbTrans dbTran, string aliasName)
  44. : this()
  45. {
  46. fromTable.AliasName = aliasName;
  47. this.entityList.Add(this.fromEntity);
  48. this.tableName = fromTable.Name;
  49. Field pagingField = fromEntity.PagingField;
  50. if (aliasName != null)
  51. {
  52. if ((IField)pagingField != null)
  53. {
  54. pagingField = pagingField.At(aliasName);
  55. }
  56. this.tableName += " {0}" + aliasName + "{1}";
  57. }
  58. this.query = new QuerySection<T>(this, dbProvider, dbTran, pagingField);
  59. }
  60. internal FromSection(string tableName, string relation, IList<Entity> list)
  61. {
  62. this.tableName = tableName;
  63. this.relation = relation;
  64. this.entityList.AddRange(list);
  65. }
  66. internal FromSection(string tableName, string aliasName)
  67. : this()
  68. {
  69. fromTable.AliasName = aliasName;
  70. this.entityList.Add(this.fromEntity);
  71. this.tableName = tableName;
  72. if (aliasName != null)
  73. {
  74. this.tableName += " {0}" + aliasName + "{1}";
  75. }
  76. }
  77. internal FromSection(Table table)
  78. : this()
  79. {
  80. this.tableName = table == null ? fromTable.Name : table.Name;
  81. fromTable.AliasName = this.tableName;
  82. this.query = new QuerySection<T>(this);
  83. }
  84. #endregion
  85. #region ??IQuerySection
  86. #region ??IDataQuery
  87. /// <summary>
  88. /// ?????????Page?
  89. /// </summary>
  90. /// <typeparam name="TResult"></typeparam>
  91. /// <param name="pageSize"></param>
  92. /// <returns></returns>
  93. public PageSection<TEntity> GetPage<TEntity>(int pageSize)
  94. where TEntity : Entity
  95. {
  96. return query.GetPage<TEntity>(pageSize);
  97. }
  98. /// <summary>
  99. /// ??????
  100. /// </summary>
  101. /// <typeparam name="TResult"></typeparam>
  102. /// <returns></returns>
  103. public TEntity ToSingle<TEntity>()
  104. where TEntity : Entity
  105. {
  106. return query.ToSingle<TEntity>();
  107. }
  108. #endregion
  109. #region ?????
  110. /// <summary>
  111. /// ???????
  112. /// </summary>
  113. /// <returns></returns>
  114. public QuerySection<T> SubQuery()
  115. {
  116. return query.SubQuery();
  117. }
  118. /// <summary>
  119. /// ???????
  120. /// </summary>
  121. /// <returns></returns>
  122. public QuerySection<T> SubQuery(string aliasName)
  123. {
  124. return query.SubQuery(aliasName);
  125. }
  126. /// <summary>
  127. /// ???????
  128. /// </summary>
  129. /// <returns></returns>
  130. public QuerySection<TSub> SubQuery<TSub>()
  131. where TSub : Entity
  132. {
  133. return query.SubQuery<TSub>();
  134. }
  135. /// <summary>
  136. /// ???????????
  137. /// </summary>
  138. /// <param name="aliasName"></param>
  139. /// <returns></returns>
  140. public QuerySection<TSub> SubQuery<TSub>(string aliasName)
  141. where TSub : Entity
  142. {
  143. return query.SubQuery<TSub>(aliasName);
  144. }
  145. #endregion
  146. #region ??????
  147. /// <summary>
  148. /// ??GroupBy??
  149. /// </summary>
  150. /// <param name="groupBy"></param>
  151. /// <returns></returns>
  152. public QuerySection<T> GroupBy(GroupByClip groupBy)
  153. {
  154. return query.GroupBy(groupBy);
  155. }
  156. /// <summary>
  157. /// ??OrderBy??
  158. /// </summary>
  159. /// <param name="orderBy"></param>
  160. /// <returns></returns>
  161. public QuerySection<T> OrderBy(OrderByClip orderBy)
  162. {
  163. return query.OrderBy(orderBy);
  164. }
  165. /// <summary>
  166. /// ??????
  167. /// </summary>
  168. /// <param name="fields"></param>
  169. /// <returns></returns>
  170. public QuerySection<T> Select(params Field[] fields)
  171. {
  172. return query.Select(fields);
  173. }
  174. /// <summary>
  175. /// ????????????????????????
  176. /// </summary>
  177. /// <param name="field"></param>
  178. /// <returns></returns>
  179. public QuerySection<T> Select(ExcludeField field)
  180. {
  181. return query.Select(field);
  182. }
  183. /// <summary>
  184. /// ?????????
  185. /// </summary>
  186. /// <param name="where"></param>
  187. /// <returns></returns>
  188. public QuerySection<T> Where(WhereClip where)
  189. {
  190. return query.Where(where);
  191. }
  192. /// <summary>
  193. /// ??Union??
  194. /// </summary>
  195. /// <param name="query"></param>
  196. /// <returns></returns>
  197. public QuerySection<T> Union(QuerySection<T> uquery)
  198. {
  199. return query.Union(uquery);
  200. }
  201. /// <summary>
  202. /// ??Union??
  203. /// </summary>
  204. /// <param name="query"></param>
  205. /// <returns></returns>
  206. public QuerySection<T> UnionAll(QuerySection<T> uquery)
  207. {
  208. return query.UnionAll(uquery);
  209. }
  210. /// <summary>
  211. /// ??Having??
  212. /// </summary>
  213. /// <param name="where"></param>
  214. /// <returns></returns>
  215. public QuerySection<T> Having(WhereClip where)
  216. {
  217. return query.Having(where);
  218. }
  219. /// <summary>
  220. /// ??????
  221. /// </summary>
  222. /// <param name="pagingField"></param>
  223. /// <returns></returns>
  224. public QuerySection<T> SetPagingField(Field pagingField)
  225. {
  226. return query.SetPagingField(pagingField);
  227. }
  228. /// <summary>
  229. /// ???n?
  230. /// </summary>
  231. /// <param name="topSize"></param>
  232. /// <returns></returns>
  233. public TopSection<T> GetTop(int topSize)
  234. {
  235. return query.GetTop(topSize);
  236. }
  237. /// <summary>
  238. /// ??Distinct??
  239. /// </summary>
  240. /// <returns></returns>
  241. public QuerySection<T> Distinct()
  242. {
  243. return query.Distinct();
  244. }
  245. #endregion
  246. #region ????
  247. /// <summary>
  248. /// ?????????Page?
  249. /// </summary>
  250. /// <param name="pageSize"></param>
  251. /// <returns></returns>
  252. public PageSection<T> GetPage(int pageSize)
  253. {
  254. return query.GetPage(pageSize);
  255. }
  256. /// <summary>
  257. /// ??????
  258. /// </summary>
  259. /// <returns></returns>
  260. public T ToSingle()
  261. {
  262. return query.ToSingle();
  263. }
  264. #region ??object
  265. /// <summary>
  266. /// ????Object??
  267. /// </summary>
  268. /// <typeparam name="TResult"></typeparam>
  269. /// <param name="startIndex"></param>
  270. /// <param name="endIndex"></param>
  271. /// <returns></returns>
  272. public IArrayList<object> ToListResult(int startIndex, int endIndex)
  273. {
  274. return query.ToListResult(startIndex, endIndex);
  275. }
  276. /// <summary>
  277. /// ????Object??
  278. /// </summary>
  279. /// <typeparam name="TResult"></typeparam>
  280. /// <returns></returns>
  281. public IArrayList<object> ToListResult()
  282. {
  283. return query.ToListResult();
  284. }
  285. /// <summary>
  286. /// ????Object??
  287. /// </summary>
  288. /// <typeparam name="TResult"></typeparam>
  289. /// <param name="startIndex"></param>
  290. /// <param name="endIndex"></param>
  291. /// <returns></returns>
  292. public IArrayList<TResult> ToListResult<TResult>(int startIndex, int endIndex)
  293. {
  294. return query.ToListResult<TResult>(startIndex, endIndex);
  295. }
  296. /// <summary>
  297. /// ????Object??
  298. /// </summary>
  299. /// <typeparam name="TResult"></typeparam>
  300. /// <returns></returns>
  301. public IArrayList<TResult> ToListResult<TResult>()
  302. {
  303. return query.ToListResult<TResult>();
  304. }
  305. #endregion
  306. /// <summary>
  307. /// ????DbReader
  308. /// </summary>
  309. /// <param name="startIndex"></param>
  310. /// <param name="endIndex"></param>
  311. /// <returns></returns>
  312. public ISourceReader ToReader(int startIndex, int endIndex)
  313. {
  314. return query.ToReader(startIndex, endIndex);
  315. }
  316. /// <summary>
  317. /// ????DbReader
  318. /// </summary>
  319. /// <returns></returns>
  320. public ISourceReader ToReader()
  321. {
  322. return query.ToReader();
  323. }
  324. #region ????
  325. /// <summary>
  326. /// ??IArrayList
  327. /// </summary>
  328. /// <returns></returns>
  329. public ISourceList<T> ToList()
  330. {
  331. return query.ToList();
  332. }
  333. /// <summary>
  334. /// ??IArrayList
  335. /// </summary>
  336. /// <returns></returns>
  337. public ISourceList<T> ToList(int startIndex, int endIndex)
  338. {
  339. return query.ToList(startIndex, endIndex);
  340. }
  341. /// <summary>
  342. /// ??IArrayList
  343. /// </summary>
  344. /// <returns></returns>
  345. public ISourceList<TEntity> ToList<TEntity>() where TEntity : Entity
  346. {
  347. return query.ToList<TEntity>();
  348. }
  349. /// <summary>
  350. /// ??IArrayList
  351. /// </summary>
  352. /// <returns></returns>
  353. public ISourceList<TEntity> ToList<TEntity>(int startIndex, int endIndex) where TEntity : Entity
  354. {
  355. return query.ToList<TEntity>(startIndex, endIndex);
  356. }
  357. #endregion
  358. /// <summary>
  359. /// ????DataTable
  360. /// </summary>
  361. /// <param name="startIndex"></param>
  362. /// <param name="endIndex"></param>
  363. /// <returns></returns>
  364. public ISourceTable ToTable(int startIndex, int endIndex)
  365. {
  366. return query.ToTable(startIndex, endIndex);
  367. }
  368. /// <summary>
  369. /// ????DataTable
  370. /// </summary>
  371. /// <returns></returns>
  372. public ISourceTable ToTable()
  373. {
  374. return query.ToTable();
  375. }
  376. /// <summary>
  377. /// ?????
  378. /// </summary>
  379. /// <typeparam name="TResult"></typeparam>
  380. /// <returns></returns>
  381. public TResult ToScalar<TResult>()
  382. {
  383. return query.ToScalar<TResult>();
  384. }
  385. /// <summary>
  386. /// ?????
  387. /// </summary>
  388. /// <returns></returns>
  389. public object ToScalar()
  390. {
  391. return query.ToScalar();
  392. }
  393. /// <summary>
  394. /// ?????????
  395. /// </summary>
  396. /// <returns></returns>
  397. public int Count()
  398. {
  399. return query.Count();
  400. }
  401. #endregion
  402. #region ??????
  403. public IDataPage<IList<T>> ToListPage(int pageSize, int pageIndex)
  404. {
  405. return query.ToListPage(pageSize, pageIndex);
  406. }
  407. /// <summary>
  408. /// ????????????
  409. /// </summary>
  410. /// <param name="pageSize"></param>
  411. /// <param name="pageIndex"></param>
  412. /// <returns></returns>
  413. public IDataPage<DataTable> ToTablePage(int pageSize, int pageIndex)
  414. {
  415. return query.ToTablePage(pageSize, pageIndex);
  416. }
  417. #endregion
  418. #endregion
  419. #region ????
  420. #region ???
  421. public FromSection<T> InnerJoin<TJoin>(WhereClip onWhere)
  422. where TJoin : Entity
  423. {
  424. return InnerJoin<TJoin>((Table)null, onWhere);
  425. }
  426. public FromSection<T> InnerJoin<TJoin>(Table table, WhereClip onWhere)
  427. where TJoin : Entity
  428. {
  429. return Join<TJoin>(table, onWhere, JoinType.InnerJoin);
  430. }
  431. public FromSection<T> InnerJoin<TJoin>(string aliasName, WhereClip onWhere)
  432. where TJoin : Entity
  433. {
  434. return Join<TJoin>(aliasName, onWhere, JoinType.InnerJoin);
  435. }
  436. #endregion
  437. #region ???
  438. public FromSection<T> LeftJoin<TJoin>(WhereClip onWhere)
  439. where TJoin : Entity
  440. {
  441. return LeftJoin<TJoin>((Table)null, onWhere);
  442. }
  443. public FromSection<T> LeftJoin<TJoin>(Table table, WhereClip onWhere)
  444. where TJoin : Entity
  445. {
  446. return Join<TJoin>(table, onWhere, JoinType.LeftJoin);
  447. }
  448. public FromSection<T> LeftJoin<TJoin>(string aliasName, WhereClip onWhere)
  449. where TJoin : Entity
  450. {
  451. return Join<TJoin>(aliasName, onWhere, JoinType.LeftJoin);
  452. }
  453. #endregion
  454. #region ???
  455. public FromSection<T> RightJoin<TJoin>(WhereClip onWhere)
  456. where TJoin : Entity
  457. {
  458. return RightJoin<TJoin>((Table)null, onWhere);
  459. }
  460. public FromSection<T> RightJoin<TJoin>(Table table, WhereClip onWhere)
  461. where TJoin : Entity
  462. {
  463. return Join<TJoin>(table, onWhere, JoinType.RightJoin);
  464. }
  465. public FromSection<T> RightJoin<TJoin>(string aliasName, WhereClip onWhere)
  466. where TJoin : Entity
  467. {
  468. return Join<TJoin>(aliasName, onWhere, JoinType.RightJoin);
  469. }
  470. #endregion
  471. #region ????
  472. private FromSection<T> Join<TJoin>(Table table, WhereClip onWhere, JoinType joinType)
  473. where TJoin : Entity
  474. {
  475. TJoin entity = DataUtils.CreateInstance<TJoin>();
  476. this.entityList.Add(entity);
  477. if ((IField)query.PagingField == null)
  478. {
  479. //????????,??ID?????
  480. query.PagingField = entity.PagingField;
  481. }
  482. FromSection<TJoin> from = new FromSection<TJoin>(table);
  483. string strJoin = string.Empty;
  484. if (onWhere != null)
  485. {
  486. strJoin = " on " + onWhere.ToString();
  487. }
  488. string join = GetJoinEnumString(joinType);
  489. if (this.relation != null)
  490. {
  491. this.tableName = " {2} " + this.tableName;
  492. this.relation += " {3} ";
  493. }
  494. this.relation += join + from.TableName + strJoin;
  495. return this;
  496. }
  497. private FromSection<T> Join<TJoin>(string aliasName, WhereClip onWhere, JoinType joinType)
  498. where TJoin : Entity
  499. {
  500. TJoin entity = DataUtils.CreateInstance<TJoin>();
  501. entity.GetTable().AliasName = aliasName;
  502. this.entityList.Add(entity);
  503. if ((IField)query.PagingField == null)
  504. {
  505. //????????,??ID?????
  506. query.PagingField = entity.PagingField;
  507. }
  508. FromSection<TJoin> from = new FromSection<TJoin>(null);
  509. if (aliasName != null)
  510. {
  511. query.PagingField = query.PagingField.At(aliasName);
  512. from.tableName += " {0}" + aliasName + "{1}";
  513. }
  514. string strJoin = string.Empty;
  515. if (onWhere != null)
  516. {
  517. strJoin = " on " + onWhere.ToString();
  518. }
  519. string join = GetJoinEnumString(joinType);
  520. if (this.relation != null)
  521. {
  522. this.tableName = " {2} " + this.tableName;
  523. this.relation += " {3} ";
  524. }
  525. this.relation += join + from.TableName + strJoin;
  526. return this;
  527. }
  528. #endregion
  529. #endregion
  530. #region ?????
  531. public FromSection<T> InnerJoin<TJoin>(QuerySection<TJoin> joinquery, WhereClip onWhere)
  532. where TJoin : Entity
  533. {
  534. return InnerJoin<TJoin>(joinquery, joinquery.FromSection.TableName, onWhere);
  535. }
  536. public FromSection<T> LeftJoin<TJoin>(QuerySection<TJoin> joinquery, WhereClip onWhere)
  537. where TJoin : Entity
  538. {
  539. return LeftJoin<TJoin>(joinquery, joinquery.FromSection.TableName, onWhere);
  540. }
  541. public FromSection<T> RightJoin<TJoin>(QuerySection<TJoin> joinquery, WhereClip onWhere)
  542. where TJoin : Entity
  543. {
  544. return RightJoin<TJoin>(joinquery, joinquery.FromSection.TableName, onWhere);
  545. }
  546. #region ????????
  547. public FromSection<T> InnerJoin<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere)
  548. where TJoin : Entity
  549. {
  550. return Join<TJoin>(joinquery, aliasName, onWhere, JoinType.InnerJoin);
  551. }
  552. public FromSection<T> LeftJoin<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere)
  553. where TJoin : Entity
  554. {
  555. return Join<TJoin>(joinquery, aliasName, onWhere, JoinType.LeftJoin);
  556. }
  557. public FromSection<T> RightJoin<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere)
  558. where TJoin : Entity
  559. {
  560. return Join<TJoin>(joinquery, aliasName, onWhere, JoinType.RightJoin);
  561. }
  562. #endregion
  563. private FromSection<T> Join<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere, JoinType joinType)
  564. where TJoin : Entity
  565. {
  566. string queryString = joinquery.QueryString;
  567. List<Entity> list = joinquery.FromSection.EntityList;
  568. foreach (Entity entity in list)
  569. {
  570. entity.GetTable().AliasName = aliasName;
  571. }
  572. this.entityList.AddRange(list.ToArray());
  573. if ((IField)query.PagingField == null)
  574. {
  575. //????????,??ID?????
  576. query.PagingField = joinquery.PagingField;
  577. query.PagingField = query.PagingField.At(aliasName);
  578. }
  579. string strJoin = string.Empty;
  580. if (onWhere != null)
  581. {
  582. strJoin = " on " + onWhere.ToString();
  583. }
  584. string join = GetJoinEnumString(joinType);
  585. if (this.relation != null)
  586. {
  587. this.tableName = " {2} " + this.tableName;
  588. this.relation += " {3} ";
  589. }
  590. this.relation += join + "(" + queryString + ") {0}" + aliasName + "{1} " + strJoin;
  591. return this;
  592. }
  593. private string GetJoinEnumString(JoinType joinType)
  594. {
  595. switch (joinType)
  596. {
  597. case JoinType.LeftJoin:
  598. return " left outer join ";
  599. case JoinType.RightJoin:
  600. return " right outer join ";
  601. case JoinType.InnerJoin:
  602. return " inner join ";
  603. default:
  604. return " inner join ";
  605. }
  606. }
  607. #endregion
  608. #region ????
  609. internal Field[] GetSelectFields()
  610. {
  611. IDictionary<string, Field> dictfields = new Dictionary<string, Field>();
  612. List<Field> fieldlist = new List<Field>();
  613. foreach (Entity entity in this.entityList)
  614. {
  615. Table table = entity.GetTable();
  616. IList<Field> fields = entity.Fields;
  617. if (fields == null || fields.Count == 0)
  618. {
  619. throw new YUIException("?????????????");
  620. }
  621. else
  622. {
  623. foreach (Field field in fields)
  624. {
  625. if (!dictfields.ContainsKey(field.OriginalName))
  626. {
  627. if (table.AliasName != null)
  628. {
  629. dictfields.Add(field.OriginalName, field.At(table.AliasName));
  630. }
  631. else
  632. {
  633. dictfields.Add(field.OriginalName, field);
  634. }
  635. }
  636. }
  637. }
  638. }
  639. //??????Field??
  640. foreach (KeyValuePair<string, Field> kv in dictfields)
  641. {
  642. fieldlist.Add(kv.Value);
  643. }
  644. return fieldlist.ToArray();
  645. }
  646. #endregion
  647. #region ????
  648. private string tableName;
  649. internal string TableName
  650. {
  651. get
  652. {
  653. return tableName;
  654. }
  655. }
  656. private string relation;
  657. internal string Relation
  658. {
  659. get
  660. {
  661. return relation;
  662. }
  663. }
  664. #endregion
  665. }
  666. }