Update GBase

This commit is contained in:
sunkaixuan
2022-08-28 13:50:06 +08:00
parent 8f659c618a
commit 721fc3246d
3 changed files with 122 additions and 71 deletions

View File

@@ -11,6 +11,16 @@ namespace SqlSugar.GBase
{
this.Context.Ado.ExecuteCommand("select '不支持修改表' from dual ");
}
protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
{
var result= base.EntityColumnToDbColumn(entityInfo, tableName, item);
if (item.UnderType == UtilConstants.GuidType)
{
item.Length = 36;
}
return result;
}
protected override string GetTableName(EntityInfo entityInfo)
{
var table= this.Context.EntityMaintenance.GetTableName(entityInfo.EntityName);

View File

@@ -342,8 +342,8 @@ where a.tabtype in ('V') and not (a.tabname like 'sys%') AND a.tabname <>'dual'
}
else if (item.IsPrimarykey)
{
dataSize = "";
dataType = (dataType + dataSize + " primary key");
dataSize = "";
}
//string identity = item.IsIdentity ? this.CreateTableIdentity : null;
string addItem = string.Format(this.CreateTableColumn, columnName, dataType, dataSize, nullType, "", "");

View File

@@ -9,40 +9,69 @@ namespace SqlSugar.GBase
{
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
#region Sql Template
public override string PageTempalte
{
get
{
return "SELECT {0}{" + UtilConstants.ReplaceKey + "} FROM {1}{2}{3}{4}";
/*
SELECT * FROM TABLE WHERE CONDITION ORDER BY ID DESC LIMIT 0,10
*/
var template = "SELECT {0} FROM {1} {2} {3} {4} LIMIT {5},{6}";
return template;
}
}
public override string DefaultOrderByTemplate
{
get
{
return "ORDER BY SysDate ";
}
}
#endregion
#region Common Methods
public override bool IsComplexModel(string sql)
{
return Regex.IsMatch(sql, @"AS \`\w+\.\w+\`") || Regex.IsMatch(sql, @"AS \`\w+\.\w+\.\w+\`");
}
public override string ToSqlString()
{
var oldTake = Take;
var oldSkip = Skip;
var isDistinctPage = IsDistinct && (Take > 1 || Skip > 1);
if (isDistinctPage)
base.AppendFilter();
string oldOrderValue = this.OrderByValue;
string result = null;
sql = new StringBuilder();
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
if (IsCount) { return sql.ToString(); }
if (Skip != null && Take == null)
{
Take = null;
Skip = null;
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue);
}
var result = _ToSqlString();
if (isDistinctPage)
else if (Skip == null && Take != null)
{
if (this.OrderByValue.HasValue())
{
Take = int.MaxValue;
result = result.Replace("DISTINCT", $" DISTINCT TOP {int.MaxValue} ");
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt());
}
Take = oldTake;
Skip = oldSkip;
result = this.Context.SqlQueryable<object>(result).Skip(Skip ?? 0).Take(Take ?? 0).ToSql().Key;
else if (Skip != null && Take != null)
{
if (Skip == 0 && Take == 1 && this.OrderByValue == "ORDER BY SysDate ")
{
this.OrderByValue = null;
}
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
}
else
{
result = sql.ToString();
}
this.OrderByValue = oldOrderValue;
result = GetSqlQuerySql(result);
if (result.IndexOf("-- No table") > 0)
{
return "-- No table";
}
if (TranLock != null)
{
@@ -50,70 +79,82 @@ namespace SqlSugar.GBase
}
return result;
}
public string _ToSqlString()
private string ToCountSqlString()
{
string oldOrderBy = this.OrderByValue;
string externalOrderBy = oldOrderBy;
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
AppendFilter();
//base.AppendFilter();
string oldOrderValue = this.OrderByValue;
string result = null;
sql = new StringBuilder();
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY " + this.Builder.SqlDateNow + " ";
if (this.PartitionByValue.HasValue())
sql.AppendFormat(SqlTemplate, "Count(*)", GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
if (IsCount)
{
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
}
var isRowNumber = Skip != null || Take != null;
var rowNumberString = string.Format(",CAST(ROW_NUMBER() OVER({0}) AS INT) AS RowIndex ", GetOrderByString);
string groupByValue = GetGroupByString + HavingInfos;
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
if (isIgnoreOrderBy) { orderByValue = null; }
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
var result = ToPageSql(sql.ToString(), this.Take, this.Skip);
if (ExternalPageIndex > 0)
{
if (externalOrderBy.IsNullOrEmpty())
{
externalOrderBy = " ORDER BY " + this.Builder.SqlDateNow + " ";
}
result = string.Format("SELECT ExternalTable.*,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
}
this.OrderByValue = oldOrderBy;
result = GetSqlQuerySql(result);
if (result.IndexOf("-- No table") > 0)
if (sql.ToString().Contains("-- No table"))
{
return "-- No table";
}
return result;
return sql.ToString();
}
public override string ToPageSql(string sql, int? take, int? skip, bool isExternal = false)
if (Skip != null && Take == null)
{
string temp = isExternal ? ExternalPageTempalte : PageTempalte;
if (skip != null && take == null)
{
return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, long.MaxValue);
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue);
}
else if (skip == null && take != null)
else if (Skip == null && Take != null)
{
return string.Format(temp, sql.ToString(), 1, take.ObjToInt());
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 0, Take.ObjToInt());
}
else if (skip != null && take != null)
else if (Skip != null && Take != null)
{
return string.Format(temp, sql.ToString(), skip.ObjToInt() + 1, skip.ObjToInt() + take.ObjToInt());
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
result = string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt() : 0, Take);
}
else
{
return sql.ToString();
result = sql.ToString();
}
this.OrderByValue = oldOrderValue;
return result;
}
public override string ToPageSql2(string sql, int? pageIndex, int? pageSize, bool isExternal = false)
public override string ToCountSql(string sql)
{
string temp = isExternal ? ExternalPageTempalte : PageTempalte;
return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize + 1, pageIndex * pageSize);
if (this.GroupByValue.HasValue() || this.IsDistinct)
{
return base.ToCountSql(sql);
}
else
{
return ToCountSqlString();
}
}
#endregion
#region Get SQL Partial
public override string GetSelectValue
{
get
{
string result = string.Empty;
if (this.SelectValue == null || this.SelectValue is string)
{
result = GetSelectValueByString();
}
else
{
result = GetSelectValueByExpression();
}
if (this.SelectType == ResolveExpressType.SelectMultiple)
{
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this.JoinQueryInfos.Select(it => it.TableName));
}
if (IsDistinct)
{
result = " DISTINCT " + result;
}
return result;
}
}
#endregion
}
}