You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.4 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SqlSugar;
  7. using System.Linq.Expressions;
  8. using OrmTest.Models;
  9. namespace OrmTest.UnitTest
  10. {
  11. public class JoinQuery : UnitTestBase
  12. {
  13. private JoinQuery() { }
  14. public JoinQuery(int eachCount)
  15. {
  16. this.Count = eachCount;
  17. }
  18. internal void Init()
  19. {
  20. base.Begin();
  21. for (int i = 0; i < base.Count; i++)
  22. {
  23. Q1();
  24. Q2();
  25. Q3();
  26. Q4();
  27. q5();
  28. q6();
  29. q7();
  30. }
  31. base.End("Method Test");
  32. }
  33. private void q6()
  34. {
  35. using (var db = GetInstance())
  36. {
  37. var join6 = db.Queryable<Student, School>((st, sc) => new object[] {
  38. JoinType.Left,st.SchoolId==sc.Id
  39. }).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id) }).ToSql();
  40. string sql = @"SELECT [st].[Name] AS [Name] , MIN([sc].[Id]) AS [SchoolId] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) ";
  41. base.Check(sql, null, join6.Key, null, "join 6 Error");
  42. }
  43. }
  44. private void q7()
  45. {
  46. using (var db = GetInstance())
  47. {
  48. var join7 = db.Queryable<Student, School>((st, sc) => new object[] {
  49. JoinType.Left,st.SchoolId==sc.Id
  50. }).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id*1) }).ToSql();
  51. string sql = @"SELECT [st].[Name] AS [Name] , MIN(( [sc].[Id] * @Id0 )) AS [SchoolId] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) ";
  52. base.Check(sql, new List<SugarParameter>() {
  53. new SugarParameter("@Id0",1)
  54. }, join7.Key, join7.Value, "join 7 Error");
  55. }
  56. }
  57. private void q5()
  58. {
  59. using (var db = GetInstance())
  60. {
  61. db.MappingTables.Add("School", "SchoolTable");
  62. var join5= db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st => st)
  63. .GroupBy(st=> new{ st.Id,st.Name })
  64. .ToSql();
  65. string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] )GROUP BY [st].[ID],[st].[Name] ";
  66. base.Check(sql, null, join5.Key, null, "join 5 Error");
  67. }
  68. }
  69. private void Q4()
  70. {
  71. using (var db = GetInstance())
  72. {
  73. db.MappingTables.Add("School", "SchoolTable");
  74. var join4 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st=>st).ToSql();
  75. string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] ) ";
  76. base.Check(sql, null, join4.Key, null, "join 4 Error");
  77. }
  78. }
  79. private void Q3()
  80. {
  81. using (var db = GetInstance())
  82. {
  83. var join3 = db.Queryable("Student", "st")
  84. .AddJoinInfo("School", "sh", "sh.id=st.schoolid")
  85. .Where("st.id>@id")
  86. .AddParameters(new { id = 1 })
  87. .Select("st.*").ToSql();
  88. string sql = @"SELECT st.* FROM [Student] st Left JOIN [School] sh ON sh.id=st.schoolid WHERE st.id>@id ";
  89. base.Check(sql,new List<SugarParameter>() {new SugarParameter("@id",1)}, join3.Key, join3.Value, "join 3 Error");
  90. }
  91. }
  92. public void Q1()
  93. {
  94. using (var db = GetInstance())
  95. {
  96. var join1 = db.Queryable<Student, School>((st, sc) => new object[] {
  97. JoinType.Left,st.SchoolId==sc.Id
  98. }).Where(st => st.Id > 0).Select<Student>("*").ToSql();
  99. base.Check(@"SELECT * FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) ",
  100. new List<SugarParameter>() {
  101. new SugarParameter("@Id0",0)
  102. }, join1.Key, join1.Value, "join 1 Error");
  103. }
  104. }
  105. public void Q2()
  106. {
  107. using (var db = GetInstance())
  108. {
  109. var join2 = db.Queryable<Student, School>((st, sc) => new object[] {
  110. JoinType.Left,st.SchoolId==sc.Id
  111. }).Where(st => st.Id > 2).Select<Student>("*").ToSql();
  112. base.Check(@"SELECT * FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) ",
  113. new List<SugarParameter>() {
  114. new SugarParameter("@Id0",2)
  115. }, join2.Key, join2.Value, "join 2 Error");
  116. }
  117. }
  118. public new SqlSugarClient GetInstance()
  119. {
  120. SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
  121. db.Ado.IsEnableLogEvent = true;
  122. db.Ado.LogEventStarting = (sql, pars) =>
  123. {
  124. Console.WriteLine(sql + " " + pars);
  125. };
  126. return db;
  127. }
  128. }
  129. }