PostgreSQLQueryBuilder.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Linq;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. namespace SqlSugar
  5. {
  6. public partial class PostgreSQLQueryBuilder : 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 10 offset 0
  15. */
  16. var template = "SELECT {0} FROM {1} {2} {3} {4} LIMIT {6} offset {5}";
  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 string GetTableNameString
  30. {
  31. get
  32. {
  33. if (this.TableShortName != null&&this.Context.CurrentConnectionConfig?.MoreSettings?.PgSqlIsAutoToLower==false)
  34. {
  35. this.TableShortName = Builder.GetTranslationColumnName(this.TableShortName);
  36. }
  37. return base.GetTableNameString;
  38. }
  39. }
  40. public override bool IsComplexModel(string sql)
  41. {
  42. return Regex.IsMatch(sql, @"AS ""\w+\.\w+""")|| Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
  43. }
  44. public override string ToSqlString()
  45. {
  46. base.AppendFilter();
  47. string oldOrderValue = this.OrderByValue;
  48. var isNullOrderValue = Skip == 0 && Take == 1 && oldOrderValue == "ORDER BY NOW() ";
  49. if (isNullOrderValue)
  50. {
  51. this.OrderByValue = null;
  52. }
  53. string result = null;
  54. sql = new StringBuilder();
  55. sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
  56. if (IsCount) { return sql.ToString(); }
  57. if (Skip != null && Take == null)
  58. {
  59. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  60. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue);
  61. }
  62. else if (Skip == null && Take != null)
  63. {
  64. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  65. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt());
  66. }
  67. else if (Skip != null && Take != null)
  68. {
  69. if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
  70. result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
  71. }
  72. else
  73. {
  74. result = sql.ToString();
  75. }
  76. this.OrderByValue = oldOrderValue;
  77. result = GetSqlQuerySql(result);
  78. if (result.IndexOf("-- No table") > 0)
  79. {
  80. return "-- No table";
  81. }
  82. if (TranLock != null)
  83. {
  84. result = result + TranLock;
  85. }
  86. //if (result.Contains("uuid_generate_v4()"))
  87. //{
  88. // result=" CREATE EXTENSION IF NOT EXISTS pgcrypto;CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"; "+ result;
  89. //}
  90. return result;
  91. }
  92. #endregion
  93. #region Get SQL Partial
  94. public override string GetSelectValue
  95. {
  96. get
  97. {
  98. string result = string.Empty;
  99. if (this.SelectValue == null || this.SelectValue is string)
  100. {
  101. result = GetSelectValueByString();
  102. }
  103. else
  104. {
  105. result = GetSelectValueByExpression();
  106. }
  107. if (this.SelectType == ResolveExpressType.SelectMultiple)
  108. {
  109. this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this.JoinQueryInfos.Select(it => it.TableName));
  110. }
  111. if (IsDistinct&&result?.TrimStart()?.StartsWith("distinct ")!=true)
  112. {
  113. result = "distinct "+result;
  114. }
  115. if (this.SubToListParameters != null && this.SubToListParameters.Any())
  116. {
  117. result = SubToListMethod(result);
  118. }
  119. return result;
  120. }
  121. }
  122. #endregion
  123. }
  124. }