TDengineQueryBuilder.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using SqlSugar;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace SqlSugar.BzTDengineCore
  6. {
  7. public partial class TDengineQueryBuilder : QueryBuilder
  8. {
  9. #region Sql Template
  10. public override string PageTempalte
  11. {
  12. get
  13. {
  14. /*
  15. SELECT * FROM TABLE WHERE CONDITION ORDER BY ID DESC LIMIT 10 offset 0
  16. */
  17. var template = "SELECT {0} FROM {1} {2} {3} {4} LIMIT {6} offset {5}";
  18. return template;
  19. }
  20. }
  21. public override string DefaultOrderByTemplate
  22. {
  23. get
  24. {
  25. return "ORDER BY NOW() ";
  26. }
  27. }
  28. #endregion Sql Template
  29. #region Common Methods
  30. public override string GetTableNameString
  31. {
  32. get
  33. {
  34. if (TableShortName != null && Context.CurrentConnectionConfig?.MoreSettings?.PgSqlIsAutoToLower == false)
  35. {
  36. TableShortName = Builder.GetTranslationColumnName(TableShortName);
  37. }
  38. return base.GetTableNameString;
  39. }
  40. }
  41. public override bool IsComplexModel(string sql)
  42. {
  43. return Regex.IsMatch(sql, @"AS ""\w+\.\w+""") || Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
  44. }
  45. public override string ToSqlString()
  46. {
  47. base.AppendFilter();
  48. string oldOrderValue = OrderByValue;
  49. string result = null;
  50. sql = new StringBuilder();
  51. sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, Skip != null || Take != null ? null : GetOrderByString);
  52. if (IsCount) { return sql.ToString(); }
  53. if (Skip != null && Take == null)
  54. {
  55. if (OrderByValue == "ORDER BY ") OrderByValue += GetSelectValue.Split(',')[0];
  56. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, Skip != null || Take != null ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue);
  57. }
  58. else if (Skip == null && Take != null)
  59. {
  60. if (OrderByValue == "ORDER BY ") OrderByValue += GetSelectValue.Split(',')[0];
  61. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt());
  62. }
  63. else if (Skip != null && Take != null)
  64. {
  65. if (OrderByValue == "ORDER BY ") OrderByValue += GetSelectValue.Split(',')[0];
  66. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
  67. }
  68. else
  69. {
  70. result = sql.ToString();
  71. }
  72. OrderByValue = oldOrderValue;
  73. result = GetSqlQuerySql(result);
  74. if (result.IndexOf("-- No table") > 0)
  75. {
  76. return "-- No table";
  77. }
  78. if (TranLock != null)
  79. {
  80. result = result + TranLock;
  81. }
  82. return result;
  83. }
  84. #endregion Common Methods
  85. #region Get SQL Partial
  86. public override string GetSelectValue
  87. {
  88. get
  89. {
  90. string result = string.Empty;
  91. if (SelectValue == null || SelectValue is string)
  92. {
  93. result = GetSelectValueByString();
  94. }
  95. else
  96. {
  97. result = GetSelectValueByExpression();
  98. }
  99. if (SelectType == ResolveExpressType.SelectMultiple)
  100. {
  101. SelectCacheKey = SelectCacheKey + string.Join("-", JoinQueryInfos.Select(it => it.TableName));
  102. }
  103. if (IsDistinct)
  104. {
  105. result = "distinct " + result;
  106. }
  107. if (SubToListParameters != null && SubToListParameters.Any())
  108. {
  109. result = SubToListMethod(result);
  110. }
  111. return result;
  112. }
  113. }
  114. #endregion Get SQL Partial
  115. }
  116. }