From cb4b157b5351ae354ed70d30307b87243e4b23b1 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 13 Aug 2023 23:24:18 +0800 Subject: [PATCH] Synchronization code --- .../DbBindProvider/DbBindAccessory.cs | 33 ++++++++++++-- .../Entities/QueryableFormat.cs | 17 +++++++ .../SqlBuilderProvider/QueryBuilder.cs | 2 + .../ResolveItems/BaseResolve_Item.cs | 27 +++++++++++ .../MethodCallExpressionResolve.cs | 2 +- .../SqlSugar/Infrastructure/ContextMethods.cs | 7 +++ .../SqlSugar/Utilities/UtilMethods.cs | 45 +++++++++++++++++++ 7 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs index 3c6baa4a4..8b05367a1 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs @@ -32,7 +32,12 @@ namespace SqlSugar { //try //{ - result.Add(entytyList.Build(dataReader)); + var addItem = entytyList.Build(dataReader); + if (this.QueryBuilder?.QueryableFormats?.Any() == true) + { + FormatT(addItem); + } + result.Add(addItem); //} //catch (Exception ex) //{ @@ -76,7 +81,12 @@ namespace SqlSugar { //try //{ - result.Add(entytyList.Build(dataReader)); + var addItem = entytyList.Build(dataReader); + if (this.QueryBuilder?.QueryableFormats?.Any() == true) + { + FormatT(addItem); + } + result.Add(addItem); //} //catch (Exception ex) //{ @@ -99,7 +109,24 @@ namespace SqlSugar } return result; } - + + private void FormatT(T addItem) + { + var formats = this.QueryBuilder.QueryableFormats; + var columns = this.QueryBuilder.Context.EntityMaintenance.GetEntityInfoWithAttr(typeof(T)) + .Columns.Where(it => formats.Any(y => y.PropertyName == it.PropertyName)).ToList(); + if (columns.Any()) + { + foreach (var item in formats) + { + var columnInfo = columns.FirstOrDefault(it => it.PropertyName == item.PropertyName); + var value = columnInfo.PropertyInfo.GetValue(addItem); + value = UtilMethods.GetFormatValue(value, item); + columnInfo.PropertyInfo.SetValue(addItem, value); + } + } + } + private static void ExecuteDataAfterFun(SqlSugarProvider context, Action dataAfterFunc, List result) { if (dataAfterFunc != null) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs new file mode 100644 index 000000000..50a3d7151 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/QueryableProvider/Entities/QueryableFormat.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + internal class QueryableFormat + { + public Type Type { get; set; } + public string TypeString { get; set; } + public string Format { get; set; } + public string PropertyName { get; set; } + public string MethodName { get; set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index 442fe44c2..37748cbf9 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -1003,6 +1003,8 @@ namespace SqlSugar #endregion #region NoCopy + + internal List QueryableFormats { get; set; } internal bool IsClone { get; set; } public bool NoCheckInclude { get; set; } public virtual bool IsSelectNoAll { get; set; } = false; diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs index d3c2ba3c8..ae248757a 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve_Item.cs @@ -13,6 +13,33 @@ namespace SqlSugar { private void ResloveOtherMUC(ExpressionParameter parameter, Expression item, string asName) { + if (ExpressionTool.GetMethodName(item) == "NewGuid") + { + parameter.Context.Result.Append(this.Context.GetAsString2(asName, this.Context.DbMehtods.NewUid(null))); + return; + } + else if (ExpressionTool.GetMethodName(item) == "ToString" + &&(item as MethodCallExpression)?.Arguments?.Count()==1 + && (item as MethodCallExpression)?.Object?.Type!=UtilConstants.DateType + && this.Context?.SugarContext?.QueryBuilder!=null) + { + var format=ExpressionTool.GetExpressionValue((item as MethodCallExpression)?.Arguments[0]); + var childExpression = (item as MethodCallExpression)?.Object; + var type=childExpression.Type; + if (this.Context.SugarContext.QueryBuilder.QueryableFormats == null) + { + this.Context.SugarContext.QueryBuilder.QueryableFormats = new List(); + } + this.Context.SugarContext.QueryBuilder.QueryableFormats.Add(new QueryableFormat() { + Format=format+"", + PropertyName=asName, + Type=type, + TypeString=type.FullName, + MethodName= "ToString" + }); + parameter.Context.Result.Append(this.Context.GetAsString2(asName, GetNewExpressionValue(childExpression))); + return; + } this.Expression = item; this.Start(); if (ExpressionTool.GetMethodName(item) == "MappingColumn") diff --git a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 29fa5146d..c34d10256 100644 --- a/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.NetCore2/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -44,7 +44,7 @@ namespace SqlSugar } else if (methodName == "NewGuid") { - this.Context.Result.Append(this.Context.DbMehtods.GuidNew()); + this.Context.Result.Append(this.Context.DbMehtods.NewUid(null)); return; } else if (methodName == "GetConfigValue") diff --git a/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs index 521e6a3dd..606bd1077 100644 --- a/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs @@ -482,6 +482,12 @@ namespace SqlSugar if (readerValues.Any(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase))) { var addValue = readerValues.ContainsKey(name) ? readerValues[name] : readerValues.First(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Value; + if (addValue!=null&&this.QueryBuilder?.QueryableFormats?.Any(it=>it.PropertyName==name)==true) + { + var valueFomatInfo = this.QueryBuilder?.QueryableFormats?.First(it => it.PropertyName == name); + addValue =UtilMethods.GetFormatValue(addValue,valueFomatInfo); + + } if (addValue == DBNull.Value || addValue == null) { if (item.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.DecType, UtilConstants.DobType, UtilConstants.ByteType)) @@ -524,6 +530,7 @@ namespace SqlSugar return result; } + private void SetAppendColumns(IDataReader dataReader) { diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs index 399636a26..125a3e697 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/UtilMethods.cs @@ -18,6 +18,51 @@ namespace SqlSugar { public class UtilMethods { + /// + /// Available only in Select,Handles logic that cannot be completed by an expression + /// + /// + /// + /// + internal static object GetFormatValue(object addValue, QueryableFormat valueFomatInfo) + { + if (valueFomatInfo.MethodName == "ToString") + { + if (valueFomatInfo.Type == UtilConstants.GuidType) + { + addValue = Guid.Parse(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.ByteType) + { + addValue = Convert.ToByte(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.IntType) + { + addValue = Convert.ToInt32(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.LongType) + { + addValue = Convert.ToInt64(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.UIntType) + { + addValue = Convert.ToUInt32(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.ULongType) + { + addValue = Convert.ToUInt64(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.DecType) + { + addValue = Convert.ToDecimal(addValue + "").ToString(valueFomatInfo.Format); + } + else if (valueFomatInfo.Type == UtilConstants.DobType) + { + addValue = Convert.ToDouble(addValue + "").ToString(valueFomatInfo.Format); + } + } + return addValue; + } public static int CountSubstringOccurrences(string mainString, string searchString) { string[] substrings = mainString.Split(new string[] { searchString }, StringSplitOptions.None);