Synchronization code

This commit is contained in:
sunkaixuan
2023-05-07 22:44:48 +08:00
parent 505ec864b6
commit ce46021b26
12 changed files with 138 additions and 6 deletions

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
internal class ReSetValueBySqlExpListModel
{
public string DbColumnName { get; set; }
public string Sql { get; set; }
}
}

View File

@@ -36,7 +36,8 @@ namespace SqlSugar
public List<string> UpdateColumns { get; set; }
public List<JoinQueryInfo> JoinInfos { get; set; }
public string ShortName { get; set; }
internal Dictionary<string, ReSetValueBySqlExpListModel> ReSetValueBySqlExpList { get; set; }
public virtual string ReSetValueBySqlExpListType { get; set; }
public virtual string SqlTemplate
{
get
@@ -423,6 +424,10 @@ namespace SqlSugar
{
return LambdaExpressions.DbMehtods.GetDate();
}
else if (IsListSetExp(columnInfo)|| IsSingleSetExp(columnInfo))
{
return this.ReSetValueBySqlExpList[columnInfo.PropertyName].Sql;
}
else if (columnInfo.UpdateSql.HasValue())
{
return columnInfo.UpdateSql;
@@ -481,6 +486,17 @@ namespace SqlSugar
return name + "";
}
}
private bool IsSingleSetExp(DbColumnInfo columnInfo)
{
return this.ReSetValueBySqlExpList != null &&
this.ReSetValueBySqlExpList.ContainsKey(columnInfo.PropertyName) &&
this.IsListUpdate == null&&
DbColumnInfoList.GroupBy(it => it.TableId).Count()==1;
}
private bool IsListSetExp(DbColumnInfo columnInfo)
{
return this.ReSetValueBySqlExpListType != null && this.ReSetValueBySqlExpList != null && this.ReSetValueBySqlExpList.ContainsKey(columnInfo.PropertyName);
}
//public virtual string GetDbColumn(DbColumnInfo columnInfo, string name)
//{
// if (columnInfo.UpdateServerTime)

View File

@@ -360,6 +360,36 @@ namespace SqlSugar
}
return this;
}
public IUpdateable<T> PublicSetColumns(Expression<Func<T, object>> filedNameExpression, Expression<Func<T, object>> ValueExpExpression)
{
if (UpdateParameterIsNull == true)
{
return SetColumns(filedNameExpression, ValueExpExpression);
}
else
{
var name = ExpressionTool.GetMemberName(filedNameExpression);
if (name == null)
{
Check.ExceptionEasy(filedNameExpression + " format error ", filedNameExpression + "参数格式错误");
}
var value = this.UpdateBuilder.GetExpressionValue(ValueExpExpression, ResolveExpressType.WhereSingle).GetResultString();
if (this.UpdateBuilder.ReSetValueBySqlExpList == null)
{
this.UpdateBuilder.ReSetValueBySqlExpList = new Dictionary<string, ReSetValueBySqlExpListModel>();
}
if (!this.UpdateBuilder.ReSetValueBySqlExpList.ContainsKey(name))
{
this.UpdateBuilder.ReSetValueBySqlExpList.Add(name, new ReSetValueBySqlExpListModel()
{
Sql = value,
DbColumnName =this.SqlBuilder.GetTranslationColumnName(this.EntityInfo.Columns.First(it => it.PropertyName == name).DbColumnName)
}); ;
}
}
return this;
}
#endregion
#region Update by object

View File

@@ -100,6 +100,7 @@ namespace SqlSugar
IUpdateable<T> EnableDiffLogEvent(object businessData = null);
IUpdateable<T> EnableDiffLogEventIF(bool isEnableDiffLog,object businessData = null);
IUpdateable<T> ReSetValue(Action<T> setValueExpression);
IUpdateable<T> PublicSetColumns (Expression<Func<T,object>> filedNameExpression, Expression<Func<T, object>> ValueExpExpression);
IUpdateable<T> RemoveDataCache();
IUpdateable<T> RemoveDataCache(string likeString);
IUpdateable<T> CallEntityMethod(Expression<Action<T>> method);

View File

@@ -7,6 +7,7 @@ namespace SqlSugar
{
public class DmUpdateBuilder : UpdateBuilder
{
public override string ReSetValueBySqlExpListType { get; set; } = "dm";
protected override string GetJoinUpdate(string columnsString, ref string whereString)
{
var joinString = $" {Builder.GetTranslationColumnName(this.TableName)} {Builder.GetTranslationColumnName(this.ShortName)} ";

View File

@@ -178,9 +178,25 @@ namespace SqlSugar
batchUpdateSql.Replace("${0}", format);
batchUpdateSql.Append(";");
}
batchUpdateSql = GetBatchUpdateSql(batchUpdateSql);
return batchUpdateSql.ToString();
}
private StringBuilder GetBatchUpdateSql(StringBuilder batchUpdateSql)
{
if (ReSetValueBySqlExpListType == null && ReSetValueBySqlExpList != null)
{
var result = batchUpdateSql.ToString();
foreach (var item in ReSetValueBySqlExpList)
{
var dbColumnName = item.Value.DbColumnName;
result = result.Replace($"{dbColumnName}=T.{dbColumnName}", $"{dbColumnName}={item.Value.Sql.Replace(dbColumnName, $"{Builder.GetTranslationColumnName(this.TableName)}.{dbColumnName}")}");
batchUpdateSql = new StringBuilder(result);
}
}
return batchUpdateSql;
}
protected override string GetJoinUpdate(string columnsString, ref string whereString)
{
var formString = $" {Builder.GetTranslationColumnName(this.TableName)} AS {Builder.GetTranslationColumnName(this.ShortName)} ";

View File

@@ -97,8 +97,25 @@ namespace SqlSugar
batchUpdateSql.Replace("${0}",format);
batchUpdateSql.Append(";");
}
batchUpdateSql = GetBatchUpdateSql(batchUpdateSql);
return batchUpdateSql.ToString();
}
private StringBuilder GetBatchUpdateSql(StringBuilder batchUpdateSql)
{
if (ReSetValueBySqlExpListType == null && ReSetValueBySqlExpList != null)
{
var result = batchUpdateSql.ToString();
foreach (var item in ReSetValueBySqlExpList)
{
var dbColumnName = item.Value.DbColumnName;
result = result.Replace($"T.{dbColumnName}", item.Value.Sql.Replace(dbColumnName, "S." + dbColumnName));
batchUpdateSql = new StringBuilder(result);
}
}
return batchUpdateSql;
}
int i = 0;
public object FormatValue(object value,string name)
{

View File

@@ -8,6 +8,7 @@ namespace SqlSugar
{
public class OracleUpdateBuilder : UpdateBuilder
{
public override string ReSetValueBySqlExpListType { get; set; } = "oracle";
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{
if (groupList.Count == 0)

View File

@@ -198,8 +198,25 @@ namespace SqlSugar
batchUpdateSql.Replace("${0}", format);
batchUpdateSql.Append(";");
}
batchUpdateSql = GetBatchUpdateSql(batchUpdateSql);
return batchUpdateSql.ToString();
}
private StringBuilder GetBatchUpdateSql(StringBuilder batchUpdateSql)
{
if (ReSetValueBySqlExpListType == null && ReSetValueBySqlExpList != null)
{
var result = batchUpdateSql.ToString();
foreach (var item in ReSetValueBySqlExpList)
{
var dbColumnName = item.Value.DbColumnName;
result = result.Replace($"{dbColumnName}=T.{dbColumnName}", $"{dbColumnName}={item.Value.Sql.Replace(dbColumnName,$"{Builder.GetTranslationColumnName( this.TableName)}.{dbColumnName}")}");
batchUpdateSql = new StringBuilder(result);
}
}
return batchUpdateSql;
}
protected override string GetJoinUpdate(string columnsString, ref string whereString)
{
var formString = $" {Builder.GetTranslationColumnName(this.TableName)} AS {Builder.GetTranslationColumnName(this.ShortName)} ";

View File

@@ -8,6 +8,7 @@ namespace SqlSugar
{
public class QuestDBUpdateBuilder : UpdateBuilder
{
public override string ReSetValueBySqlExpListType { get; set; } = "questdb";
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{
StringBuilder sb = new StringBuilder();

View File

@@ -68,9 +68,26 @@ namespace SqlSugar
}
batchUpdateSql.AppendFormat(SqlTemplateJoin, updateTable, whereString);
}
batchUpdateSql = GetBatchUpdateSql(batchUpdateSql);
return batchUpdateSql.ToString();
}
private StringBuilder GetBatchUpdateSql(StringBuilder batchUpdateSql)
{
if (ReSetValueBySqlExpListType == null && ReSetValueBySqlExpList != null)
{
var result = batchUpdateSql.ToString();
foreach (var item in ReSetValueBySqlExpList)
{
var dbColumnName = item.Value.DbColumnName;
result = result.Replace($"T.{dbColumnName}", item.Value.Sql.Replace(dbColumnName,"S."+ dbColumnName));
batchUpdateSql = new StringBuilder(result);
}
}
return batchUpdateSql;
}
private object GetValue(DbColumnInfo it)
{
if (it.SqlParameterDbType!=null&&it.SqlParameterDbType.Equals(System.Data.DbType.AnsiString))

View File

@@ -7,6 +7,7 @@ namespace SqlSugar
{
public class SqliteUpdateBuilder : UpdateBuilder
{
public override string ReSetValueBySqlExpListType { get; set; }="sqlite";
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{
StringBuilder sb = new StringBuilder();