From 44a15fdbfa03446d139fadf94f408494ad6a8331 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 2 Apr 2025 22:45:48 +0800 Subject: [PATCH] Synchronization code --- .../ExecuteNavProvider/InsertNavProvider.cs | 6 ++++- .../InsertNavProviderOneToMany.cs | 16 ++++++++++++ .../ExecuteNavProvider/UpdateNavOneToMany.cs | 15 +++++++++++ .../ExecuteNavProvider/UpdateNavProvider.cs | 4 +++ .../Abstract/FastestProvider/Private.cs | 5 +++- .../InsertableProvider/SplitInsertable.cs | 6 +++++ .../QueryableProvider/QueryableHelper.cs | 8 +++++- .../SqlBuilderProvider/InsertBuilder.cs | 5 ++++ .../SqlBuilderProvider/QueryBuilder.cs | 5 ++++ .../SugarProvider/SqlSugarAccessory.cs | 4 +++ Src/Asp.Net/SqlSugar/Enum/DbType.cs | 1 + .../ExpressionsToSql/Common/ExpressionTool.cs | 23 +++++++++++++++++ .../ResolveItems/BaseResolve_Append.cs | 7 +++++- .../Infrastructure/InstanceFactory.cs | 4 +++ .../Json2Sql/Queryable/QueryableProvider.cs | 25 +++++++++++++++++-- .../SqlSugar/Utilities/UtilConstants.cs | 1 + 16 files changed, 129 insertions(+), 6 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs index 742501ff3..0639dd4e3 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProvider.cs @@ -93,6 +93,10 @@ namespace SqlSugar private InsertNavProvider _ThenInclude(Expression>> expression) where TChild : class, new() { var name = ExpressionTool.GetMemberName(expression); + if (name == null) + { + name =ExpressionTool.GetMemberNameByMethod(expression, name); + } var isRoot = false; if (this._ParentEntity == null) { @@ -121,7 +125,7 @@ namespace SqlSugar InitParentList(); InsertManyToMany(name, nav); } - AddContextInfo(name,isRoot); + AddContextInfo(name, isRoot); return GetResult(); } diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs index d0ee266f1..6911ef1ac 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/InsertNavProviderOneToMany.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -40,6 +41,10 @@ namespace SqlSugar } children.AddRange(childs); } + else if (childs == null && parentNavigateProperty.PropertyInfo.GetValue(item) is IList ilist && ilist != null&& ilist.Count>0) + { + childs = GetIChildsBylList(children, thisFkColumn, parentValue, ilist); + } } var isTreeChild = GetIsTreeChild(parentEntity, thisEntity); Check.ExceptionEasy(thisPkColumn == null, $"{thisEntity.EntityName}need primary key", $"实体{thisEntity.EntityName}需要主键"); @@ -54,6 +59,17 @@ namespace SqlSugar SetNewParent(thisEntity, thisPkColumn); } + private static List GetIChildsBylList(List children, EntityColumnInfo thisFkColumn, object parentValue, IList ilist) where TChild : class, new() + { + List childs = ilist.Cast().ToList(); + foreach (var child in childs) + { + thisFkColumn.PropertyInfo.SetValue(child, parentValue, null); + } + children.AddRange(childs); + return childs; + } + private bool GetIsTreeChild(EntityInfo parentEntity , EntityInfo thisEntity) { return this.NavContext?.Items?.Any() == true && parentEntity.Type == thisEntity.Type; diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavOneToMany.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavOneToMany.cs index 448bd0c0d..fdad7ec1c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavOneToMany.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavOneToMany.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -137,6 +138,10 @@ namespace SqlSugar } children.AddRange(childs); } + else if (childs == null && parentNavigateProperty.PropertyInfo.GetValue(item) is IList ilist && ilist != null && ilist.Count > 0) + { + childs = GetIChildsBylList(children, thisFkColumn, parentValue, ilist); + } ids.Add(parentValue); if (_Options?.OneToManyNoDeleteNull == true && childs == null) { @@ -194,6 +199,16 @@ namespace SqlSugar _NavigateType = null; SetNewParent(thisEntity, thisPkColumn); } + private static List GetIChildsBylList(List children, EntityColumnInfo thisFkColumn, object parentValue, IList ilist) where TChild : class, new() + { + List childs = ilist.Cast().ToList(); + foreach (var child in childs) + { + thisFkColumn.PropertyInfo.SetValue(child, parentValue, null); + } + children.AddRange(childs); + return childs; + } private static bool ParentIsPk(EntityColumnInfo parentNavigateProperty) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs index e53829cef..7f55004cf 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/ExecuteNavProvider/UpdateNavProvider.cs @@ -97,6 +97,10 @@ namespace SqlSugar IsFirst = isRoot && this._ParentList == null; InitParentList(); var name = ExpressionTool.GetMemberName(expression); + if (name == null) + { + name = ExpressionTool.GetMemberNameByMethod(expression, name); + } var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); if (nav.Navigat == null) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs index cb5891328..03f19277d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs @@ -50,8 +50,11 @@ namespace SqlSugar case DbType.Custom: className = InstanceFactory.CustomNamespace + "." + InstanceFactory.CustomDbName + "FastBuilder"; break; + case DbType.GaussDBNative: + className = "SqlSugar.GaussDB.GaussDBFastBuilder"; + break; default: - className = $"SqlSugar.{this.context.CurrentConnectionConfig.DbType}FastBuilder"; + className = $"SqlSugar.{this.context.CurrentConnectionConfig.DbType.ToString().Replace("Native","")}FastBuilder"; break; } var reslut = InstanceFactory.CreateInstance(className); diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs index 8a194fe5a..16a0b1a3e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/SplitInsertable.cs @@ -129,7 +129,10 @@ namespace SqlSugar foreach (var item in groups) { var list = item.Select(it => it.Value as T).ToList(); + var dataEvent = this.Context.CurrentConnectionConfig.AopEvents?.DataExecuting; + this.Context.Aop.DataExecuting = null; var groupInserable = (InsertableProvider)this.Context.Insertable(list); + this.Context.Aop.DataExecuting = dataEvent; groupInserable.InsertBuilder.TableWithString = parent.InsertBuilder.TableWithString; groupInserable.RemoveCacheFunc = parent.RemoveCacheFunc; groupInserable.diffModel = parent.diffModel; @@ -150,7 +153,10 @@ namespace SqlSugar foreach (var item in groups) { var list = item.Select(it => it.Value as T).ToList(); + var dataEvent = this.Context.CurrentConnectionConfig.AopEvents?.DataExecuting; + this.Context.Aop.DataExecuting = null; var groupInserable = (InsertableProvider)this.Context.Insertable(list); + this.Context.Aop.DataExecuting = dataEvent; groupInserable.InsertBuilder.TableWithString = parent.InsertBuilder.TableWithString; groupInserable.RemoveCacheFunc = parent.RemoveCacheFunc; groupInserable.diffModel = parent.diffModel; diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs index 6ca5badb7..1c945ee94 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableHelper.cs @@ -2352,7 +2352,13 @@ namespace SqlSugar var p = new SugarParameter[] { new SugarParameter("@p",re.Value) }; - var value = UtilMethods.GetSqlString(config.DbType, "@p", p, true); + var isNvarchar = true; + if (this.Context.CurrentConnectionConfig?.DbType == DbType.SqlServer + &&this.Context.CurrentConnectionConfig?.MoreSettings?.DisableNvarchar!=true) + { + isNvarchar = false; + } + var value = UtilMethods.GetSqlString(config.DbType, "@p", p, isNvarchar); sql = sql.Replace(re.Name, value); } diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs index cad17a11e..560be8780 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Text; using System.Linq; using System.Linq.Expressions; +using System.Globalization; namespace SqlSugar { public partial class InsertBuilder : IDMLBuilder @@ -266,6 +267,10 @@ namespace SqlSugar { return N+"'" +Convert.ToDouble(value).ToString() + "'"; } + else if (value is decimal v) + { + return v.ToString(CultureInfo.InvariantCulture); + } else { return N+"'" + value.ToString() + "'"; diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index d22153674..7b369ab80 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -965,6 +965,11 @@ namespace SqlSugar { result = "*"; } + if (result.StartsWith(UtilConstants.GroupReplaceKey)) + { + this.GroupByIsReplace = true; + result = result.Replace(UtilConstants.GroupReplaceKey, string.Empty); + } return result; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs index 081a3c35d..f0ecb2c32 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs @@ -553,6 +553,10 @@ namespace SqlSugar Check.Exception(SugarCompatible.IsFramework, "Db2 only support .net core"); InstanceFactory.CustomDllName = "SqlSugar.Db2Core"; break; + case DbType.GaussDBNative: + Check.Exception(SugarCompatible.IsFramework, "GaussDBNative only support .net core"); + InstanceFactory.CustomDllName = "SqlSugar.GaussDBCore"; + break; default: throw new Exception("ConnectionConfig.DbType is null"); } diff --git a/Src/Asp.Net/SqlSugar/Enum/DbType.cs b/Src/Asp.Net/SqlSugar/Enum/DbType.cs index ba2865cd3..c2451a061 100644 --- a/Src/Asp.Net/SqlSugar/Enum/DbType.cs +++ b/Src/Asp.Net/SqlSugar/Enum/DbType.cs @@ -37,6 +37,7 @@ namespace SqlSugar TDSQL, HANA, DB2, + GaussDBNative, Custom =900 } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs index 91d4d7158..9a8d20c44 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs @@ -10,6 +10,29 @@ namespace SqlSugar { public class ExpressionTool { + + public static string GetMemberNameByMethod(Expression expression, string name) + { + if (expression is LambdaExpression lambda) + { + if (lambda.Body is MethodCallExpression method) + { + if (method.Method.Name == "ToList") + { + if (method.Arguments.FirstOrDefault() is { } arg) + { + if (arg is MemberExpression member) + { + name = member.Member.Name; + } + } + } + } + } + + return name; + } + internal static string ResolveMemberValue(ExpressionContext context, Expression item, string value) { if (item is MemberExpression member) diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs index bb6c56aca..9cfa036f0 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Append.cs @@ -179,7 +179,12 @@ namespace SqlSugar } else { - this.Context.Parameters.Add(new SugarParameter(appendValue, value)); + var p = new SugarParameter(appendValue, value); + if (p.DbType == System.Data.DbType.String && this.Context?.SugarContext?.Context?.CurrentConnectionConfig?.MoreSettings?.DisableNvarchar == true) + { + p.DbType = System.Data.DbType.AnsiString; + } + this.Context.Parameters.Add(p); } } else diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/InstanceFactory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/InstanceFactory.cs index d05de1e9f..9a7f0b4af 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/InstanceFactory.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/InstanceFactory.cs @@ -444,6 +444,10 @@ namespace SqlSugar { return "SqlSugar.DB2."+ type+ name; } + else if (type == "GaussDBNative") + { + return "SqlSugar.GaussDB.GaussDB" + name; + } else { //if (!string.IsNullOrEmpty(CustomDllName)) diff --git a/Src/Asp.Net/SqlSugar/Json2Sql/Queryable/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Json2Sql/Queryable/QueryableProvider.cs index da266f654..b124aaff5 100644 --- a/Src/Asp.Net/SqlSugar/Json2Sql/Queryable/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Json2Sql/Queryable/QueryableProvider.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace SqlSugar @@ -47,13 +48,33 @@ namespace SqlSugar return this; } var orderObj = this.SqlBuilder.GroupByModelToSql(models); - this.GroupBy(orderObj.Key); - this.QueryBuilder.Parameters.AddRange(orderObj.Value); + if (orderObj.Value?.Length > 0 && this.Context.CurrentConnectionConfig?.DbType == DbType.SqlServer) + { + var groupBySql = UtilMethods.GetSqlString(DbType.SqlServer, orderObj.Key, orderObj.Value); + this.QueryBuilder.GroupBySql = groupBySql; + this.QueryBuilder.GroupBySqlOld = orderObj.Key; + this.QueryBuilder.GroupParameters = orderObj.Value.ToList(); + this.GroupBy(orderObj.Key); + } + else + { + this.GroupBy(orderObj.Key); + this.QueryBuilder.Parameters.AddRange(orderObj.Value); + } return this; } public ISugarQueryable Select(List models) { var orderObj = this.SqlBuilder.SelectModelToSql(models); + if (this.QueryBuilder.GroupParameters?.Any() == true && this.QueryBuilder.GroupBySql.HasValue()) + { + var selectSql = UtilMethods.GetSqlString(DbType.SqlServer, orderObj.Key, UtilMethods.CopySugarParameters(orderObj.Value.ToList()).ToArray()); + if (selectSql.Contains(this.QueryBuilder.GroupBySql)) + { + this.Select(UtilConstants.GroupReplaceKey+selectSql); + return this; + } + } this.Select(orderObj.Key); this.QueryBuilder.Parameters.AddRange(orderObj.Value); return this; diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilConstants.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilConstants.cs index 6cc194734..421a8000d 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilConstants.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilConstants.cs @@ -15,6 +15,7 @@ namespace SqlSugar internal const string AssemblyName = "SqlSugar"; internal static string ReplaceKey = "{"+Guid.NewGuid()+"}"; internal const string ReplaceCommaKey = "{112A689B-17A1-4A06-9D27-A39EAB8BC3D5}"; + internal const string GroupReplaceKey = "{GroupReplaceKey_l33asdysaas1231s}"; internal static Type UShortType = typeof(ushort); internal static Type ULongType = typeof(ulong);