SqlSugar/Src/Asp.Net/SqlSugar.Access/Access/SqlBuilder/AccessQueryBuilder.cs

68 lines
2.9 KiB
C#
Raw Normal View History

2022-02-20 13:54:58 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar.Access
{
2022-02-20 16:32:32 +08:00
public class AccessQueryBuilder : QueryBuilder
2022-02-20 13:54:58 +08:00
{
public override string SqlTemplate
{
get
{
return "SELECT {0}{"+UtilConstants.ReplaceKey+"} FROM {1}{2}{3}{4}";
}
}
public override string ToSqlString()
{
string oldOrderBy = this.OrderByValue;
string externalOrderBy = oldOrderBy;
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
AppendFilter();
sql = new StringBuilder();
2022-02-21 00:04:23 +08:00
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY now() ";
2022-02-20 13:54:58 +08:00
if (this.PartitionByValue.HasValue())
{
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
}
var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false;
var isRowNumber = (Skip != null || Take != null) && !isFirst;
2022-02-21 00:04:23 +08:00
var isPage = isRowNumber;
isRowNumber = false;
Skip = null;
Take = null;
2022-02-20 13:54:58 +08:00
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
string groupByValue = GetGroupByString + HavingInfos;
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
if (isIgnoreOrderBy) { orderByValue = null; }
sql.AppendFormat(SqlTemplate, isFirst ? (" TOP 1 " + GetSelectValue) : GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
var result = isFirst ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip);
if (ExternalPageIndex > 0)
{
if (externalOrderBy.IsNullOrEmpty())
{
2022-02-21 00:04:23 +08:00
externalOrderBy = " ORDER BY now() ";
2022-02-20 13:54:58 +08:00
}
result = string.Format("SELECT *,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
}
this.OrderByValue = oldOrderBy;
if (!string.IsNullOrEmpty(this.Offset))
{
if (this.OrderByValue.IsNullOrEmpty())
{
2022-02-21 00:04:23 +08:00
result += " ORDER BY now() ";
2022-02-20 13:54:58 +08:00
}
result += this.Offset;
}
result = GetSqlQuerySql(result);
return result;
}
}
}