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.

211 lines
7.8 KiB

2 years ago
  1. using System;
  2. namespace SqlSugar
  3. {
  4. public class SqliteExpressionContext : ExpressionContext, ILambdaExpressions
  5. {
  6. public SqlSugarClient Context { get; set; }
  7. public SqliteExpressionContext()
  8. {
  9. base.DbMehtods = new SqliteMethod();
  10. }
  11. public override string SqlTranslationLeft { get { return "`"; } }
  12. public override string SqlTranslationRight { get { return "`"; } }
  13. }
  14. public class SqliteMethod : DefaultDbMethod, IDbMethods
  15. {
  16. public override string Length(MethodCallExpressionModel model)
  17. {
  18. var parameter = model.Args[0];
  19. return string.Format("LENGTH({0})", 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 '%'||{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 {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 '%'||{1}) ", parameter.MemberName, parameter2.MemberName);
  38. }
  39. public override string ToInt32(MethodCallExpressionModel model)
  40. {
  41. var parameter = model.Args[0];
  42. return string.Format(" CAST({0} AS INTEGER)", parameter.MemberName);
  43. }
  44. public override string ToInt64(MethodCallExpressionModel model)
  45. {
  46. var parameter = model.Args[0];
  47. return string.Format(" CAST({0} AS INTEGER)", parameter.MemberName);
  48. }
  49. public override string ToString(MethodCallExpressionModel model)
  50. {
  51. var parameter = model.Args[0];
  52. return string.Format(" CAST({0} AS TEXT)", parameter.MemberName);
  53. }
  54. public override string ToGuid(MethodCallExpressionModel model)
  55. {
  56. var parameter = model.Args[0];
  57. return string.Format(" CAST({0} AS TEXT)", parameter.MemberName);
  58. }
  59. public override string ToDouble(MethodCallExpressionModel model)
  60. {
  61. var parameter = model.Args[0];
  62. return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName);
  63. }
  64. public override string ToBool(MethodCallExpressionModel model)
  65. {
  66. var parameter = model.Args[0];
  67. return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName);
  68. }
  69. public override string ToDecimal(MethodCallExpressionModel model)
  70. {
  71. var parameter = model.Args[0];
  72. return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName);
  73. }
  74. public override string ToDate(MethodCallExpressionModel model)
  75. {
  76. var parameter = model.Args[0];
  77. return string.Format(" DATETIME({0})", parameter.MemberName);
  78. }
  79. public override string DateAddDay(MethodCallExpressionModel model)
  80. {
  81. var parameter = model.Args[0];
  82. var parameter2 = model.Args[1];
  83. if (parameter2.MemberValue.ObjToInt() < 0)
  84. {
  85. return string.Format(" DATE(DATETIME({0}), '-{1} days')", parameter.MemberName, Math.Abs(parameter2.MemberValue.ObjToInt()));
  86. }
  87. else
  88. {
  89. return string.Format(" DATE(DATETIME({0}), '+{1} days')", parameter.MemberName, parameter2.MemberValue);
  90. }
  91. }
  92. public override string DateAddByType(MethodCallExpressionModel model)
  93. {
  94. var parameter = model.Args[0].MemberName;
  95. var parameter2 = model.Args[1].MemberValue;
  96. var parameter3 = model.Args[2].MemberValue;
  97. if (parameter2.ObjToInt() < 0)
  98. {
  99. return string.Format(" DATETIME(DATETIME({0}), '+{1} {2}s')", parameter, Math.Abs(parameter2.ObjToInt()), parameter3);
  100. }
  101. else
  102. {
  103. return string.Format(" DATETIME(DATETIME({0}), '+{1} {2}s')", parameter, parameter2, parameter3);
  104. }
  105. }
  106. public override string DateValue(MethodCallExpressionModel model)
  107. {
  108. var parameter = model.Args[0];
  109. var typeName = model.Args[1].MemberValue.ToString();
  110. var parameter2 = typeName;
  111. var type = (DateType)Enum.Parse(typeof(DateType), parameter2, false);
  112. switch (type)
  113. {
  114. case DateType.Year:
  115. parameter2 = "%Y";
  116. break;
  117. case DateType.Month:
  118. parameter2 = "%m";
  119. break;
  120. case DateType.Day:
  121. parameter2 = "%d";
  122. break;
  123. case DateType.Hour:
  124. parameter2 = "%H";
  125. break;
  126. case DateType.Second:
  127. parameter2 = "%S";
  128. break;
  129. case DateType.Minute:
  130. parameter2 = "%M";
  131. break;
  132. case DateType.Millisecond:
  133. default:
  134. Check.ThrowNotSupportedException(typeName);
  135. break;
  136. }
  137. return string.Format(" CAST(STRFTIME('{1}', DATETIME(DATETIME({0}), 'LOCALTIME')) AS INTEGER)", parameter.MemberName, parameter2);
  138. }
  139. public override string DateIsSameDay(MethodCallExpressionModel model)
  140. {
  141. var parameter = model.Args[0].MemberName;
  142. var parameter2 = model.Args[1].MemberName;
  143. var time = 1;
  144. return string.Format(" Cast((JulianDay({0}) - JulianDay({1})) *{2} As INTEGER)", parameter, parameter2, time);
  145. }
  146. public override string DateIsSameByType(MethodCallExpressionModel model)
  147. {
  148. var parameter = model.Args[0].MemberName;
  149. var parameter2 = model.Args[1].MemberName;
  150. var parameter3 = model.Args[2].MemberValue;
  151. var type = (DateType)Enum.Parse(typeof(DateType), parameter3.ObjToString(), false);
  152. var time = 1;
  153. switch (type)
  154. {
  155. case DateType.Year:
  156. time = time * 1 / 365;
  157. break;
  158. case DateType.Month:
  159. time = time * 1 / 30;
  160. break;
  161. case DateType.Day:
  162. break;
  163. case DateType.Hour:
  164. time = time * 24;
  165. break;
  166. case DateType.Second:
  167. time = time * 24 * 60 * 60;
  168. break;
  169. case DateType.Minute:
  170. time = time * 24 * 60;
  171. break;
  172. case DateType.Millisecond:
  173. time = time * 24 * 60 * 60 * 1000;
  174. break;
  175. }
  176. return string.Format(" Cast((JulianDay({0}) - JulianDay({1})) *{2} As INTEGER)", parameter, parameter2, time);
  177. }
  178. public override string MergeString(params string[] strings)
  179. {
  180. return string.Join("||", strings).Replace("+","");
  181. }
  182. public override string IsNull(MethodCallExpressionModel model)
  183. {
  184. var parameter = model.Args[0];
  185. var parameter1 = model.Args[1];
  186. return string.Format("IFNULL({0},{1})", parameter.MemberName, parameter1.MemberName);
  187. }
  188. public override string GetDate()
  189. {
  190. return "DATETIME('now')";
  191. }
  192. }
  193. }