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.

352 lines
11 KiB

2 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace SqlSugar
  6. {
  7. public partial class DefaultDbMethod : IDbMethods
  8. {
  9. public virtual string IIF(MethodCallExpressionModel model)
  10. {
  11. var parameter = model.Args[0];
  12. var parameter2 = model.Args[1];
  13. var parameter3 = model.Args[2];
  14. return $"( CASE WHEN {parameter.MemberName} THEN {parameter2.MemberName} ELSE {parameter3.MemberName} END )";
  15. }
  16. public virtual string IsNullOrEmpty(MethodCallExpressionModel model)
  17. {
  18. var parameter = model.Args[0];
  19. return $"( {parameter.MemberName}='' OR {parameter.MemberName} IS NULL )";
  20. }
  21. public virtual string HasValue(MethodCallExpressionModel model)
  22. {
  23. var parameter = model.Args[0];
  24. return $"( {parameter.MemberName}<>'' AND {parameter.MemberName} IS NOT NULL )";
  25. }
  26. public virtual string HasNumber(MethodCallExpressionModel model)
  27. {
  28. var parameter = model.Args[0];
  29. return $"( {parameter.MemberName}>0 AND {parameter.MemberName} IS NOT NULL )";
  30. }
  31. public virtual string ToUpper(MethodCallExpressionModel model)
  32. {
  33. var parameter = model.Args[0];
  34. return $" (UPPER({parameter.MemberName})) ";
  35. }
  36. public virtual string ToLower(MethodCallExpressionModel model)
  37. {
  38. var parameter = model.Args[0];
  39. return $" (LOWER({parameter.MemberName})) ";
  40. }
  41. public virtual string Trim(MethodCallExpressionModel model)
  42. {
  43. var parameter = model.Args[0];
  44. return $" (rtrim(ltrim({parameter.MemberName}))) ";
  45. }
  46. public virtual string Contains(MethodCallExpressionModel model)
  47. {
  48. var parameter = model.Args[0];
  49. var parameter2 = model.Args[1];
  50. return $" ({parameter.MemberName} like '%'+{parameter2.MemberName}+'%') ";
  51. }
  52. public virtual string ContainsArray(MethodCallExpressionModel model)
  53. {
  54. var inValueIEnumerable = (IEnumerable)model.Args[0].MemberValue;
  55. var inValues = new List<object>();
  56. if (inValueIEnumerable != null)
  57. {
  58. foreach (var item in inValueIEnumerable)
  59. {
  60. if (item != null && item.GetType().IsEnum())
  61. {
  62. inValues.Add(Convert.ToInt64(item));
  63. }
  64. else
  65. {
  66. inValues.Add(item);
  67. }
  68. }
  69. }
  70. var value = model.Args[1].MemberName;
  71. string inValueString = null;
  72. if (inValues != null && inValues.Count > 0)
  73. {
  74. inValueString = inValues.ToArray().ToJoinSqlInVals();
  75. }
  76. if (inValueString.IsNullOrEmpty())
  77. {
  78. return " (1=2) ";
  79. }
  80. else
  81. {
  82. return $" ({value} IN ({inValueString})) ";
  83. }
  84. }
  85. public virtual string Equals(MethodCallExpressionModel model)
  86. {
  87. var parameter = model.Args[0];
  88. var parameter2 = model.Args[1];
  89. return $" ({parameter.MemberName} = {parameter2.MemberName}) "; ;
  90. }
  91. public virtual string DateIsSameDay(MethodCallExpressionModel model)
  92. {
  93. var parameter = model.Args[0];
  94. var parameter2 = model.Args[1];
  95. return $" (DATEDIFF(day,{parameter.MemberName},{parameter2.MemberName})=0) "; ;
  96. }
  97. public virtual string DateIsSameByType(MethodCallExpressionModel model)
  98. {
  99. var parameter = model.Args[0];
  100. var parameter2 = model.Args[1];
  101. var parameter3 = model.Args[2];
  102. return $" (DATEDIFF({parameter3.MemberValue},{parameter.MemberName},{parameter2.MemberName})=0) ";
  103. }
  104. public virtual string DateAddByType(MethodCallExpressionModel model)
  105. {
  106. var parameter = model.Args[0];
  107. var parameter2 = model.Args[1];
  108. var parameter3 = model.Args[2];
  109. return $" (DATEADD({parameter3.MemberValue},{parameter2.MemberName},{parameter.MemberName})) ";
  110. }
  111. public virtual string DateAddDay(MethodCallExpressionModel model)
  112. {
  113. var parameter = model.Args[0];
  114. var parameter2 = model.Args[1];
  115. return $" (DATEADD(day,{parameter2.MemberName},{parameter.MemberName})) ";
  116. }
  117. public virtual string Between(MethodCallExpressionModel model)
  118. {
  119. var parameter = model.Args[0];
  120. var parameter1 = model.Args[1];
  121. var parameter2 = model.Args[2];
  122. return $" ({parameter.MemberName} BETWEEN {parameter1.MemberName} AND {parameter2.MemberName}) ";
  123. }
  124. public virtual string StartsWith(MethodCallExpressionModel model)
  125. {
  126. var parameter = model.Args[0];
  127. var parameter2 = model.Args[1];
  128. return $" ({parameter.MemberName} like {parameter2.MemberName}+'%') ";
  129. }
  130. public virtual string EndsWith(MethodCallExpressionModel model)
  131. {
  132. var parameter = model.Args[0];
  133. var parameter2 = model.Args[1];
  134. return $" ({parameter.MemberName} like '%'+{parameter2.MemberName}) ";
  135. }
  136. public virtual string DateValue(MethodCallExpressionModel model)
  137. {
  138. var parameter = model.Args[0];
  139. var parameter2 = model.Args[1];
  140. return $" DateName({parameter2.MemberValue},{parameter.MemberName}) ";
  141. }
  142. public virtual string ToInt32(MethodCallExpressionModel model)
  143. {
  144. var parameter = model.Args[0];
  145. return $" CAST({parameter.MemberName} AS INT)";
  146. }
  147. public virtual string ToInt64(MethodCallExpressionModel model)
  148. {
  149. var parameter = model.Args[0];
  150. return $" CAST({parameter.MemberName} AS BIGINT)";
  151. }
  152. public virtual string ToString(MethodCallExpressionModel model)
  153. {
  154. var parameter = model.Args[0];
  155. return $" CAST({parameter.MemberName} AS NVARCHAR(MAX))";
  156. }
  157. public virtual string ToGuid(MethodCallExpressionModel model)
  158. {
  159. var parameter = model.Args[0];
  160. return $" CAST({parameter.MemberName} AS UNIQUEIDENTIFIER)";
  161. }
  162. public virtual string ToDouble(MethodCallExpressionModel model)
  163. {
  164. var parameter = model.Args[0];
  165. return $" CAST({parameter.MemberName} AS FLOAT)";
  166. }
  167. public virtual string ToBool(MethodCallExpressionModel model)
  168. {
  169. var parameter = model.Args[0];
  170. return $" CAST({parameter.MemberName} AS BIT)";
  171. }
  172. public virtual string ToDate(MethodCallExpressionModel model)
  173. {
  174. var parameter = model.Args[0];
  175. return $" CAST({parameter.MemberName} AS DATETIME)";
  176. }
  177. public virtual string ToTime(MethodCallExpressionModel model)
  178. {
  179. var parameter = model.Args[0];
  180. return $" CAST({parameter.MemberName} AS TIME)";
  181. }
  182. public virtual string ToDecimal(MethodCallExpressionModel model)
  183. {
  184. var parameter = model.Args[0];
  185. return $" CAST({parameter.MemberName} AS MONEY)";
  186. }
  187. public virtual string Substring(MethodCallExpressionModel model)
  188. {
  189. var parameter = model.Args[0];
  190. var parameter2 = model.Args[1];
  191. var parameter3 = model.Args[2];
  192. return $"SUBSTRING({parameter.MemberName},1 + {parameter2.MemberName},{parameter3.MemberName})";
  193. }
  194. public virtual string Length(MethodCallExpressionModel model)
  195. {
  196. var parameter = model.Args[0];
  197. return $"LEN({parameter.MemberName})";
  198. }
  199. public virtual string Replace(MethodCallExpressionModel model)
  200. {
  201. var parameter = model.Args[0];
  202. var parameter2 = model.Args[1];
  203. var parameter3 = model.Args[2];
  204. return $"REPLACE({parameter.MemberName},{parameter2.MemberName},{parameter3.MemberName})";
  205. }
  206. public virtual string AggregateSum(MethodCallExpressionModel model)
  207. {
  208. var parameter = model.Args[0];
  209. return $"SUM({parameter.MemberName})";
  210. }
  211. public virtual string AggregateAvg(MethodCallExpressionModel model)
  212. {
  213. var parameter = model.Args[0];
  214. return $"AVG({parameter.MemberName})";
  215. }
  216. public virtual string AggregateMin(MethodCallExpressionModel model)
  217. {
  218. var parameter = model.Args[0];
  219. return $"MIN({parameter.MemberName})";
  220. }
  221. public virtual string AggregateMax(MethodCallExpressionModel model)
  222. {
  223. var parameter = model.Args[0];
  224. return $"MAX({parameter.MemberName})";
  225. }
  226. public virtual string AggregateCount(MethodCallExpressionModel model)
  227. {
  228. var parameter = model.Args[0];
  229. return $"COUNT({parameter.MemberName})";
  230. }
  231. public virtual string MappingColumn(MethodCallExpressionModel model)
  232. {
  233. var parameter = model.Args[0];
  234. var parameter1 = model.Args[1];
  235. return $"{parameter1.MemberValue}";
  236. }
  237. public virtual string IsNull(MethodCallExpressionModel model)
  238. {
  239. var parameter = model.Args[0];
  240. var parameter1 = model.Args[1];
  241. return $"ISNULL({parameter.MemberName},{parameter1.MemberName})";
  242. }
  243. public virtual string True()
  244. {
  245. return "( 1 = 1 ) ";
  246. }
  247. public virtual string False()
  248. {
  249. return "( 1 = 2 ) ";
  250. }
  251. public string GuidNew()
  252. {
  253. return "'" + Guid.NewGuid() + "' ";
  254. }
  255. public string GetSelfAndAutoFill(string shortName, bool isSingle)
  256. {
  257. if (isSingle) return "*";
  258. else
  259. return $"{shortName}.*";
  260. }
  261. public virtual string MergeString(params string[] strings)
  262. {
  263. return string.Join("+", strings);
  264. }
  265. public virtual string Pack(string sql)
  266. {
  267. return "(" + sql + ")";
  268. }
  269. public virtual string EqualTrue(string fieldName)
  270. {
  271. return "( " + fieldName + "=1 )";
  272. }
  273. public virtual string Null()
  274. {
  275. return "NULL";
  276. }
  277. public virtual string GetDate()
  278. {
  279. return "GETDATE()";
  280. }
  281. public virtual string CaseWhen(List<KeyValuePair<string, string>> sqls)
  282. {
  283. var reslut = new StringBuilder();
  284. foreach (var item in sqls)
  285. {
  286. if (item.Key == "IF")
  287. {
  288. reslut.AppendFormat(" ( CASE WHEN {0} ", item.Value);
  289. }
  290. else if (item.Key == "End")
  291. {
  292. reslut.AppendFormat("ELSE {0} END )", item.Value);
  293. }
  294. else if (item.Key == "Return")
  295. {
  296. reslut.AppendFormat(" THEN {0} ", item.Value);
  297. }
  298. else
  299. {
  300. reslut.AppendFormat(" WHEN {0} ", item.Value);
  301. }
  302. }
  303. return reslut.ToString();
  304. }
  305. }
  306. }