/YUI/YUI.Core/Dao/Query/FromSection.cs
C# | 789 lines | 491 code | 123 blank | 175 comment | 36 complexity | b5616ba5b672325e6cacafb6c416c656 MD5 | raw file
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Reflection;
- using System.Data;
- using System.Data.Common;
- using YUI.Core.Design;
- using YUI.Core.Dao.Session;
- using YUI.Core.Common;
- using YUI.Core.Dao.Expression;
- using YUI.Core.Dao.Source;
-
- namespace YUI.Core.Dao.Query
- {
- public class FromSection<T> : IQuerySection<T>
- where T : Entity
- {
- private Entity fromEntity;
- private Table fromTable;
- private QuerySection<T> query;
- private List<Entity> entityList = new List<Entity>();
- internal List<Entity> EntityList
- {
- get { return entityList; }
- set { entityList = value; }
- }
-
- #region ???FromSection
-
- internal FromSection()
- {
- this.fromEntity = DataUtils.CreateInstance<T>();
- this.fromTable = this.fromEntity.GetTable();
- }
-
- internal FromSection(DbProvider dbProvider, DbTrans dbTran, Table table)
- : this()
- {
- this.entityList.Add(this.fromEntity);
- //????????????
- this.tableName = table == null ? fromTable.Name : table.Name;
- fromTable.AliasName = this.tableName;
- Field pagingField = fromEntity.PagingField;
- this.query = new QuerySection<T>(this, dbProvider, dbTran, pagingField);
- }
-
- internal FromSection(DbProvider dbProvider, DbTrans dbTran, string aliasName)
- : this()
- {
- fromTable.AliasName = aliasName;
- this.entityList.Add(this.fromEntity);
- this.tableName = fromTable.Name;
- Field pagingField = fromEntity.PagingField;
- if (aliasName != null)
- {
- if ((IField)pagingField != null)
- {
- pagingField = pagingField.At(aliasName);
- }
-
- this.tableName += " {0}" + aliasName + "{1}";
- }
- this.query = new QuerySection<T>(this, dbProvider, dbTran, pagingField);
- }
-
- internal FromSection(string tableName, string relation, IList<Entity> list)
- {
- this.tableName = tableName;
- this.relation = relation;
- this.entityList.AddRange(list);
- }
-
- internal FromSection(string tableName, string aliasName)
- : this()
- {
- fromTable.AliasName = aliasName;
- this.entityList.Add(this.fromEntity);
- this.tableName = tableName;
- if (aliasName != null)
- {
- this.tableName += " {0}" + aliasName + "{1}";
- }
- }
-
- internal FromSection(Table table)
- : this()
- {
- this.tableName = table == null ? fromTable.Name : table.Name;
- fromTable.AliasName = this.tableName;
- this.query = new QuerySection<T>(this);
- }
-
- #endregion
-
- #region ??IQuerySection
-
- #region ??IDataQuery
-
- /// <summary>
- /// ?????????Page?
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="pageSize"></param>
- /// <returns></returns>
- public PageSection<TEntity> GetPage<TEntity>(int pageSize)
- where TEntity : Entity
- {
- return query.GetPage<TEntity>(pageSize);
- }
-
- /// <summary>
- /// ??????
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <returns></returns>
- public TEntity ToSingle<TEntity>()
- where TEntity : Entity
- {
- return query.ToSingle<TEntity>();
- }
-
- #endregion
-
- #region ?????
-
- /// <summary>
- /// ???????
- /// </summary>
- /// <returns></returns>
- public QuerySection<T> SubQuery()
- {
- return query.SubQuery();
- }
-
- /// <summary>
- /// ???????
- /// </summary>
- /// <returns></returns>
- public QuerySection<T> SubQuery(string aliasName)
- {
- return query.SubQuery(aliasName);
- }
-
- /// <summary>
- /// ???????
- /// </summary>
- /// <returns></returns>
- public QuerySection<TSub> SubQuery<TSub>()
- where TSub : Entity
- {
- return query.SubQuery<TSub>();
- }
-
- /// <summary>
- /// ???????????
- /// </summary>
- /// <param name="aliasName"></param>
- /// <returns></returns>
- public QuerySection<TSub> SubQuery<TSub>(string aliasName)
- where TSub : Entity
- {
- return query.SubQuery<TSub>(aliasName);
- }
-
- #endregion
-
- #region ??????
-
- /// <summary>
- /// ??GroupBy??
- /// </summary>
- /// <param name="groupBy"></param>
- /// <returns></returns>
- public QuerySection<T> GroupBy(GroupByClip groupBy)
- {
- return query.GroupBy(groupBy);
- }
-
- /// <summary>
- /// ??OrderBy??
- /// </summary>
- /// <param name="orderBy"></param>
- /// <returns></returns>
- public QuerySection<T> OrderBy(OrderByClip orderBy)
- {
- return query.OrderBy(orderBy);
- }
-
- /// <summary>
- /// ??????
- /// </summary>
- /// <param name="fields"></param>
- /// <returns></returns>
- public QuerySection<T> Select(params Field[] fields)
- {
- return query.Select(fields);
- }
-
- /// <summary>
- /// ????????????????????????
- /// </summary>
- /// <param name="field"></param>
- /// <returns></returns>
- public QuerySection<T> Select(ExcludeField field)
- {
- return query.Select(field);
- }
-
- /// <summary>
- /// ?????????
- /// </summary>
- /// <param name="where"></param>
- /// <returns></returns>
- public QuerySection<T> Where(WhereClip where)
- {
- return query.Where(where);
- }
-
- /// <summary>
- /// ??Union??
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- public QuerySection<T> Union(QuerySection<T> uquery)
- {
- return query.Union(uquery);
- }
-
- /// <summary>
- /// ??Union??
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- public QuerySection<T> UnionAll(QuerySection<T> uquery)
- {
- return query.UnionAll(uquery);
- }
-
- /// <summary>
- /// ??Having??
- /// </summary>
- /// <param name="where"></param>
- /// <returns></returns>
- public QuerySection<T> Having(WhereClip where)
- {
- return query.Having(where);
- }
-
- /// <summary>
- /// ??????
- /// </summary>
- /// <param name="pagingField"></param>
- /// <returns></returns>
- public QuerySection<T> SetPagingField(Field pagingField)
- {
- return query.SetPagingField(pagingField);
- }
-
- /// <summary>
- /// ???n?
- /// </summary>
- /// <param name="topSize"></param>
- /// <returns></returns>
- public TopSection<T> GetTop(int topSize)
- {
- return query.GetTop(topSize);
- }
-
- /// <summary>
- /// ??Distinct??
- /// </summary>
- /// <returns></returns>
- public QuerySection<T> Distinct()
- {
- return query.Distinct();
- }
-
- #endregion
-
- #region ????
-
- /// <summary>
- /// ?????????Page?
- /// </summary>
- /// <param name="pageSize"></param>
- /// <returns></returns>
- public PageSection<T> GetPage(int pageSize)
- {
- return query.GetPage(pageSize);
- }
-
- /// <summary>
- /// ??????
- /// </summary>
- /// <returns></returns>
- public T ToSingle()
- {
- return query.ToSingle();
- }
-
- #region ??object
-
- /// <summary>
- /// ????Object??
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="startIndex"></param>
- /// <param name="endIndex"></param>
- /// <returns></returns>
- public IArrayList<object> ToListResult(int startIndex, int endIndex)
- {
- return query.ToListResult(startIndex, endIndex);
- }
-
- /// <summary>
- /// ????Object??
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <returns></returns>
- public IArrayList<object> ToListResult()
- {
- return query.ToListResult();
- }
-
- /// <summary>
- /// ????Object??
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="startIndex"></param>
- /// <param name="endIndex"></param>
- /// <returns></returns>
- public IArrayList<TResult> ToListResult<TResult>(int startIndex, int endIndex)
- {
- return query.ToListResult<TResult>(startIndex, endIndex);
- }
-
- /// <summary>
- /// ????Object??
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <returns></returns>
- public IArrayList<TResult> ToListResult<TResult>()
- {
- return query.ToListResult<TResult>();
- }
-
- #endregion
-
- /// <summary>
- /// ????DbReader
- /// </summary>
- /// <param name="startIndex"></param>
- /// <param name="endIndex"></param>
- /// <returns></returns>
- public ISourceReader ToReader(int startIndex, int endIndex)
- {
- return query.ToReader(startIndex, endIndex);
- }
-
- /// <summary>
- /// ????DbReader
- /// </summary>
- /// <returns></returns>
- public ISourceReader ToReader()
- {
- return query.ToReader();
- }
-
- #region ????
-
- /// <summary>
- /// ??IArrayList
- /// </summary>
- /// <returns></returns>
- public ISourceList<T> ToList()
- {
- return query.ToList();
- }
-
- /// <summary>
- /// ??IArrayList
- /// </summary>
- /// <returns></returns>
- public ISourceList<T> ToList(int startIndex, int endIndex)
- {
- return query.ToList(startIndex, endIndex);
- }
-
- /// <summary>
- /// ??IArrayList
- /// </summary>
- /// <returns></returns>
- public ISourceList<TEntity> ToList<TEntity>() where TEntity : Entity
- {
- return query.ToList<TEntity>();
- }
-
- /// <summary>
- /// ??IArrayList
- /// </summary>
- /// <returns></returns>
- public ISourceList<TEntity> ToList<TEntity>(int startIndex, int endIndex) where TEntity : Entity
- {
- return query.ToList<TEntity>(startIndex, endIndex);
- }
-
- #endregion
-
- /// <summary>
- /// ????DataTable
- /// </summary>
- /// <param name="startIndex"></param>
- /// <param name="endIndex"></param>
- /// <returns></returns>
- public ISourceTable ToTable(int startIndex, int endIndex)
- {
- return query.ToTable(startIndex, endIndex);
- }
-
- /// <summary>
- /// ????DataTable
- /// </summary>
- /// <returns></returns>
- public ISourceTable ToTable()
- {
- return query.ToTable();
- }
-
- /// <summary>
- /// ?????
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <returns></returns>
- public TResult ToScalar<TResult>()
- {
- return query.ToScalar<TResult>();
- }
-
- /// <summary>
- /// ?????
- /// </summary>
- /// <returns></returns>
- public object ToScalar()
- {
- return query.ToScalar();
- }
-
- /// <summary>
- /// ?????????
- /// </summary>
- /// <returns></returns>
- public int Count()
- {
- return query.Count();
- }
-
- #endregion
-
- #region ??????
-
- public IDataPage<IList<T>> ToListPage(int pageSize, int pageIndex)
- {
- return query.ToListPage(pageSize, pageIndex);
- }
-
- /// <summary>
- /// ????????????
- /// </summary>
- /// <param name="pageSize"></param>
- /// <param name="pageIndex"></param>
- /// <returns></returns>
- public IDataPage<DataTable> ToTablePage(int pageSize, int pageIndex)
- {
- return query.ToTablePage(pageSize, pageIndex);
- }
-
- #endregion
-
- #endregion
-
- #region ????
-
- #region ???
-
- public FromSection<T> InnerJoin<TJoin>(WhereClip onWhere)
- where TJoin : Entity
- {
- return InnerJoin<TJoin>((Table)null, onWhere);
- }
-
- public FromSection<T> InnerJoin<TJoin>(Table table, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(table, onWhere, JoinType.InnerJoin);
- }
-
- public FromSection<T> InnerJoin<TJoin>(string aliasName, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(aliasName, onWhere, JoinType.InnerJoin);
- }
-
- #endregion
-
- #region ???
-
- public FromSection<T> LeftJoin<TJoin>(WhereClip onWhere)
- where TJoin : Entity
- {
- return LeftJoin<TJoin>((Table)null, onWhere);
- }
-
- public FromSection<T> LeftJoin<TJoin>(Table table, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(table, onWhere, JoinType.LeftJoin);
- }
-
- public FromSection<T> LeftJoin<TJoin>(string aliasName, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(aliasName, onWhere, JoinType.LeftJoin);
- }
-
- #endregion
-
- #region ???
-
- public FromSection<T> RightJoin<TJoin>(WhereClip onWhere)
- where TJoin : Entity
- {
- return RightJoin<TJoin>((Table)null, onWhere);
- }
-
- public FromSection<T> RightJoin<TJoin>(Table table, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(table, onWhere, JoinType.RightJoin);
- }
-
- public FromSection<T> RightJoin<TJoin>(string aliasName, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(aliasName, onWhere, JoinType.RightJoin);
- }
-
- #endregion
-
- #region ????
-
- private FromSection<T> Join<TJoin>(Table table, WhereClip onWhere, JoinType joinType)
- where TJoin : Entity
- {
- TJoin entity = DataUtils.CreateInstance<TJoin>();
- this.entityList.Add(entity);
-
- if ((IField)query.PagingField == null)
- {
- //????????,??ID?????
- query.PagingField = entity.PagingField;
- }
-
- FromSection<TJoin> from = new FromSection<TJoin>(table);
- string strJoin = string.Empty;
- if (onWhere != null)
- {
- strJoin = " on " + onWhere.ToString();
- }
-
- string join = GetJoinEnumString(joinType);
-
- if (this.relation != null)
- {
- this.tableName = " {2} " + this.tableName;
- this.relation += " {3} ";
- }
- this.relation += join + from.TableName + strJoin;
-
- return this;
- }
-
- private FromSection<T> Join<TJoin>(string aliasName, WhereClip onWhere, JoinType joinType)
- where TJoin : Entity
- {
-
- TJoin entity = DataUtils.CreateInstance<TJoin>();
- entity.GetTable().AliasName = aliasName;
- this.entityList.Add(entity);
-
- if ((IField)query.PagingField == null)
- {
- //????????,??ID?????
- query.PagingField = entity.PagingField;
- }
-
- FromSection<TJoin> from = new FromSection<TJoin>(null);
- if (aliasName != null)
- {
- query.PagingField = query.PagingField.At(aliasName);
- from.tableName += " {0}" + aliasName + "{1}";
- }
- string strJoin = string.Empty;
- if (onWhere != null)
- {
- strJoin = " on " + onWhere.ToString();
- }
-
- string join = GetJoinEnumString(joinType);
-
- if (this.relation != null)
- {
- this.tableName = " {2} " + this.tableName;
- this.relation += " {3} ";
- }
- this.relation += join + from.TableName + strJoin;
-
- return this;
- }
-
- #endregion
-
- #endregion
-
- #region ?????
-
- public FromSection<T> InnerJoin<TJoin>(QuerySection<TJoin> joinquery, WhereClip onWhere)
- where TJoin : Entity
- {
- return InnerJoin<TJoin>(joinquery, joinquery.FromSection.TableName, onWhere);
- }
-
- public FromSection<T> LeftJoin<TJoin>(QuerySection<TJoin> joinquery, WhereClip onWhere)
- where TJoin : Entity
- {
- return LeftJoin<TJoin>(joinquery, joinquery.FromSection.TableName, onWhere);
- }
-
- public FromSection<T> RightJoin<TJoin>(QuerySection<TJoin> joinquery, WhereClip onWhere)
- where TJoin : Entity
- {
- return RightJoin<TJoin>(joinquery, joinquery.FromSection.TableName, onWhere);
- }
-
- #region ????????
-
- public FromSection<T> InnerJoin<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(joinquery, aliasName, onWhere, JoinType.InnerJoin);
- }
-
- public FromSection<T> LeftJoin<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(joinquery, aliasName, onWhere, JoinType.LeftJoin);
- }
-
- public FromSection<T> RightJoin<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere)
- where TJoin : Entity
- {
- return Join<TJoin>(joinquery, aliasName, onWhere, JoinType.RightJoin);
- }
-
- #endregion
-
- private FromSection<T> Join<TJoin>(QuerySection<TJoin> joinquery, string aliasName, WhereClip onWhere, JoinType joinType)
- where TJoin : Entity
- {
- string queryString = joinquery.QueryString;
- List<Entity> list = joinquery.FromSection.EntityList;
- foreach (Entity entity in list)
- {
- entity.GetTable().AliasName = aliasName;
- }
- this.entityList.AddRange(list.ToArray());
-
- if ((IField)query.PagingField == null)
- {
- //????????,??ID?????
- query.PagingField = joinquery.PagingField;
- query.PagingField = query.PagingField.At(aliasName);
- }
-
- string strJoin = string.Empty;
- if (onWhere != null)
- {
- strJoin = " on " + onWhere.ToString();
- }
-
- string join = GetJoinEnumString(joinType);
-
- if (this.relation != null)
- {
- this.tableName = " {2} " + this.tableName;
- this.relation += " {3} ";
- }
- this.relation += join + "(" + queryString + ") {0}" + aliasName + "{1} " + strJoin;
-
- return this;
- }
-
- private string GetJoinEnumString(JoinType joinType)
- {
- switch (joinType)
- {
- case JoinType.LeftJoin:
- return " left outer join ";
- case JoinType.RightJoin:
- return " right outer join ";
- case JoinType.InnerJoin:
- return " inner join ";
- default:
- return " inner join ";
- }
- }
-
- #endregion
-
- #region ????
-
- internal Field[] GetSelectFields()
- {
- IDictionary<string, Field> dictfields = new Dictionary<string, Field>();
- List<Field> fieldlist = new List<Field>();
-
- foreach (Entity entity in this.entityList)
- {
- Table table = entity.GetTable();
- IList<Field> fields = entity.Fields;
-
- if (fields == null || fields.Count == 0)
- {
- throw new YUIException("?????????????");
- }
- else
- {
- foreach (Field field in fields)
- {
- if (!dictfields.ContainsKey(field.OriginalName))
- {
- if (table.AliasName != null)
- {
- dictfields.Add(field.OriginalName, field.At(table.AliasName));
- }
- else
- {
- dictfields.Add(field.OriginalName, field);
- }
- }
- }
- }
- }
-
- //??????Field??
- foreach (KeyValuePair<string, Field> kv in dictfields)
- {
- fieldlist.Add(kv.Value);
- }
-
- return fieldlist.ToArray();
-
- }
-
- #endregion
-
- #region ????
-
- private string tableName;
- internal string TableName
- {
- get
- {
- return tableName;
- }
- }
-
- private string relation;
- internal string Relation
- {
- get
- {
- return relation;
- }
- }
-
- #endregion
- }
- }