mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
Update Async
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public partial class DbBindAccessory
|
public partial class DbBindAccessory
|
||||||
@@ -32,6 +35,31 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
protected async Task<List<T>> GetEntityListAsync<T>(SqlSugarProvider context, IDataReader dataReader)
|
||||||
|
{
|
||||||
|
Type type = typeof(T);
|
||||||
|
var fieldNames = GetDataReaderNames(dataReader);
|
||||||
|
string cacheKey = GetCacheKey(type, fieldNames);
|
||||||
|
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
|
||||||
|
{
|
||||||
|
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader, fieldNames).CreateBuilder(type);
|
||||||
|
return cacheResult;
|
||||||
|
});
|
||||||
|
List<T> result = new List<T>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (dataReader == null) return result;
|
||||||
|
while (await((DbDataReader)dataReader).ReadAsync())
|
||||||
|
{
|
||||||
|
result.Add(entytyList.Build(dataReader));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Check.Exception(true, ErrorMessage.EntityMappingError, ex.Message);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private string GetCacheKey(Type type,List<string> keys)
|
private string GetCacheKey(Type type,List<string> keys)
|
||||||
{
|
{
|
||||||
@@ -61,40 +89,68 @@ namespace SqlSugar
|
|||||||
List<T> result = new List<T>();
|
List<T> result = new List<T>();
|
||||||
while (dataReader.Read())
|
while (dataReader.Read())
|
||||||
{
|
{
|
||||||
if (UtilConstants.DicOO == type)
|
GetKeyValueList(type, dataReader, result);
|
||||||
{
|
}
|
||||||
var kv = new KeyValuePair<object, object>(dataReader.GetValue(0), dataReader.GetValue(1));
|
return result;
|
||||||
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<object, object>)));
|
}
|
||||||
}
|
|
||||||
else if (UtilConstants.DicIS == type)
|
protected async Task<List<T>> GetKeyValueListAsync<T>(Type type, IDataReader dataReader)
|
||||||
{
|
{
|
||||||
var kv = new KeyValuePair<int, string>(dataReader.GetValue(0).ObjToInt(), dataReader.GetValue(1).ObjToString());
|
List<T> result = new List<T>();
|
||||||
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, string>)));
|
while (await ((DbDataReader)dataReader).ReadAsync())
|
||||||
}
|
{
|
||||||
else if (UtilConstants.Dicii == type)
|
GetKeyValueList(type, dataReader, result);
|
||||||
{
|
}
|
||||||
var kv = new KeyValuePair<int, int>(dataReader.GetValue(0).ObjToInt(), dataReader.GetValue(1).ObjToInt());
|
return result;
|
||||||
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, int>)));
|
}
|
||||||
}
|
|
||||||
else if (UtilConstants.DicSi == type)
|
private static void GetKeyValueList<T>(Type type, IDataReader dataReader, List<T> result)
|
||||||
{
|
{
|
||||||
var kv = new KeyValuePair<string, int>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1).ObjToInt());
|
if (UtilConstants.DicOO == type)
|
||||||
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, int>)));
|
{
|
||||||
}
|
var kv = new KeyValuePair<object, object>(dataReader.GetValue(0), dataReader.GetValue(1));
|
||||||
else if (UtilConstants.DicSo == type)
|
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<object, object>)));
|
||||||
{
|
}
|
||||||
var kv = new KeyValuePair<string, object>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1));
|
else if (UtilConstants.DicIS == type)
|
||||||
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, object>)));
|
{
|
||||||
}
|
var kv = new KeyValuePair<int, string>(dataReader.GetValue(0).ObjToInt(), dataReader.GetValue(1).ObjToString());
|
||||||
else if (UtilConstants.DicSS == type)
|
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, string>)));
|
||||||
{
|
}
|
||||||
var kv = new KeyValuePair<string, string>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1).ObjToString());
|
else if (UtilConstants.Dicii == type)
|
||||||
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, string>)));
|
{
|
||||||
}
|
var kv = new KeyValuePair<int, int>(dataReader.GetValue(0).ObjToInt(), dataReader.GetValue(1).ObjToInt());
|
||||||
else
|
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, int>)));
|
||||||
{
|
}
|
||||||
Check.Exception(true, ErrorMessage.NotSupportedDictionary);
|
else if (UtilConstants.DicSi == type)
|
||||||
}
|
{
|
||||||
|
var kv = new KeyValuePair<string, int>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1).ObjToInt());
|
||||||
|
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, int>)));
|
||||||
|
}
|
||||||
|
else if (UtilConstants.DicSo == type)
|
||||||
|
{
|
||||||
|
var kv = new KeyValuePair<string, object>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1));
|
||||||
|
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, object>)));
|
||||||
|
}
|
||||||
|
else if (UtilConstants.DicSS == type)
|
||||||
|
{
|
||||||
|
var kv = new KeyValuePair<string, string>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1).ObjToString());
|
||||||
|
result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, string>)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Check.Exception(true, ErrorMessage.NotSupportedDictionary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected async Task<List<T>> GetArrayListAsync<T>(Type type, IDataReader dataReader)
|
||||||
|
{
|
||||||
|
List<T> result = new List<T>();
|
||||||
|
int count = dataReader.FieldCount;
|
||||||
|
var childType = type.GetElementType();
|
||||||
|
while (await((DbDataReader)dataReader).ReadAsync())
|
||||||
|
{
|
||||||
|
GetArrayList(type, dataReader, result, count, childType);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -106,57 +162,78 @@ namespace SqlSugar
|
|||||||
var childType = type.GetElementType();
|
var childType = type.GetElementType();
|
||||||
while (dataReader.Read())
|
while (dataReader.Read())
|
||||||
{
|
{
|
||||||
object[] array = new object[count];
|
GetArrayList(type, dataReader, result, count, childType);
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
array[i] = Convert.ChangeType(dataReader.GetValue(i), childType);
|
|
||||||
}
|
|
||||||
if (childType == UtilConstants.StringType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToString()).ToArray(), type));
|
|
||||||
else if (childType == UtilConstants.ObjType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? null : (object)it).ToArray(), type));
|
|
||||||
else if (childType == UtilConstants.BoolType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToBool()).ToArray(), type));
|
|
||||||
else if (childType == UtilConstants.ByteType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? 0 : (byte)it).ToArray(), type));
|
|
||||||
else if (childType == UtilConstants.DecType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToDecimal()).ToArray(), type));
|
|
||||||
else if (childType == UtilConstants.GuidType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? Guid.Empty : (Guid)it).ToArray(), type));
|
|
||||||
else if (childType == UtilConstants.DateType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? DateTime.MinValue : (DateTime)it).ToArray(), type));
|
|
||||||
else if (childType == UtilConstants.IntType)
|
|
||||||
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToInt()).ToArray(), type));
|
|
||||||
else
|
|
||||||
Check.Exception(true, ErrorMessage.NotSupportedArray);
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static void GetArrayList<T>(Type type, IDataReader dataReader, List<T> result, int count, Type childType)
|
||||||
|
{
|
||||||
|
object[] array = new object[count];
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
array[i] = Convert.ChangeType(dataReader.GetValue(i), childType);
|
||||||
|
}
|
||||||
|
if (childType == UtilConstants.StringType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToString()).ToArray(), type));
|
||||||
|
else if (childType == UtilConstants.ObjType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? null : (object)it).ToArray(), type));
|
||||||
|
else if (childType == UtilConstants.BoolType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToBool()).ToArray(), type));
|
||||||
|
else if (childType == UtilConstants.ByteType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? 0 : (byte)it).ToArray(), type));
|
||||||
|
else if (childType == UtilConstants.DecType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToDecimal()).ToArray(), type));
|
||||||
|
else if (childType == UtilConstants.GuidType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? Guid.Empty : (Guid)it).ToArray(), type));
|
||||||
|
else if (childType == UtilConstants.DateType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? DateTime.MinValue : (DateTime)it).ToArray(), type));
|
||||||
|
else if (childType == UtilConstants.IntType)
|
||||||
|
result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToInt()).ToArray(), type));
|
||||||
|
else
|
||||||
|
Check.Exception(true, ErrorMessage.NotSupportedArray);
|
||||||
|
}
|
||||||
|
|
||||||
protected List<T> GetValueTypeList<T>(Type type, IDataReader dataReader)
|
protected List<T> GetValueTypeList<T>(Type type, IDataReader dataReader)
|
||||||
{
|
{
|
||||||
List<T> result = new List<T>();
|
List<T> result = new List<T>();
|
||||||
while (dataReader.Read())
|
while (dataReader.Read())
|
||||||
{
|
{
|
||||||
var value = dataReader.GetValue(0);
|
GetValueTypeList(type, dataReader, result);
|
||||||
if (type == UtilConstants.GuidType)
|
|
||||||
{
|
|
||||||
value = Guid.Parse(value.ToString());
|
|
||||||
}
|
|
||||||
if (value == DBNull.Value)
|
|
||||||
{
|
|
||||||
result.Add(default(T));
|
|
||||||
}
|
|
||||||
else if (type.IsEnum)
|
|
||||||
{
|
|
||||||
result.Add((T)Enum.Parse(type, value.ObjToString()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result.Add((T)Convert.ChangeType(value, UtilMethods.GetUnderType(type)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
protected async Task<List<T>> GetValueTypeListAsync<T>(Type type, IDataReader dataReader)
|
||||||
|
{
|
||||||
|
List<T> result = new List<T>();
|
||||||
|
while (await ((DbDataReader)dataReader).ReadAsync())
|
||||||
|
{
|
||||||
|
GetValueTypeList(type, dataReader, result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GetValueTypeList<T>(Type type, IDataReader dataReader, List<T> result)
|
||||||
|
{
|
||||||
|
var value = dataReader.GetValue(0);
|
||||||
|
if (type == UtilConstants.GuidType)
|
||||||
|
{
|
||||||
|
value = Guid.Parse(value.ToString());
|
||||||
|
}
|
||||||
|
if (value == DBNull.Value)
|
||||||
|
{
|
||||||
|
result.Add(default(T));
|
||||||
|
}
|
||||||
|
else if (type.IsEnum)
|
||||||
|
{
|
||||||
|
result.Add((T)Enum.Parse(type, value.ObjToString()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.Add((T)Convert.ChangeType(value, UtilMethods.GetUnderType(type)));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,6 +5,8 @@ using System.Data.SqlClient;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public abstract partial class DbBindProvider : DbBindAccessory, IDbBind
|
public abstract partial class DbBindProvider : DbBindAccessory, IDbBind
|
||||||
@@ -201,6 +203,28 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public virtual async Task<List<T>> DataReaderToListAsync<T>(Type type, IDataReader dataReader)
|
||||||
|
{
|
||||||
|
using (dataReader)
|
||||||
|
{
|
||||||
|
if (type.Name.Contains("KeyValuePair"))
|
||||||
|
{
|
||||||
|
return await GetKeyValueListAsync<T>(type, dataReader);
|
||||||
|
}
|
||||||
|
else if (type.IsValueType() || type == UtilConstants.StringType || type == UtilConstants.ByteArrayType)
|
||||||
|
{
|
||||||
|
return await GetValueTypeListAsync<T>(type, dataReader);
|
||||||
|
}
|
||||||
|
else if (type.IsArray)
|
||||||
|
{
|
||||||
|
return await GetArrayListAsync<T>(type, dataReader);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return await GetEntityListAsync<T>(Context, dataReader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
public virtual List<T> DataReaderToListNoUsing<T>(Type type, IDataReader dataReader)
|
public virtual List<T> DataReaderToListNoUsing<T>(Type type, IDataReader dataReader)
|
||||||
{
|
{
|
||||||
if (type.Name.Contains("KeyValuePair"))
|
if (type.Name.Contains("KeyValuePair"))
|
||||||
|
@@ -916,7 +916,8 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public Task<List<T>> ToListAsync()
|
public Task<List<T>> ToListAsync()
|
||||||
{
|
{
|
||||||
return Task.FromResult(ToList());
|
InitMapping();
|
||||||
|
return _ToListAsync<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<string> ToJsonAsync()
|
public Task<string> ToJsonAsync()
|
||||||
@@ -1132,6 +1133,23 @@ namespace SqlSugar
|
|||||||
_Mapper(result);
|
_Mapper(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
protected async Task<List<TResult>> _ToListAsync<TResult>()
|
||||||
|
{
|
||||||
|
List<TResult> result = null;
|
||||||
|
var sqlObj = this.ToSql();
|
||||||
|
if (IsCache)
|
||||||
|
{
|
||||||
|
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
|
||||||
|
result = CacheSchemeMain.GetOrCreate<List<TResult>>(cacheService, this.QueryBuilder, () => { return GetData<TResult>(sqlObj); }, CacheTime, this.Context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result =await GetDataAsync<TResult>(sqlObj);
|
||||||
|
}
|
||||||
|
RestoreMapping();
|
||||||
|
_Mapper(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
protected void _Mapper<TResult>(List<TResult> result)
|
protected void _Mapper<TResult>(List<TResult> result)
|
||||||
{
|
{
|
||||||
@@ -1544,7 +1562,7 @@ namespace SqlSugar
|
|||||||
var isComplexModel = QueryBuilder.IsComplexModel(sqlObj.Key);
|
var isComplexModel = QueryBuilder.IsComplexModel(sqlObj.Key);
|
||||||
var entityType = typeof(TResult);
|
var entityType = typeof(TResult);
|
||||||
var dataReader = await this.Db.GetDataReaderAsync(sqlObj.Key, sqlObj.Value.ToArray());
|
var dataReader = await this.Db.GetDataReaderAsync(sqlObj.Key, sqlObj.Value.ToArray());
|
||||||
result = GetData<TResult>(isComplexModel, entityType, dataReader);
|
result =await GetDataAsync<TResult>(isComplexModel, entityType, dataReader);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1570,6 +1588,29 @@ namespace SqlSugar
|
|||||||
SetContextModel(result, entityType);
|
SetContextModel(result, entityType);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
private async Task<List<TResult>> GetDataAsync<TResult>(bool isComplexModel, Type entityType, IDataReader dataReader)
|
||||||
|
{
|
||||||
|
List<TResult> result;
|
||||||
|
if (entityType == UtilConstants.DynamicType)
|
||||||
|
{
|
||||||
|
result =await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader) as List<TResult>;
|
||||||
|
}
|
||||||
|
else if (entityType == UtilConstants.ObjType)
|
||||||
|
{
|
||||||
|
var expObj = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader);
|
||||||
|
result = expObj.Select(it => ((TResult)(object)it)).ToList();
|
||||||
|
}
|
||||||
|
else if (entityType.IsAnonymousType() || isComplexModel)
|
||||||
|
{
|
||||||
|
result =await this.Context.Utilities.DataReaderToListAsync<TResult>(dataReader);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result =await this.Bind.DataReaderToListAsync<TResult>(entityType, dataReader);
|
||||||
|
}
|
||||||
|
SetContextModel(result, entityType);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
protected void _InQueryable(Expression expression, KeyValuePair<string, List<SugarParameter>> sqlObj)
|
protected void _InQueryable(Expression expression, KeyValuePair<string, List<SugarParameter>> sqlObj)
|
||||||
{
|
{
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
using System.Dynamic;
|
using System.Dynamic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -60,6 +61,26 @@ namespace SqlSugar
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
///DataReader to Dynamic List
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<List<ExpandoObject>> DataReaderToExpandoObjectListAsync(IDataReader reader)
|
||||||
|
{
|
||||||
|
using (reader)
|
||||||
|
{
|
||||||
|
List<ExpandoObject> result = new List<ExpandoObject>();
|
||||||
|
if (reader != null && !reader.IsClosed)
|
||||||
|
{
|
||||||
|
while (await((DbDataReader)reader).ReadAsync())
|
||||||
|
{
|
||||||
|
result.Add(DataReaderToExpandoObject(reader));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///DataReader to DataReaderToDictionary
|
///DataReader to DataReaderToDictionary
|
||||||
@@ -130,52 +151,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
var readerValues = DataReaderToDictionary(reader, tType);
|
Dictionary<string, object> result = DataReaderToList(reader, tType, classProperties, reval);
|
||||||
var result = new Dictionary<string, object>();
|
|
||||||
foreach (var item in classProperties)
|
|
||||||
{
|
|
||||||
var name = item.Name;
|
|
||||||
var typeName = tType.Name;
|
|
||||||
if (item.PropertyType.IsClass())
|
|
||||||
{
|
|
||||||
result.Add(name, DataReaderToDynamicList_Part(readerValues, item, reval));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (readerValues.Any(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
|
|
||||||
{
|
|
||||||
var addValue = readerValues.ContainsKey(name) ? readerValues[name] : readerValues.First(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Value;
|
|
||||||
if (addValue == DBNull.Value || addValue == null)
|
|
||||||
{
|
|
||||||
if (item.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.DecType, UtilConstants.DobType, UtilConstants.ByteType))
|
|
||||||
{
|
|
||||||
addValue = 0;
|
|
||||||
}
|
|
||||||
else if (item.PropertyType == UtilConstants.GuidType)
|
|
||||||
{
|
|
||||||
addValue = Guid.Empty;
|
|
||||||
}
|
|
||||||
else if (item.PropertyType == UtilConstants.DateType)
|
|
||||||
{
|
|
||||||
addValue = DateTime.MinValue;
|
|
||||||
}
|
|
||||||
else if (item.PropertyType == UtilConstants.StringType)
|
|
||||||
{
|
|
||||||
addValue = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
addValue = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (item.PropertyType == UtilConstants.IntType)
|
|
||||||
{
|
|
||||||
addValue = Convert.ToInt32(addValue);
|
|
||||||
}
|
|
||||||
result.Add(name, addValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var stringValue = SerializeObject(result);
|
var stringValue = SerializeObject(result);
|
||||||
reval.Add((T)DeserializeObject<T>(stringValue));
|
reval.Add((T)DeserializeObject<T>(stringValue));
|
||||||
}
|
}
|
||||||
@@ -183,6 +159,83 @@ namespace SqlSugar
|
|||||||
return reval;
|
return reval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// DataReaderToList
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="reader"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<List<T>> DataReaderToListAsync<T>(IDataReader reader)
|
||||||
|
{
|
||||||
|
using (reader)
|
||||||
|
{
|
||||||
|
var tType = typeof(T);
|
||||||
|
var classProperties = tType.GetProperties().ToList();
|
||||||
|
var reval = new List<T>();
|
||||||
|
if (reader != null && !reader.IsClosed)
|
||||||
|
{
|
||||||
|
while (await ((DbDataReader)reader).ReadAsync())
|
||||||
|
{
|
||||||
|
Dictionary<string, object> result = DataReaderToList(reader, tType, classProperties, reval);
|
||||||
|
var stringValue = SerializeObject(result);
|
||||||
|
reval.Add((T)DeserializeObject<T>(stringValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return reval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<string, object> DataReaderToList<T>(IDataReader reader, Type tType, List<PropertyInfo> classProperties, List<T> reval)
|
||||||
|
{
|
||||||
|
var readerValues = DataReaderToDictionary(reader, tType);
|
||||||
|
var result = new Dictionary<string, object>();
|
||||||
|
foreach (var item in classProperties)
|
||||||
|
{
|
||||||
|
var name = item.Name;
|
||||||
|
var typeName = tType.Name;
|
||||||
|
if (item.PropertyType.IsClass())
|
||||||
|
{
|
||||||
|
result.Add(name, DataReaderToDynamicList_Part(readerValues, item, reval));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (readerValues.Any(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
{
|
||||||
|
var addValue = readerValues.ContainsKey(name) ? readerValues[name] : readerValues.First(it => it.Key.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Value;
|
||||||
|
if (addValue == DBNull.Value || addValue == null)
|
||||||
|
{
|
||||||
|
if (item.PropertyType.IsIn(UtilConstants.IntType, UtilConstants.DecType, UtilConstants.DobType, UtilConstants.ByteType))
|
||||||
|
{
|
||||||
|
addValue = 0;
|
||||||
|
}
|
||||||
|
else if (item.PropertyType == UtilConstants.GuidType)
|
||||||
|
{
|
||||||
|
addValue = Guid.Empty;
|
||||||
|
}
|
||||||
|
else if (item.PropertyType == UtilConstants.DateType)
|
||||||
|
{
|
||||||
|
addValue = DateTime.MinValue;
|
||||||
|
}
|
||||||
|
else if (item.PropertyType == UtilConstants.StringType)
|
||||||
|
{
|
||||||
|
addValue = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
addValue = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (item.PropertyType == UtilConstants.IntType)
|
||||||
|
{
|
||||||
|
addValue = Convert.ToInt32(addValue);
|
||||||
|
}
|
||||||
|
result.Add(name, addValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private Dictionary<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval)
|
private Dictionary<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval)
|
||||||
{
|
{
|
||||||
|
@@ -13,7 +13,9 @@ namespace SqlSugar
|
|||||||
SqlSugarProvider Context { get; set; }
|
SqlSugarProvider Context { get; set; }
|
||||||
ExpandoObject DataReaderToExpandoObject(IDataReader reader);
|
ExpandoObject DataReaderToExpandoObject(IDataReader reader);
|
||||||
List<ExpandoObject> DataReaderToExpandoObjectList(IDataReader reader);
|
List<ExpandoObject> DataReaderToExpandoObjectList(IDataReader reader);
|
||||||
|
Task<List<ExpandoObject>> DataReaderToExpandoObjectListAsync(IDataReader dataReader);
|
||||||
List<T> DataReaderToList<T>(IDataReader reader);
|
List<T> DataReaderToList<T>(IDataReader reader);
|
||||||
|
Task<List<T>> DataReaderToListAsync<T>(IDataReader dataReader);
|
||||||
string SerializeObject(object value);
|
string SerializeObject(object value);
|
||||||
string SerializeObject(object value, Type type);
|
string SerializeObject(object value, Type type);
|
||||||
T DeserializeObject<T>(string value);
|
T DeserializeObject<T>(string value);
|
||||||
@@ -27,6 +29,5 @@ namespace SqlSugar
|
|||||||
void RemoveCacheAll<T>();
|
void RemoveCacheAll<T>();
|
||||||
void RemoveCache<T>(string key);
|
void RemoveCache<T>(string key);
|
||||||
void PageEach<T>(IEnumerable<T> pageItems, int pageSize, Action<List<T>> action);
|
void PageEach<T>(IEnumerable<T> pageItems, int pageSize, Action<List<T>> action);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public partial interface IDbBind
|
public partial interface IDbBind
|
||||||
@@ -21,6 +23,8 @@ namespace SqlSugar
|
|||||||
string GetCsharpTypeName(string dbTypeName);
|
string GetCsharpTypeName(string dbTypeName);
|
||||||
List<KeyValuePair<string, CSharpDataType>> MappingTypes { get; }
|
List<KeyValuePair<string, CSharpDataType>> MappingTypes { get; }
|
||||||
List<T> DataReaderToList<T>(Type type, IDataReader reader);
|
List<T> DataReaderToList<T>(Type type, IDataReader reader);
|
||||||
|
Task<List<T>> DataReaderToListAsync<T>(Type entityType, IDataReader dataReader);
|
||||||
List<T> DataReaderToListNoUsing<T>(Type type, IDataReader reader);
|
List<T> DataReaderToListNoUsing<T>(Type type, IDataReader reader);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user