SplitTableContext.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace SqlSugar
  9. {
  10. public class SplitTableContextResult<T>
  11. {
  12. public List<T> Items { get; set; }
  13. public SplitTableContext Helper { get; set; }
  14. public string GetTableName()
  15. {
  16. return GetTableNames().First();
  17. }
  18. public string [] GetTableNames()
  19. {
  20. List<string> result = new List<string>();
  21. var attribute = typeof(T).GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;
  22. Check.Exception(attribute == null, $" {typeof(T).Name} need SplitTableAttribute");
  23. foreach (var item in Items)
  24. {
  25. result.Add(Helper.GetTableName(Helper.GetValue(attribute.SplitType,item)));
  26. }
  27. return result.Distinct().ToArray();
  28. }
  29. public string[] GetTableNames(SplitType splitType)
  30. {
  31. List<string> result = new List<string>();
  32. foreach (var item in Items)
  33. {
  34. result.Add(Helper.GetTableName(Helper.GetValue(splitType, item)));
  35. }
  36. return result.ToArray();
  37. }
  38. }
  39. public class SplitTableContext
  40. {
  41. internal SqlSugarProvider Context { get; set; }
  42. internal EntityInfo EntityInfo { get; set; }
  43. internal ISplitTableService Service { get; set; }
  44. private SplitTableContext() { }
  45. internal SplitTableContext(SqlSugarProvider context)
  46. {
  47. this.Context = context;
  48. if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null&&this.Context.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService!=null)
  49. {
  50. Service = this.Context.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService;
  51. }
  52. else
  53. {
  54. Service = new DateSplitTableService();
  55. }
  56. }
  57. public List<SplitTableInfo> GetTables()
  58. {
  59. if (StaticConfig.SplitTableGetTablesFunc != null)
  60. {
  61. return StaticConfig.SplitTableGetTablesFunc();
  62. }
  63. var oldIsEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
  64. this.Context.Ado.IsEnableLogEvent = false;
  65. List<DbTableInfo> tableInfos =((DbMaintenanceProvider)this.Context.DbMaintenance).GetSchemaTables(EntityInfo);
  66. if (tableInfos == null)
  67. {
  68. tableInfos = this.Context.DbMaintenance.GetTableInfoList(false);
  69. }
  70. List<SplitTableInfo> result = Service.GetAllTables(this.Context, EntityInfo, tableInfos);
  71. this.Context.Ado.IsEnableLogEvent = oldIsEnableLogEvent;
  72. return result;
  73. }
  74. public string GetDefaultTableName()
  75. {
  76. return Service.GetTableName(this.Context,EntityInfo);
  77. }
  78. public string GetTableName(SplitType splitType)
  79. {
  80. return Service.GetTableName(this.Context,EntityInfo, splitType);
  81. }
  82. public string GetTableName(SplitType splitType, object fieldValue)
  83. {
  84. return Service.GetTableName(this.Context,EntityInfo, splitType, fieldValue);
  85. }
  86. public string GetTableName(object fieldValue)
  87. {
  88. var attribute = EntityInfo.Type.GetCustomAttribute<SplitTableAttribute>() as SplitTableAttribute;
  89. Check.Exception(attribute == null, $" {EntityInfo.EntityName} need SplitTableAttribute");
  90. return Service.GetTableName(this.Context, EntityInfo, attribute.SplitType, fieldValue);
  91. }
  92. public object GetValue(SplitType splitType, object entityValue)
  93. {
  94. return Service.GetFieldValue(this.Context,EntityInfo, splitType, entityValue);
  95. }
  96. internal void CheckPrimaryKey()
  97. {
  98. Check.Exception(EntityInfo.Columns.Any(it => it.IsIdentity == true), ErrorMessage.GetThrowMessage("Split table can't IsIdentity=true", "分表禁止使用自增列"));
  99. }
  100. }
  101. }