DataSource.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Vit.Core.Util.ConfigurationManager;
  2. using Vitorm.Sql;
  3. namespace Vitorm.MsTest
  4. {
  5. [System.ComponentModel.DataAnnotations.Schema.Table("User")]
  6. public class User
  7. {
  8. [System.ComponentModel.DataAnnotations.Key]
  9. [System.ComponentModel.DataAnnotations.Schema.Column("userId")]
  10. public int id { get; set; }
  11. [System.ComponentModel.DataAnnotations.Schema.Column("userName")]
  12. public string name { get; set; }
  13. [System.ComponentModel.DataAnnotations.Schema.Column("userBirth")]
  14. public DateTime? birth { get; set; }
  15. [System.ComponentModel.DataAnnotations.Schema.Column("userFatherId")]
  16. public int? fatherId { get; set; }
  17. [System.ComponentModel.DataAnnotations.Schema.Column("userMotherId")]
  18. public int? motherId { get; set; }
  19. [System.ComponentModel.DataAnnotations.Schema.Column("userClassId")]
  20. public int? classId { get; set; }
  21. public static User NewUser(int id, bool forAdd = false) => new User { id = id, name = "testUser" + id };
  22. public static List<User> NewUsers(int startId, int count = 1, bool forAdd = false)
  23. {
  24. return Enumerable.Range(startId, count).Select(id => NewUser(id, forAdd)).ToList();
  25. }
  26. }
  27. [System.ComponentModel.DataAnnotations.Schema.Table("UserClass")]
  28. public class UserClass
  29. {
  30. [System.ComponentModel.DataAnnotations.Key]
  31. [System.ComponentModel.DataAnnotations.Schema.Column("classId")]
  32. public int id { get; set; }
  33. [System.ComponentModel.DataAnnotations.Schema.Column("className")]
  34. public string name { get; set; }
  35. public static List<UserClass> NewClasses(int startId, int count = 1)
  36. {
  37. return Enumerable.Range(startId, count).Select(id => new UserClass { id = id, name = "class" + id }).ToList();
  38. }
  39. }
  40. public class DataSource
  41. {
  42. public static void WaitForUpdate() => Thread.Sleep(1000);
  43. readonly static string connectionString = Appsettings.json.GetStringByPath("Vitorm.ClickHouse.connectionString");
  44. public static SqlDbContext CreateDbContextForWriting(bool autoInit = true)
  45. {
  46. var dbContext = new SqlDbContext();
  47. dbContext.UseClickHouse(connectionString);
  48. dbContext.ChangeDatabase(dbContext.databaseName + "2");
  49. if (autoInit) InitDbContext(dbContext);
  50. return dbContext;
  51. }
  52. static bool initedDefaultIndex = false;
  53. public static SqlDbContext CreateDbContext()
  54. {
  55. var dbContext = new SqlDbContext();
  56. dbContext.UseClickHouse(connectionString);
  57. lock (typeof(DataSource))
  58. {
  59. if (!initedDefaultIndex)
  60. {
  61. InitDbContext(dbContext);
  62. initedDefaultIndex = true;
  63. }
  64. }
  65. return dbContext;
  66. }
  67. static void InitDbContext(SqlDbContext dbContext)
  68. {
  69. #region #1 init User
  70. {
  71. dbContext.TryDropTable<User>();
  72. dbContext.TryCreateTable<User>();
  73. var users = new List<User> {
  74. new User { id=1, name="u146", fatherId=4, motherId=6 },
  75. new User { id=2, name="u246", fatherId=4, motherId=6 },
  76. new User { id=3, name="u356", fatherId=5, motherId=6 },
  77. new User { id=4, name="u400" },
  78. new User { id=5, name="u500" },
  79. new User { id=6, name="u600" },
  80. };
  81. users.ForEach(user =>
  82. {
  83. user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
  84. user.classId = user.id % 2 + 1;
  85. });
  86. dbContext.AddRange(users);
  87. }
  88. #endregion
  89. #region #2 init Class
  90. {
  91. dbContext.TryDropTable<UserClass>();
  92. dbContext.TryCreateTable<UserClass>();
  93. dbContext.AddRange(UserClass.NewClasses(1, 6));
  94. }
  95. #endregion
  96. WaitForUpdate();
  97. }
  98. }
  99. }