Files
SqlSugar/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs

589 lines
25 KiB
C#
Raw Normal View History

2017-01-07 21:54:51 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2017-05-19 15:26:06 +08:00
using System.Linq.Expressions;
2017-01-07 21:54:51 +08:00
using System.Text;
2019-04-21 20:15:07 +08:00
using System.Text.RegularExpressions;
2017-01-07 21:54:51 +08:00
namespace SqlSugar
{
public class UpdateBuilder : IDMLBuilder
{
2017-05-19 15:26:06 +08:00
public UpdateBuilder()
{
this.sql = new StringBuilder();
this.DbColumnInfoList = new List<DbColumnInfo>();
2017-05-19 20:28:51 +08:00
this.SetValues = new List<KeyValuePair<string, string>>();
2017-05-21 01:14:55 +08:00
this.WhereValues = new List<string>();
2017-06-05 11:11:50 +08:00
this.Parameters = new List<SugarParameter>();
2017-05-19 15:26:06 +08:00
}
2019-05-11 14:39:00 +08:00
public SqlSugarProvider Context { get; set; }
2017-05-19 15:26:06 +08:00
public ILambdaExpressions LambdaExpressions { get; set; }
public ISqlBuilder Builder { get; set; }
public StringBuilder sql { get; set; }
public List<SugarParameter> Parameters { get; set; }
public string TableName { get; set; }
public string TableWithString { get; set; }
public List<DbColumnInfo> DbColumnInfoList { get; set; }
2017-05-20 18:48:14 +08:00
public List<string> WhereValues { get; set; }
2023-09-10 00:30:51 +08:00
public string AppendWhere { get; set; }
2017-05-19 20:28:51 +08:00
public List<KeyValuePair<string, string>> SetValues { get; set; }
2017-06-09 20:44:59 +08:00
public bool IsNoUpdateNull { get; set; }
2019-05-11 09:44:22 +08:00
public bool IsNoUpdateDefaultValue { get; set; }
2017-05-21 01:40:06 +08:00
public List<string> PrimaryKeys { get; set; }
public List<string> OldPrimaryKeys { get; set; }
2017-05-21 11:16:11 +08:00
public bool IsOffIdentity { get; set; }
2019-06-09 21:19:49 +08:00
public bool IsWhereColumns { get; set; }
2021-08-26 14:29:58 +08:00
public bool? IsListUpdate { get; set; }
public List<string> UpdateColumns { get; set; }
2023-04-24 16:50:58 +08:00
public List<JoinQueryInfo> JoinInfos { get; set; }
public string ShortName { get; set; }
2023-05-31 19:12:05 +08:00
public Dictionary<string, ReSetValueBySqlExpListModel> ReSetValueBySqlExpList { get; set; }
2023-05-07 18:19:15 +08:00
public virtual string ReSetValueBySqlExpListType { get; set; }
2024-01-20 20:43:31 +08:00
public EntityInfo EntityInfo { get; set; }
2017-05-19 15:26:06 +08:00
public virtual string SqlTemplate
2017-01-07 21:54:51 +08:00
{
get
{
2017-05-21 01:14:55 +08:00
return @"UPDATE {0} SET
{1} {2}";
2017-01-07 21:54:51 +08:00
}
}
2017-05-19 15:26:06 +08:00
public virtual string SqlTemplateBatch
2017-04-30 22:49:41 +08:00
{
get
{
2017-05-21 11:16:11 +08:00
return @"UPDATE S SET {0} FROM {1} S {2} INNER JOIN ";
}
}
public virtual string SqlTemplateJoin
{
get
{
return @" (
{0}
2017-05-21 01:14:55 +08:00
2017-05-21 11:16:11 +08:00
) T ON {1}
2017-05-22 02:21:43 +08:00
; ";
2017-04-30 22:49:41 +08:00
}
}
2017-05-21 01:14:55 +08:00
public virtual string SqlTemplateBatchSet
{
get
{
return "{0} AS {1}";
}
}
2017-05-19 15:26:06 +08:00
public virtual string SqlTemplateBatchSelect
2017-01-07 21:54:51 +08:00
{
get
{
2017-05-19 15:26:06 +08:00
return "{0} AS {1}";
2017-01-07 21:54:51 +08:00
}
2017-05-19 15:26:06 +08:00
}
2017-01-07 21:54:51 +08:00
2017-05-19 15:26:06 +08:00
public virtual string SqlTemplateBatchUnion
{
get
2017-01-07 21:54:51 +08:00
{
2017-05-21 11:16:11 +08:00
return "\t\t\r\nUNION ALL ";
2017-01-07 21:54:51 +08:00
}
}
2017-05-19 15:26:06 +08:00
public virtual void Clear()
{
}
public virtual string GetTableNameString
2017-01-07 21:54:51 +08:00
{
get
{
2017-05-19 15:26:06 +08:00
var result = Builder.GetTranslationTableName(TableName);
2017-08-25 22:22:12 +08:00
result += UtilConstants.Space;
2017-10-10 19:49:16 +08:00
if (this.TableWithString.HasValue())
2017-05-19 15:26:06 +08:00
{
2017-08-25 22:22:12 +08:00
result += TableWithString + UtilConstants.Space;
2017-05-19 15:26:06 +08:00
}
return result;
2017-01-07 21:54:51 +08:00
}
}
2017-05-21 02:40:13 +08:00
public virtual string GetTableNameStringNoWith
{
get
{
var result = Builder.GetTranslationTableName(TableName);
return result;
}
}
2017-05-21 11:16:11 +08:00
2021-08-26 14:29:58 +08:00
2017-05-21 01:14:55 +08:00
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType, bool isMapping = true)
2017-05-19 15:26:06 +08:00
{
ILambdaExpressions resolveExpress = this.LambdaExpressions;
this.LambdaExpressions.Clear();
2019-12-11 17:11:19 +08:00
if (this.Context.CurrentConnectionConfig.MoreSettings != null)
{
resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
2021-12-29 21:22:34 +08:00
resolveExpress.TableEnumIsString = this.Context.CurrentConnectionConfig.MoreSettings.TableEnumIsString;
2019-12-11 17:11:19 +08:00
}
else
{
resolveExpress.PgSqlIsAutoToLower = true;
}
2017-05-19 20:28:51 +08:00
if (isMapping)
{
resolveExpress.MappingColumns = Context.MappingColumns;
resolveExpress.MappingTables = Context.MappingTables;
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
2017-11-17 14:08:25 +08:00
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
2017-05-19 20:28:51 +08:00
}
2019-05-17 20:34:53 +08:00
resolveExpress.InitMappingInfo = Context.InitMappingInfo;
2019-04-30 15:00:12 +08:00
resolveExpress.RefreshMapping = () =>
{
resolveExpress.MappingColumns = Context.MappingColumns;
resolveExpress.MappingTables = Context.MappingTables;
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
};
2022-06-14 12:16:31 +08:00
resolveExpress.SugarContext = new ExpressionOutParameter() { Context = this.Context };
2017-05-19 15:26:06 +08:00
resolveExpress.Resolve(expression, resolveType);
this.Parameters.AddRange(resolveExpress.Parameters);
2018-01-31 16:41:30 +08:00
var result = resolveExpress.Result;
return result;
2017-05-19 15:26:06 +08:00
}
public virtual string ToSqlString()
2017-01-07 21:54:51 +08:00
{
2017-06-09 20:44:59 +08:00
if (IsNoUpdateNull)
{
2023-02-01 00:17:47 +08:00
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null||(it.UpdateServerTime == true ||!string.IsNullOrEmpty(it.UpdateSql))).ToList();
2017-06-09 20:44:59 +08:00
}
2019-05-11 09:44:22 +08:00
if (IsNoUpdateDefaultValue)
{
2022-08-15 18:58:02 +08:00
DbColumnInfoList = DbColumnInfoList.Where(it => {
2023-02-01 00:17:47 +08:00
if (it.Value.ObjToString() == "0" && it.PropertyType.IsEnum)
2022-08-15 18:58:02 +08:00
{
2022-08-18 22:05:47 +08:00
return it.Value.ObjToLong() != UtilMethods.DefaultForType(it.PropertyType).ObjToLong();
2022-08-15 18:58:02 +08:00
}
2023-02-01 00:17:47 +08:00
else if (it.UpdateServerTime == true || !string.IsNullOrEmpty(it.UpdateSql))
{
return true;
}
2022-08-15 18:58:02 +08:00
else
{
2023-02-01 00:17:47 +08:00
return it.Value.ObjToString() != UtilMethods.DefaultForType(it.PropertyType).ObjToString();
2022-08-15 18:58:02 +08:00
}
}).ToList();
2019-05-11 09:44:22 +08:00
}
2017-05-19 15:26:06 +08:00
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
var isSingle = groupList.Count() == 1;
2021-08-26 14:29:58 +08:00
if (isSingle&&this.IsListUpdate==null)
2017-05-19 15:26:06 +08:00
{
2022-05-13 22:43:12 +08:00
ActionMinDate();
2017-05-21 11:16:11 +08:00
return ToSingleSqlString(groupList);
}
else
{
return TomultipleSqlString(groupList);
}
}
2017-06-26 00:44:27 +08:00
protected virtual string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
2017-05-21 11:16:11 +08:00
{
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
int pageSize = 200;
int pageIndex = 1;
int totalRecord = groupList.Count;
int pageCount = (totalRecord + pageSize - 1) / pageSize;
StringBuilder batchUpdateSql = new StringBuilder();
while (pageCount >= pageIndex)
{
StringBuilder updateTable = new StringBuilder();
2017-07-15 19:51:13 +08:00
string setValues = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it =>
{
if (SetValues.IsValuable())
{
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName));
if (setValue != null && setValue.Any())
{
return setValue.First().Value;
}
}
var result = string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName));
return result;
}));
2017-05-21 11:16:11 +08:00
batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString);
int i = 0;
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
{
var isFirst = i == 0;
if (!isFirst)
{
updateTable.Append(SqlTemplateBatchUnion);
}
2017-11-06 17:18:03 +08:00
updateTable.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), Builder.GetTranslationColumnName(it.DbColumnName)))));
2017-05-21 11:16:11 +08:00
++i;
}
pageIndex++;
updateTable.Append("\r\n");
2017-05-21 01:14:55 +08:00
string whereString = null;
2017-10-10 19:49:16 +08:00
if (this.WhereValues.HasValue())
2017-05-21 01:14:55 +08:00
{
foreach (var item in WhereValues)
{
var isFirst = whereString == null;
2017-05-21 11:16:11 +08:00
whereString += (isFirst ? null : " AND ");
2019-04-27 11:31:52 +08:00
whereString += Regex.Replace(item,"\\"+this.Builder.SqlTranslationLeft,"S."+ this.Builder.SqlTranslationLeft);
2017-05-21 01:14:55 +08:00
}
}
2019-04-21 20:15:07 +08:00
if (PrimaryKeys.HasValue())
2017-05-21 01:14:55 +08:00
{
foreach (var item in PrimaryKeys)
{
var isFirst = whereString == null;
2017-05-21 11:16:11 +08:00
whereString += (isFirst ? null : " AND ");
whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item));
2017-05-21 01:14:55 +08:00
}
}
2017-05-21 11:16:11 +08:00
batchUpdateSql.AppendFormat(SqlTemplateJoin, updateTable, whereString);
2017-05-19 15:26:06 +08:00
}
2017-05-21 11:16:11 +08:00
return batchUpdateSql.ToString();
}
2017-06-26 00:44:27 +08:00
protected virtual string ToSingleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
2017-05-21 11:16:11 +08:00
{
string columnsString = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it =>
2017-05-19 15:26:06 +08:00
{
2017-05-21 11:16:11 +08:00
if (SetValues.IsValuable())
2017-05-21 02:40:13 +08:00
{
2017-11-06 17:18:03 +08:00
var setValue = SetValues.Where(sv => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName) || sv.Key == Builder.GetTranslationColumnName(it.PropertyName));
2017-05-21 11:16:11 +08:00
if (setValue != null && setValue.Any())
2017-05-21 02:40:13 +08:00
{
2017-05-21 11:16:11 +08:00
return setValue.First().Value;
2017-05-21 02:40:13 +08:00
}
2023-04-24 16:50:58 +08:00
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));
2024-02-18 17:38:37 +08:00
return Builder.GetTranslationColumnName(this.ShortName)+"."+ Builder.GetTranslationColumnName(setValue.First().Key)+"="+ setValue.First().Value ;
2023-04-24 16:50:58 +08:00
}
2017-05-21 11:16:11 +08:00
}
2022-12-20 15:24:26 +08:00
var result = Builder.GetTranslationColumnName(it.DbColumnName) + "=" + GetDbColumn(it,this.Context.Ado.SqlParameterKeyWord + it.DbColumnName);
2017-05-21 11:16:11 +08:00
return result;
}));
string whereString = null;
2017-10-10 19:49:16 +08:00
if (this.WhereValues.HasValue())
2017-05-21 11:16:11 +08:00
{
foreach (var item in WhereValues)
2017-05-19 15:26:06 +08:00
{
2017-05-21 11:16:11 +08:00
var isFirst = whereString == null;
whereString += (isFirst ? " WHERE " : " AND ");
whereString += item;
2017-05-19 15:26:06 +08:00
}
}
2017-10-10 19:49:16 +08:00
else if (PrimaryKeys.HasValue())
2017-05-21 11:16:11 +08:00
{
2021-09-22 22:37:59 +08:00
if (IsWhereColumns == false)
2017-05-21 11:16:11 +08:00
{
2024-01-20 20:43:31 +08:00
int i = 100000;
2021-09-22 22:37:59 +08:00
foreach (var item in PrimaryKeys)
{
2024-01-20 20:43:31 +08:00
i++;
2021-09-22 22:37:59 +08:00
var isFirst = whereString == null;
whereString += (isFirst ? " WHERE " : " AND ");
2024-01-20 20:43:31 +08:00
var pkIsSugarDataConverter = GetPkIsSugarDataConverter();
if (pkIsSugarDataConverter && GetColumnInfo(item)!=null)
{
var columnInfo = GetColumnInfo(item);
var value=this.DbColumnInfoList.FirstOrDefault(it => it.DbColumnName.EqualCase(item) || it.PropertyName.EqualCase(item))?.Value;
var p = UtilMethods.GetParameterConverter(i, this.Context, value, this.EntityInfo, this.EntityInfo?.Columns.First(it => it.DbColumnName.Equals(item) || it.PropertyName.Equals(item)));
whereString += Builder.GetTranslationColumnName(item) + "=" + p.ParameterName;
this.Parameters.Add(p);
}
else
{
whereString += Builder.GetTranslationColumnName(item) + "=" + this.Context.Ado.SqlParameterKeyWord + item;
}
2021-09-22 22:37:59 +08:00
}
2017-05-21 11:16:11 +08:00
}
}
2019-06-09 21:19:49 +08:00
if (PrimaryKeys.HasValue()&&IsWhereColumns)
{
foreach (var item in PrimaryKeys)
{
var isFirst = whereString == null;
whereString += (isFirst ? " WHERE " : " AND ");
whereString += Builder.GetTranslationColumnName(item) + "=" + this.Context.Ado.SqlParameterKeyWord + item;
}
}
2023-04-24 16:50:58 +08:00
if (this.JoinInfos != null && this.JoinInfos.Any())
{
return GetJoinUpdate(columnsString, ref whereString);
}
2017-05-21 11:16:11 +08:00
return string.Format(SqlTemplate, GetTableNameString, columnsString, whereString);
2017-01-07 21:54:51 +08:00
}
2023-04-24 16:50:58 +08:00
2024-01-20 20:43:31 +08:00
private EntityColumnInfo GetColumnInfo(string item)
{
var columnInfo= this.EntityInfo?.Columns?.FirstOrDefault(it => it.DbColumnName.Equals(item) || it.PropertyName.Equals(item));
return columnInfo;
}
private bool GetPkIsSugarDataConverter()
{
return this.EntityInfo?.Columns.Any(it => it.IsPrimarykey && it.SqlParameterDbType is Type&&typeof(ISugarDataConverter).IsAssignableFrom((it.SqlParameterDbType as Type))) == true;
}
2023-04-24 16:50:58 +08:00
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);
}
2022-05-13 22:43:12 +08:00
public virtual void ActionMinDate()
{
if (this.Parameters != null)
{
foreach (var item in this.Parameters)
{
if (item.DbType == System.Data.DbType.Date || item.DbType == System.Data.DbType.DateTime)
{
if (item.Value != null && item.Value != DBNull.Value)
{
if (item.Value is DateTime)
{
if (Convert.ToDateTime(item.Value) == DateTime.MinValue)
{
item.Value = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
}
}
}
}
}
}
}
2017-07-10 01:15:54 +08:00
public virtual object FormatValue(object value)
2017-01-07 21:54:51 +08:00
{
2017-05-19 15:26:06 +08:00
if (value == null)
{
return "NULL";
}
else
{
2019-01-26 00:17:16 +08:00
var type = UtilMethods.GetUnderType(value.GetType());
2017-08-25 22:22:12 +08:00
if (type == UtilConstants.DateType)
2017-05-19 15:26:06 +08:00
{
2017-05-21 11:16:11 +08:00
var date = value.ObjToDate();
2022-05-13 22:06:08 +08:00
if (date < UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig))
2017-07-15 19:51:13 +08:00
{
2022-05-13 22:06:08 +08:00
date = UtilMethods.GetMinDate(this.Context.CurrentConnectionConfig);
2017-05-21 11:16:11 +08:00
}
2017-06-20 23:14:36 +08:00
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
2017-05-19 15:26:06 +08:00
}
2017-11-06 17:18:03 +08:00
else if (type == UtilConstants.ByteArrayType)
{
2018-05-11 16:44:51 +08:00
string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
2017-11-10 15:09:23 +08:00
return bytesString;
2017-11-06 17:18:03 +08:00
}
2017-07-16 17:43:22 +08:00
else if (type.IsEnum())
2017-07-15 19:51:13 +08:00
{
2021-12-29 20:12:20 +08:00
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{
2021-12-29 21:22:34 +08:00
return value.ToSqlValue();
2021-12-29 20:12:20 +08:00
}
else
{
return Convert.ToInt64(value);
}
2017-07-15 19:51:13 +08:00
}
2017-08-25 22:22:12 +08:00
else if (type == UtilConstants.BoolType)
2017-06-19 02:09:30 +08:00
{
return value.ObjToBool() ? "1" : "0";
}
2021-12-07 21:44:23 +08:00
else if (type == UtilConstants.DateTimeOffsetType)
{
2022-09-24 00:13:26 +08:00
return FormatDateTimeOffset(value);
2021-12-07 21:44:23 +08:00
}
2017-08-25 22:22:12 +08:00
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
2017-05-19 15:26:06 +08:00
{
return "N'" + value.ToString().ToSqlFilter() + "'";
}
2022-08-03 01:12:27 +08:00
else if (type==UtilConstants.IntType||type==UtilConstants.LongType)
{
return value;
}
2022-09-08 19:51:14 +08:00
else if (UtilMethods.IsNumber(type.Name))
{
2022-09-14 00:49:02 +08:00
if (value.ObjToString().Contains(","))
{
return $"'{value}'";
}
else
{
return value;
}
2022-09-08 19:51:14 +08:00
}
2017-05-19 15:26:06 +08:00
else
{
return "N'" + value.ToString() + "'";
}
}
2017-01-07 21:54:51 +08:00
}
2022-09-24 00:13:26 +08:00
public virtual string FormatDateTimeOffset(object value)
{
var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
}
2022-12-22 18:47:13 +08:00
private int GetDbColumnIndex = 0;
2022-12-20 15:24:26 +08:00
public virtual string GetDbColumn(DbColumnInfo columnInfo, object name)
{
if (columnInfo.UpdateServerTime)
{
return LambdaExpressions.DbMehtods.GetDate();
}
2024-02-20 13:40:17 +08:00
else if (columnInfo.PropertyType.FullName == "NetTopologySuite.Geometries.Geometry")
{
var pname = Builder.SqlParameterKeyWord + "Geometry" + GetDbColumnIndex;
var p = new SugarParameter(pname, columnInfo.Value);
p.DbType= System.Data.DbType.Object;
this.Parameters.Add(p);
GetDbColumnIndex++;
return pname;
}
else if (UtilMethods.IsErrorDecimalString() == true)
2023-12-01 15:50:37 +08:00
{
var pname = Builder.SqlParameterKeyWord + "Decimal" + GetDbColumnIndex;
var p = new SugarParameter(pname, columnInfo.Value);
this.Parameters.Add(p);
GetDbColumnIndex++;
return pname;
}
else if (IsListSetExp(columnInfo) || IsSingleSetExp(columnInfo))
2023-05-07 18:19:15 +08:00
{
2023-05-21 17:49:53 +08:00
if (this.ReSetValueBySqlExpList[columnInfo.PropertyName].Type == ReSetValueBySqlExpListModelType.List)
{
2023-12-01 15:50:37 +08:00
return Builder.GetTranslationColumnName(columnInfo.DbColumnName) + this.ReSetValueBySqlExpList[columnInfo.PropertyName].Sql + name;
2023-05-21 17:49:53 +08:00
}
else
{
return this.ReSetValueBySqlExpList[columnInfo.PropertyName].Sql;
}
2023-05-07 18:19:15 +08:00
}
2022-12-20 15:24:26 +08:00
else if (columnInfo.UpdateSql.HasValue())
{
2024-03-27 18:12:52 +08:00
if (columnInfo.UpdateSql.Contains("{0}"))
2024-03-25 12:01:55 +08:00
{
2024-03-27 19:25:26 +08:00
if (columnInfo.Value == null)
{
return string.Format(columnInfo.UpdateSql, "null");
}
else
{
return string.Format(columnInfo.UpdateSql, columnInfo.Value?.ObjToString().ToSqlFilter());
}
2024-03-25 12:01:55 +08:00
}
2022-12-20 15:24:26 +08:00
return columnInfo.UpdateSql;
}
2023-05-07 18:19:15 +08:00
else if (columnInfo.SqlParameterDbType is Type && (Type)columnInfo.SqlParameterDbType == UtilConstants.SqlConvertType)
{
var type = columnInfo.SqlParameterDbType as Type;
var ParameterConverter = type.GetMethod("ParameterConverter").MakeGenericMethod(typeof(string));
var obj = Activator.CreateInstance(type);
var p = ParameterConverter.Invoke(obj, new object[] { columnInfo.Value, GetDbColumnIndex }) as SugarParameter;
return p.ParameterName;
}
else if (columnInfo.SqlParameterDbType is Type)
2023-03-03 22:48:50 +08:00
{
var type = columnInfo.SqlParameterDbType as Type;
2023-03-03 23:51:22 +08:00
var ParameterConverter = type.GetMethod("ParameterConverter").MakeGenericMethod(columnInfo.PropertyType);
2023-03-04 16:26:47 +08:00
var obj = Activator.CreateInstance(type);
2023-05-07 18:19:15 +08:00
var p = ParameterConverter.Invoke(obj, new object[] { columnInfo.Value, GetDbColumnIndex }) as SugarParameter;
2023-03-03 22:48:50 +08:00
GetDbColumnIndex++;
//this.Parameters.RemoveAll(it => it.ParameterName == it.ParameterName);
this.Parameters.Add(p);
return p.ParameterName;
}
2023-05-07 18:19:15 +08:00
else if (columnInfo.PropertyType != null && columnInfo.PropertyType.Name == "TimeOnly" && name != null && !name.ObjToString().StartsWith(Builder.SqlParameterKeyWord))
2022-12-22 18:47:13 +08:00
{
var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value);
var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex;
2023-04-04 15:48:11 +08:00
if (timeSpan == null)
{
this.Parameters.Add(new SugarParameter(pname, null) { DbType = System.Data.DbType.Date });
}
else
{
this.Parameters.Add(new SugarParameter(pname, timeSpan));
}
2022-12-22 18:47:13 +08:00
GetDbColumnIndex++;
return pname;
}
2023-04-12 17:33:01 +08:00
else if (columnInfo.PropertyType != null && columnInfo.PropertyType.Name == "DateOnly")
{
var timeSpan = UtilMethods.DateOnlyToDateTime(columnInfo.Value);
var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex;
if (timeSpan == null)
{
this.Parameters.Add(new SugarParameter(pname, null) { DbType = System.Data.DbType.Date });
}
else
{
this.Parameters.Add(new SugarParameter(pname, Convert.ToDateTime(timeSpan)));
}
GetDbColumnIndex++;
return pname;
}
else if (UtilMethods.IsErrorParameterName(this.Context.CurrentConnectionConfig, columnInfo))
{
var pname = Builder.SqlParameterKeyWord + "CrorrPara" + GetDbColumnIndex;
var p = new SugarParameter(pname, columnInfo.Value);
this.Parameters.Add(p);
GetDbColumnIndex++;
return pname;
}
2022-12-20 15:24:26 +08:00
else
{
return name + "";
}
}
2023-05-07 18:38:26 +08:00
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);
}
2023-03-03 22:48:50 +08:00
//public virtual string GetDbColumn(DbColumnInfo columnInfo, string name)
//{
// if (columnInfo.UpdateServerTime)
// {
// return LambdaExpressions.DbMehtods.GetDate();
// }
// else if (columnInfo.UpdateSql.HasValue())
// {
// return columnInfo.UpdateSql;
// }
// else
// {
// return name + "";
// }
//}
2017-01-07 21:54:51 +08:00
}
}