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.

152 lines
5.7 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 Insert : UnitTestBase
  11. {
  12. private Insert() { }
  13. public Insert(int eachCount)
  14. {
  15. this.Count = eachCount;
  16. }
  17. public void Init()
  18. {
  19. var db = GetInstance();
  20. var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId=0 };
  21. db.IgnoreColumns.Add("TestId", "Student");
  22. //db.MappingColumns.Add("id","dbid", "Student");
  23. var t1 = db.Insertable(insertObj).ToSql();
  24. base.Check(@"INSERT INTO [STudent]
  25. ([SchoolId],[Name],[CreateTime])
  26. VALUES
  27. (@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();",
  28. new List<SugarParameter>() {
  29. new SugarParameter("@SchoolId",0),
  30. new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
  31. new SugarParameter("@Name","jack")
  32. }, t1.Key, t1.Value, "Insert t1 error"
  33. );
  34. //Insert reutrn Command Count
  35. var t2 = db.Insertable(insertObj).ExecuteReturnEntity();
  36. db.IgnoreColumns = null;
  37. //Only insert Name
  38. var t3 = db.Insertable(insertObj).InsertColumns(it => new { it.Name }).ToSql();
  39. base.Check(@"INSERT INTO [STudent]
  40. ([Name])
  41. VALUES
  42. (@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
  43. new SugarParameter("@Name","jack")
  44. }, t3.Key, t3.Value, "Insert t3 error");
  45. //Ignore Name and TestId
  46. var t4 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ToSql();
  47. base.Check(@"INSERT INTO [STudent]
  48. ([SchoolId],[CreateTime])
  49. VALUES
  50. (@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
  51. new List<SugarParameter>() {
  52. new SugarParameter("@SchoolId",0),
  53. new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
  54. }, t4.Key, t4.Value, "Insert t4 error"
  55. );
  56. //Ignore Name and TestId
  57. var t5 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ToSql();
  58. base.Check(@"INSERT INTO [STudent] WITH(UPDLOCK)
  59. ([SchoolId],[CreateTime])
  60. VALUES
  61. (@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
  62. new List<SugarParameter>() {
  63. new SugarParameter("@SchoolId",0),
  64. new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
  65. }, t5.Key, t5.Value, "Insert t5 error"
  66. );
  67. //Use Lock
  68. var t6 = db.Insertable(insertObj).With(SqlWith.UpdLock).ToSql();
  69. base.Check(@"INSERT INTO [STudent] WITH(UPDLOCK)
  70. ([SchoolId],[Name],[CreateTime])
  71. VALUES
  72. (@SchoolId,@Name,@CreateTime) ;SELECT SCOPE_IDENTITY();",
  73. new List<SugarParameter>() {
  74. new SugarParameter("@SchoolId",0),
  75. new SugarParameter("@CreateTime",Convert.ToDateTime("2010-1-1")),
  76. new SugarParameter("@Name","jack")
  77. }, t6.Key, t6.Value, "Insert t6 error"
  78. );
  79. var insertObj2 = new Student() { Name = null,SchoolId=0, CreateTime = Convert.ToDateTime("2010-1-1") };
  80. var t8 = db.Insertable(insertObj2).Where(true/* Is insert null */, true/*off identity*/).ToSql();
  81. base.Check(@"INSERT INTO [STudent]
  82. ([ID],[SchoolId],[CreateTime])
  83. VALUES
  84. (@ID,@SchoolId,@CreateTime) ;SELECT SCOPE_IDENTITY();",
  85. new List<SugarParameter>() {
  86. new SugarParameter("@SchoolId", 0),
  87. new SugarParameter("@ID", 0),
  88. new SugarParameter("@CreateTime", Convert.ToDateTime("2010-1-1"))
  89. },
  90. t8.Key,
  91. t8.Value,
  92. "Insert t8 error"
  93. );
  94. db.IgnoreColumns = new IgnoreColumnList();
  95. db.IgnoreColumns.Add("TestId", "Student");
  96. //Insert List<T>
  97. var insertObjs = new List<Student>();
  98. for (int i = 0; i < 1000; i++)
  99. {
  100. insertObjs.Add(new Student() { Name = "name" + i });
  101. }
  102. var s9 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).With(SqlWith.UpdLock).ToSql();
  103. insertObj.Name = null;
  104. var t10 = db.Insertable(insertObj).ExecuteCommand();
  105. var t11 = db.Insertable(new MyStudent() { Id = 1, Name = "张三" }).AS("Student").ToSql();
  106. base.Check(@"INSERT INTO [Student]
  107. ([Name])
  108. VALUES
  109. (@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
  110. new SugarParameter("@Name","张三")
  111. }, t11.Key, t11.Value, "Insert t11 error");
  112. var t12 = db.Insertable<Student>(new { Name = "a" }).ToSql();
  113. base.Check(@"INSERT INTO [STudent]
  114. ([Name])
  115. VALUES
  116. (@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
  117. new SugarParameter("@Name","a")
  118. }, t12.Key, t12.Value, "Insert t12 error");
  119. var t13 = db.Insertable<Student>(new Dictionary<string, object>() { {"id",0 },{ "name","2"} }).ToSql();
  120. base.Check(@"INSERT INTO [STudent]
  121. ([Name])
  122. VALUES
  123. (@Name) ;SELECT SCOPE_IDENTITY();", new List<SugarParameter>() {
  124. new SugarParameter("@Name","2")
  125. }, t13.Key, t13.Value, "Insert t13 error");
  126. }
  127. }
  128. public class MyStudent {
  129. public int Id { get; set; }
  130. public string Name { get; set; }
  131. }
  132. }