From 314e3f8020e1fe3784f6137b9cbe95d1ea4c2db4 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 27 May 2017 13:31:34 +0800 Subject: [PATCH] - --- SqlSugar/SqlSugarAccessory.cs | 108 ++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 50 deletions(-) diff --git a/SqlSugar/SqlSugarAccessory.cs b/SqlSugar/SqlSugarAccessory.cs index 81e17ed03..602817402 100644 --- a/SqlSugar/SqlSugarAccessory.cs +++ b/SqlSugar/SqlSugarAccessory.cs @@ -9,6 +9,7 @@ namespace SqlSugar { public partial class SqlSugarAccessory { + #region Properties public SqlSugarClient Context { get; set; } public string EntityNamespace { get; set; } public IConnectionConfig CurrentConnectionConfig { get; set; } @@ -16,8 +17,10 @@ namespace SqlSugar public Guid ContextID { get; set; } public MappingTableList MappingTables = new MappingTableList(); public MappingColumnList MappingColumns = new MappingColumnList(); - public IgnoreComumnList IgnoreColumns = new IgnoreComumnList(); + public IgnoreComumnList IgnoreColumns = new IgnoreComumnList(); + #endregion + #region Fields protected ISqlBuilder _SqlBuilder; protected EntityProvider _EntityProvider; protected IAdo _Ado; @@ -25,9 +28,10 @@ namespace SqlSugar protected IRewritableMethods _RewritableMethods; protected IDbFirst _DbFirst; protected ICodeFirst _CodeFirst; - protected IDbMaintenance _DbMaintenance; - + protected IDbMaintenance _DbMaintenance; + #endregion + #region Init mppingInfo protected void InitMppingInfo() { InitMppingInfo(); @@ -45,7 +49,6 @@ namespace SqlSugar InitMppingInfo(); InitMppingInfo(); } - protected void InitMppingInfo() { string cacheKey = "Context.InitAttributeMappingTables"; @@ -80,65 +83,26 @@ namespace SqlSugar if (entityInfo.Columns.Any(it => it.EntityName == entityInfo.EntityName)) { var mappingColumnInfos = this.MappingColumns.Where(it => it.EntityName == entityInfo.EntityName); - foreach (var item in entityInfo.Columns.Where(it=>it.IsIgnore==false)) + foreach (var item in entityInfo.Columns.Where(it => it.IsIgnore == false)) { if (!mappingColumnInfos.Any(it => it.PropertyName == item.PropertyName)) if (item.PropertyName != item.DbColumnName && item.DbColumnName.IsValuable()) this.MappingColumns.Add(item.PropertyName, item.DbColumnName, item.EntityName); } var ignoreInfos = this.IgnoreColumns.Where(it => it.EntityName == entityInfo.EntityName); - foreach (var item in entityInfo.Columns.Where(it=>it.IsIgnore)) + foreach (var item in entityInfo.Columns.Where(it => it.IsIgnore)) { if (!ignoreInfos.Any(it => it.PropertyName == item.PropertyName)) - this.IgnoreColumns.Add(item.PropertyName, item.EntityName); + this.IgnoreColumns.Add(item.PropertyName, item.EntityName); } } } - protected List GetJoinInfos(Expression joinExpression, ref string shortName, params Type[] entityTypeArray) - { - List reval = new List(); - var lambdaParameters = ((LambdaExpression)joinExpression).Parameters.ToList(); - ExpressionContext exp = new ExpressionContext(); - exp.MappingColumns = this.Context.MappingColumns; - exp.MappingTables = this.Context.MappingTables; - exp.Resolve(joinExpression, ResolveExpressType.Join); - int i = 0; - var joinArray = exp.Result.GetResultArray(); - foreach (var type in entityTypeArray) - { - var isFirst = i == 0; - ++i; - JoinQueryInfo joinInfo = new JoinQueryInfo(); - var hasMappingTable = exp.MappingTables.IsValuable(); - MappingTable mappingInfo = null; - if (hasMappingTable) - { - mappingInfo = exp.MappingTables.FirstOrDefault(it => it.EntityName.Equals(type.Name, StringComparison.CurrentCultureIgnoreCase)); - joinInfo.TableName = mappingInfo != null ? mappingInfo.DbTableName : type.Name; - } - else - { - joinInfo.TableName = type.Name; - } - if (isFirst) - { - var firstItem = lambdaParameters.First(); - lambdaParameters.Remove(firstItem); - shortName = firstItem.Name; - } - var joinString = joinArray[i * 2 - 2]; - joinInfo.ShortName = lambdaParameters[i - 1].Name; - joinInfo.JoinType = (JoinType)Enum.Parse(typeof(JoinType), joinString); - joinInfo.JoinWhere = joinArray[i * 2 - 1]; - joinInfo.JoinIndex = i; - reval.Add((joinInfo)); - } - return reval; - } + #endregion + #region Create Instance protected ISugarQueryable CreateQueryable() where T : class, new() { - ISugarQueryable result= InstanceFactory.GetQueryable(this.CurrentConnectionConfig); + ISugarQueryable result = InstanceFactory.GetQueryable(this.CurrentConnectionConfig); return CreateQueryable(result); } protected ISugarQueryable CreateQueryable(ISugarQueryable result) where T : class, new() @@ -181,7 +145,6 @@ namespace SqlSugar sqlBuilder.Context = reval.SqlBuilder.DeleteBuilder.Context = this.Context; return reval; } - protected void CreateQueryJoin(Expression joinExpression, Type[] types, ISugarQueryable queryable) where T : class, new() { this.CreateQueryable(queryable); @@ -189,5 +152,50 @@ namespace SqlSugar queryable.SqlBuilder.QueryBuilder.JoinQueryInfos = this.GetJoinInfos(joinExpression, ref shortName, types); queryable.SqlBuilder.QueryBuilder.TableShortName = shortName; } + #endregion + + #region Private methods + protected List GetJoinInfos(Expression joinExpression, ref string shortName, params Type[] entityTypeArray) + { + List reval = new List(); + var lambdaParameters = ((LambdaExpression)joinExpression).Parameters.ToList(); + ExpressionContext exp = new ExpressionContext(); + exp.MappingColumns = this.Context.MappingColumns; + exp.MappingTables = this.Context.MappingTables; + exp.Resolve(joinExpression, ResolveExpressType.Join); + int i = 0; + var joinArray = exp.Result.GetResultArray(); + foreach (var type in entityTypeArray) + { + var isFirst = i == 0; + ++i; + JoinQueryInfo joinInfo = new JoinQueryInfo(); + var hasMappingTable = exp.MappingTables.IsValuable(); + MappingTable mappingInfo = null; + if (hasMappingTable) + { + mappingInfo = exp.MappingTables.FirstOrDefault(it => it.EntityName.Equals(type.Name, StringComparison.CurrentCultureIgnoreCase)); + joinInfo.TableName = mappingInfo != null ? mappingInfo.DbTableName : type.Name; + } + else + { + joinInfo.TableName = type.Name; + } + if (isFirst) + { + var firstItem = lambdaParameters.First(); + lambdaParameters.Remove(firstItem); + shortName = firstItem.Name; + } + var joinString = joinArray[i * 2 - 2]; + joinInfo.ShortName = lambdaParameters[i - 1].Name; + joinInfo.JoinType = (JoinType)Enum.Parse(typeof(JoinType), joinString); + joinInfo.JoinWhere = joinArray[i * 2 - 1]; + joinInfo.JoinIndex = i; + reval.Add((joinInfo)); + } + return reval; + } + #endregion } }