SqlGenerator_MethodHandlers.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using Ant.Core;
  2. using Ant.DbExpressions;
  3. using Ant.ORM;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. namespace Ant.Oracle
  12. {
  13. partial class SqlGenerator : DbExpressionVisitor<DbExpression>
  14. {
  15. static Dictionary<string, Action<DbMethodCallExpression, SqlGenerator>> InitMethodHandlers()
  16. {
  17. var methodHandlers = new Dictionary<string, Action<DbMethodCallExpression, SqlGenerator>>();
  18. methodHandlers.Add("Trim", Method_Trim);
  19. methodHandlers.Add("TrimStart", Method_TrimStart);
  20. methodHandlers.Add("TrimEnd", Method_TrimEnd);
  21. methodHandlers.Add("StartsWith", Method_StartsWith);
  22. methodHandlers.Add("EndsWith", Method_EndsWith);
  23. methodHandlers.Add("ToUpper", Method_String_ToUpper);
  24. methodHandlers.Add("ToLower", Method_String_ToLower);
  25. methodHandlers.Add("Substring", Method_String_Substring);
  26. methodHandlers.Add("IsNullOrEmpty", Method_String_IsNullOrEmpty);
  27. methodHandlers.Add("Contains", Method_Contains);
  28. methodHandlers.Add("Count", Method_Count);
  29. methodHandlers.Add("LongCount", Method_LongCount);
  30. methodHandlers.Add("Sum", Method_Sum);
  31. methodHandlers.Add("Max", Method_Max);
  32. methodHandlers.Add("Min", Method_Min);
  33. methodHandlers.Add("Average", Method_Average);
  34. methodHandlers.Add("AddYears", Method_DateTime_AddYears);
  35. methodHandlers.Add("AddMonths", Method_DateTime_AddMonths);
  36. methodHandlers.Add("AddDays", Method_DateTime_AddDays);
  37. methodHandlers.Add("AddHours", Method_DateTime_AddHours);
  38. methodHandlers.Add("AddMinutes", Method_DateTime_AddMinutes);
  39. methodHandlers.Add("AddSeconds", Method_DateTime_AddSeconds);
  40. methodHandlers.Add("AddMilliseconds", Method_DateTime_AddMilliseconds);
  41. methodHandlers.Add("Parse", Method_Parse);
  42. methodHandlers.Add("NewGuid", Method_Guid_NewGuid);
  43. methodHandlers.Add("DiffYears", Method_DbFunctions_DiffYears);
  44. methodHandlers.Add("DiffMonths", Method_DbFunctions_DiffMonths);
  45. methodHandlers.Add("DiffDays", Method_DbFunctions_DiffDays);
  46. methodHandlers.Add("DiffHours", Method_DbFunctions_DiffHours);
  47. methodHandlers.Add("DiffMinutes", Method_DbFunctions_DiffMinutes);
  48. methodHandlers.Add("DiffSeconds", Method_DbFunctions_DiffSeconds);
  49. methodHandlers.Add("DiffMilliseconds", Method_DbFunctions_DiffMilliseconds);
  50. methodHandlers.Add("DiffMicroseconds", Method_DbFunctions_DiffMicroseconds);
  51. var ret = Utils.Clone(methodHandlers);
  52. return ret;
  53. }
  54. static void Method_Trim(DbMethodCallExpression exp, SqlGenerator generator)
  55. {
  56. EnsureMethod(exp, UtilConstants.MethodInfo_String_Trim);
  57. generator._sqlBuilder.Append("TRIM(");
  58. exp.Object.Accept(generator);
  59. generator._sqlBuilder.Append(")");
  60. }
  61. static void Method_TrimStart(DbMethodCallExpression exp, SqlGenerator generator)
  62. {
  63. EnsureMethod(exp, UtilConstants.MethodInfo_String_TrimStart);
  64. EnsureTrimCharArgumentIsSpaces(exp.Arguments[0]);
  65. generator._sqlBuilder.Append("LTRIM(");
  66. exp.Object.Accept(generator);
  67. generator._sqlBuilder.Append(")");
  68. }
  69. static void Method_TrimEnd(DbMethodCallExpression exp, SqlGenerator generator)
  70. {
  71. EnsureMethod(exp, UtilConstants.MethodInfo_String_TrimEnd);
  72. EnsureTrimCharArgumentIsSpaces(exp.Arguments[0]);
  73. generator._sqlBuilder.Append("RTRIM(");
  74. exp.Object.Accept(generator);
  75. generator._sqlBuilder.Append(")");
  76. }
  77. static void Method_StartsWith(DbMethodCallExpression exp, SqlGenerator generator)
  78. {
  79. EnsureMethod(exp, UtilConstants.MethodInfo_String_StartsWith);
  80. exp.Object.Accept(generator);
  81. generator._sqlBuilder.Append(" LIKE ");
  82. exp.Arguments.First().Accept(generator);
  83. generator._sqlBuilder.Append(" || '%'");
  84. }
  85. static void Method_EndsWith(DbMethodCallExpression exp, SqlGenerator generator)
  86. {
  87. EnsureMethod(exp, UtilConstants.MethodInfo_String_EndsWith);
  88. exp.Object.Accept(generator);
  89. generator._sqlBuilder.Append(" LIKE '%' || ");
  90. exp.Arguments.First().Accept(generator);
  91. }
  92. static void Method_String_Contains(DbMethodCallExpression exp, SqlGenerator generator)
  93. {
  94. EnsureMethod(exp, UtilConstants.MethodInfo_String_Contains);
  95. exp.Object.Accept(generator);
  96. generator._sqlBuilder.Append(" LIKE '%' || ");
  97. exp.Arguments.First().Accept(generator);
  98. generator._sqlBuilder.Append(" || '%'");
  99. }
  100. static void Method_String_ToUpper(DbMethodCallExpression exp, SqlGenerator generator)
  101. {
  102. EnsureMethod(exp, UtilConstants.MethodInfo_String_ToUpper);
  103. generator._sqlBuilder.Append("UPPER(");
  104. exp.Object.Accept(generator);
  105. generator._sqlBuilder.Append(")");
  106. }
  107. static void Method_String_ToLower(DbMethodCallExpression exp, SqlGenerator generator)
  108. {
  109. EnsureMethod(exp, UtilConstants.MethodInfo_String_ToLower);
  110. generator._sqlBuilder.Append("LOWER(");
  111. exp.Object.Accept(generator);
  112. generator._sqlBuilder.Append(")");
  113. }
  114. static void Method_String_Substring(DbMethodCallExpression exp, SqlGenerator generator)
  115. {
  116. generator._sqlBuilder.Append("SUBSTR(");
  117. exp.Object.Accept(generator);
  118. generator._sqlBuilder.Append(",");
  119. exp.Arguments[0].Accept(generator);
  120. generator._sqlBuilder.Append(" + 1");
  121. generator._sqlBuilder.Append(",");
  122. if (exp.Method == UtilConstants.MethodInfo_String_Substring_Int32)
  123. {
  124. var string_LengthExp = DbExpression.MemberAccess(UtilConstants.PropertyInfo_String_Length, exp.Object);
  125. string_LengthExp.Accept(generator);
  126. }
  127. else if (exp.Method == UtilConstants.MethodInfo_String_Substring_Int32_Int32)
  128. {
  129. exp.Arguments[1].Accept(generator);
  130. }
  131. else
  132. throw UtilExceptions.NotSupportedMethod(exp.Method);
  133. generator._sqlBuilder.Append(")");
  134. }
  135. static void Method_String_IsNullOrEmpty(DbMethodCallExpression exp, SqlGenerator generator)
  136. {
  137. EnsureMethod(exp, UtilConstants.MethodInfo_String_IsNullOrEmpty);
  138. DbExpression e = exp.Arguments.First();
  139. DbEqualExpression equalNullExpression = DbExpression.Equal(e, DbExpression.Constant(null, UtilConstants.TypeOfString));
  140. DbEqualExpression equalEmptyExpression = DbExpression.Equal(e, DbExpression.Constant(string.Empty));
  141. DbOrElseExpression orElseExpression = DbExpression.OrElse(equalNullExpression, equalEmptyExpression);
  142. DbCaseWhenExpression.WhenThenExpressionPair whenThenPair = new DbCaseWhenExpression.WhenThenExpressionPair(orElseExpression, DbConstantExpression.One);
  143. List<DbCaseWhenExpression.WhenThenExpressionPair> whenThenExps = new List<DbCaseWhenExpression.WhenThenExpressionPair>(1);
  144. whenThenExps.Add(whenThenPair);
  145. DbCaseWhenExpression caseWhenExpression = DbExpression.CaseWhen(whenThenExps, DbConstantExpression.Zero, UtilConstants.TypeOfBoolean);
  146. var eqExp = DbExpression.Equal(caseWhenExpression, DbConstantExpression.One);
  147. eqExp.Accept(generator);
  148. }
  149. static void Method_Contains(DbMethodCallExpression exp, SqlGenerator generator)
  150. {
  151. MethodInfo method = exp.Method;
  152. if (method.DeclaringType == UtilConstants.TypeOfString)
  153. {
  154. Method_String_Contains(exp, generator);
  155. return;
  156. }
  157. List<DbExpression> exps = new List<DbExpression>();
  158. IEnumerable values = null;
  159. DbExpression arg = null;
  160. var declaringType = method.DeclaringType;
  161. if (typeof(IList).IsAssignableFrom(declaringType) || (declaringType.IsGenericType && typeof(ICollection<>).MakeGenericType(declaringType.GetGenericArguments()).IsAssignableFrom(declaringType)))
  162. {
  163. DbMemberExpression memberExp = exp.Object as DbMemberExpression;
  164. if (memberExp == null || !memberExp.CanEvaluate())
  165. throw new NotSupportedException(exp.ToString());
  166. values = DbExpressionExtensions.GetExpressionValue(memberExp) as IEnumerable; //Enumerable
  167. arg = exp.Arguments.First();
  168. goto constructInState;
  169. }
  170. if (method.IsStatic && declaringType == typeof(Enumerable) && exp.Arguments.Count == 2)
  171. {
  172. DbMemberExpression memberExp = exp.Arguments.First() as DbMemberExpression;
  173. if (memberExp == null || !memberExp.CanEvaluate())
  174. throw new NotSupportedException(exp.ToString());
  175. values = DbExpressionExtensions.GetExpressionValue(memberExp) as IEnumerable;
  176. arg = exp.Arguments.Skip(1).First();
  177. goto constructInState;
  178. }
  179. throw UtilExceptions.NotSupportedMethod(exp.Method);
  180. constructInState:
  181. foreach (object value in values)
  182. {
  183. if (value == null)
  184. exps.Add(DbExpression.Constant(null, arg.Type));
  185. else
  186. exps.Add(DbExpression.Parameter(value));
  187. }
  188. In(generator, exps, arg);
  189. }
  190. static void In(SqlGenerator generator, List<DbExpression> elementExps, DbExpression arg)
  191. {
  192. if (elementExps.Count == 0)
  193. {
  194. generator._sqlBuilder.Append("1=0");
  195. return;
  196. }
  197. arg.Accept(generator);
  198. generator._sqlBuilder.Append(" IN (");
  199. var first = true;
  200. foreach (DbExpression ele in elementExps)
  201. {
  202. if (first)
  203. first = false;
  204. else
  205. generator._sqlBuilder.Append(",");
  206. ele.Accept(generator);
  207. }
  208. generator._sqlBuilder.Append(")");
  209. return;
  210. }
  211. static void Method_Count(DbMethodCallExpression exp, SqlGenerator generator)
  212. {
  213. EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
  214. Aggregate_Count(generator);
  215. }
  216. static void Method_LongCount(DbMethodCallExpression exp, SqlGenerator generator)
  217. {
  218. EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
  219. Aggregate_LongCount(generator);
  220. }
  221. static void Method_Sum(DbMethodCallExpression exp, SqlGenerator generator)
  222. {
  223. EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
  224. Aggregate_Sum(generator, exp.Arguments.First(), exp.Method.ReturnType);
  225. }
  226. static void Method_Max(DbMethodCallExpression exp, SqlGenerator generator)
  227. {
  228. EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
  229. Aggregate_Max(generator, exp.Arguments.First(), exp.Method.ReturnType);
  230. }
  231. static void Method_Min(DbMethodCallExpression exp, SqlGenerator generator)
  232. {
  233. EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
  234. Aggregate_Min(generator, exp.Arguments.First(), exp.Method.ReturnType);
  235. }
  236. static void Method_Average(DbMethodCallExpression exp, SqlGenerator generator)
  237. {
  238. EnsureMethodDeclaringType(exp, typeof(AggregateFunctions));
  239. Aggregate_Average(generator, exp.Arguments.First(), exp.Method.ReturnType);
  240. }
  241. static void Method_DateTime_AddYears(DbMethodCallExpression exp, SqlGenerator generator)
  242. {
  243. EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);
  244. /* add_months(systimestamp,12 * 2) */
  245. generator._sqlBuilder.Append("ADD_MONTHS(");
  246. exp.Object.Accept(generator);
  247. generator._sqlBuilder.Append(",12 * ");
  248. exp.Arguments[0].Accept(generator);
  249. generator._sqlBuilder.Append(")");
  250. }
  251. static void Method_DateTime_AddMonths(DbMethodCallExpression exp, SqlGenerator generator)
  252. {
  253. EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);
  254. /* add_months(systimestamp,2) */
  255. generator._sqlBuilder.Append("ADD_MONTHS(");
  256. exp.Object.Accept(generator);
  257. generator._sqlBuilder.Append(",");
  258. exp.Arguments[0].Accept(generator);
  259. generator._sqlBuilder.Append(")");
  260. }
  261. static void Method_DateTime_AddDays(DbMethodCallExpression exp, SqlGenerator generator)
  262. {
  263. EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);
  264. /* (systimestamp + 3) */
  265. generator._sqlBuilder.Append("(");
  266. exp.Object.Accept(generator);
  267. generator._sqlBuilder.Append(" + ");
  268. exp.Arguments[0].Accept(generator);
  269. generator._sqlBuilder.Append(")");
  270. }
  271. static void Method_DateTime_AddHours(DbMethodCallExpression exp, SqlGenerator generator)
  272. {
  273. EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);
  274. DbFunction_DATEADD(generator, "HOUR", exp);
  275. }
  276. static void Method_DateTime_AddMinutes(DbMethodCallExpression exp, SqlGenerator generator)
  277. {
  278. EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);
  279. DbFunction_DATEADD(generator, "MINUTE", exp);
  280. }
  281. static void Method_DateTime_AddSeconds(DbMethodCallExpression exp, SqlGenerator generator)
  282. {
  283. EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);
  284. DbFunction_DATEADD(generator, "SECOND", exp);
  285. }
  286. static void Method_DateTime_AddMilliseconds(DbMethodCallExpression exp, SqlGenerator generator)
  287. {
  288. EnsureMethodDeclaringType(exp, UtilConstants.TypeOfDateTime);
  289. throw UtilExceptions.NotSupportedMethod(exp.Method);
  290. }
  291. static void Method_Parse(DbMethodCallExpression exp, SqlGenerator generator)
  292. {
  293. if (exp.Arguments.Count != 1)
  294. throw UtilExceptions.NotSupportedMethod(exp.Method);
  295. DbExpression arg = exp.Arguments[0];
  296. if (arg.Type != UtilConstants.TypeOfString)
  297. throw UtilExceptions.NotSupportedMethod(exp.Method);
  298. Type retType = exp.Method.ReturnType;
  299. EnsureMethodDeclaringType(exp, retType);
  300. DbExpression e = DbExpression.Convert(arg, retType);
  301. if (retType == UtilConstants.TypeOfBoolean)
  302. {
  303. e.Accept(generator);
  304. generator._sqlBuilder.Append(" = ");
  305. DbConstantExpression.True.Accept(generator);
  306. }
  307. else
  308. e.Accept(generator);
  309. }
  310. static void Method_Guid_NewGuid(DbMethodCallExpression exp, SqlGenerator generator)
  311. {
  312. EnsureMethod(exp, UtilConstants.MethodInfo_Guid_NewGuid);
  313. throw UtilExceptions.NotSupportedMethod(exp.Method);
  314. //返回的是一个长度为 16 的 byte[]
  315. generator._sqlBuilder.Append("SYS_GUID()");
  316. }
  317. static void Method_DbFunctions_DiffYears(DbMethodCallExpression exp, SqlGenerator generator)
  318. {
  319. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffYears);
  320. throw UtilExceptions.NotSupportedMethod(exp.Method);
  321. }
  322. static void Method_DbFunctions_DiffMonths(DbMethodCallExpression exp, SqlGenerator generator)
  323. {
  324. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffMonths);
  325. throw UtilExceptions.NotSupportedMethod(exp.Method);
  326. }
  327. static void Method_DbFunctions_DiffDays(DbMethodCallExpression exp, SqlGenerator generator)
  328. {
  329. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffDays);
  330. throw new NotSupportedException(AppendNotSupportedDbFunctionsMsg(exp.Method, "TotalDays"));
  331. }
  332. static void Method_DbFunctions_DiffHours(DbMethodCallExpression exp, SqlGenerator generator)
  333. {
  334. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffHours);
  335. throw new NotSupportedException(AppendNotSupportedDbFunctionsMsg(exp.Method, "TotalHours"));
  336. }
  337. static void Method_DbFunctions_DiffMinutes(DbMethodCallExpression exp, SqlGenerator generator)
  338. {
  339. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffMinutes);
  340. throw new NotSupportedException(AppendNotSupportedDbFunctionsMsg(exp.Method, "TotalMinutes"));
  341. }
  342. static void Method_DbFunctions_DiffSeconds(DbMethodCallExpression exp, SqlGenerator generator)
  343. {
  344. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffSeconds);
  345. throw new NotSupportedException(AppendNotSupportedDbFunctionsMsg(exp.Method, "TotalSeconds"));
  346. }
  347. static void Method_DbFunctions_DiffMilliseconds(DbMethodCallExpression exp, SqlGenerator generator)
  348. {
  349. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffMilliseconds);
  350. throw new NotSupportedException(AppendNotSupportedDbFunctionsMsg(exp.Method, "TotalMilliseconds"));
  351. }
  352. static void Method_DbFunctions_DiffMicroseconds(DbMethodCallExpression exp, SqlGenerator generator)
  353. {
  354. EnsureMethod(exp, UtilConstants.MethodInfo_DbFunctions_DiffMicroseconds);
  355. throw UtilExceptions.NotSupportedMethod(exp.Method);
  356. }
  357. static string AppendNotSupportedDbFunctionsMsg(MethodInfo method, string insteadProperty)
  358. {
  359. return string.Format("'{0}' is not supported.Instead of using '{1}.{2}'.", Utils.ToMethodString(method), Utils.ToMethodString(UtilConstants.MethodInfo_DateTime_Subtract_DateTime), insteadProperty);
  360. }
  361. }
  362. }