mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
Update join
This commit is contained in:
@@ -34,7 +34,8 @@ namespace SqlSugar
|
|||||||
public bool IsWhereColumns { get; set; }
|
public bool IsWhereColumns { get; set; }
|
||||||
public bool? IsListUpdate { get; set; }
|
public bool? IsListUpdate { get; set; }
|
||||||
public List<string> UpdateColumns { get; set; }
|
public List<string> UpdateColumns { get; set; }
|
||||||
public List<JoinQueryInfo> JoinInfos { get; set; } = new List<JoinQueryInfo>();
|
public List<JoinQueryInfo> JoinInfos { get; set; }
|
||||||
|
public string ShortName { get; set; }
|
||||||
|
|
||||||
public virtual string SqlTemplate
|
public virtual string SqlTemplate
|
||||||
{
|
{
|
||||||
@@ -260,6 +261,11 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return setValue.First().Value;
|
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);
|
var result = Builder.GetTranslationColumnName(it.DbColumnName) + "=" + GetDbColumn(it,this.Context.Ado.SqlParameterKeyWord + it.DbColumnName);
|
||||||
return result;
|
return result;
|
||||||
@@ -295,8 +301,26 @@ namespace SqlSugar
|
|||||||
whereString += Builder.GetTranslationColumnName(item) + "=" + this.Context.Ado.SqlParameterKeyWord + item;
|
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);
|
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()
|
public virtual void ActionMinDate()
|
||||||
{
|
{
|
||||||
if (this.Parameters != null)
|
if (this.Parameters != null)
|
||||||
|
@@ -169,6 +169,7 @@ namespace SqlSugar
|
|||||||
result.updateableObj = this;
|
result.updateableObj = this;
|
||||||
var querybale=this.Context.Queryable<T>().LeftJoin<T2>(joinExpress);
|
var querybale=this.Context.Queryable<T>().LeftJoin<T2>(joinExpress);
|
||||||
result.updateableObj.UpdateBuilder.JoinInfos = querybale.QueryBuilder.JoinQueryInfos;
|
result.updateableObj.UpdateBuilder.JoinInfos = querybale.QueryBuilder.JoinQueryInfos;
|
||||||
|
result.updateableObj.UpdateBuilder.ShortName = joinExpress.Parameters.FirstOrDefault()?.Name;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public IUpdateable<T> Clone()
|
public IUpdateable<T> Clone()
|
||||||
|
@@ -1,9 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Linq;
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class Updateable<T, T2> : IUpdateable<T, T2> where T : class,new()
|
public class Updateable<T, T2> : IUpdateable<T, T2> where T : class,new()
|
||||||
@@ -11,12 +14,12 @@ namespace SqlSugar
|
|||||||
public IUpdateable<T> updateableObj { get; set; }
|
public IUpdateable<T> updateableObj { get; set; }
|
||||||
public int ExecuteCommand()
|
public int ExecuteCommand()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
return this.updateableObj.ExecuteCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<int> ExecuteCommandAsync()
|
public Task<int> ExecuteCommandAsync()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
return this.updateableObj.ExecuteCommandAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IUpdateable<T, T2, T3> InnerJoin<T3>(Expression<Func<T, T2, T3, bool>> joinExpress)
|
public IUpdateable<T, T2, T3> InnerJoin<T3>(Expression<Func<T, T2, T3, bool>> joinExpress)
|
||||||
@@ -26,15 +29,26 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public IUpdateable<T, T2> SetColumns(Expression<Func<T, T2, T>> columns)
|
public IUpdateable<T, T2> SetColumns(Expression<Func<T, T2, T>> columns)
|
||||||
{
|
{
|
||||||
// ((UpdateableProvider<T>)updateableObj).SetColumnsByExpression(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(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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public IUpdateable<T, T2> Where(Expression<Func<T, T2, bool>> whereExpression)
|
public IUpdateable<T, T2> Where(Expression<Func<T, T2, bool>> whereExpression)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var value = updateableObj.UpdateBuilder.GetExpressionValue(whereExpression, ResolveExpressType.WhereMultiple).GetString();
|
||||||
|
updateableObj.UpdateBuilder.WhereValues.Add(value);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -8,6 +9,23 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
public class ExpressionTool
|
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)
|
public static bool IsVariable(Expression expr)
|
||||||
{
|
{
|
||||||
var ps = new ParameterExpressionVisitor();
|
var ps = new ParameterExpressionVisitor();
|
||||||
|
@@ -26,6 +26,16 @@ namespace SqlSugar
|
|||||||
";
|
";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
protected override string GetJoinUpdate(string columnsString, ref string whereString)
|
||||||
|
{
|
||||||
|
var joinString = $" {Builder.GetTranslationColumnName(this.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} ";
|
||||||
|
}
|
||||||
|
var tableName = joinString+ "\r\n ";
|
||||||
|
return string.Format(SqlTemplate, tableName, columnsString, whereString);
|
||||||
|
}
|
||||||
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
|
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
|
||||||
{
|
{
|
||||||
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
|
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
|
||||||
|
Reference in New Issue
Block a user