Browse Source

Update .net core

SqlSugar5
sunkaixuan 2 years ago
parent
commit
cc580bf62d
  1. 4
      Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs
  2. 1
      Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs
  3. 1
      Src/Asp.NetCore2/SqlSugar/Entities/DbColumnInfo.cs
  4. 2
      Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs
  5. 82
      Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerUpdateBuilder.cs

4
Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs

@ -350,6 +350,10 @@ namespace SqlSugar
{ {
return "N'" + value.ToString().ToSqlFilter() + "'"; return "N'" + value.ToString().ToSqlFilter() + "'";
} }
else if (type==UtilConstants.IntType||type==UtilConstants.LongType)
{
return value;
}
else else
{ {
return "N'" + value.ToString() + "'"; return "N'" + value.ToString() + "'";

1
Src/Asp.NetCore2/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs

@ -191,6 +191,7 @@ namespace SqlSugar
DbColumnName = GetDbColumnName(column.PropertyName), DbColumnName = GetDbColumnName(column.PropertyName),
PropertyName = column.PropertyName, PropertyName = column.PropertyName,
PropertyType = UtilMethods.GetUnderType(column.PropertyInfo), PropertyType = UtilMethods.GetUnderType(column.PropertyInfo),
SqlParameterDbType = column.SqlParameterDbType,
TableId = i TableId = i
}; };
if (columnInfo.PropertyType.IsEnum() && columnInfo.Value != null) if (columnInfo.PropertyType.IsEnum() && columnInfo.Value != null)

1
Src/Asp.NetCore2/SqlSugar/Entities/DbColumnInfo.cs

@ -25,5 +25,6 @@ namespace SqlSugar
internal bool IsJson { get; set; } internal bool IsJson { get; set; }
public bool? IsUnsigned { get; set; } public bool? IsUnsigned { get; set; }
public int CreateTableFieldSort { get; set; } public int CreateTableFieldSort { get; set; }
internal object SqlParameterDbType { get; set; }
} }
} }

2
Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs

@ -436,7 +436,7 @@ namespace SqlSugar
{ {
var key = typeName + "." + name; var key = typeName + "." + name;
var info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower()); var info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
if (info == null && typeName !=null && typeName.Contains("<>f__AnonymousType")) if (info == null)
{ {
key = item.Name + "." + name; key = item.Name + "." + name;
info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower()); info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());

82
Src/Asp.NetCore2/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerUpdateBuilder.cs

@ -2,11 +2,93 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SqlSugar namespace SqlSugar
{ {
public class SqlServerUpdateBuilder: UpdateBuilder public class SqlServerUpdateBuilder: UpdateBuilder
{ {
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{
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();
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;
}));
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);
}
updateTable.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(base.SqlTemplateBatchSelect, GetValue(it), Builder.GetTranslationColumnName(it.DbColumnName)))));
++i;
}
pageIndex++;
updateTable.Append("\r\n");
string whereString = null;
if (this.WhereValues.HasValue())
{
foreach (var item in WhereValues)
{
var isFirst = whereString == null;
whereString += (isFirst ? null : " AND ");
whereString += Regex.Replace(item, "\\" + this.Builder.SqlTranslationLeft, "S." + this.Builder.SqlTranslationLeft);
}
}
if (PrimaryKeys.HasValue())
{
foreach (var item in PrimaryKeys)
{
var isFirst = whereString == null;
whereString += (isFirst ? null : " AND ");
whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item));
}
}
batchUpdateSql.AppendFormat(SqlTemplateJoin, updateTable, whereString);
}
return batchUpdateSql.ToString();
}
private object GetValue(DbColumnInfo it)
{
if (it.SqlParameterDbType!=null&&it.SqlParameterDbType.Equals(System.Data.DbType.AnsiString))
{
var value = FormatValue(it.Value);
if (value is string&&value.ObjToString().StartsWith("N'"))
{
return value.ObjToString().TrimStart('N');
}
else
{
return value;
}
}
else
{
return FormatValue(it.Value);
}
}
} }
} }

Loading…
Cancel
Save