You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.9 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. namespace SqlSugar
  7. {
  8. public partial class DbBindAccessory
  9. {
  10. protected List<T> GetEntityList<T>(SqlSugarClient context, IDataReader dataReader)
  11. {
  12. var type = typeof(T);
  13. var fieldNames = GetDataReaderNames(dataReader);
  14. var cacheKey = GetCacheKey(type, fieldNames);
  15. var entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
  16. {
  17. var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader, fieldNames).CreateBuilder(type);
  18. return cacheResult;
  19. });
  20. var result = new List<T>();
  21. try
  22. {
  23. if (dataReader == null) return result;
  24. while (dataReader.Read())
  25. {
  26. result.Add(entytyList.Build(dataReader));
  27. }
  28. }
  29. catch (Exception ex)
  30. {
  31. Check.Exception(true, ErrorMessage.EntityMappingError, ex.Message);
  32. }
  33. return result;
  34. }
  35. private string GetCacheKey(Type type, List<string> keys)
  36. {
  37. var sb = new StringBuilder("DataReaderToList.");
  38. sb.Append(type.FullName);
  39. sb.Append(".");
  40. foreach (var item in keys)
  41. {
  42. sb.Append(item);
  43. }
  44. return sb.ToString();
  45. }
  46. private List<string> GetDataReaderNames(IDataReader dataReader)
  47. {
  48. var keys = new List<string>();
  49. var count = dataReader.FieldCount;
  50. for (int i = 0; i < count; i++)
  51. {
  52. keys.Add(dataReader.GetName(i));
  53. }
  54. return keys;
  55. }
  56. protected List<T> GetKeyValueList<T>(Type type, IDataReader dataReader)
  57. {
  58. var result = new List<T>();
  59. while (dataReader.Read())
  60. {
  61. if (UtilConstants.DicOO == type)
  62. {
  63. var kv = new KeyValuePair<object, object>(dataReader.GetValue(0), dataReader.GetValue(1));
  64. result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<object, object>)));
  65. }
  66. else if (UtilConstants.DicIS == type)
  67. {
  68. var kv = new KeyValuePair<int, string>(dataReader.GetValue(0).ObjToInt(), dataReader.GetValue(1).ObjToString());
  69. result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, string>)));
  70. }
  71. else if (UtilConstants.Dicii == type)
  72. {
  73. var kv = new KeyValuePair<int, int>(dataReader.GetValue(0).ObjToInt(), dataReader.GetValue(1).ObjToInt());
  74. result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, int>)));
  75. }
  76. else if (UtilConstants.DicSi == type)
  77. {
  78. var kv = new KeyValuePair<string, int>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1).ObjToInt());
  79. result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, int>)));
  80. }
  81. else if (UtilConstants.DicSo == type)
  82. {
  83. var kv = new KeyValuePair<string, object>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1));
  84. result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, object>)));
  85. }
  86. else if (UtilConstants.DicSS == type)
  87. {
  88. var kv = new KeyValuePair<string, string>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1).ObjToString());
  89. result.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, string>)));
  90. }
  91. else
  92. {
  93. Check.Exception(true, ErrorMessage.NotSupportedDictionary);
  94. }
  95. }
  96. return result;
  97. }
  98. protected List<T> GetArrayList<T>(Type type, IDataReader dataReader)
  99. {
  100. var result = new List<T>();
  101. var count = dataReader.FieldCount;
  102. var childType = type.GetElementType();
  103. while (dataReader.Read())
  104. {
  105. var array = new object[count];
  106. for (int i = 0; i < count; i++)
  107. {
  108. array[i] = Convert.ChangeType(dataReader.GetValue(i), childType);
  109. }
  110. if (childType == UtilConstants.StringType)
  111. result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToString()).ToArray(), type));
  112. else if (childType == UtilConstants.ObjType)
  113. result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? null : (object)it).ToArray(), type));
  114. else if (childType == UtilConstants.BoolType)
  115. result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToBool()).ToArray(), type));
  116. else if (childType == UtilConstants.ByteType)
  117. result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? 0 : (byte)it).ToArray(), type));
  118. else if (childType == UtilConstants.DecType)
  119. result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToDecimal()).ToArray(), type));
  120. else if (childType == UtilConstants.GuidType)
  121. result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? Guid.Empty : (Guid)it).ToArray(), type));
  122. else if (childType == UtilConstants.DateType)
  123. result.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? DateTime.MinValue : (DateTime)it).ToArray(), type));
  124. else if (childType == UtilConstants.IntType)
  125. result.Add((T)Convert.ChangeType(array.Select(it => it.ObjToInt()).ToArray(), type));
  126. else
  127. Check.Exception(true, ErrorMessage.NotSupportedArray);
  128. }
  129. return result;
  130. }
  131. protected List<T> GetValueTypeList<T>(Type type, IDataReader dataReader)
  132. {
  133. var result = new List<T>();
  134. while (dataReader.Read())
  135. {
  136. var value = dataReader.GetValue(0);
  137. if (type == UtilConstants.GuidType)
  138. {
  139. value = Guid.Parse(value.ToString());
  140. }
  141. if (value == DBNull.Value)
  142. {
  143. result.Add(default(T));
  144. }
  145. else if (type.IsEnum)
  146. {
  147. result.Add((T)Enum.Parse(type, value.ObjToString()));
  148. }
  149. else
  150. {
  151. result.Add((T)Convert.ChangeType(value, UtilMethods.GetUnderType(type)));
  152. }
  153. }
  154. return result;
  155. }
  156. }
  157. }