Synchronization code

This commit is contained in:
sunkaixuan 2023-04-24 16:50:58 +08:00
parent f3f8a5ad4f
commit 8d7ee3197e
9 changed files with 246 additions and 2 deletions

View File

@ -1076,7 +1076,7 @@ namespace SqlSugar
return sql;
}
protected JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
internal JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
{
QueryBuilder.CheckExpressionNew(joinExpression, "Join");
QueryBuilder.JoinExpression = joinExpression;

View File

@ -34,6 +34,8 @@ namespace SqlSugar
public bool IsWhereColumns { get; set; }
public bool? IsListUpdate { get; set; }
public List<string> UpdateColumns { get; set; }
public List<JoinQueryInfo> JoinInfos { get; set; }
public string ShortName { get; set; }
public virtual string SqlTemplate
{
@ -259,6 +261,11 @@ namespace SqlSugar
{
return setValue.First().Value;
}
else if (JoinInfos!=null&&JoinInfos.Any())
{
setValue = SetValues.Where(sv => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Where(sv => sv.Key == Builder.GetNoTranslationColumnName(it.DbColumnName) || sv.Key == Builder.GetNoTranslationColumnName(it.PropertyName));
return Builder.GetTranslationColumnName(this.ShortName)+"."+ setValue.First().Key+"="+ setValue.First().Value ;
}
}
var result = Builder.GetTranslationColumnName(it.DbColumnName) + "=" + GetDbColumn(it,this.Context.Ado.SqlParameterKeyWord + it.DbColumnName);
return result;
@ -294,8 +301,26 @@ namespace SqlSugar
whereString += Builder.GetTranslationColumnName(item) + "=" + this.Context.Ado.SqlParameterKeyWord + item;
}
}
if (this.JoinInfos != null && this.JoinInfos.Any())
{
return GetJoinUpdate(columnsString, ref whereString);
}
return string.Format(SqlTemplate, GetTableNameString, columnsString, whereString);
}
protected virtual string GetJoinUpdate(string columnsString, ref string whereString)
{
var tableName = Builder.GetTranslationColumnName(this.TableName);
this.TableName = Builder.GetTranslationColumnName(this.ShortName);
var joinString = $" FROM {tableName} {Builder.GetTranslationColumnName(this.ShortName)} ";
foreach (var item in this.JoinInfos)
{
joinString += $"\r\n JOIN {Builder.GetTranslationColumnName(item.TableName)} {Builder.GetTranslationColumnName(item.ShortName)} ON {item.JoinWhere} ";
}
whereString = joinString + "\r\n" + whereString;
return string.Format(SqlTemplate, GetTableNameString, columnsString, whereString);
}
public virtual void ActionMinDate()
{
if (this.Parameters != null)

View File

@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace SqlSugar
{
public partial class UpdateableProvider<T> : IUpdateable<T> where T : class, new()
public partial class UpdateableProvider<T> : IUpdateable<T> where T : class, new()
{
#region Property
public SqlSugarProvider Context { get; internal set; }
@ -163,6 +163,15 @@ namespace SqlSugar
#endregion
#region Common
public IUpdateable<T, T2> InnerJoin<T2>(Expression<Func<T, T2, bool>> joinExpress)
{
UpdateableProvider<T, T2> result = new UpdateableProvider<T, T2>();
result.updateableObj = this;
var querybale=this.Context.Queryable<T>().LeftJoin<T2>(joinExpress);
result.updateableObj.UpdateBuilder.JoinInfos = querybale.QueryBuilder.JoinQueryInfos;
result.updateableObj.UpdateBuilder.ShortName = joinExpress.Parameters.FirstOrDefault()?.Name;
return result;
}
public IUpdateable<T> Clone()
{
this.Context.SugarActionType = SugarActionType.Update;

View File

@ -0,0 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace SqlSugar
{
public class UpdateableProvider<T, T2> : IUpdateable<T, T2> where T : class,new()
{
public IUpdateable<T> updateableObj { get; set; }
public int ExecuteCommand()
{
return this.updateableObj.ExecuteCommand();
}
public Task<int> ExecuteCommandAsync()
{
return this.updateableObj.ExecuteCommandAsync();
}
public IUpdateable<T, T2, T3> InnerJoin<T3>(Expression<Func<T, T2, T3, bool>> joinExpress)
{
UpdateableProvider<T, T2,T3> result = new UpdateableProvider<T, T2,T3>();
result.updateableObj = updateableObj;
var joinIno = ((QueryableProvider<T>)updateableObj.UpdateBuilder.Context.Queryable<T>()).GetJoinInfo(joinExpress, JoinType.Inner);
result.updateableObj.UpdateBuilder.JoinInfos.Add(joinIno);
result.updateableObj.UpdateBuilder.ShortName = joinExpress.Parameters.FirstOrDefault()?.Name;
return result;
}
public IUpdateable<T, T2> SetColumns(Expression<Func<T, T2, T>> columns)
{
var exp = ((columns as LambdaExpression).Body as MemberInitExpression).Bindings;
var items=ExpressionTool.GetMemberBindingItemList(exp);
var UpdateBuilder = updateableObj.UpdateBuilder;
var SqlBuilder = UpdateBuilder.Builder;
foreach (var item in items)
{
var dbColumnName=updateableObj.UpdateBuilder.Context.EntityMaintenance.GetDbColumnName<T>(item.Key);
var value = updateableObj.UpdateBuilder.GetExpressionValue(ExpressionTool.RemoveConvert(item.Value), ResolveExpressType.WhereMultiple).GetString();
this.updateableObj.UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(dbColumnName, value));
}
UpdateBuilder.DbColumnInfoList = UpdateBuilder.DbColumnInfoList
.Where(it => it.UpdateServerTime == true || it.UpdateSql.HasValue() || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
return this;
}
public IUpdateable<T, T2> Where(Expression<Func<T, T2, bool>> whereExpression)
{
var value = updateableObj.UpdateBuilder.GetExpressionValue(whereExpression, ResolveExpressType.WhereMultiple).GetString();
updateableObj.UpdateBuilder.WhereValues.Add(value);
return this;
}
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace SqlSugar
{
public class UpdateableProvider<T, T2, T3> : IUpdateable<T, T2, T3> where T : class, new()
{
internal IUpdateable<T> updateableObj;
public int ExecuteCommand()
{
return this.updateableObj.ExecuteCommand();
}
public Task<int> ExecuteCommandAsync()
{
return this.updateableObj.ExecuteCommandAsync();
}
public IUpdateable<T, T2, T3, T4> InnerJoin<T4>(Expression<Func<T, T2, T3, T4, bool>> joinExpress)
{
UpdateableProvider<T, T2, T3,T4> result = new UpdateableProvider<T, T2, T3,T4>();
result.updateableObj = updateableObj;
var joinIno = ((QueryableProvider<T>)updateableObj.UpdateBuilder.Context.Queryable<T>()).GetJoinInfo(joinExpress, JoinType.Inner);
result.updateableObj.UpdateBuilder.JoinInfos.Add(joinIno);
result.updateableObj.UpdateBuilder.ShortName = joinExpress.Parameters.FirstOrDefault()?.Name;
return result;
}
public IUpdateable<T, T2, T3> SetColumns(Expression<Func<T, T2, T3, T>> columns)
{
var exp = ((columns as LambdaExpression).Body as MemberInitExpression).Bindings;
var items = ExpressionTool.GetMemberBindingItemList(exp);
var UpdateBuilder = updateableObj.UpdateBuilder;
var SqlBuilder = UpdateBuilder.Builder;
foreach (var item in items)
{
var dbColumnName = updateableObj.UpdateBuilder.Context.EntityMaintenance.GetDbColumnName<T>(item.Key);
var value = updateableObj.UpdateBuilder.GetExpressionValue(ExpressionTool.RemoveConvert(item.Value), ResolveExpressType.WhereMultiple).GetString();
this.updateableObj.UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(dbColumnName, value));
}
UpdateBuilder.DbColumnInfoList = UpdateBuilder.DbColumnInfoList
.Where(it => it.UpdateServerTime == true || it.UpdateSql.HasValue() || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
return this;
}
public IUpdateable<T, T2, T3> Where(Expression<Func<T, T2, T3, bool>> whereExpression)
{
var value = updateableObj.UpdateBuilder.GetExpressionValue(whereExpression, ResolveExpressType.WhereMultiple).GetString();
updateableObj.UpdateBuilder.WhereValues.Add(value);
return this;
}
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace SqlSugar
{
public class UpdateableProvider<T, T2, T3, T4> : IUpdateable<T, T2, T3, T4> where T : class, new()
{
public IUpdateable<T> updateableObj { get; set; }
public int ExecuteCommand()
{
return this.updateableObj.ExecuteCommand();
}
public Task<int> ExecuteCommandAsync()
{
return this.updateableObj.ExecuteCommandAsync();
}
public IUpdateable<T, T2, T3, T4> SetColumns(Expression<Func<T, T2, T3, T4, T>> columns)
{
var exp = ((columns as LambdaExpression).Body as MemberInitExpression).Bindings;
var items = ExpressionTool.GetMemberBindingItemList(exp);
var UpdateBuilder = updateableObj.UpdateBuilder;
var SqlBuilder = UpdateBuilder.Builder;
foreach (var item in items)
{
var dbColumnName = updateableObj.UpdateBuilder.Context.EntityMaintenance.GetDbColumnName<T>(item.Key);
var value = updateableObj.UpdateBuilder.GetExpressionValue(ExpressionTool.RemoveConvert(item.Value), ResolveExpressType.WhereMultiple).GetString();
this.updateableObj.UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(dbColumnName, value));
}
UpdateBuilder.DbColumnInfoList = UpdateBuilder.DbColumnInfoList
.Where(it => it.UpdateServerTime == true || it.UpdateSql.HasValue() || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
return this;
}
public IUpdateable<T, T2, T3, T4> Where(Expression<Func<T, T2, T3, T4, bool>> whereExpression)
{
var value = updateableObj.UpdateBuilder.GetExpressionValue(whereExpression, ResolveExpressType.WhereMultiple).GetString();
updateableObj.UpdateBuilder.WhereValues.Add(value);
return this;
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
@ -8,6 +9,23 @@ namespace SqlSugar
{
public class ExpressionTool
{
public static Dictionary<string, Expression> GetMemberBindingItemList(ReadOnlyCollection<MemberBinding> exp)
{
Dictionary<string, Expression> dict = new Dictionary<string, Expression>();
// 获取MemberInitExpression中的每一个MemberBinding
foreach (var binding in exp)
{
// 判断是MemberAssignment还是MemberListBinding
if (binding is MemberAssignment assignment)
{
// 获取属性名和属性值
string propertyName = assignment.Member.Name;
dict.Add(assignment.Member.Name, assignment.Expression);
}
}
return dict;
}
public static bool IsVariable(Expression expr)
{
var ps = new ParameterExpressionVisitor();

View File

@ -110,5 +110,29 @@ namespace SqlSugar
SplitTableUpdateByObjectProvider<T> SplitTable();
IUpdateable<T> EnableQueryFilter();
IUpdateable<T> Clone();
IUpdateable<T,T2> InnerJoin<T2>(Expression<Func<T,T2,bool>> joinExpress);
}
public interface IUpdateable<T, T2>
{
int ExecuteCommand();
Task<int> ExecuteCommandAsync();
IUpdateable<T, T2,T3> InnerJoin<T3>(Expression<Func<T, T2,T3, bool>> joinExpress);
IUpdateable<T, T2> SetColumns(Expression<Func<T, T2,T>> columns);
IUpdateable<T, T2> Where(Expression<Func<T, T2,bool>> whereExpression);
}
public interface IUpdateable<T, T2,T3>
{
IUpdateable<T, T2, T3,T4> InnerJoin<T4>(Expression<Func<T, T2, T3,T4, bool>> joinExpress);
int ExecuteCommand();
Task<int> ExecuteCommandAsync();
IUpdateable<T, T2,T3> SetColumns(Expression<Func<T, T2,T3, T>> columns);
IUpdateable<T, T2,T3> Where(Expression<Func<T, T2,T3, bool>> whereExpression);
}
public interface IUpdateable<T, T2, T3,T4>
{
int ExecuteCommand();
Task<int> ExecuteCommandAsync();
IUpdateable<T, T2, T3,T4> SetColumns(Expression<Func<T, T2, T3,T4, T>> columns);
IUpdateable<T, T2, T3,T4> Where(Expression<Func<T, T2, T3,T4, bool>> whereExpression);
}
}

View File

@ -142,6 +142,9 @@
<Compile Include="Abstract\SqlBuilderProvider\SqlBuilderProvider_Condition.cs" />
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
<Compile Include="Abstract\DeleteProvider\DeleteMethodInfo.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableProviderT2.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableProviderT3.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableProviderT4.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateMethodInfo.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableHelper.cs" />
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
@ -168,6 +171,7 @@
<Compile Include="ExpressionsToSql\Subquery\Items\SubToList.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubWithNoLock.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubEnableTableFilter.cs" />
<Compile Include="Infrastructure\DependencyManagement.cs" />
<Compile Include="Interface\ICustomConditionalFunc.cs" />
<Compile Include="Interface\ISugarDataConverter.cs" />
<Compile Include="Json2Sql\Entities\JsonDeleteResult.cs" />