SqlGenerator_Helper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.SQLite
  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. /* DATETIME(@P_0,'+' || 1 || ' years') */
  89. generator._sqlBuilder.Append("DATETIME(");
  90. exp.Object.Accept(generator);
  91. generator._sqlBuilder.Append(",'+' || ");
  92. exp.Arguments[0].Accept(generator);
  93. generator._sqlBuilder.Append(" || ' ", interval, "'");
  94. generator._sqlBuilder.Append(")");
  95. }
  96. static void DbFunction_DATEPART(SqlGenerator generator, string interval, DbExpression exp)
  97. {
  98. /* CAST(STRFTIME('%M','2016-08-06 09:01:24') AS INTEGER) */
  99. generator._sqlBuilder.Append("CAST(");
  100. generator._sqlBuilder.Append("STRFTIME('%", interval, "',");
  101. exp.Accept(generator);
  102. generator._sqlBuilder.Append(")");
  103. generator._sqlBuilder.Append(" AS INTEGER)");
  104. }
  105. #region AggregateFunction
  106. static void Aggregate_Count(SqlGenerator generator)
  107. {
  108. generator._sqlBuilder.Append("COUNT(1)");
  109. }
  110. static void Aggregate_LongCount(SqlGenerator generator)
  111. {
  112. generator._sqlBuilder.Append("COUNT(1)");
  113. }
  114. static void Aggregate_Max(SqlGenerator generator, DbExpression exp, Type retType)
  115. {
  116. AppendAggregateFunction(generator, exp, retType, "MAX", false);
  117. }
  118. static void Aggregate_Min(SqlGenerator generator, DbExpression exp, Type retType)
  119. {
  120. AppendAggregateFunction(generator, exp, retType, "MIN", false);
  121. }
  122. static void Aggregate_Sum(SqlGenerator generator, DbExpression exp, Type retType)
  123. {
  124. AppendAggregateFunction(generator, exp, retType, "SUM", true);
  125. }
  126. static void Aggregate_Average(SqlGenerator generator, DbExpression exp, Type retType)
  127. {
  128. AppendAggregateFunction(generator, exp, retType, "AVG", true);
  129. }
  130. static void AppendAggregateFunction(SqlGenerator generator, DbExpression exp, Type retType, string functionName, bool withCast)
  131. {
  132. string dbTypeString = null;
  133. if (withCast == true)
  134. {
  135. Type unType = Utils.GetUnderlyingType(retType);
  136. 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))
  137. {
  138. generator._sqlBuilder.Append("CAST(");
  139. }
  140. }
  141. generator._sqlBuilder.Append(functionName, "(");
  142. exp.Accept(generator);
  143. generator._sqlBuilder.Append(")");
  144. if (dbTypeString != null)
  145. {
  146. generator._sqlBuilder.Append(" AS ", dbTypeString, ")");
  147. }
  148. }
  149. #endregion
  150. }
  151. }