/// <summary> /// 多表连查分页 /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="page"></param> /// <returns></returns> public PageDataSource <TModel> GetPage <TModel>(PageFilter page) { List <JoinRelation> tables = new List <JoinRelation>(); SqlBuilder.AppendFormat(" SELECT {0} FROM {1}", SelectFields.ToString().TrimEnd(','), JoinRelations[0].LeftTable); if (WithAlias) { SqlBuilder.AppendFormat(" AS {0}", JoinRelations[0].LeftTableAlias); } foreach (JoinRelation j in JoinRelations) { if (j.JoinType == JoinType.Outer) { SqlBuilder.Append(" LEFT OUTER JOIN "); } else { SqlBuilder.Append(" INNER JOIN "); } if (WithAlias) { if (tables.Count(m => m.LeftTableAlias == j.RightTableAlias || m.RightTableAlias == j.RightTableAlias) == 0) { SqlBuilder.AppendFormat("{0} AS {1}", j.RightTable, j.RightTableAlias); tables.Add(j); } else { SqlBuilder.AppendFormat("{0} AS {1}", j.LeftTable, j.LeftTableAlias); tables.Add(j); } } else { if (tables.Count(m => m.LeftTable == j.RightTable || m.RightTable == j.RightTable) == 0) { SqlBuilder.Append(j.RightTable); tables.Add(j); } else { SqlBuilder.Append(j.LeftTable); tables.Add(j); } } SqlBuilder.AppendFormat(" ON {0}", j.OnSql.TrimEnd("AND".ToCharArray())); } if (WhereClause.Length > 0) { SqlBuilder.AppendFormat(" WHERE {0}", WhereClause.ToString().Trim().TrimEnd("AND".ToCharArray())); } if (GroupByFields.Length > 0) { SqlBuilder.AppendFormat(" GROUP BY {0}", GroupByFields.ToString().Trim().TrimEnd(',')); } if (OrderByFields.Length > 0) { page.OrderText = OrderByFields.ToString().Trim().TrimEnd(','); } //开始组装sql //开始组装sql PageDataSource <TModel> result = DbContext.GetPage <TModel>(SqlBuilder.ToString(), page, Parameters.ToArray()); if (this.IsAutoDisposeDbContext) { DbContext.Dispose(); } return(result); }
/// <summary> /// 返回数量 /// </summary> /// <returns></returns> public int GetCount() { List <JoinRelation> tables = new List <JoinRelation>(); SqlBuilder.AppendFormat(" SELECT COUNT(1) AS CT FROM {0}", JoinRelations[0].LeftTable); if (WithAlias) { SqlBuilder.AppendFormat(" AS {0}", JoinRelations[0].LeftTableAlias); } foreach (JoinRelation j in JoinRelations) { if (j.JoinType == JoinType.Outer) { SqlBuilder.Append(" LEFT OUTER JOIN "); } else { SqlBuilder.Append(" INNER JOIN "); } if (WithAlias) { if (tables.Count(m => m.LeftTableAlias == j.RightTableAlias || m.RightTableAlias == j.RightTableAlias) == 0) { SqlBuilder.AppendFormat("{0} AS {1}", j.RightTable, j.RightTableAlias); tables.Add(j); } else { SqlBuilder.AppendFormat("{0} AS {1}", j.LeftTable, j.LeftTableAlias); tables.Add(j); } } else { if (tables.Count(m => m.LeftTable == j.RightTable || m.RightTable == j.RightTable) == 0) { SqlBuilder.Append(j.RightTable); tables.Add(j); } else { SqlBuilder.Append(j.LeftTable); tables.Add(j); } } SqlBuilder.AppendFormat(" ON {0}", j.OnSql.TrimEnd("AND".ToCharArray())); } if (WhereClause.Length > 0) { SqlBuilder.AppendFormat(" WHERE {0}", WhereClause.ToString().Trim().TrimEnd("AND".ToCharArray())); } if (GroupByFields.Length > 0) { SqlBuilder.AppendFormat(" GROUP BY {0}", GroupByFields.ToString().Trim().TrimEnd(',')); } if (OrderByFields.Length > 0) { SqlBuilder.AppendFormat(" ORDER BY {0}", OrderByFields.ToString().Trim().TrimEnd(',')); } //开始组装sql int result = DbContext.GetSingle <int>(SqlBuilder.ToString(), Parameters.ToArray()); if (this.IsAutoDisposeDbContext) { DbContext.Dispose(); } return(result); }