Synchronization code

This commit is contained in:
sunkaixuan
2023-08-13 23:24:18 +08:00
parent 516f3f50e3
commit cb4b157b53
7 changed files with 129 additions and 4 deletions

View File

@@ -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>(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<T>(SqlSugarProvider context, Action<object, DataAfterModel> dataAfterFunc, List<T> result)
{
if (dataAfterFunc != null)

View File

@@ -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; }
}
}

View File

@@ -1003,6 +1003,8 @@ namespace SqlSugar
#endregion
#region NoCopy
internal List<QueryableFormat> QueryableFormats { get; set; }
internal bool IsClone { get; set; }
public bool NoCheckInclude { get; set; }
public virtual bool IsSelectNoAll { get; set; } = false;

View File

@@ -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<QueryableFormat>();
}
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")

View File

@@ -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")

View File

@@ -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)
{

View File

@@ -18,6 +18,51 @@ namespace SqlSugar
{
public class UtilMethods
{
/// <summary>
/// Available only in Select,Handles logic that cannot be completed by an expression
/// </summary>
/// <param name="addValue"></param>
/// <param name="valueFomatInfo"></param>
/// <returns></returns>
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);