Skip to content

Commit

Permalink
Synchronization code
Browse files Browse the repository at this point in the history
  • Loading branch information
DotNetNext committed Sep 29, 2023
1 parent 4ba9ecc commit 9dcff07
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ equals navColumn.PropertyInfo.GetValue(n).ObjToString()
private void Dynamic(List<object> list, Func<ISugarQueryable<object>, List<object>> selector, EntityInfo listItemEntity, System.Reflection.PropertyInfo navObjectNamePropety, EntityColumnInfo navObjectNameColumnInfo,Expression expression)
{
var args = navObjectNameColumnInfo.PropertyInfo.PropertyType.GetGenericArguments();
if (args.Length == 0)
if (args.Length == 0)
{
DynamicOneToOne(list,selector,listItemEntity, navObjectNamePropety, navObjectNameColumnInfo,expression);
return;
Expand All @@ -504,6 +504,10 @@ private void Dynamic(List<object> list, Func<ISugarQueryable<object>, List<objec
childDb.InitMappingInfo(navEntity);
var navEntityInfo = childDb.EntityMaintenance.GetEntityInfo(navEntity);
var sqlObj = GetWhereSql(navObjectNameColumnInfo.Navigat.Name);
if (IsJsonMapping(navObjectNameColumnInfo, sqlObj))
{
CreateDynamicMappingExpression(sqlObj, navObjectNameColumnInfo.Navigat.Name, navEntityInfo, listItemEntity);
}
Check.ExceptionEasy(sqlObj.MappingExpressions.IsNullOrEmpty(), $"{expression} error,dynamic need MappingField ,Demo: Includes(it => it.Books.MappingField(z=>z.studenId,()=>it.StudentId).ToList())", $"{expression} 解析出错,自定义映射需要 MappingField ,例子: Includes(it => it.Books.MappingField(z=>z.studenId,()=>it.StudentId).ToList())");
if (list.Any() && navObjectNamePropety.GetValue(list.First()) == null)
{
Expand All @@ -530,6 +534,10 @@ private void DynamicOneToOne(List<object> list, Func<ISugarQueryable<object>, Li
var navEntityInfo = this.Context.EntityMaintenance.GetEntityInfo(navEntity);
this.Context.InitMappingInfo(navEntity);
var sqlObj = GetWhereSql(navObjectNameColumnInfo.Navigat.Name);
if (IsJsonMapping(navObjectNameColumnInfo, sqlObj))
{
CreateDynamicMappingExpression(sqlObj, navObjectNameColumnInfo.Navigat.Name, navEntityInfo, listItemEntity);
}
Check.ExceptionEasy(sqlObj.MappingExpressions.IsNullOrEmpty(), $"{expression} error,dynamic need MappingField ,Demo: Includes(it => it.Books.MappingField(z=>z.studenId,()=>it.StudentId).ToList())", $"{expression}解析出错, 自定义映射需要 MappingField ,例子: Includes(it => it.Books.MappingField(z=>z.studenId,()=>it.StudentId).ToList())");
if (list.Any() && navObjectNamePropety.GetValue(list.First()) == null)
{
Expand Down Expand Up @@ -846,5 +854,31 @@ private bool IsEnumNumber(EntityColumnInfo navPkColumn)
navPkColumn?.SqlParameterDbType == null &&
this.Context?.CurrentConnectionConfig?.MoreSettings?.TableEnumIsString != true;
}

private static bool IsJsonMapping(EntityColumnInfo navObjectNameColumnInfo, SqlInfo sqlObj)
{
return sqlObj.MappingExpressions == null && navObjectNameColumnInfo.Navigat.Name.HasValue();
}

private void CreateDynamicMappingExpression(SqlInfo sqlObj, string name, EntityInfo navEntityInfo, EntityInfo listItemEntity)
{
var json = Newtonsoft.Json.Linq.JArray.Parse(name);
sqlObj.MappingExpressions = new List<MappingFieldsExpression>();
foreach (var item in json)
{
string m = item["m"]+"";
string c = item["c"] + "";
Check.ExceptionEasy(m.IsNullOrEmpty() || c.IsNullOrEmpty(), $"{name} Navigation json format error, see documentation", $"{name}导航json格式错误,请看文档");
var cColumn= navEntityInfo.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(c));
Check.ExceptionEasy(cColumn==null, $"{c} does not exist in {navEntityInfo.EntityName}", $"{c}不存在于{navEntityInfo.EntityName}");
var mColumn = listItemEntity.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(m));
Check.ExceptionEasy(cColumn == null, $"{m} does not exist in {listItemEntity.EntityName}", $"{m}不存在于{listItemEntity.EntityName}");
sqlObj.MappingExpressions.Add(new MappingFieldsExpression() {

LeftEntityColumn = cColumn,
RightEntityColumn = mColumn,
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Text.RegularExpressions;
using System.Reflection;
using System.Dynamic;
using System.Threading.Tasks;
using System.Threading.Tasks;

namespace SqlSugar
{
Expand Down Expand Up @@ -1339,6 +1339,15 @@ public virtual ISugarQueryable<TResult> Select<TResult>(Expression expression)
}
return _Select<TResult>(expression);
}
public ISugarQueryable<TResult> Select<TResult>(string expShortName, FormattableString expSelect, Type propertyType)
{
var exp = DynamicCoreHelper.GetMember(typeof(TResult),propertyType, expShortName, expSelect);
return _Select<TResult>(exp);
}
public ISugarQueryable<T> Select(string expShortName, FormattableString expSelect,Type propertyType)
{
return Select<T>(expShortName, expSelect, propertyType);
}
public virtual ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression)
{
if (IsAppendNavColumns())
Expand Down
2 changes: 2 additions & 0 deletions Src/Asp.Net/SqlSugar/Interface/IQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public partial interface ISugarQueryable<T>
Task<bool> AnyAsync(Expression<Func<T, bool>> expression, CancellationToken token);
bool Any();
Task<bool> AnyAsync();
ISugarQueryable<TResult> Select<TResult>(string expShortName, FormattableString expSelect, Type propertyType);
ISugarQueryable<T> Select(string expShortName, FormattableString expSelect, Type propertyType);
ISugarQueryable<TResult> Select<TResult>(Expression expression);
ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression);
ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression,bool isAutoFill);
Expand Down
20 changes: 20 additions & 0 deletions Src/Asp.Net/SqlSugar/Json2Sql/DynamicLinq/DynamicCoreHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ public static LambdaExpression GetWhere(Type entityType, string shortName, Forma

return lambda;
}
public static LambdaExpression GetMember(Type entityType,Type propertyType, string shortName, FormattableString memberSql)
{
var parameter = Expression.Parameter(entityType, "it");

// 提取 FormattableString 中的参数值
var arguments = memberSql.GetArguments();


var sql = ReplaceFormatParameters(memberSql.Format);

// 构建动态表达式,使用常量表达式和 whereSql 中的参数值
var lambda = SqlSugarDynamicExpressionParser.ParseLambda(
new[] { parameter },
propertyType,
sql,
memberSql.GetArguments()
);

return lambda;
}
private static string ReplaceFormatParameters(string format)
{
int parameterIndex = 0; // 起始参数索引
Expand Down

0 comments on commit 9dcff07

Please sign in to comment.