From 615d31a3111ec5ba5cc7fb3ebef63eea87681f27 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 18 Jul 2023 20:20:06 +0800 Subject: [PATCH] Support Discrimator --- .../CodeFirstProvider/CodeFirstProvider.cs | 11 +++++++++++ .../SqlSugar/Abstract/DynamicBuilder/Helper.cs | 8 ++++++-- .../EntityMaintenance/EntityMaintenance.cs | 1 + .../InsertableProvider/InsertableHelper.cs | 12 ++++++++++++ .../SqlSugar/Entities/DiscriminatorObject .cs | 14 ++++++++++++++ Src/Asp.Net/SqlSugar/Entities/EntityInfo.cs | 1 + .../Entities/Mapping/SugarMappingAttribute.cs | 1 + Src/Asp.Net/SqlSugar/SqlSugar.csproj | 1 + 8 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 Src/Asp.Net/SqlSugar/Entities/DiscriminatorObject .cs diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index dac379001..567d43226 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -227,6 +227,17 @@ namespace SqlSugar protected virtual void Execute(Type entityType) { var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(entityType); + if (entityInfo.Discrimator.HasValue()) + { + Check.ExceptionEasy(!Regex.IsMatch(entityInfo.Discrimator, @"^(?:\w+:\w+)(?:,\w+:\w+)*$"), "The format should be type:cat for this type, and if there are multiple, it can be FieldName:cat,FieldName2:dog ", "格式错误应该是type:cat这种格式,如果是多个可以FieldName:cat,FieldName2:dog,不要有空格"); + var array = entityInfo.Discrimator.Split(','); + foreach (var disItem in array) + { + var name = disItem.Split(':').First(); + var value = disItem.Split(':').Last(); + entityInfo.Columns.Add(new EntityColumnInfo() { IsOnlyIgnoreUpdate = true, DbColumnName = name, UnderType = typeof(string), PropertyName = name, Length = 50 }); + } + } if (this.MappingTables.ContainsKey(entityType)) { entityInfo.DbTableName = this.MappingTables[entityType]; diff --git a/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs index a8cf8b4c0..6dbb6b911 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs @@ -39,13 +39,17 @@ namespace SqlSugar attributeType.GetProperty(nameof(SugarTable.TableName)), attributeType.GetProperty(nameof(SugarTable.TableDescription)) , attributeType.GetProperty(nameof(SugarTable.IsDisabledUpdateAll)) , - attributeType.GetProperty(nameof(SugarTable.IsDisabledDelete)) + attributeType.GetProperty(nameof(SugarTable.IsDisabledDelete)), + attributeType.GetProperty(nameof(SugarTable.IsCreateTableFiledSort)), + attributeType.GetProperty(nameof(SugarTable.Discrimator)) } , new object[] { sugarTable.TableName, sugarTable.TableDescription , sugarTable.IsDisabledUpdateAll, - sugarTable.IsDisabledDelete + sugarTable.IsDisabledDelete, + sugarTable.IsCreateTableFiledSort, + sugarTable.Discrimator }); return attributeBuilder; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index 1c679b13f..ed1e5d80f 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -62,6 +62,7 @@ namespace SqlSugar result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll; result.IsDisabledDelete = sugarTable.IsDisabledDelete; result.IsCreateTableFiledSort = sugarTable.IsCreateTableFiledSort; + result.Discrimator = sugarTable.Discrimator; } var indexs = type.GetCustomAttributes(typeof(SugarIndexAttribute)); if (indexs != null && indexs.Any()) diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs index 6152fc7e3..552f9ecfc 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace SqlSugar @@ -292,6 +293,17 @@ namespace SqlSugar } insertItem.Add(columnInfo); } + if (EntityInfo.Discrimator.HasValue()) + { + Check.ExceptionEasy(!Regex.IsMatch(EntityInfo.Discrimator, @"^(?:\w+:\w+)(?:,\w+:\w+)*$"), "The format should be type:cat for this type, and if there are multiple, it can be FieldName:cat,FieldName2:dog ", "格式错误应该是type:cat这种格式,如果是多个可以FieldName:cat,FieldName2:dog,不要有空格"); + var array = EntityInfo.Discrimator.Split(','); + foreach (var disItem in array) + { + var name = disItem.Split(':').First(); + var value = disItem.Split(':').Last(); + insertItem.Add(new DbColumnInfo() { DbColumnName = name, PropertyName = name, PropertyType = typeof(string), Value = value }); + } + } } private string GetDbColumnName(string propertyName) diff --git a/Src/Asp.Net/SqlSugar/Entities/DiscriminatorObject .cs b/Src/Asp.Net/SqlSugar/Entities/DiscriminatorObject .cs new file mode 100644 index 000000000..a45102e52 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Entities/DiscriminatorObject .cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public struct DiscriminatorObject + { + public string FieldName { get; set; } + public string FieldValue { get; set; } + } +} diff --git a/Src/Asp.Net/SqlSugar/Entities/EntityInfo.cs b/Src/Asp.Net/SqlSugar/Entities/EntityInfo.cs index 46833ac66..a98878631 100644 --- a/Src/Asp.Net/SqlSugar/Entities/EntityInfo.cs +++ b/Src/Asp.Net/SqlSugar/Entities/EntityInfo.cs @@ -19,5 +19,6 @@ namespace SqlSugar public bool IsDisabledUpdateAll { get; set; } public List Indexs { get; set; } public bool IsCreateTableFiledSort { get; set; } + public string Discrimator { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs index 75da9a602..7e1e0fbd4 100644 --- a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs +++ b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs @@ -14,6 +14,7 @@ namespace SqlSugar public bool IsDisabledDelete { get; set; } public bool IsDisabledUpdateAll { get; set; } public bool IsCreateTableFiledSort { get; set; } + public string Discrimator { get; set; } public SugarTable(string tableName) { this.TableName = tableName; } diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 9f2c88f44..7f3ee553b 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -151,6 +151,7 @@ +