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.

168 lines
5.6 KiB

2 years ago
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Text;
  5. namespace SqlSugar
  6. {
  7. public class DeleteBuilder : IDMLBuilder
  8. {
  9. #region Fields
  10. private List<string> _WhereInfos;
  11. #endregion
  12. #region Common Properties
  13. public EntityInfo EntityInfo { get; set; }
  14. public SqlSugarClient Context { get; set; }
  15. public ILambdaExpressions LambdaExpressions { get; set; }
  16. public List<SugarParameter> Parameters { get; set; }
  17. public StringBuilder sql { get; set; }
  18. public ISqlBuilder Builder { get; set; }
  19. public string TableWithString { get; set; }
  20. public virtual List<string> WhereInfos
  21. {
  22. get
  23. {
  24. _WhereInfos = UtilMethods.IsNullReturnNew(_WhereInfos);
  25. return _WhereInfos;
  26. }
  27. set { _WhereInfos = value; }
  28. }
  29. public virtual List<object> BigDataInValues { get; set; }
  30. public virtual string BigDataFiled { get; set; }
  31. #endregion
  32. #region Sql Template
  33. public string SqlTemplate
  34. {
  35. get
  36. {
  37. return "DELETE FROM {0}{1}";
  38. }
  39. }
  40. public string WhereInTemplate
  41. {
  42. get
  43. {
  44. return "{0} IN ({1})";
  45. }
  46. }
  47. public string WhereInOrTemplate
  48. {
  49. get
  50. {
  51. return "OR";
  52. }
  53. }
  54. public string WhereInAndTemplate
  55. {
  56. get
  57. {
  58. return "AND";
  59. }
  60. }
  61. public string WhereInEqualTemplate
  62. {
  63. get
  64. {
  65. return Builder.SqlTranslationLeft+"{0}"+Builder.SqlTranslationRight+"=N'{1}'";
  66. }
  67. }
  68. public string WhereInAreaTemplate
  69. {
  70. get
  71. {
  72. return "({0})";
  73. }
  74. }
  75. #endregion
  76. #region Get Sql
  77. public virtual string GetTableNameString
  78. {
  79. get
  80. {
  81. var result = Builder.GetTranslationTableName(EntityInfo.EntityName);
  82. result += UtilConstants.Space;
  83. if (this.TableWithString.HasValue())
  84. {
  85. result += TableWithString + UtilConstants.Space;
  86. }
  87. return result;
  88. }
  89. }
  90. public virtual string GetWhereString
  91. {
  92. get
  93. {
  94. if (_WhereInfos == null || _WhereInfos.Count == 0) return null;
  95. string whereString = null;
  96. var i = 0;
  97. foreach (var item in _WhereInfos)
  98. {
  99. var isFirst = i == 0;
  100. whereString += isFirst ? "WHERE " : "AND ";
  101. whereString += (item + UtilConstants.Space);
  102. ++i;
  103. }
  104. return whereString;
  105. }
  106. }
  107. #endregion
  108. #region Public methods
  109. public virtual void Clear()
  110. {
  111. }
  112. public virtual string ToSqlString()
  113. {
  114. if (this.BigDataInValues.IsNullOrEmpty())
  115. {
  116. return string.Format(SqlTemplate, GetTableNameString, GetWhereString);
  117. }
  118. else//big data
  119. {
  120. var whereString = GetWhereString;
  121. var sql = string.Format(SqlTemplate, GetTableNameString, whereString);
  122. sql += whereString.IsNullOrEmpty() ? " WHERE " : " AND ";
  123. var batchDeleteSql = new StringBuilder();
  124. var pageSize = 1000;
  125. var pageIndex = 1;
  126. var totalRecord = this.BigDataInValues.Count;
  127. var pageCount = (totalRecord + pageSize - 1) / pageSize;
  128. while (pageCount >= pageIndex)
  129. {
  130. var inValues = this.BigDataInValues.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  131. batchDeleteSql.Append(sql+string.Format(WhereInTemplate,BigDataFiled,inValues.ToArray().ToJoinSqlInVals()));
  132. batchDeleteSql.Append(";");
  133. pageIndex++;
  134. }
  135. return batchDeleteSql.ToString();
  136. }
  137. }
  138. public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
  139. {
  140. var resolveExpress = this.LambdaExpressions;
  141. this.LambdaExpressions.Clear();
  142. resolveExpress.MappingColumns = Context.MappingColumns;
  143. resolveExpress.MappingTables = Context.MappingTables;
  144. resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
  145. resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices?.SqlFuncServices;
  146. resolveExpress.InitMappingInfo = Context.InitMppingInfo;
  147. resolveExpress.RefreshMapping = () =>
  148. {
  149. resolveExpress.MappingColumns = Context.MappingColumns;
  150. resolveExpress.MappingTables = Context.MappingTables;
  151. resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
  152. resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices?.SqlFuncServices;
  153. };
  154. resolveExpress.Resolve(expression, resolveType);
  155. if (this.Parameters == null)
  156. this.Parameters = new List<SugarParameter>();
  157. this.Parameters.AddRange(resolveExpress.Parameters);
  158. var result = resolveExpress.Result;
  159. return result;
  160. }
  161. #endregion
  162. }
  163. }