You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
8.3 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace SqlSugar
  6. {
  7. ///<summary>
  8. /// ** description:Expression to sql
  9. /// ** author:sunkaixuan
  10. /// ** date:2017/1/14
  11. /// ** email:610262374@qq.com
  12. public class ExpressionContext : ExpResolveAccessory
  13. {
  14. #region Fields
  15. private bool _IsSingle = true;
  16. private IDbMethods _DbMehtods { get; set; }
  17. #endregion Fields
  18. #region Properties
  19. public IDbMethods DbMehtods
  20. {
  21. get
  22. {
  23. if (_DbMehtods == null)
  24. {
  25. _DbMehtods = new DefaultDbMethod();
  26. }
  27. return _DbMehtods;
  28. }
  29. set
  30. {
  31. _DbMehtods = value;
  32. }
  33. }
  34. public int Index { get; set; }
  35. public int ParameterIndex { get; set; }
  36. public string SingleTableNameSubqueryShortName { get; set; }
  37. public MappingColumnList MappingColumns { get; set; }
  38. public MappingTableList MappingTables { get; set; }
  39. public IgnoreColumnList IgnoreComumnList { get; set; }
  40. public List<SqlFuncExternal> SqlFuncServices { get; set; }
  41. public bool IsSingle
  42. {
  43. get
  44. {
  45. return _IsSingle;
  46. }
  47. set
  48. {
  49. _IsSingle = value;
  50. }
  51. }
  52. public bool IsJoin
  53. {
  54. get
  55. {
  56. return !IsSingle;
  57. }
  58. }
  59. public List<JoinQueryInfo> JoinQueryInfos { get; set; }
  60. public ResolveExpressType ResolveType { get; set; }
  61. public Expression Expression { get; set; }
  62. public ExpressionResult Result
  63. {
  64. get
  65. {
  66. if (base._Result == null)
  67. {
  68. this.Result = new ExpressionResult(this.ResolveType);
  69. }
  70. return base._Result;
  71. }
  72. set
  73. {
  74. this._Result = value;
  75. }
  76. }
  77. public List<SugarParameter> Parameters
  78. {
  79. get
  80. {
  81. if (base._Parameters == null)
  82. base._Parameters = new List<SugarParameter>();
  83. return base._Parameters;
  84. }
  85. set
  86. {
  87. base._Parameters = value;
  88. }
  89. }
  90. public virtual string SqlParameterKeyWord
  91. {
  92. get
  93. {
  94. return "@";
  95. }
  96. }
  97. public virtual string SqlTranslationLeft { get { return "["; } }
  98. public virtual string SqlTranslationRight { get { return "]"; } }
  99. public virtual Action<Type> InitMappingInfo { get; set; }
  100. public virtual Action RefreshMapping { get; set; }
  101. #endregion Properties
  102. #region Core methods
  103. public void Resolve(Expression expression, ResolveExpressType resolveType)
  104. {
  105. this.ResolveType = resolveType;
  106. this.Expression = expression;
  107. var resolve = new BaseResolve(new ExpressionParameter { CurrentExpression = this.Expression, Context = this });
  108. resolve.Start();
  109. }
  110. public void Clear()
  111. {
  112. base._Result = null;
  113. base._Parameters = new List<SugarParameter>();
  114. }
  115. public ExpressionContext GetCopyContext()
  116. {
  117. var copyContext = (ExpressionContext)Activator.CreateInstance(this.GetType(), true);
  118. copyContext.Index = this.Index;
  119. copyContext.ParameterIndex = this.ParameterIndex;
  120. return copyContext;
  121. }
  122. public ExpressionContext GetCopyContextWithMapping()
  123. {
  124. var copyContext = (ExpressionContext)Activator.CreateInstance(this.GetType(), true);
  125. copyContext.Index = this.Index;
  126. copyContext.ParameterIndex = this.ParameterIndex;
  127. copyContext.MappingColumns = this.MappingColumns;
  128. copyContext.MappingTables = this.MappingTables;
  129. copyContext.IgnoreComumnList = this.IgnoreComumnList;
  130. copyContext.SqlFuncServices = this.SqlFuncServices;
  131. return copyContext;
  132. }
  133. #endregion Core methods
  134. #region Override methods
  135. public virtual string GetTranslationTableName(string entityName, bool isMapping = true)
  136. {
  137. Check.ArgumentNullException(entityName, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
  138. if (IsTranslationText(entityName)) return entityName;
  139. isMapping = isMapping && this.MappingTables.HasValue();
  140. var isComplex = entityName.Contains(UtilConstants.Dot);
  141. if (isMapping && isComplex)
  142. {
  143. var columnInfo = entityName.Split(UtilConstants.DotChar);
  144. var mappingInfo = this.MappingTables.FirstOrDefault(it => it.EntityName.Equals(columnInfo.Last(), StringComparison.CurrentCultureIgnoreCase));
  145. if (mappingInfo != null)
  146. {
  147. columnInfo[columnInfo.Length - 1] = mappingInfo.EntityName;
  148. }
  149. return string.Join(UtilConstants.Dot, columnInfo.Select(it => GetTranslationText(it)));
  150. }
  151. else if (isMapping)
  152. {
  153. var mappingInfo = this.MappingTables.FirstOrDefault(it => it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase));
  154. return SqlTranslationLeft + (mappingInfo == null ? entityName : mappingInfo.DbTableName) + SqlTranslationRight;
  155. }
  156. else if (isComplex)
  157. {
  158. return string.Join(UtilConstants.Dot, entityName.Split(UtilConstants.DotChar).Select(it => GetTranslationText(it)));
  159. }
  160. else
  161. {
  162. return GetTranslationText(entityName);
  163. }
  164. }
  165. public virtual string GetTranslationColumnName(string columnName)
  166. {
  167. Check.ArgumentNullException(columnName, string.Format(ErrorMessage.ObjNotExist, "Column Name"));
  168. if (columnName.Substring(0, 1) == this.SqlParameterKeyWord)
  169. {
  170. return columnName;
  171. }
  172. if (IsTranslationText(columnName)) return columnName;
  173. if (columnName.Contains(UtilConstants.Dot))
  174. {
  175. return string.Join(UtilConstants.Dot, columnName.Split(UtilConstants.DotChar).Select(it => GetTranslationText(it)));
  176. }
  177. else
  178. {
  179. return GetTranslationText(columnName);
  180. }
  181. }
  182. public virtual string GetDbColumnName(string entityName, string propertyName)
  183. {
  184. if (this.MappingColumns.HasValue())
  185. {
  186. var mappingInfo = this.MappingColumns.SingleOrDefault(it => it.EntityName == entityName && it.PropertyName == propertyName);
  187. return mappingInfo == null ? propertyName : mappingInfo.DbColumnName;
  188. }
  189. else
  190. {
  191. return propertyName;
  192. }
  193. }
  194. public virtual bool IsTranslationText(string name)
  195. {
  196. var result = name.IsContainsIn(SqlTranslationLeft, SqlTranslationRight, UtilConstants.Space, ExpressionConst.LeftParenthesis, ExpressionConst.RightParenthesis);
  197. return result;
  198. }
  199. public virtual string GetTranslationText(string name)
  200. {
  201. return SqlTranslationLeft + name + SqlTranslationRight;
  202. }
  203. public virtual string GetAsString(string asName, string fieldValue)
  204. {
  205. if (fieldValue.Contains(".*") || fieldValue == "*") return fieldValue;
  206. return $" {GetTranslationColumnName(fieldValue)} {"AS"} {GetTranslationColumnName(asName)} ";
  207. }
  208. public virtual string GetEqString(string eqName, string fieldValue)
  209. {
  210. return $" {GetTranslationColumnName(eqName)} {"="} {GetTranslationColumnName(fieldValue)} ";
  211. }
  212. public virtual string GetAsString(string asName, string fieldValue, string fieldShortName)
  213. {
  214. if (fieldValue.Contains(".*") || fieldValue == "*") return fieldValue;
  215. return $" {GetTranslationColumnName(fieldShortName + "." + fieldValue)} {"AS"} {GetTranslationColumnName(asName)} ";
  216. }
  217. #endregion Override methods
  218. }
  219. }