Runner_Vitorm.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Vitorm;
  2. namespace App.OrmRunner
  3. {
  4. public partial class Runner_Vitorm : IRunner
  5. {
  6. RunConfig config;
  7. int? skip => config.skip;
  8. int take => config.take;
  9. bool executeQuery => config.executeQuery;
  10. IQueryable<User> userQuery;
  11. public IQueryable<User> GetQueryable() => userQuery;
  12. public void Run(RunConfig config)
  13. {
  14. this.config = config;
  15. for (int i = 0; i < config.repeatCount; i++)
  16. {
  17. userQuery = Data.Query<User>();
  18. if (config.queryJoin) QueryJoin();
  19. else Query();
  20. }
  21. }
  22. #region Executor
  23. int exceptUserId = 1;
  24. public void QueryJoin()
  25. {
  26. var userSet = GetQueryable();
  27. var minId = 1;
  28. var config = new { maxId = 10000 };
  29. var offsetId = 100;
  30. var query =
  31. from user in userSet
  32. from father in userSet.Where(father => user.fatherId == father.id).DefaultIfEmpty()
  33. from mother in userSet.Where(mother => user.motherId == mother.id).DefaultIfEmpty()
  34. where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  35. orderby user.id
  36. select new
  37. {
  38. user,
  39. father,
  40. mother,
  41. testId = user.id + offsetId,
  42. hasFather = father.name != null ? true : false
  43. }
  44. ;
  45. Execute(query);
  46. }
  47. public void Query()
  48. {
  49. var userSet = GetQueryable();
  50. var minId = 1;
  51. var config = new { maxId = 10000 };
  52. var query =
  53. from user in userSet
  54. where user.id > minId && user.id < config.maxId && user.id != exceptUserId
  55. orderby user.id
  56. select user;
  57. Execute(query);
  58. }
  59. #endregion
  60. public void Execute<Result>(IQueryable<Result> query)
  61. {
  62. if (skip > 0) query = query.Skip(skip.Value);
  63. query = query.Take(take);
  64. if (executeQuery)
  65. {
  66. var userList = query.ToList();
  67. var rowCount = userList.Count();
  68. if (rowCount != take) throw new Exception($"query failed, expected row count : {take} , actual count: {rowCount} ");
  69. }
  70. else
  71. {
  72. var sql = query.ToExecuteString();
  73. if (string.IsNullOrEmpty(sql)) throw new Exception($"query failed, can not generated sql script");
  74. }
  75. }
  76. // Entity Definition
  77. [System.ComponentModel.DataAnnotations.Schema.Table("User")]
  78. public class User
  79. {
  80. [System.ComponentModel.DataAnnotations.Key]
  81. public int id { get; set; }
  82. public string name { get; set; }
  83. public DateTime? birth { get; set; }
  84. public int? fatherId { get; set; }
  85. public int? motherId { get; set; }
  86. }
  87. }
  88. }