DB.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace WCS.Core.DbHelper
  6. {
  7. /// <summary>
  8. /// DB,禁止跨上下文使用
  9. /// 1.异步情况: 在同一串await 中是一个上下文 (await 会改变线程和同步是不一样的)
  10. /// 2.同步情况: 在同一个线程是同一个上下文
  11. /// </summary>
  12. public class Db
  13. {
  14. /// <summary>
  15. /// 上下文集合
  16. /// </summary>
  17. private static readonly List<ContextList> _contexts = new List<ContextList>();
  18. /// <summary>
  19. /// 默认上下文类类型
  20. /// </summary>
  21. public static string DefaultDbContextType { get; private set; } = null!;
  22. /// <summary>
  23. /// 指定连接连接
  24. /// </summary>
  25. /// <typeparam name="T">业务代码</typeparam>
  26. /// <param name="func">业务逻辑</param>
  27. /// <param name="key">连接对应的Key</param>
  28. /// <returns></returns>
  29. /// <exception cref="Exception"></exception>
  30. public static T Do<T>(Func<Db, T> func, string key)
  31. {
  32. var db = new Db();
  33. db.Context(key).BeginTran();
  34. var res = func(db);
  35. db.Context(key).CommitTran();
  36. return res;
  37. }
  38. /// <summary>
  39. /// 使用设置好的默认链接
  40. /// </summary>
  41. /// <param name="act">执行内容</param>
  42. public static void Do(Action<Db> act)
  43. {
  44. Do(db =>
  45. {
  46. act(db);
  47. return 1;
  48. }, DefaultDbContextType);
  49. }
  50. /// <summary>
  51. /// 使用指定默认链接
  52. /// </summary>
  53. /// <param name="act">执行内容</param>
  54. /// <param name="key">连接对应的Key</param>
  55. public static void Do(Action<Db> act, string key)
  56. {
  57. Do(db =>
  58. {
  59. act(db);
  60. return 1;
  61. }, key);
  62. }
  63. /// <summary>
  64. /// 设置默认链接
  65. /// </summary>
  66. /// <param name="key"></param>
  67. public static void SetDefaultDbContextType(string key)
  68. {
  69. DefaultDbContextType = key;
  70. }
  71. /// <summary>
  72. /// 默认链接
  73. /// </summary>
  74. public SqlSugarScope Default
  75. {
  76. get
  77. {
  78. if (DefaultDbContextType == null)
  79. throw new Exception("请先设置默认数据库,调用静态方法SetDefaultDbContextType()");
  80. return Context(DefaultDbContextType);
  81. }
  82. }
  83. /// <summary>
  84. /// 创建一个数据库连接
  85. /// </summary>
  86. /// <param name="config">数据库配置信息</param>
  87. /// <param name="key">数据库Key</param>
  88. /// <returns></returns>
  89. public static SqlSugarScope CreateContext(ConnectionConfig config, string key)
  90. {
  91. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  92. if (ctx != null) return ctx.Client;
  93. ctx = new ContextList(key, new SqlSugarScope(new ConnectionConfig()
  94. {
  95. ConnectionString = config.ConnectionString,
  96. DbType = config.DbType,
  97. IsAutoCloseConnection = true
  98. }), config);
  99. _contexts.Add(ctx);
  100. return ctx.Client;
  101. }
  102. /// <summary>
  103. /// 根据Key获取指定数据库连接
  104. /// </summary>
  105. /// <param name="key">目标数据库在配置中的Key</param>
  106. /// <returns></returns>
  107. /// <exception cref="Exception"></exception>
  108. public SqlSugarScope Context(string key)
  109. {
  110. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  111. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  112. return ctx.Client;
  113. }
  114. }
  115. /// <summary>
  116. /// 链接
  117. /// </summary>
  118. public class ContextList
  119. {
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. /// <param name="key"></param>
  124. /// <param name="client"></param>
  125. /// <param name="connectionConfig"></param>
  126. public ContextList(string key, SqlSugarScope client, ConnectionConfig connectionConfig)
  127. {
  128. Key = key;
  129. Client = client;
  130. ConnectionConfig = connectionConfig;
  131. }
  132. /// <summary>
  133. ///
  134. /// </summary>
  135. public string Key { get; set; }
  136. /// <summary>
  137. ///
  138. /// </summary>
  139. public SqlSugarScope Client { get; set; }
  140. /// <summary>
  141. ///
  142. /// </summary>
  143. public ConnectionConfig ConnectionConfig { get; set; }
  144. }
  145. }