SqlGenerator_Helper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Ant.Core;
  2. using Ant.DbExpressions;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. namespace Ant.MySql
  11. {
  12. partial class SqlGenerator : DbExpressionVisitor<DbExpression>
  13. {
  14. static string GenParameterName(int ordinal)
  15. {
  16. if (ordinal < CacheParameterNames.Count)
  17. {
  18. return CacheParameterNames[ordinal];
  19. }
  20. return ParameterPrefix + ordinal.ToString();
  21. }
  22. static Stack<DbExpression> GatherBinaryExpressionOperand(DbBinaryExpression exp)
  23. {
  24. DbExpressionType nodeType = exp.NodeType;
  25. Stack<DbExpression> items = new Stack<DbExpression>();
  26. items.Push(exp.Right);
  27. DbExpression left = exp.Left;
  28. while (left.NodeType == nodeType)
  29. {
  30. exp = (DbBinaryExpression)left;
  31. items.Push(exp.Right);
  32. left = exp.Left;
  33. }
  34. items.Push(left);
  35. return items;
  36. }
  37. static void EnsureMethodDeclaringType(DbMethodCallExpression exp, Type ensureType)
  38. {
  39. if (exp.Method.DeclaringType != ensureType)
  40. throw UtilExceptions.NotSupportedMethod(exp.Method);
  41. }
  42. static void EnsureMethod(DbMethodCallExpression exp, MethodInfo methodInfo)
  43. {
  44. if (exp.Method != methodInfo)
  45. throw UtilExceptions.NotSupportedMethod(exp.Method);
  46. }
  47. static void EnsureTrimCharArgumentIsSpaces(DbExpression exp)
  48. {
  49. var m = exp as DbMemberExpression;
  50. if (m == null)
  51. throw new NotSupportedException();
  52. DbParameterExpression p;
  53. if (!DbExpressionExtensions.TryParseToParameterExpression(m, out p))
  54. {
  55. throw new NotSupportedException();
  56. }
  57. var arg = p.Value;
  58. if (arg == null)
  59. throw new NotSupportedException();
  60. var chars = arg as char[];
  61. if (chars.Length != 1 || chars[0] != ' ')
  62. {
  63. throw new NotSupportedException();
  64. }
  65. }
  66. static bool TryGetCastTargetDbTypeString(Type sourceType, Type targetType, out string dbTypeString, bool throwNotSupportedException = true)
  67. {
  68. dbTypeString = null;
  69. sourceType = Utils.GetUnderlyingType(sourceType);
  70. targetType = Utils.GetUnderlyingType(targetType);
  71. if (sourceType == targetType)
  72. return false;
  73. if (CastTypeMap.TryGetValue(targetType, out dbTypeString))
  74. {
  75. return true;
  76. }
  77. if (throwNotSupportedException)
  78. throw new NotSupportedException(AppendNotSupportedCastErrorMsg(sourceType, targetType));
  79. else
  80. return false;
  81. }
  82. static string AppendNotSupportedCastErrorMsg(Type sourceType, Type targetType)
  83. {
  84. return string.Format("Does not support the type '{0}' converted to type '{1}'.", sourceType.FullName, targetType.FullName);
  85. }
  86. static void DbFunction_DATEADD(SqlGenerator generator, string interval, DbMethodCallExpression exp)
  87. {
  88. //DATE_ADD(now(),INTERVAL 1 day),DATE_ADD(now(),INTERVAL 10 MINUTE)
  89. generator._sqlBuilder.Append("DATE_ADD(");
  90. exp.Object.Accept(generator);
  91. generator._sqlBuilder.Append(",INTERVAL ");
  92. exp.Arguments[0].Accept(generator);
  93. generator._sqlBuilder.Append(" ", interval);
  94. generator._sqlBuilder.Append(")");
  95. }
  96. static void DbFunction_DATEPART(SqlGenerator generator, string functionName, DbExpression exp)
  97. {
  98. generator._sqlBuilder.Append(functionName);
  99. generator._sqlBuilder.Append("(");
  100. exp.Accept(generator);
  101. generator._sqlBuilder.Append(")");
  102. }
  103. static void DbFunction_DATEDIFF(SqlGenerator generator, string interval, DbExpression startDateTimeExp, DbExpression endDateTimeExp)
  104. {
  105. //TIMESTAMPDIFF(HOUR,'2003-02-01 11:00','2003-02-01 12:00');
  106. generator._sqlBuilder.Append("TIMESTAMPDIFF(");
  107. generator._sqlBuilder.Append(interval);
  108. generator._sqlBuilder.Append(",");
  109. startDateTimeExp.Accept(generator);
  110. generator._sqlBuilder.Append(",");
  111. endDateTimeExp.Accept(generator);
  112. generator._sqlBuilder.Append(")");
  113. }
  114. #region AggregateFunction
  115. static void Aggregate_Count(SqlGenerator generator)
  116. {
  117. generator._sqlBuilder.Append("COUNT(1)");
  118. }
  119. static void Aggregate_LongCount(SqlGenerator generator)
  120. {
  121. generator._sqlBuilder.Append("COUNT(1)");
  122. }
  123. static void Aggregate_Max(SqlGenerator generator, DbExpression exp, Type retType)
  124. {
  125. AppendAggregateFunction(generator, exp, retType, "MAX", false);
  126. }
  127. static void Aggregate_Min(SqlGenerator generator, DbExpression exp, Type retType)
  128. {
  129. AppendAggregateFunction(generator, exp, retType, "MIN", false);
  130. }
  131. static void Aggregate_Sum(SqlGenerator generator, DbExpression exp, Type retType)
  132. {
  133. AppendAggregateFunction(generator, exp, retType, "SUM", true);
  134. }
  135. static void Aggregate_Average(SqlGenerator generator, DbExpression exp, Type retType)
  136. {
  137. AppendAggregateFunction(generator, exp, retType, "AVG", true);
  138. }
  139. static void AppendAggregateFunction(SqlGenerator generator, DbExpression exp, Type retType, string functionName, bool withCast)
  140. {
  141. string dbTypeString = null;
  142. if (withCast == true)
  143. {
  144. Type unType = Utils.GetUnderlyingType(retType);
  145. if (unType != UtilConstants.TypeOfDecimal/* We don't know the precision and scale,so,we can not cast exp to decimal,otherwise cause problems. */ && CastTypeMap.TryGetValue(unType, out dbTypeString))
  146. {
  147. generator._sqlBuilder.Append("CAST(");
  148. }
  149. }
  150. generator._sqlBuilder.Append(functionName, "(");
  151. exp.Accept(generator);
  152. generator._sqlBuilder.Append(")");
  153. if (dbTypeString != null)
  154. {
  155. generator._sqlBuilder.Append(" AS ", dbTypeString, ")");
  156. }
  157. }
  158. #endregion
  159. }
  160. }