MySqlQueryBuilder.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System.Linq;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. namespace SqlSugar
  5. {
  6. public partial class MySqlQueryBuilder : QueryBuilder
  7. {
  8. #region Sql Template
  9. public override string PageTempalte
  10. {
  11. get
  12. {
  13. /*
  14. SELECT * FROM TABLE WHERE CONDITION ORDER BY ID DESC LIMIT 0,10
  15. */
  16. var template = "SELECT {0} FROM {1} {2} {3} {4} LIMIT {5},{6}";
  17. return template;
  18. }
  19. }
  20. public override string DefaultOrderByTemplate
  21. {
  22. get
  23. {
  24. return "ORDER BY NOW() ";
  25. }
  26. }
  27. #endregion
  28. #region Common Methods
  29. public override bool IsComplexModel(string sql)
  30. {
  31. return Regex.IsMatch(sql, @"AS \`\w+\.\w+\`")|| Regex.IsMatch(sql, @"AS \`\w+\.\w+\.\w+\`");
  32. }
  33. public override string ToSqlString()
  34. {
  35. base.AppendFilter();
  36. string oldOrderValue = this.OrderByValue;
  37. string result = null;
  38. sql = new StringBuilder();
  39. sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
  40. if (IsCount) { return sql.ToString(); }
  41. if (Skip != null && Take == null)
  42. {
  43. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  44. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt(), long.MaxValue);
  45. }
  46. else if (Skip == null && Take != null)
  47. {
  48. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  49. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt());
  50. }
  51. else if (Skip != null && Take != null)
  52. {
  53. if (Skip == 0 && Take == 1 && this.OrderByValue == "ORDER BY NOW() ")
  54. {
  55. this.OrderByValue = null;
  56. }
  57. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  58. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
  59. }
  60. else
  61. {
  62. result = sql.ToString();
  63. }
  64. this.OrderByValue = oldOrderValue;
  65. result = GetSqlQuerySql(result);
  66. if (result.IndexOf("-- No table") > 0)
  67. {
  68. return "-- No table";
  69. }
  70. if (TranLock != null)
  71. {
  72. result = result + TranLock;
  73. }
  74. return result;
  75. }
  76. private string ToCountSqlString()
  77. {
  78. //base.AppendFilter();
  79. string oldOrderValue = this.OrderByValue;
  80. string result = null;
  81. sql = new StringBuilder();
  82. sql.AppendFormat(SqlTemplate, "Count(*)", GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
  83. if (IsCount)
  84. {
  85. if (sql.ToString().Contains("-- No table"))
  86. {
  87. return "-- No table";
  88. }
  89. return sql.ToString();
  90. }
  91. if (Skip != null && Take == null)
  92. {
  93. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  94. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue);
  95. }
  96. else if (Skip == null && Take != null)
  97. {
  98. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  99. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt());
  100. }
  101. else if (Skip != null && Take != null)
  102. {
  103. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  104. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
  105. }
  106. else
  107. {
  108. result = sql.ToString();
  109. }
  110. this.OrderByValue = oldOrderValue;
  111. return result;
  112. }
  113. public override string ToCountSql(string sql)
  114. {
  115. if (this.GroupByValue.HasValue()||this.IsDistinct)
  116. {
  117. return base.ToCountSql(sql);
  118. }
  119. else
  120. {
  121. return ToCountSqlString();
  122. }
  123. }
  124. #endregion
  125. #region Get SQL Partial
  126. public override string GetSelectValue
  127. {
  128. get
  129. {
  130. string result = string.Empty;
  131. if (this.SelectValue == null || this.SelectValue is string)
  132. {
  133. result = GetSelectValueByString();
  134. }
  135. else
  136. {
  137. result = GetSelectValueByExpression();
  138. }
  139. if (this.SelectType == ResolveExpressType.SelectMultiple)
  140. {
  141. this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this.JoinQueryInfos.Select(it => it.TableName));
  142. }
  143. if (IsDistinct)
  144. {
  145. result = " DISTINCT " + result;
  146. }
  147. if (this.SubToListParameters != null && this.SubToListParameters.Any())
  148. {
  149. result = SubToListMethod(result);
  150. }
  151. return result;
  152. }
  153. }
  154. #endregion
  155. }
  156. }