mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 02:14:53 +08:00
Performance optimization
This commit is contained in:
@@ -620,14 +620,19 @@ namespace SqlSugar
|
|||||||
foreach (var column in EntityInfo.Columns)
|
foreach (var column in EntityInfo.Columns)
|
||||||
{
|
{
|
||||||
if (column.IsIgnore || column.IsOnlyIgnoreInsert) continue;
|
if (column.IsIgnore || column.IsOnlyIgnoreInsert) continue;
|
||||||
|
var isMapping = IsMappingColumns;
|
||||||
var columnInfo = new DbColumnInfo()
|
var columnInfo = new DbColumnInfo()
|
||||||
{
|
{
|
||||||
Value = column.PropertyInfo.GetValue(item, null),
|
Value = PropertyCallAdapterProvider<T>.GetInstance(column.PropertyName).InvokeGet(item),
|
||||||
DbColumnName = GetDbColumnName(column.PropertyName),
|
DbColumnName = column.DbColumnName,
|
||||||
PropertyName = column.PropertyName,
|
PropertyName = column.PropertyName,
|
||||||
PropertyType = UtilMethods.GetUnderType(column.PropertyInfo),
|
PropertyType = UtilMethods.GetUnderType(column.PropertyInfo),
|
||||||
TableId = i
|
TableId = i
|
||||||
};
|
};
|
||||||
|
if (isMapping)
|
||||||
|
{
|
||||||
|
columnInfo.DbColumnName = GetDbColumnName(column.PropertyName);
|
||||||
|
}
|
||||||
if (column.IsJson)
|
if (column.IsJson)
|
||||||
{
|
{
|
||||||
columnInfo.IsJson = true;
|
columnInfo.IsJson = true;
|
||||||
@@ -645,8 +650,8 @@ namespace SqlSugar
|
|||||||
if(columnInfo.Value!=null)
|
if(columnInfo.Value!=null)
|
||||||
columnInfo.Value = this.Context.Utilities.SerializeObject(columnInfo.Value);
|
columnInfo.Value = this.Context.Utilities.SerializeObject(columnInfo.Value);
|
||||||
}
|
}
|
||||||
var tranColumn=EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase));
|
//var tranColumn=EntityInfo.Columns.FirstOrDefault(it => it.IsTranscoding && it.DbColumnName.Equals(column.DbColumnName, StringComparison.CurrentCultureIgnoreCase));
|
||||||
if (tranColumn!=null&&columnInfo.Value.HasValue()) {
|
if (column.IsTranscoding&&columnInfo.Value.HasValue()) {
|
||||||
columnInfo.Value = UtilMethods.EncodeBase64(columnInfo.Value.ToString());
|
columnInfo.Value = UtilMethods.EncodeBase64(columnInfo.Value.ToString());
|
||||||
}
|
}
|
||||||
insertItem.Add(columnInfo);
|
insertItem.Add(columnInfo);
|
||||||
|
|||||||
@@ -123,14 +123,7 @@ namespace SqlSugar
|
|||||||
foreach (var rowInfos in DbColumnInfoList)
|
foreach (var rowInfos in DbColumnInfoList)
|
||||||
{
|
{
|
||||||
var dr = dt.NewRow();
|
var dr = dt.NewRow();
|
||||||
foreach (DataColumn item in dt.Columns)
|
foreach (var value in rowInfos)
|
||||||
{
|
|
||||||
var rows = rowInfos.ToList();
|
|
||||||
var value = rows.FirstOrDefault(it =>
|
|
||||||
it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) ||
|
|
||||||
it.PropertyName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase)
|
|
||||||
);
|
|
||||||
if (value != null)
|
|
||||||
{
|
{
|
||||||
if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType)
|
if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType)
|
||||||
{
|
{
|
||||||
@@ -143,8 +136,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
value.Value = DBNull.Value;
|
value.Value = DBNull.Value;
|
||||||
}
|
}
|
||||||
dr[item.ColumnName] = value.Value;
|
dr[value.DbColumnName] = value.Value;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
dt.Rows.Add(dr);
|
dt.Rows.Add(dr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -224,6 +224,7 @@
|
|||||||
<Compile Include="SqlSugarClient.cs" />
|
<Compile Include="SqlSugarClient.cs" />
|
||||||
<Compile Include="Utilities\CallContext.cs" />
|
<Compile Include="Utilities\CallContext.cs" />
|
||||||
<Compile Include="Utilities\CallContextAsync.cs" />
|
<Compile Include="Utilities\CallContextAsync.cs" />
|
||||||
|
<Compile Include="Utilities\PropertyCallAdapterProvider.cs" />
|
||||||
<Compile Include="Utilities\SugarRetry.cs" />
|
<Compile Include="Utilities\SugarRetry.cs" />
|
||||||
<Compile Include="Utilities\DataTableExtensions.cs" />
|
<Compile Include="Utilities\DataTableExtensions.cs" />
|
||||||
<Compile Include="Utilities\ReflectionExtensions.cs" />
|
<Compile Include="Utilities\ReflectionExtensions.cs" />
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public interface IPropertyCallAdapter<TThis>
|
||||||
|
{
|
||||||
|
object InvokeGet(TThis @this);
|
||||||
|
//add void InvokeSet(TThis @this, object value) if necessary
|
||||||
|
}
|
||||||
|
public class PropertyCallAdapter<TThis, TResult> : IPropertyCallAdapter<TThis>
|
||||||
|
{
|
||||||
|
private readonly Func<TThis, TResult> _getterInvocation;
|
||||||
|
|
||||||
|
public PropertyCallAdapter(Func<TThis, TResult> getterInvocation)
|
||||||
|
{
|
||||||
|
_getterInvocation = getterInvocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object InvokeGet(TThis @this)
|
||||||
|
{
|
||||||
|
return _getterInvocation.Invoke(@this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class PropertyCallAdapterProvider<TThis>
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<string, IPropertyCallAdapter<TThis>> _instances =
|
||||||
|
new Dictionary<string, IPropertyCallAdapter<TThis>>();
|
||||||
|
|
||||||
|
public static IPropertyCallAdapter<TThis> GetInstance(string forPropertyName)
|
||||||
|
{
|
||||||
|
IPropertyCallAdapter<TThis> instance;
|
||||||
|
if (!_instances.TryGetValue(forPropertyName, out instance))
|
||||||
|
{
|
||||||
|
var property = typeof(TThis).GetProperty(
|
||||||
|
forPropertyName,
|
||||||
|
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||||
|
|
||||||
|
MethodInfo getMethod;
|
||||||
|
Delegate getterInvocation = null;
|
||||||
|
if (property != null && (getMethod = property.GetGetMethod(true)) != null)
|
||||||
|
{
|
||||||
|
var openGetterType = typeof(Func<,>);
|
||||||
|
var concreteGetterType = openGetterType
|
||||||
|
.MakeGenericType(typeof(TThis), property.PropertyType);
|
||||||
|
|
||||||
|
getterInvocation =
|
||||||
|
Delegate.CreateDelegate(concreteGetterType, null, getMethod);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//throw exception or create a default getterInvocation returning null
|
||||||
|
}
|
||||||
|
|
||||||
|
var openAdapterType = typeof(PropertyCallAdapter<,>);
|
||||||
|
var concreteAdapterType = openAdapterType
|
||||||
|
.MakeGenericType(typeof(TThis), property.PropertyType);
|
||||||
|
instance = Activator
|
||||||
|
.CreateInstance(concreteAdapterType, getterInvocation)
|
||||||
|
as IPropertyCallAdapter<TThis>;
|
||||||
|
|
||||||
|
_instances.Add(forPropertyName, instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user