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.

80 lines
3.5 KiB

2 years ago
  1. using System.Text;
  2. using System.Text.RegularExpressions;
  3. namespace SqlSugar
  4. {
  5. public class OracleQueryBuilder : QueryBuilder
  6. {
  7. public override bool IsComplexModel(string sql)
  8. {
  9. return Regex.IsMatch(sql, @"AS ""\w+\.\w+""");
  10. }
  11. public override string SqlTemplate
  12. {
  13. get
  14. {
  15. return "SELECT {0}{"+UtilConstants.ReplaceKey+"} FROM {1}{2}{3}{4}";
  16. }
  17. }
  18. public override string ToSqlString()
  19. {
  20. var oldOrderBy = this.OrderByValue;
  21. var externalOrderBy = oldOrderBy;
  22. var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
  23. AppendFilter();
  24. sql = new StringBuilder();
  25. if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY "+ this.Builder.SqlDateNow + " ";
  26. if (this.PartitionByValue.HasValue())
  27. {
  28. this.OrderByValue = this.PartitionByValue + this.OrderByValue;
  29. }
  30. var isRowNumber = Skip != null || Take != null;
  31. var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
  32. var groupByValue = GetGroupByString + HavingInfos;
  33. var orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
  34. if (isIgnoreOrderBy) { orderByValue = null; }
  35. sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
  36. sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
  37. if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
  38. var result = ToPageSql(sql.ToString(), this.Take, this.Skip);
  39. if (ExternalPageIndex > 0)
  40. {
  41. if (externalOrderBy.IsNullOrEmpty())
  42. {
  43. externalOrderBy = " ORDER BY "+ this.Builder.SqlDateNow + " ";
  44. }
  45. result = string.Format("SELECT *,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
  46. result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
  47. }
  48. this.OrderByValue = oldOrderBy;
  49. return result;
  50. }
  51. public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false)
  52. {
  53. var temp = isExternal ? ExternalPageTempalte : PageTempalte;
  54. if (skip != null && take == null)
  55. {
  56. return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, long.MaxValue);
  57. }
  58. else if (skip == null && take != null)
  59. {
  60. return string.Format(temp, sql.ToString(), 1, take.ObjToInt());
  61. }
  62. else if (skip != null && take != null)
  63. {
  64. return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, skip.ObjToInt() + take.ObjToInt());
  65. }
  66. else
  67. {
  68. return sql.ToString();
  69. }
  70. }
  71. public override string ToPageSql2(string sql, int? pageIndex, int? pageSize, bool isExternal = false)
  72. {
  73. var temp = isExternal ? ExternalPageTempalte : PageTempalte;
  74. return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize + 1, pageIndex * pageSize);
  75. }
  76. }
  77. }