mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
-
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -9,9 +10,33 @@ namespace SqlSugar
|
||||
public class EntityProvider
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public EntityInfo GetEntityInfo<T>()where T:class,new()
|
||||
|
||||
public List<EntityInfo> GetAllEntities()
|
||||
{
|
||||
string cacheKey = "GetEntityInfo"+typeof(T).FullName;
|
||||
string cacheKey = "GetAllEntities";
|
||||
return CacheFactory.Func<List<EntityInfo>>(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[key];
|
||||
|
||||
}, (cm, key) =>
|
||||
{
|
||||
List<EntityInfo> reval = new List<EntityInfo>();
|
||||
var classes = Assembly.Load(this.Context.EntityNamespace.Split('.').First()).GetTypes();
|
||||
foreach (var item in classes)
|
||||
{
|
||||
reval.Add(GetEntityInfo(item));
|
||||
}
|
||||
return reval;
|
||||
});
|
||||
}
|
||||
public EntityInfo GetEntityInfo<T>()
|
||||
{
|
||||
return GetEntityInfo(typeof(T));
|
||||
}
|
||||
public EntityInfo GetEntityInfo(Type type)
|
||||
{
|
||||
string cacheKey = "GetEntityInfo" + type.FullName;
|
||||
return CacheFactory.Func<EntityInfo>(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
@@ -20,18 +45,17 @@ namespace SqlSugar
|
||||
}, (cm, key) =>
|
||||
{
|
||||
EntityInfo result = new EntityInfo();
|
||||
var type = typeof(T);
|
||||
var sugarAttributeInfo = type.GetCustomAttributes(typeof(SugarTable), true).Where(it => it is SugarTable).SingleOrDefault();
|
||||
if (sugarAttributeInfo.IsValuable())
|
||||
{
|
||||
var sugarTable = (SugarTable)sugarAttributeInfo;
|
||||
result.DbTableName = sugarTable.TableName;
|
||||
}
|
||||
result.Type = type;
|
||||
result.Type.GetProperties();
|
||||
result.Name =result.Type.Name;
|
||||
result.EntityName = result.Type.Name;
|
||||
result.Columns = new List<EntityColumnInfo>();
|
||||
foreach (var item in result.Type.GetProperties())
|
||||
{
|
||||
EntityColumnInfo column = new EntityColumnInfo();
|
||||
column.Name = item.Name;
|
||||
column.PropertyInfo = item;
|
||||
result.Columns.Add(column);
|
||||
}
|
||||
SetColumns(result);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
@@ -74,5 +98,18 @@ namespace SqlSugar
|
||||
return mappingInfo == null ? dbColumnName : mappingInfo.DbColumnName;
|
||||
}
|
||||
}
|
||||
|
||||
#region Primary key
|
||||
private static void SetColumns(EntityInfo result)
|
||||
{
|
||||
foreach (var item in result.Type.GetProperties())
|
||||
{
|
||||
EntityColumnInfo column = new EntityColumnInfo();
|
||||
column.PropertyName = item.Name;
|
||||
column.PropertyInfo = item;
|
||||
result.Columns.Add(column);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@@ -41,7 +41,7 @@ namespace SqlSugar
|
||||
foreach (var deleteObj in deleteObjs)
|
||||
{
|
||||
var entityPropertyName = this.Context.EntityProvider.GetEntityPropertyName<T>(primaryField);
|
||||
var columnInfo = entityInfo.Columns.Single(it => it.Name == entityPropertyName);
|
||||
var columnInfo = entityInfo.Columns.Single(it => it.PropertyName == entityPropertyName);
|
||||
var value = columnInfo.PropertyInfo.GetValue(deleteObj, null);
|
||||
primaryKeyValues.Add(value);
|
||||
}
|
||||
@@ -66,7 +66,7 @@ namespace SqlSugar
|
||||
if (i == 0)
|
||||
andString.Append(DeleteBuilder.WhereInAndTemplate + PubConst.Space);
|
||||
var entityPropertyName = this.Context.EntityProvider.GetEntityPropertyName<T>(primaryField);
|
||||
var columnInfo = entityInfo.Columns.Single(it => it.Name == entityPropertyName);
|
||||
var columnInfo = entityInfo.Columns.Single(it => it.PropertyName == entityPropertyName);
|
||||
var entityValue = columnInfo.PropertyInfo.GetValue(deleteObj, null);
|
||||
andString.AppendFormat(DeleteBuilder.WhereInEqualTemplate, primaryField, entityValue);
|
||||
++i;
|
||||
|
@@ -109,7 +109,7 @@ namespace SqlSugar
|
||||
#region IgnoreColumns
|
||||
if (this.Context.IgnoreColumns != null && this.Context.IgnoreColumns.Any())
|
||||
{
|
||||
var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.Name).ToList();
|
||||
var currentIgnoreColumns = this.Context.IgnoreColumns.Where(it => it.EntityName == this.EntityInfo.EntityName).ToList();
|
||||
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it =>
|
||||
{
|
||||
return !currentIgnoreColumns.Any(i => it.EntityPropertyName.Equals(i.EntityPropertyName, StringComparison.CurrentCulture));
|
||||
@@ -127,10 +127,10 @@ namespace SqlSugar
|
||||
}
|
||||
internal void Init()
|
||||
{
|
||||
this.InsertBuilder.TableName = EntityInfo.Name;
|
||||
this.InsertBuilder.TableName = EntityInfo.EntityName;
|
||||
if (IsMappingTable)
|
||||
{
|
||||
var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.Name);
|
||||
var mappingInfo = this.Context.MappingTables.SingleOrDefault(it => it.EntityName == EntityInfo.EntityName);
|
||||
if (mappingInfo != null)
|
||||
{
|
||||
this.InsertBuilder.TableName = mappingInfo.DbTableName;
|
||||
@@ -146,8 +146,8 @@ namespace SqlSugar
|
||||
var columnInfo = new DbColumnInfo()
|
||||
{
|
||||
Value = column.PropertyInfo.GetValue(item),
|
||||
ColumnName = GetDbColumnName(column.Name),
|
||||
EntityPropertyName = column.Name,
|
||||
ColumnName = GetDbColumnName(column.PropertyName),
|
||||
EntityPropertyName = column.PropertyName,
|
||||
TableId = i
|
||||
};
|
||||
insertItem.Add(columnInfo);
|
||||
@@ -162,9 +162,9 @@ namespace SqlSugar
|
||||
{
|
||||
return entityName;
|
||||
}
|
||||
if (this.Context.MappingColumns.Any(it => it.EntityName.Equals(EntityInfo.Name, StringComparison.CurrentCultureIgnoreCase)))
|
||||
if (this.Context.MappingColumns.Any(it => it.EntityName.Equals(EntityInfo.EntityName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
this.MappingColumnList = this.Context.MappingColumns.Where(it => it.EntityName.Equals(EntityInfo.Name, StringComparison.CurrentCultureIgnoreCase)).ToList();
|
||||
this.MappingColumnList = this.Context.MappingColumns.Where(it => it.EntityName.Equals(EntityInfo.EntityName, StringComparison.CurrentCultureIgnoreCase)).ToList();
|
||||
}
|
||||
if (MappingColumnList == null || !MappingColumnList.Any())
|
||||
{
|
||||
|
@@ -10,7 +10,8 @@ namespace SqlSugar
|
||||
public class EntityColumnInfo
|
||||
{
|
||||
public PropertyInfo PropertyInfo { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string PropertyName { get; set; }
|
||||
public string DbColumnName { get; set; }
|
||||
public int Length { get; set; }
|
||||
public string ColumnDescription { get; set; }
|
||||
public string DefaultValue { get; set; }
|
||||
|
@@ -9,7 +9,8 @@ namespace SqlSugar
|
||||
{
|
||||
public class EntityInfo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string EntityName { get; set; }
|
||||
public string DbTableName { get; set; }
|
||||
public Type Type { get; set; }
|
||||
public List<EntityColumnInfo> Columns { get; set; }
|
||||
}
|
||||
|
Reference in New Issue
Block a user