Runner_SqlSugar.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using SqlSugar;
  2. using Vit.Core.Util.ConfigurationManager;
  3. namespace App.OrmRunner
  4. {
  5. public class Runner_SqlSuger : IRunner
  6. {
  7. RunConfig config;
  8. int? skip => config.skip;
  9. int? take => config.take;
  10. bool executeQuery => config.executeQuery;
  11. static string connectionString = Appsettings.json.GetStringByPath("Vitorm.Data[0].connectionString");
  12. SqlSugarClient db;
  13. public string sql;
  14. public void Run(RunConfig config)
  15. {
  16. this.config = config;
  17. for (int i = 0; i < config.repeatCount; i++)
  18. {
  19. Action<SqlSugarClient> configAction;
  20. if (executeQuery)
  21. {
  22. configAction = db => { };
  23. }
  24. else
  25. {
  26. configAction = db =>
  27. {
  28. db.Aop.OnLogExecuting = (sql, pars) =>
  29. {
  30. //Console.WriteLine(sql); //输出sql,查看执行sql 性能无影响
  31. //获取原生SQL推荐 5.1.4.63 性能OK
  32. var nativeSql = UtilMethods.GetNativeSql(sql, pars);
  33. //Console.WriteLine(nativeSql);
  34. sql = nativeSql;
  35. //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用
  36. //var sqlString = UtilMethods.GetSqlString(DbType.SqlServer, sql, pars);
  37. //Console.WriteLine(sqlString);
  38. };
  39. };
  40. }
  41. using SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
  42. {
  43. DbType = DbType.Sqlite,
  44. ConnectionString = connectionString,
  45. IsAutoCloseConnection = true,
  46. }, configAction);
  47. this.db = db;
  48. if (config.queryJoin) QueryJoin();
  49. else Query();
  50. }
  51. }
  52. #region Executor
  53. int exceptUserId = 1;
  54. public void QueryJoin()
  55. {
  56. var minId = 1;
  57. var config = new { maxId = 10000 };
  58. var offsetId = 100;
  59. //var query =
  60. // from user in userSet
  61. // from father in userSet.Where(father => user.fatherId == father.id).DefaultIfEmpty()
  62. // from mother in userSet.Where(mother => user.motherId == mother.id).DefaultIfEmpty()
  63. // where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  64. // orderby user.id
  65. // select new
  66. // {
  67. // user,
  68. // father,
  69. // mother,
  70. // testId = user.id + offsetId,
  71. // hasFather = father.name != null ? true : false
  72. // }
  73. // ;
  74. var query = db.Queryable<User>().LeftJoin<User>((user, father) => user.fatherId == father.id)
  75. .LeftJoin<User>((user, father, mother) => user.fatherId == mother.id)
  76. .Where((user, father, mother) => user.id > minId && user.id < config.maxId && user.id != exceptUserId)
  77. .OrderBy((user, father, mother) => user.id, OrderByType.Asc)
  78. .Select((user, father, mother) =>
  79. new
  80. {
  81. user,
  82. father,
  83. mother,
  84. testId = user.id + offsetId,
  85. hasFather = father.name != null ? true : false
  86. });
  87. Execute(query);
  88. }
  89. public void Query()
  90. {
  91. var minId = 1;
  92. var config = new { maxId = 10000 };
  93. //var query =
  94. // from user in userSet
  95. // where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  96. // orderby user.id
  97. // select user;
  98. var query = db.Queryable<User>().Where(user => user.id > minId && user.id < config.maxId && user.id != exceptUserId).OrderBy(user => user.id, OrderByType.Asc);
  99. Execute(query);
  100. }
  101. #endregion
  102. public void Execute<Result>(ISugarQueryable<Result> query)
  103. {
  104. if (skip.HasValue) query = query.Skip(skip.Value);
  105. if (take.HasValue) query = query.Take(take.Value);
  106. if (executeQuery)
  107. {
  108. var userList = query.ToList();
  109. var rowCount = userList.Count();
  110. if (rowCount != take) throw new Exception($"query failed, expected row count : {take} , actual count: {rowCount} ");
  111. }
  112. else
  113. {
  114. sql = null;
  115. var count = query.Count();
  116. //query.Single();
  117. //if (string.IsNullOrEmpty(sql))
  118. // throw new Exception($"query failed, can not generated sql script");
  119. }
  120. }
  121. // Entity Definition
  122. [SugarTable("User")]
  123. public class User
  124. {
  125. [SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
  126. public int id { get; set; }
  127. public string name { get; set; }
  128. public DateTime? birth { get; set; }
  129. public int? fatherId { get; set; }
  130. public int? motherId { get; set; }
  131. }
  132. }
  133. }