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.

135 lines
5.1 KiB

2 years ago
  1. namespace SqlSugar
  2. {
  3. public class MySqlExpressionContext : ExpressionContext, ILambdaExpressions
  4. {
  5. public SqlSugarClient Context { get; set; }
  6. public MySqlExpressionContext()
  7. {
  8. base.DbMehtods = new MySqlMethod();
  9. }
  10. public override string SqlTranslationLeft { get { return "`"; } }
  11. public override string SqlTranslationRight { get { return "`"; } }
  12. }
  13. public class MySqlMethod : DefaultDbMethod, IDbMethods
  14. {
  15. public override string DateValue(MethodCallExpressionModel model)
  16. {
  17. var parameter = model.Args[0];
  18. var parameter2 = model.Args[1];
  19. return string.Format(" {0}({1}) ", parameter2.MemberValue, parameter.MemberName);
  20. }
  21. public override string Contains(MethodCallExpressionModel model)
  22. {
  23. var parameter = model.Args[0];
  24. var parameter2 = model.Args[1];
  25. return string.Format(" ({0} like concat('%',{1},'%')) ", parameter.MemberName, parameter2.MemberName );
  26. }
  27. public override string StartsWith(MethodCallExpressionModel model)
  28. {
  29. var parameter = model.Args[0];
  30. var parameter2 = model.Args[1];
  31. return string.Format(" ({0} like concat({1},'%')) ", parameter.MemberName, parameter2.MemberName);
  32. }
  33. public override string EndsWith(MethodCallExpressionModel model)
  34. {
  35. var parameter = model.Args[0];
  36. var parameter2 = model.Args[1];
  37. return string.Format(" ({0} like concat('%',{1}))", parameter.MemberName,parameter2.MemberName);
  38. }
  39. public override string DateIsSameDay(MethodCallExpressionModel model)
  40. {
  41. var parameter = model.Args[0];
  42. var parameter2 = model.Args[1];
  43. return string.Format(" (TIMESTAMPDIFF(day,{0},{1})=0) ", parameter.MemberName, parameter2.MemberName); ;
  44. }
  45. public override string DateIsSameByType(MethodCallExpressionModel model)
  46. {
  47. var parameter = model.Args[0];
  48. var parameter2 = model.Args[1];
  49. var parameter3 = model.Args[2];
  50. return string.Format(" (TIMESTAMPDIFF({2},{0},{1})=0) ", parameter.MemberName, parameter2.MemberName, parameter3.MemberValue);
  51. }
  52. public override string DateAddByType(MethodCallExpressionModel model)
  53. {
  54. var parameter = model.Args[0];
  55. var parameter2 = model.Args[1];
  56. var parameter3 = model.Args[2];
  57. return string.Format(" (DATE_ADD({1} , INTERVAL {2} {0})) ", parameter3.MemberValue, parameter.MemberName, parameter2.MemberName);
  58. }
  59. public override string DateAddDay(MethodCallExpressionModel model)
  60. {
  61. var parameter = model.Args[0];
  62. var parameter2 = model.Args[1];
  63. return string.Format(" (DATE_ADD({1} INTERVAL {0} day)) ", parameter.MemberName, parameter2.MemberName);
  64. }
  65. public override string ToInt32(MethodCallExpressionModel model)
  66. {
  67. var parameter = model.Args[0];
  68. return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName);
  69. }
  70. public override string ToInt64(MethodCallExpressionModel model)
  71. {
  72. var parameter = model.Args[0];
  73. return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName);
  74. }
  75. public override string ToString(MethodCallExpressionModel model)
  76. {
  77. var parameter = model.Args[0];
  78. return string.Format(" CAST({0} AS CHAR)", parameter.MemberName);
  79. }
  80. public override string ToGuid(MethodCallExpressionModel model)
  81. {
  82. var parameter = model.Args[0];
  83. return string.Format(" CAST({0} AS CHAR)", parameter.MemberName);
  84. }
  85. public override string ToDouble(MethodCallExpressionModel model)
  86. {
  87. var parameter = model.Args[0];
  88. return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName);
  89. }
  90. public override string ToBool(MethodCallExpressionModel model)
  91. {
  92. var parameter = model.Args[0];
  93. return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName);
  94. }
  95. public override string ToDecimal(MethodCallExpressionModel model)
  96. {
  97. var parameter = model.Args[0];
  98. return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName);
  99. }
  100. public override string Length(MethodCallExpressionModel model)
  101. {
  102. var parameter = model.Args[0];
  103. return string.Format(" LENGTH({0})", parameter.MemberName);
  104. }
  105. public override string MergeString(params string[] strings)
  106. {
  107. return " concat("+string.Join(",", strings).Replace("+", "") + ") ";
  108. }
  109. public override string IsNull(MethodCallExpressionModel model)
  110. {
  111. var parameter = model.Args[0];
  112. var parameter1 = model.Args[1];
  113. return string.Format("IFNULL({0},{1})", parameter.MemberName, parameter1.MemberName);
  114. }
  115. public override string GetDate()
  116. {
  117. return "NOW()";
  118. }
  119. }
  120. }