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.

170 lines
7.4 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace SqlSugar
  6. {
  7. public class EntityMaintenance
  8. {
  9. public SqlSugarClient Context { get; set; }
  10. public EntityInfo GetEntityInfo<T>()
  11. {
  12. return GetEntityInfo(typeof(T));
  13. }
  14. public EntityInfo GetEntityInfo(Type type)
  15. {
  16. var cacheKey = nameof(GetEntityInfo) + type.FullName;
  17. return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
  18. () =>
  19. {
  20. var result = new EntityInfo();
  21. var sugarAttributeInfo = type.GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).SingleOrDefault(it => it is SugarTable);
  22. if (sugarAttributeInfo.HasValue())
  23. {
  24. var sugarTable = (SugarTable)sugarAttributeInfo;
  25. result.DbTableName = sugarTable.TableName;
  26. }
  27. result.Type = type;
  28. result.EntityName = result.Type.Name;
  29. result.Columns = new List<EntityColumnInfo>();
  30. SetColumns(result);
  31. return result;
  32. });
  33. }
  34. public string GetTableName<T>()
  35. {
  36. var typeName = typeof(T).Name;
  37. if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) return typeName;
  38. else
  39. {
  40. var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
  41. return mappingInfo == null ? typeName : mappingInfo.DbTableName;
  42. }
  43. }
  44. public string GetTableName(Type entityType)
  45. {
  46. var typeName = entityType.Name;
  47. if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) return typeName;
  48. else
  49. {
  50. var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
  51. return mappingInfo == null ? typeName : mappingInfo.DbTableName;
  52. }
  53. }
  54. public string GetTableName(string entityName)
  55. {
  56. var typeName = entityName;
  57. if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) return typeName;
  58. else
  59. {
  60. var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
  61. return mappingInfo == null ? typeName : mappingInfo.DbTableName;
  62. }
  63. }
  64. public string GetEntityName(string tableName)
  65. {
  66. if (this.Context.MappingTables == null || this.Context.MappingTables.Count == 0) return tableName;
  67. else
  68. {
  69. var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.DbTableName == tableName);
  70. return mappingInfo == null ? tableName : mappingInfo.EntityName;
  71. }
  72. }
  73. public string GetDbColumnName<T>(string propertyName)
  74. {
  75. var isAny = this.GetEntityInfo<T>().Columns.Any(it => it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
  76. Check.Exception(!isAny, "Property " + propertyName + " is Invalid");
  77. var typeName = typeof(T).Name;
  78. if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) return propertyName;
  79. else
  80. {
  81. var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.PropertyName == propertyName);
  82. return mappingInfo == null ? propertyName : mappingInfo.DbColumnName;
  83. }
  84. }
  85. public string GetPropertyName<T>(string dbColumnName)
  86. {
  87. var typeName = typeof(T).Name;
  88. if (this.Context.MappingColumns == null || this.Context.MappingColumns.Count == 0) return dbColumnName;
  89. else
  90. {
  91. var mappingInfo = this.Context.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.DbColumnName == dbColumnName);
  92. return mappingInfo == null ? dbColumnName : mappingInfo.PropertyName;
  93. }
  94. }
  95. public PropertyInfo GetProperty<T>(string dbColumnName)
  96. {
  97. var propertyName = GetPropertyName<T>(dbColumnName);
  98. return typeof(T).GetProperties().First(it => it.Name == propertyName);
  99. }
  100. #region Primary key
  101. private void SetColumns(EntityInfo result)
  102. {
  103. foreach (var property in result.Type.GetProperties())
  104. {
  105. var column = new EntityColumnInfo();
  106. //var isVirtual = property.GetGetMethod().IsVirtual;
  107. //if (isVirtual) continue;
  108. var sugarColumn = property.GetCustomAttributes(typeof(SugarColumn), true)
  109. .Where(it => it is SugarColumn)
  110. .Select(it => (SugarColumn)it)
  111. .FirstOrDefault();
  112. column.DbTableName = result.DbTableName;
  113. column.EntityName = result.EntityName;
  114. column.PropertyName = property.Name;
  115. column.PropertyInfo = property;
  116. if (sugarColumn.IsNullOrEmpty())
  117. {
  118. column.DbColumnName = property.Name;
  119. }
  120. else
  121. {
  122. if (!sugarColumn.IsIgnore)
  123. {
  124. column.DbColumnName = sugarColumn.ColumnName.IsNullOrEmpty() ? property.Name : sugarColumn.ColumnName;
  125. column.IsPrimarykey = sugarColumn.IsPrimaryKey;
  126. column.IsIdentity = sugarColumn.IsIdentity;
  127. column.ColumnDescription = sugarColumn.ColumnDescription;
  128. column.IsNullable = sugarColumn.IsNullable;
  129. column.Length = sugarColumn.Length;
  130. column.OldDbColumnName = sugarColumn.OldColumnName;
  131. column.DataType = sugarColumn.ColumnDataType;
  132. column.DecimalDigits = sugarColumn.DecimalDigits;
  133. column.OracleSequenceName = sugarColumn.OracleSequenceName;
  134. column.IsOnlyIgnoreInsert = sugarColumn.IsOnlyIgnoreInsert;
  135. }
  136. else
  137. {
  138. column.IsIgnore = true;
  139. }
  140. }
  141. if (this.Context.MappingColumns.HasValue())
  142. {
  143. var golbalMappingInfo = this.Context.MappingColumns.FirstOrDefault(it => it.EntityName.Equals(result.EntityName, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName == column.PropertyName);
  144. if (golbalMappingInfo != null)
  145. column.DbColumnName = golbalMappingInfo.DbColumnName;
  146. }
  147. if (this.Context.IgnoreColumns.HasValue())
  148. {
  149. var golbalMappingInfo = this.Context.IgnoreColumns.FirstOrDefault(it => it.EntityName.Equals(result.EntityName, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName == column.PropertyName);
  150. if (golbalMappingInfo != null)
  151. column.IsIgnore = true;
  152. }
  153. result.Columns.Add(column);
  154. }
  155. }
  156. #endregion Primary key
  157. }
  158. }