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.

188 lines
9.4 KiB

2 years ago
  1. using OrmTest.Models;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OrmTest.UnitTest
  9. {
  10. public class Update : UnitTestBase
  11. {
  12. private Update() { }
  13. public Update(int eachCount)
  14. {
  15. this.Count = eachCount;
  16. }
  17. public void Init()
  18. {
  19. var db = GetInstance();
  20. var updateObj = new Student() { Id = 1, Name = "jack",SchoolId=0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") };
  21. var updateObjs = new List<Student>() { updateObj,new Student() { Id=2,Name="sun",SchoolId=0 } }.ToArray();
  22. db.IgnoreColumns.Add("TestId", "Student");
  23. //db.MappingColumns.Add("id","dbid", "Student");
  24. var t1 = db.Updateable(updateObj).ToSql();
  25. base.Check(@"UPDATE [STudent] SET
  26. [SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
  27. new SugarParameter("@SchoolId",0),
  28. new SugarParameter("@ID",1),
  29. new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
  30. new SugarParameter("@Name", "jack")
  31. }, t1.Key, t1.Value,"Update t1 error");
  32. //update reutrn Command Count
  33. var t2 = db.Updateable(updateObj).ExecuteCommand();
  34. db.IgnoreColumns = null;
  35. //Only update Name
  36. var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ToSql();
  37. base.Check(@"UPDATE [STudent] SET
  38. [Name]=@Name WHERE [Id]=@Id", new List<SugarParameter>() {
  39. new SugarParameter("@ID",1),
  40. new SugarParameter("@Name", "jack")
  41. }, t3.Key, t3.Value, "Update t3 error");
  42. //Ignore Name and TestId
  43. var t4 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
  44. base.Check(@"UPDATE [STudent] SET
  45. [SchoolId]=@SchoolId,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
  46. new SugarParameter("@CreateTime",Convert.ToDateTime("2017-05-21 09:56:12.610")),
  47. new SugarParameter("@SchoolId", 0),
  48. new SugarParameter("@ID",1),
  49. }, t4.Key, t4.Value, "Update t4 error");
  50. //Ignore Name and TestId
  51. var t5 = db.Updateable(updateObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
  52. base.Check(@"UPDATE [STudent] WITH(UPDLOCK) SET
  53. [SchoolId]=@SchoolId,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
  54. new SugarParameter("@CreateTime",Convert.ToDateTime("2017-05-21 09:56:12.610")),
  55. new SugarParameter("@SchoolId", 0),
  56. new SugarParameter("@ID",1),
  57. }, t5.Key, t5.Value, "Update t5 error");
  58. //Use Lock
  59. var t6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ToSql();
  60. base.Check(@"UPDATE [STudent] WITH(UPDLOCK) SET
  61. [SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE [Id]=@Id", new List<SugarParameter>() {
  62. new SugarParameter("@SchoolId",0),
  63. new SugarParameter("@ID",1),
  64. new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
  65. new SugarParameter("@Name", "jack")
  66. }, t6.Key, t6.Value, "Update t6 error");
  67. // //update List<T>
  68. // var t7 = db.Updateable(updateObjs).With(SqlWith.UpdLock).ToSql();
  69. // base.Check(@"UPDATE S SET S.[SchoolId]=T.[SchoolId],S.[Name]=T.[Name],S.[CreateTime]=T.[CreateTime] FROM [STudent] S WITH(UPDLOCK) INNER JOIN (
  70. // SELECT N'1' AS ID,N'0' AS SchoolId,N'jack' AS Name,'2017-05-21 09:56:12.610' AS CreateTime
  71. //UNION ALL
  72. // SELECT N'2' AS ID,N'0' AS SchoolId,N'sun' AS Name,NULL AS CreateTime
  73. // ) T ON S.[Id]=T.[Id]
  74. // ; ", null, t7.Key, null,"Update t7 error");
  75. //Re Set Value
  76. var t8 = db.Updateable(updateObj)
  77. .ReSetValue(it=>it.Name==(it.Name+1)).ToSql();
  78. base.Check(@"UPDATE [STudent] SET
  79. [SchoolId]=@SchoolId, [Name] =( [Name] + @Const0 ),[CreateTime]=@CreateTime WHERE [Id]=@Id",
  80. new List<SugarParameter>() {
  81. new SugarParameter("@SchoolId",0),
  82. new SugarParameter("@ID",1),
  83. new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
  84. new SugarParameter("@Const0", 1)
  85. }, t8.Key, t8.Value, "Update t8 error"
  86. );
  87. //Where By Expression
  88. var t9 = db.Updateable(updateObj)
  89. .Where(it => it.Id==1).ToSql();
  90. base.Check(@"UPDATE [STudent] SET
  91. [SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE ( [ID] = @Id0 )",
  92. new List<SugarParameter>() {
  93. new SugarParameter("@SchoolId",0),
  94. new SugarParameter("@ID",1),
  95. new SugarParameter("@Id0",1),
  96. new SugarParameter("@CreateTime", Convert.ToDateTime("2017-05-21 09:56:12.610")),
  97. new SugarParameter("@Name", "jack") },t9.Key,t9.Value,"Upate t9 error"
  98. );
  99. updateObj.SchoolId = 18;
  100. string name = "x";
  101. var t10 = db.Updateable<Student>().UpdateColumns(it => new Student() { Name =name, SchoolId=updateObj.SchoolId }).Where(it=>it.Id==11).ToSql();
  102. base.Check(@"UPDATE [STudent] SET
  103. [SchoolId] = @Const1 , [Name] = @Const0 WHERE ( [ID] = @Id2 )", new List<SugarParameter>() {
  104. new SugarParameter("@Const1",18),
  105. new SugarParameter("@Const0","x"),
  106. new SugarParameter("@Id2",11)},
  107. t10.Key,
  108. t10.Value,
  109. "Update 10 error"
  110. );
  111. var t11 = db.Updateable<DataTestInfo>().UpdateColumns(it => new DataTestInfo() { Datetime1=DateTime.MaxValue }).Where(it => it.Int1 == 11).ToSql();
  112. base.Check(@"UPDATE [DataTestInfo] SET
  113. [Datetime1] = @constant0 WHERE ( [Int1] = @Int11 )", new List<SugarParameter>() {
  114. new SugarParameter("@Int11",11),
  115. new SugarParameter("@constant0",DateTime.MaxValue) },
  116. t11.Key,
  117. t11.Value,
  118. "Update 11 error"
  119. );
  120. var t12 = db.Updateable<DataTestInfo>().UpdateColumns(it => new DataTestInfo() { Int2 = it.Int2+1 }).Where(it => it.Int1 == 11).ToSql();
  121. base.Check(@"UPDATE [DataTestInfo] SET
  122. [Int2] = ( [Int2] + @Const0 ) WHERE ( [Int1] = @Int11 )", new List<SugarParameter>() {
  123. new SugarParameter("@Int11",11),
  124. new SugarParameter("@Const0",1) },
  125. t12.Key,
  126. t12.Value,
  127. "Update 12 error"
  128. );
  129. var t13 = db.Updateable<Student>(new { Name = "a", id=1 }).ToSql();
  130. base.Check(@"UPDATE [STudent] SET
  131. [Name]=@Name WHERE [Id]=@Id", new List<SugarParameter>() {
  132. new SugarParameter("@Name","a"),
  133. new SugarParameter("@ID",1)
  134. }, t13.Key, t13.Value, "Update t13 error");
  135. var t14 = db.Updateable<Student>(new Dictionary<string, object>() { { "id", 0 }, { "name", "2" } }).ToSql();
  136. base.Check(@"UPDATE [STudent] SET
  137. [Name]=@Name WHERE [Id]=@Id", new List<SugarParameter>() {
  138. new SugarParameter("@Name", "2"),
  139. new SugarParameter("@ID", 0)
  140. }, t14.Key, t14.Value, "Update t14 error");
  141. var t15 = db.Updateable(new StudentTest() { Id = 1, Name = "1" }).AS("student").ToSql();
  142. base.Check(@"UPDATE [student] SET
  143. [SchoolId]=@SchoolId,[Name]=@Name,[CreateTime]=@CreateTime WHERE [Id]=@Id", null, t15.Key, null, "Update t15 error");
  144. var t16= db.Updateable<Student>().UpdateColumns(it => new Student()
  145. {
  146. SchoolId = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.SchoolId).Select(s => s.Id),
  147. Name = "newname"
  148. }).Where(it => it.Id == 1).ToSql();
  149. var t17 = db.Updateable<Student>().UpdateColumns(it => new Student()
  150. {
  151. SchoolId = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.SchoolId).Select(s => s.Id),
  152. Name = "newname"
  153. }).Where(it => it.Id == 1).ToSql();
  154. base.Check(@"UPDATE [STudent] SET
  155. [SchoolId] = (SELECT TOP 1 [Id] FROM [School] WHERE ( [Id] =[STudent].[SchoolId] )) , [Name] = @Const0 WHERE ( [ID] = @Id1 )", new List<SugarParameter>() {
  156. new SugarParameter("@Const0","newname"),
  157. new SugarParameter("@Id1","1")
  158. }, t17.Key, t17.Value, "Update t17 error");
  159. }
  160. }
  161. }