DataSource.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Vitorm.Sql;
  2. using Vit.Extensions;
  3. using Vit.Core.Util.ConfigurationManager;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using ClickHouse.Client.ADO;
  6. namespace Vitorm.MsTest
  7. {
  8. [System.ComponentModel.DataAnnotations.Schema.Table("User")]
  9. public class User
  10. {
  11. [System.ComponentModel.DataAnnotations.Key]
  12. public int id { get; set; }
  13. public string name { get; set; }
  14. public DateTime? birth { get; set; }
  15. public int? fatherId { get; set; }
  16. public int? motherId { get; set; }
  17. public static User NewUser(int id) => new User { id = id, name = "testUser" + id };
  18. public static List<User> NewUsers(int startId, int count = 1)
  19. {
  20. return Enumerable.Range(startId, count).Select(NewUser).ToList();
  21. }
  22. }
  23. public class DataSource
  24. {
  25. readonly static string connectionString = Appsettings.json.GetStringByPath("App.Db.ConnectionString");
  26. static int dbIndexCount = 0;
  27. public static SqlDbContext CreateDbContextForWriting()
  28. {
  29. dbIndexCount++;
  30. var dbName = "dev-orm" + dbIndexCount;
  31. var connectionString = DataSource.connectionString;
  32. // #1 create db
  33. {
  34. var dbContext = new SqlDbContext();
  35. dbContext.UseClickHouse(connectionString);
  36. dbContext.Execute(sql: $"create database if not exists `{dbName}`; ");
  37. }
  38. // #2
  39. {
  40. ClickHouseConnectionStringBuilder builder = new ClickHouseConnectionStringBuilder(connectionString);
  41. builder.Database = dbName;
  42. connectionString = builder.ToString();
  43. var dbContext = new SqlDbContext();
  44. dbContext.UseClickHouse(connectionString);
  45. dbContext.Execute(sql: "DROP TABLE if exists `User`;");
  46. dbContext.Create<User>();
  47. InitDbContext(dbContext);
  48. return dbContext;
  49. }
  50. }
  51. static bool initedDefaultIndex = false;
  52. public static SqlDbContext CreateDbContext()
  53. {
  54. var dbContext = new SqlDbContext();
  55. dbContext.UseClickHouse(connectionString);
  56. lock (typeof(DataSource))
  57. {
  58. if (!initedDefaultIndex)
  59. {
  60. //dbContext.Execute(sql: "truncate TABLE `User`;");
  61. dbContext.Execute(sql: "DROP TABLE if exists `User`;");
  62. dbContext.Create<User>();
  63. InitDbContext(dbContext);
  64. initedDefaultIndex = true;
  65. }
  66. }
  67. return dbContext;
  68. }
  69. static void InitDbContext(SqlDbContext dbContext)
  70. {
  71. var users = new List<User> {
  72. new User { id=1, name="u146", fatherId=4, motherId=6 },
  73. new User { id=2, name="u246", fatherId=4, motherId=6 },
  74. new User { id=3, name="u356", fatherId=5, motherId=6 },
  75. new User { id=4, name="u400" },
  76. new User { id=5, name="u500" },
  77. new User { id=6, name="u600" },
  78. };
  79. users.ForEach(user => { user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id); });
  80. dbContext.AddRange(users);
  81. }
  82. }
  83. }