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.

91 lines
3.7 KiB

2 years ago
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. namespace SqlSugar
  5. {
  6. public class MySqlUpdateBuilder : UpdateBuilder
  7. {
  8. public override string SqlTemplateBatch
  9. {
  10. get
  11. {
  12. return @"UPDATE {1} S {2} INNER JOIN ${{0}} SET {0} ";
  13. }
  14. }
  15. public override string SqlTemplateJoin
  16. {
  17. get
  18. {
  19. return @" (
  20. {0}
  21. ) T ON {1}
  22. ";
  23. }
  24. }
  25. protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
  26. {
  27. Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
  28. var pageSize = 200;
  29. var pageIndex = 1;
  30. var totalRecord = groupList.Count;
  31. var pageCount = (totalRecord + pageSize - 1) / pageSize;
  32. var batchUpdateSql = new StringBuilder();
  33. while (pageCount >= pageIndex)
  34. {
  35. var updateTable = new StringBuilder();
  36. var setValues = string.Join(",", groupList.First().Where(it => !it.IsPrimarykey && (!it.IsIdentity || (IsOffIdentity && it.IsIdentity))).Select(it =>
  37. {
  38. if (SetValues.IsValuable())
  39. {
  40. var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName));
  41. if (setValue != null && setValue.Any())
  42. {
  43. return setValue.First().Value;
  44. }
  45. }
  46. var result = string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName));
  47. return result;
  48. }));
  49. batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString);
  50. var i = 0;
  51. foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
  52. {
  53. var isFirst = i == 0;
  54. if (!isFirst)
  55. {
  56. updateTable.Append(SqlTemplateBatchUnion);
  57. }
  58. updateTable.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value),this.Builder.GetTranslationColumnName(it.DbColumnName)))));
  59. ++i;
  60. }
  61. pageIndex++;
  62. updateTable.Append("\r\n");
  63. string whereString = null;
  64. if (this.WhereValues.HasValue())
  65. {
  66. foreach (var item in WhereValues)
  67. {
  68. var isFirst = whereString == null;
  69. whereString += (isFirst ? null : " AND ");
  70. whereString += item;
  71. }
  72. }
  73. else if (PrimaryKeys.HasValue())
  74. {
  75. foreach (var item in PrimaryKeys)
  76. {
  77. var isFirst = whereString == null;
  78. whereString += (isFirst ? null : " AND ");
  79. whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item));
  80. }
  81. }
  82. var format= string.Format(SqlTemplateJoin, updateTable, whereString);
  83. batchUpdateSql.Replace("${0}",format);
  84. batchUpdateSql.Append(";");
  85. }
  86. return batchUpdateSql.ToString();
  87. }
  88. }
  89. }