BaseResolve_Helper.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace SqlSugar
  8. {
  9. /// <summary>
  10. /// BaseResolve-Helper
  11. /// </summary>
  12. public partial class BaseResolve
  13. {
  14. #region Set Method
  15. protected void SetNavigateResult()
  16. {
  17. if (this.Context != null)
  18. {
  19. if (this.Context.Result != null)
  20. {
  21. this.Context.Result.IsNavicate = true;
  22. }
  23. }
  24. }
  25. private void SetParameter(out Expression expression, out ExpressionParameter parameter)
  26. {
  27. Context.Index++;
  28. expression = this.Expression;
  29. parameter = new ExpressionParameter()
  30. {
  31. Context = this.Context,
  32. CurrentExpression = expression,
  33. IsLeft = this.IsLeft,
  34. BaseExpression = this.ExactExpression,
  35. BaseParameter = this.BaseParameter,
  36. Index = Context.Index
  37. };
  38. }
  39. #endregion
  40. #region Get Mehtod
  41. protected object GetMemberValue(object value, Expression exp)
  42. {
  43. if (exp is MemberExpression)
  44. {
  45. var member = (exp as MemberExpression);
  46. var memberParent = member.Expression;
  47. if (memberParent != null && this.Context?.SugarContext?.Context != null)
  48. {
  49. var entity = this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(memberParent.Type);
  50. var columnInfo = entity.Columns.FirstOrDefault(it => it.PropertyName == member.Member.Name);
  51. if (columnInfo?.SqlParameterDbType is Type)
  52. {
  53. var type = columnInfo.SqlParameterDbType as Type;
  54. var ParameterConverter = type.GetMethod("ParameterConverter").MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
  55. var obj = Activator.CreateInstance(type);
  56. var p = ParameterConverter.Invoke(obj, new object[] { value, 100 + this.Context.ParameterIndex }) as SugarParameter;
  57. value = p.Value;
  58. }
  59. }
  60. }
  61. return value;
  62. }
  63. private string GetAsName(Expression item, object shortName, PropertyInfo property)
  64. {
  65. string asName;
  66. var propertyName = property.Name;
  67. var dbColumnName = propertyName;
  68. var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
  69. if (mappingInfo.HasValue())
  70. {
  71. dbColumnName = mappingInfo.DbColumnName;
  72. }
  73. asName = this.Context.GetTranslationText(item.Type.Name + "." + propertyName);
  74. if (Context.IsJoin)
  75. {
  76. this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
  77. }
  78. else
  79. {
  80. this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
  81. }
  82. return asName;
  83. }
  84. private string GetAsNameAndShortName(Expression item, object shortName, PropertyInfo property)
  85. {
  86. string asName;
  87. var propertyName = property.Name;
  88. var dbColumnName = propertyName;
  89. var mappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName == item.Type.Name && it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
  90. if (mappingInfo.HasValue())
  91. {
  92. dbColumnName = mappingInfo.DbColumnName;
  93. }
  94. if (shortName != null && shortName.ObjToString().Contains(this.Context.SqlTranslationLeft) && this.Context.IsSingle)
  95. {
  96. asName = this.Context.GetTranslationText(item.Type.Name + "." + propertyName);
  97. }
  98. else
  99. {
  100. asName = this.Context.GetTranslationText(shortName + "." + item.Type.Name + "." + propertyName);
  101. }
  102. if (Context.IsJoin)
  103. {
  104. this.Context.Result.Append(Context.GetAsString(asName, dbColumnName, shortName.ObjToString()));
  105. }
  106. else
  107. {
  108. this.Context.Result.Append(Context.GetAsString(asName, dbColumnName));
  109. }
  110. return asName;
  111. }
  112. private EntityColumnInfo GetColumnInfo(Expression oppoSiteExpression)
  113. {
  114. var oppsite = (oppoSiteExpression as MemberExpression);
  115. if (oppsite == null) return null;
  116. if (this.Context.SugarContext == null) return null;
  117. if (this.Context.SugarContext.Context == null) return null;
  118. if (oppsite.Expression == null) return null;
  119. var columnInfo = this.Context.SugarContext.Context.EntityMaintenance
  120. .GetEntityInfo(oppsite.Expression.Type).Columns.FirstOrDefault(it => it.PropertyName == oppsite.Member.Name);
  121. return columnInfo;
  122. }
  123. protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item, string name = null)
  124. {
  125. var newContext = this.Context.GetCopyContext();
  126. newContext.MappingColumns = this.Context.MappingColumns;
  127. newContext.MappingTables = this.Context.MappingTables;
  128. newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
  129. newContext.IsSingle = this.Context.IsSingle;
  130. newContext.SqlFuncServices = this.Context.SqlFuncServices;
  131. newContext.MethodName = name;
  132. newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
  133. this.Context.Index = newContext.Index;
  134. this.Context.ParameterIndex = newContext.ParameterIndex;
  135. if (newContext.Parameters.HasValue())
  136. {
  137. this.Context.Parameters.AddRange(newContext.Parameters);
  138. }
  139. if (newContext.SingleTableNameSubqueryShortName.HasValue())
  140. {
  141. this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
  142. }
  143. var methodCallExpressionArgs = new MethodCallExpressionArgs()
  144. {
  145. IsMember = true,
  146. MemberName = newContext.Result.GetResultString()
  147. };
  148. return methodCallExpressionArgs;
  149. }
  150. private string GetAsNameResolveAnObject(ExpressionParameter parameter, Expression item, string asName, bool isSameType)
  151. {
  152. this.Start();
  153. var shortName = parameter.CommonTempData;
  154. var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
  155. foreach (var property in listProperties)
  156. {
  157. var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
  158. if (hasIgnore)
  159. {
  160. continue;
  161. }
  162. if (property.PropertyType.IsClass())
  163. {
  164. var comumnInfo = property.GetCustomAttribute<SugarColumn>();
  165. if (comumnInfo != null && comumnInfo.IsJson && isSameType)
  166. {
  167. asName = GetAsNameAndShortName(item, shortName, property);
  168. }
  169. else if (comumnInfo != null && comumnInfo.IsJson)
  170. {
  171. asName = GetAsName(item, shortName, property);
  172. }
  173. else if (comumnInfo != null && this.Context.SugarContext != null && this.Context.SugarContext.Context != null)
  174. {
  175. var entityInfo = this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type);
  176. var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name);
  177. if (entityColumn != null && entityColumn.IsJson)
  178. {
  179. asName = GetAsName(item, shortName, property);
  180. }
  181. }
  182. }
  183. else if (isSameType)
  184. {
  185. asName = GetAsNameAndShortName(item, shortName, property);
  186. }
  187. else
  188. {
  189. asName = GetAsName(item, shortName, property);
  190. }
  191. }
  192. return asName;
  193. }
  194. public object GetAsNamePackIfElse(object methodValue)
  195. {
  196. methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
  197. new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
  198. new KeyValuePair<string, string>("Return","1"),
  199. new KeyValuePair<string, string>("End","0")
  200. });
  201. return methodValue;
  202. }
  203. #endregion
  204. }
  205. }