SqlExtension.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using SqlSugar;
  2. namespace ServiceCenter.Extensions
  3. {
  4. public static class SqlExtension
  5. {
  6. /// <summary>
  7. /// 不添加共享锁和排它锁,当这个选项生效后,可能读到未提交读的数据或“脏数据”,这个选项仅仅应用于SELECT语句
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. /// <param name="obj"></param>
  11. /// <returns></returns>
  12. public static ISugarQueryable<T> NoLock<T>(this ISugarQueryable<T> obj)
  13. {
  14. return obj.With(SqlWith.NoLock);
  15. }
  16. /// <summary>
  17. /// 跳过已经加锁的数据行,这个选项将使事务读取数据时跳过那些已经被其他事务锁定的数据行,而不是阻塞直到其他事务释放锁,ReadPast仅仅应用于READ COMMITTED隔离性级别下事务操作中的SELECT语句操作。
  18. /// </summary>
  19. /// <typeparam name="T"></typeparam>
  20. /// <param name="obj"></param>
  21. /// <returns></returns>
  22. public static ISugarQueryable<T> ReadPast<T>(this ISugarQueryable<T> obj)
  23. {
  24. return obj.With(SqlWith.ReadPast);
  25. }
  26. /// <summary>
  27. /// 指定在读表中数据时设置更新锁(update lock)而不是设置共享锁,该锁一直保持到这个语句或整个事务结束,使用UpdLock的作用是允许用户先读取数据(而且不阻塞其他用户读数据),并且保证在后来再更新数据时,这一段时间内这些数据没有被其他用户修改。
  28. /// 默认持有行级更新锁
  29. /// </summary>
  30. /// <typeparam name="T"></typeparam>
  31. /// <param name="obj"></param>
  32. /// <returns></returns>
  33. public static ISugarQueryable<T> UpdLock1<T>(this ISugarQueryable<T> obj)
  34. {
  35. return obj.With("WITH(ROWLOCK,UPDLOCK)");
  36. }
  37. /// <summary>
  38. /// 使用行级锁,而不使用粒度更粗的页级锁和表级锁。
  39. /// </summary>
  40. /// <typeparam name="T"></typeparam>
  41. /// <param name="obj"></param>
  42. /// <returns></returns>
  43. public static ISugarQueryable<T> RowLock<T>(this ISugarQueryable<T> obj)
  44. {
  45. return obj.With(SqlWith.RowLock);
  46. }
  47. /// <summary>
  48. /// 更新时使用行级锁
  49. /// </summary>
  50. /// <typeparam name="T"></typeparam>
  51. /// <param name="scope"></param>
  52. /// <param name="updateObj"></param>
  53. /// <returns></returns>
  54. public static IUpdateable<T> UpdateableRowLock<T>(this SqlSugarScopeProvider scope, T updateObj) where T : class, new() => scope.ScopedContext.Updateable<T>(updateObj).With(SqlWith.RowLock);
  55. /// <summary>
  56. /// 更新时使用行级锁
  57. /// </summary>
  58. /// <typeparam name="T"></typeparam>
  59. /// <param name="scope"></param>
  60. /// <param name="updateObj"></param>
  61. /// <returns></returns>
  62. public static IUpdateable<T> UpdateableRowLock<T>(this SqlSugarScopeProvider scope, List<T> updateObj) where T : class, new() => scope.ScopedContext.Updateable<T>(updateObj).With(SqlWith.RowLock);
  63. /// <summary>
  64. /// 插入时使用行级锁
  65. /// </summary>
  66. /// <typeparam name="T"></typeparam>
  67. /// <param name="scope"></param>
  68. /// <param name="updateObj"></param>
  69. /// <returns></returns>
  70. public static IInsertable<T> InsertableRowLock<T>(this SqlSugarScopeProvider scope, T insertObj) where T : class, new() => scope.ScopedContext.Insertable<T>(insertObj).With(SqlWith.RowLock);
  71. /// <summary>
  72. /// 插入时使用行级锁
  73. /// </summary>
  74. /// <typeparam name="T"></typeparam>
  75. /// <param name="scope"></param>
  76. /// <param name="updateObj"></param>
  77. /// <returns></returns>
  78. public static IInsertable<T> InsertableRowLock<T>(this SqlSugarScopeProvider scope, List<T> insertObj) where T : class, new() => scope.ScopedContext.Insertable<T>(insertObj).With(SqlWith.RowLock);
  79. /// <summary>
  80. /// 删除时使用行级锁
  81. /// </summary>
  82. /// <typeparam name="T"></typeparam>
  83. /// <param name="scope"></param>
  84. /// <param name="deleteObj"></param>
  85. /// <returns></returns>
  86. public static IDeleteable<T> DeleteableRowLock<T>(this SqlSugarScopeProvider scope, T deleteObj) where T : class, new() => scope.ScopedContext.Deleteable<T>(deleteObj).With(SqlWith.RowLock);
  87. /// <summary>
  88. /// 删除时使用行级锁
  89. /// </summary>
  90. /// <typeparam name="T"></typeparam>
  91. /// <param name="scope"></param>
  92. /// <param name="deleteObj"></param>
  93. /// <returns></returns>
  94. public static IDeleteable<T> DeleteableRowLock<T>(this SqlSugarScopeProvider scope, List<T> deleteObj) where T : class, new() => scope.ScopedContext.Deleteable<T>(deleteObj).With(SqlWith.RowLock);
  95. }
  96. }