mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update GBase
This commit is contained in:
parent
17ec5dba52
commit
d4b4a91908
@ -8,3 +8,6 @@ dotnet_diagnostic.CS8625.severity = none
|
|||||||
|
|
||||||
# CS8604: 引用类型参数可能为 null。
|
# CS8604: 引用类型参数可能为 null。
|
||||||
dotnet_diagnostic.CS8604.severity = none
|
dotnet_diagnostic.CS8604.severity = none
|
||||||
|
|
||||||
|
# CS8600: 将 null 字面量或可能为 null 的值转换为非 null 类型。
|
||||||
|
dotnet_diagnostic.CS8600.severity = none
|
||||||
|
@ -15,6 +15,20 @@ namespace SqlSugar.GBase
|
|||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
public override string SqlDateNow
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "sysdate";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public override string FullSqlDateNow
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "select systimestamp from dual";
|
||||||
|
}
|
||||||
|
}
|
||||||
public override string GetTranslationTableName(string name)
|
public override string GetTranslationTableName(string name)
|
||||||
{
|
{
|
||||||
Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
|
Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
|
||||||
|
@ -16,6 +16,8 @@ namespace SqlSugar.GBase
|
|||||||
{
|
{
|
||||||
base.DbMehtods = new GBaseMethod();
|
base.DbMehtods = new GBaseMethod();
|
||||||
}
|
}
|
||||||
|
public override string SqlTranslationLeft { get { return ""; } }
|
||||||
|
public override string SqlTranslationRight { get { return ""; } }
|
||||||
|
|
||||||
}
|
}
|
||||||
public partial class GBaseMethod : DefaultDbMethod, IDbMethods
|
public partial class GBaseMethod : DefaultDbMethod, IDbMethods
|
||||||
@ -41,6 +43,11 @@ namespace SqlSugar.GBase
|
|||||||
return string.Format(" datepart({0},{1}) ", parameter2.MemberValue, parameter.MemberName);
|
return string.Format(" datepart({0},{1}) ", parameter2.MemberValue, parameter.MemberName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public override string GetDate()
|
||||||
|
{
|
||||||
|
return " sysdate ";
|
||||||
|
}
|
||||||
|
|
||||||
public override string HasValue(MethodCallExpressionModel model)
|
public override string HasValue(MethodCallExpressionModel model)
|
||||||
{
|
{
|
||||||
if (model.Args[0].Type == UtilConstants.GuidType)
|
if (model.Args[0].Type == UtilConstants.GuidType)
|
||||||
|
@ -2,17 +2,22 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SqlSugar.GBase
|
namespace SqlSugar.GBase
|
||||||
{
|
{
|
||||||
public class GBaseQueryBuilder: QueryBuilder
|
public class GBaseQueryBuilder: QueryBuilder
|
||||||
{
|
{
|
||||||
|
public override bool IsComplexModel(string sql)
|
||||||
|
{
|
||||||
|
return Regex.IsMatch(sql, @"AS ""\w+\.\w+""") || Regex.IsMatch(sql, @"AS ""\w+\.\w+\.\w+""");
|
||||||
|
}
|
||||||
public override string SqlTemplate
|
public override string SqlTemplate
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "SELECT {0}{"+UtilConstants.ReplaceKey+"} FROM {1}{2}{3}{4}";
|
return "SELECT {0}{" + UtilConstants.ReplaceKey + "} FROM {1}{2}{3}{4}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public override string ToSqlString()
|
public override string ToSqlString()
|
||||||
@ -35,13 +40,13 @@ namespace SqlSugar.GBase
|
|||||||
}
|
}
|
||||||
Take = oldTake;
|
Take = oldTake;
|
||||||
Skip = oldSkip;
|
Skip = oldSkip;
|
||||||
result =this.Context.SqlQueryable<object>(result).Skip(Skip??0).Take(Take??0).ToSql().Key;
|
result = this.Context.SqlQueryable<object>(result).Skip(Skip ?? 0).Take(Take ?? 0).ToSql().Key;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (result.IndexOf("-- No table") > 0)
|
if (TranLock != null)
|
||||||
{
|
{
|
||||||
return "-- No table";
|
result = result + TranLock;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -52,71 +57,63 @@ namespace SqlSugar.GBase
|
|||||||
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
|
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
|
||||||
AppendFilter();
|
AppendFilter();
|
||||||
sql = new StringBuilder();
|
sql = new StringBuilder();
|
||||||
var oldOrderByValue = this.OrderByValue;
|
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY " + this.Builder.SqlDateNow + " ";
|
||||||
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY GetDate() ";
|
|
||||||
if (this.PartitionByValue.HasValue())
|
if (this.PartitionByValue.HasValue())
|
||||||
{
|
{
|
||||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||||
}
|
}
|
||||||
var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false;
|
var isRowNumber = Skip != null || Take != null;
|
||||||
var isTop = (Skip == null && Take != null && DisableTop == false);
|
var rowNumberString = string.Format(",CAST(ROW_NUMBER() OVER({0}) AS INT) AS RowIndex ", GetOrderByString);
|
||||||
var isRowNumber = (Skip != null || Take != null) && !isFirst && !isTop;
|
|
||||||
if (!isRowNumber && oldOrderByValue == null) { this.OrderByValue = null; }
|
|
||||||
if (isFirst && oldOrderByValue == "ORDER BY GETDATE() ") { this.OrderByValue = null; }
|
|
||||||
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
|
||||||
string groupByValue = GetGroupByString + HavingInfos;
|
string groupByValue = GetGroupByString + HavingInfos;
|
||||||
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
||||||
if (isIgnoreOrderBy) { orderByValue = null; }
|
if (isIgnoreOrderBy) { orderByValue = null; }
|
||||||
sql.AppendFormat(SqlTemplate, GetSelect(isFirst,isTop), base.GetTableNameString, base.GetWhereValueString, groupByValue, orderByValue);
|
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
|
||||||
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
|
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
|
||||||
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
|
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
|
||||||
var result = (isFirst || isTop) ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip);
|
var result = ToPageSql(sql.ToString(), this.Take, this.Skip);
|
||||||
if (ExternalPageIndex > 0)
|
if (ExternalPageIndex > 0)
|
||||||
{
|
{
|
||||||
if (externalOrderBy.IsNullOrEmpty())
|
if (externalOrderBy.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
externalOrderBy = " ORDER BY GetDate() ";
|
externalOrderBy = " ORDER BY " + this.Builder.SqlDateNow + " ";
|
||||||
}
|
}
|
||||||
result = string.Format("SELECT *,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
|
result = string.Format("SELECT ExternalTable.*,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
|
||||||
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
|
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
|
||||||
}
|
}
|
||||||
this.OrderByValue = oldOrderBy;
|
this.OrderByValue = oldOrderBy;
|
||||||
if (!string.IsNullOrEmpty(this.Offset))
|
|
||||||
{
|
|
||||||
if (this.OrderByValue.IsNullOrEmpty())
|
|
||||||
{
|
|
||||||
result += " ORDER BY GETDATE() ";
|
|
||||||
if (this.OldSql.HasValue())
|
|
||||||
this.OldSql += " ORDER BY GETDATE() ";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (this.OldSql.HasValue())
|
|
||||||
this.OldSql += (" " + this.GetOrderByString);
|
|
||||||
}
|
|
||||||
result += this.Offset;
|
|
||||||
|
|
||||||
if (this.OldSql.HasValue())
|
|
||||||
this.OldSql += this.Offset;
|
|
||||||
}
|
|
||||||
result = GetSqlQuerySql(result);
|
result = GetSqlQuerySql(result);
|
||||||
|
if (result.IndexOf("-- No table") > 0)
|
||||||
|
{
|
||||||
|
return "-- No table";
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false)
|
||||||
private string GetSelect(bool isFirst,bool isTop)
|
|
||||||
{
|
{
|
||||||
if (isFirst)
|
string temp = isExternal ? ExternalPageTempalte : PageTempalte;
|
||||||
|
if (skip != null && take == null)
|
||||||
{
|
{
|
||||||
return (" TOP 1 " + GetSelectValue);
|
return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, long.MaxValue);
|
||||||
}
|
}
|
||||||
else if(isTop)
|
else if (skip == null && take != null)
|
||||||
{
|
{
|
||||||
return ($" TOP {this.Take} " + GetSelectValue);
|
return string.Format(temp, sql.ToString(), 1, take.ObjToInt());
|
||||||
|
}
|
||||||
|
else if (skip != null && take != null)
|
||||||
|
{
|
||||||
|
return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, skip.ObjToInt() + take.ObjToInt());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return GetSelectValue;
|
return sql.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string ToPageSql2(string sql, int? pageIndex, int? pageSize, bool isExternal = false)
|
||||||
|
{
|
||||||
|
string temp = isExternal ? ExternalPageTempalte : PageTempalte;
|
||||||
|
return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize + 1, pageIndex * pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user