Runner_SqlSugar.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using App.OrmRunner.SqlSugerRunner;
  2. using SqlSugar;
  3. using Vit.Core.Util.ConfigurationManager;
  4. namespace App.OrmRunner
  5. {
  6. public partial class Runner_SqlSuger : IRunner
  7. {
  8. static string provider = Appsettings.json.GetStringByPath("Vitorm.Data[0].provider");
  9. static string connectionString = Appsettings.json.GetStringByPath("Vitorm.Data[0].connectionString");
  10. public void Run(RunConfig config)
  11. {
  12. for (int i = 0; i < config.repeatCount; i++)
  13. {
  14. Action<SqlSugarClient> configAction;
  15. if (config.executeQuery)
  16. {
  17. configAction = db => { };
  18. }
  19. else
  20. {
  21. configAction = db =>
  22. {
  23. db.Aop.OnLogExecuting = (sql, pars) =>
  24. {
  25. //Console.WriteLine(sql);
  26. var nativeSql = UtilMethods.GetNativeSql(sql, pars);
  27. //Console.WriteLine(nativeSql);
  28. sql = nativeSql;
  29. //var sqlString = UtilMethods.GetSqlString(DbType.SqlServer, sql, pars);
  30. //Console.WriteLine(sqlString);
  31. };
  32. };
  33. }
  34. using SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
  35. {
  36. DbType = provider switch { "Sqlite" => DbType.Sqlite, "MySql" => DbType.MySql, "SqlServer" => DbType.SqlServer },
  37. ConnectionString = connectionString,
  38. IsAutoCloseConnection = true,
  39. }, configAction);
  40. if (config.queryJoin) QueryExecute.QueryJoin(db, config);
  41. else QueryExecute.Query(db, config);
  42. }
  43. }
  44. }
  45. }
  46. namespace App.OrmRunner.SqlSugerRunner
  47. {
  48. // Entity Definition
  49. [SugarTable("User")]
  50. public class User
  51. {
  52. [SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
  53. public int id { get; set; }
  54. public string name { get; set; }
  55. public DateTime? birth { get; set; }
  56. public int? fatherId { get; set; }
  57. public int? motherId { get; set; }
  58. }
  59. public class QueryExecute
  60. {
  61. public static void QueryJoin(SqlSugarClient db, RunConfig config)
  62. {
  63. var query = db.Queryable<User>().LeftJoin<User>((user, father) => user.fatherId == father.id)
  64. .LeftJoin<User>((user, father, mother) => user.fatherId == mother.id)
  65. .Where((user, father, mother) => user.id > 1 && user.id < 10000)
  66. .OrderBy((user, father, mother) => user.id, OrderByType.Asc)
  67. .Select((user, father, mother) =>
  68. new
  69. {
  70. user,
  71. father,
  72. mother,
  73. testId = user.id + 100,
  74. hasFather = father.name != null ? true : false
  75. });
  76. Execute(query, config);
  77. }
  78. public static void Query(SqlSugarClient db, RunConfig config)
  79. {
  80. var query = db.Queryable<User>().Where(user => user.id > 1 && user.id < 10000).OrderBy(user => user.id, OrderByType.Asc);
  81. Execute(query, config);
  82. }
  83. public static void Execute<Result>(ISugarQueryable<Result> query, RunConfig config)
  84. {
  85. if (config.skip > 0) query = query.Skip(config.skip.Value);
  86. query = query.Take(config.take);
  87. if (config.executeQuery)
  88. {
  89. var userList = query.ToList();
  90. var rowCount = userList.Count();
  91. if (rowCount != config.take) throw new Exception($"query failed, expected row count : {config.take} , actual count: {rowCount} ");
  92. }
  93. else
  94. {
  95. var count = query.Count();
  96. //query.Single();
  97. //if (string.IsNullOrEmpty(sql))
  98. // throw new Exception($"query failed, can not generated sql script");
  99. }
  100. }
  101. }
  102. }