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.

86 lines
3.2 KiB

2 years ago
  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+\`");
  32. }
  33. public override string ToSqlString()
  34. {
  35. base.AppendFilter();
  36. var 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, (Skip != null || Take != null) ? null : 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 (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  54. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
  55. }
  56. else
  57. {
  58. result = sql.ToString();
  59. }
  60. this.OrderByValue = oldOrderValue;
  61. return result;
  62. }
  63. #endregion
  64. #region Get SQL Partial
  65. public override string GetSelectValue
  66. {
  67. get
  68. {
  69. var result = string.Empty;
  70. result = this.SelectValue == null || this.SelectValue is string ? GetSelectValueByString() : GetSelectValueByExpression();
  71. if (this.SelectType == ResolveExpressType.SelectMultiple)
  72. {
  73. this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this.JoinQueryInfos.Select(it => it.TableName));
  74. }
  75. return result;
  76. }
  77. }
  78. #endregion
  79. }
  80. }