OracleQueryBuilder.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. namespace SqlSugar
  8. {
  9. public class OracleQueryBuilder : QueryBuilder
  10. {
  11. public override bool IsSelectNoAll { get; set; } = true;
  12. public override bool IsComplexModel(string sql)
  13. {
  14. return Regex.IsMatch(sql, @"AS ""\w+\.\w+""")|| Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
  15. }
  16. public override string SqlTemplate
  17. {
  18. get
  19. {
  20. return "SELECT {0}{"+UtilConstants.ReplaceKey+"} FROM {1}{2}{3}{4}";
  21. }
  22. }
  23. public override string ToSqlString()
  24. {
  25. if (this.Offset == "true")
  26. {
  27. return OffsetPage();
  28. }
  29. var oldTake = Take;
  30. var oldSkip = Skip;
  31. var isDistinctPage = IsDistinct && (Take > 1 || Skip > 1);
  32. if (isDistinctPage)
  33. {
  34. return OffsetPage();
  35. }
  36. var result = _ToSqlString();
  37. //if (isDistinctPage)
  38. //{
  39. // if (this.OrderByValue.HasValue())
  40. // {
  41. // Take = int.MaxValue;
  42. // result = result.Replace("DISTINCT", $" DISTINCT TOP {int.MaxValue} ");
  43. // }
  44. // Take = oldTake;
  45. // Skip = oldSkip;
  46. // result = this.Context.SqlQueryable<object>(result).Skip(Skip??0).Take(Take??0).ToSql().Key;
  47. //}
  48. if (TranLock != null)
  49. {
  50. result = result + TranLock;
  51. }
  52. return result;
  53. }
  54. private string OffsetPage()
  55. {
  56. var skip = this.Skip??1;
  57. var take = this.Take;
  58. this.Skip = null;
  59. this.Take = null;
  60. this.Offset = null;
  61. var pageSql = $"SELECT * FROM ( SELECT PAGETABLE1.*,ROWNUM PAGEINDEX FROM( { this.ToSqlString() }) PAGETABLE1 WHERE ROWNUM<={skip+take}) WHERE PAGEINDEX>={(skip==0?skip:(skip+1))}";
  62. return pageSql;
  63. }
  64. public string _ToSqlString()
  65. {
  66. string oldOrderBy = this.OrderByValue;
  67. string externalOrderBy = oldOrderBy;
  68. var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
  69. AppendFilter();
  70. sql = new StringBuilder();
  71. if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY "+ this.Builder.SqlDateNow + " ";
  72. if (this.PartitionByValue.HasValue())
  73. {
  74. this.OrderByValue = this.PartitionByValue + this.OrderByValue;
  75. }
  76. var isRowNumber = Skip != null || Take != null;
  77. var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
  78. string groupByValue = GetGroupByString + HavingInfos;
  79. string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
  80. if (isIgnoreOrderBy) { orderByValue = null; }
  81. sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
  82. sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
  83. if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
  84. var result = ToPageSql(sql.ToString(), this.Take, this.Skip);
  85. if (this.GetGroupByString==null&&this.Take == 1 && this.Skip == 0&&oldOrderBy== "ORDER BY sysdate ")
  86. {
  87. result = $" {sql.ToString()} {(this.WhereInfos.Any()?"AND":"WHERE")} ROWNUM = 1 ";
  88. result = result.Replace(rowNumberString, " ");
  89. }
  90. if (ExternalPageIndex > 0)
  91. {
  92. if (externalOrderBy.IsNullOrEmpty())
  93. {
  94. externalOrderBy = " ORDER BY "+ this.Builder.SqlDateNow + " ";
  95. }
  96. result = string.Format("SELECT ExternalTable.*,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
  97. result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
  98. }
  99. this.OrderByValue = oldOrderBy;
  100. result = GetSqlQuerySql(result);
  101. if (result.IndexOf("-- No table") > 0)
  102. {
  103. return "-- No table";
  104. }
  105. return result;
  106. }
  107. public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false)
  108. {
  109. string temp = isExternal ? ExternalPageTempalte : PageTempalte;
  110. if (skip != null && take == null)
  111. {
  112. return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, long.MaxValue);
  113. }
  114. else if (skip == null && take != null)
  115. {
  116. return string.Format(temp, sql.ToString(), 1, take.ObjToInt());
  117. }
  118. else if (skip != null && take != null)
  119. {
  120. return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, skip.ObjToInt() + take.ObjToInt());
  121. }
  122. else
  123. {
  124. return sql.ToString();
  125. }
  126. }
  127. public override string ToPageSql2(string sql, int? pageIndex, int? pageSize, bool isExternal = false)
  128. {
  129. string temp = isExternal ? ExternalPageTempalte : PageTempalte;
  130. return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize + 1, pageIndex * pageSize);
  131. }
  132. }
  133. }