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.

114 lines
5.0 KiB

2 years ago
  1. using OrmTest.Demo;
  2. using OrmTest.Models;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace OrmTest.Demo
  9. {
  10. /// <summary>
  11. /// Secure string operations
  12. /// </summary>
  13. public class JoinSql : DemoBase
  14. {
  15. public static void Init()
  16. {
  17. Where();
  18. OrderBy();
  19. SelectMerge();
  20. ConditionalModel();
  21. JoinExp();
  22. }
  23. private static void JoinExp()
  24. {
  25. var db = GetInstance();
  26. var exp= Expressionable.Create<Student>()
  27. .OrIF(1==1,it => it.Id == 11)
  28. .And(it=>it.Id==1)
  29. .AndIF(2==2,it => it.Id == 1)
  30. .Or(it =>it.Name == "a1").ToExpression();
  31. var list=db.Queryable<Student>().Where(exp).ToList();
  32. var list1 = db.Queryable<Student>().Where(exp).ToSql();
  33. }
  34. private static void ConditionalModel()
  35. {
  36. var db = GetInstance();
  37. List<IConditionalModel> conModels = new List<IConditionalModel>();
  38. conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1
  39. conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Like, FieldValue = "1" });// id like '%1%'
  40. conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNullOrEmpty });
  41. conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.In,FieldValue="1,2,3" });
  42. conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NotIn, FieldValue = "1,2,3" });
  43. conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.NoEqual, FieldValue = "1,2,3" });
  44. conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.IsNot,FieldValue=null});// id is not null
  45. conModels.Add(new ConditionalCollections() { ConditionalList=new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()// (id=1 or id=2 and id=1)
  46. {
  47. new KeyValuePair<WhereType, ConditionalModel>( WhereType.And ,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" }),
  48. new KeyValuePair<WhereType, ConditionalModel> (WhereType.Or,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" }),
  49. new KeyValuePair<WhereType, ConditionalModel> ( WhereType.And,new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "2" })
  50. }
  51. });
  52. var student = db.Queryable<Student>().Where(conModels).ToList();
  53. var student1 = db.Queryable<Student>().Where(conModels).ToSql();
  54. }
  55. private static void SelectMerge()
  56. {
  57. var db = GetInstance();
  58. //page join
  59. var pageJoin = db.Queryable<Student, School>((st, sc) => new object[] {
  60. JoinType.Left,st.SchoolId==sc.Id
  61. })
  62. .Where(st => st.Id==1)
  63. .Where(st => st.Id==2)
  64. .Select((st, sc) => new { id = st.Id, name = sc.Name })
  65. .MergeTable().Where(XXX => XXX.id == 1).OrderBy("name asc").ToList();// Prefix, is, not, necessary, and take the columns in select
  66. var pageJoin1 = db.Queryable<Student, School>((st, sc) => new object[] {
  67. JoinType.Left,st.SchoolId==sc.Id
  68. })
  69. .Where(st => st.Id == 1)
  70. .Where(st => st.Id == 2)
  71. .Select((st, sc) => new { id = st.Id, name = sc.Name })
  72. .MergeTable().Where(XXX => XXX.id == 1).OrderBy("name asc").ToSql();
  73. }
  74. private static void Where()
  75. {
  76. var db = GetInstance();
  77. //Parameterized processing
  78. string value = "'jack';drop table Student";
  79. var list = db.Queryable<Student>().Where("name=@name", new { name = value }).ToList();
  80. var list1 = db.Queryable<Student>().Where("name=@name", new { name = value }).ToSql();
  81. //Nothing happened
  82. }
  83. private static void OrderBy()
  84. {
  85. var db = GetInstance();
  86. //propertyName is valid
  87. string propertyName = "Id";
  88. string dbColumnName = db.EntityMaintenance.GetDbColumnName<Student>(propertyName);
  89. var list = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
  90. var list1 = db.Queryable<Student>().OrderBy(dbColumnName).ToSql();
  91. //propertyName is invalid
  92. try
  93. {
  94. propertyName = "Id'";
  95. dbColumnName = db.EntityMaintenance.GetDbColumnName<Student>(propertyName);
  96. var list2 = db.Queryable<Student>().OrderBy(dbColumnName).ToList();
  97. }
  98. catch (Exception ex)
  99. {
  100. Console.WriteLine(ex.Message);
  101. }
  102. }
  103. }
  104. }