Add new index attribute

This commit is contained in:
sunkaixuan
2022-05-04 11:33:52 +08:00
parent 0515757176
commit db8682a588
4 changed files with 84 additions and 43 deletions

View File

@@ -132,12 +132,12 @@ namespace SqlSugar
{
item.Length = DefultLength;
}
if (item.DataType!=null&&item.DataType.Contains(",")&& !Regex.IsMatch(item.DataType,@"\d\,\d"))
if (item.DataType != null && item.DataType.Contains(",") && !Regex.IsMatch(item.DataType, @"\d\,\d"))
{
var types = item.DataType.Split(',').Select(it => it.ToLower()).ToList();
var mapingTypes=this.Context.Ado.DbBind.MappingTypes.Select(it=>it.Key.ToLower()).ToList();
var mappingType=types.FirstOrDefault(it => mapingTypes.Contains(it));
if (mappingType != null)
var mapingTypes = this.Context.Ado.DbBind.MappingTypes.Select(it => it.Key.ToLower()).ToList();
var mappingType = types.FirstOrDefault(it => mapingTypes.Contains(it));
if (mappingType != null)
{
item.DataType = mappingType;
}
@@ -145,11 +145,11 @@ namespace SqlSugar
}
}
var tableName = GetTableName(entityInfo);
this.Context.MappingTables.Add(entityInfo.EntityName,tableName);
this.Context.MappingTables.Add(entityInfo.EntityName, tableName);
entityInfo.DbTableName = tableName;
entityInfo.Columns.ForEach(it => { it.DbTableName = tableName; });
var isAny = this.Context.DbMaintenance.IsAnyTable(tableName,false);
if (isAny&&entityInfo.IsDisabledUpdateAll)
var isAny = this.Context.DbMaintenance.IsAnyTable(tableName, false);
if (isAny && entityInfo.IsDisabledUpdateAll)
{
return;
}
@@ -160,8 +160,35 @@ namespace SqlSugar
this.Context.DbMaintenance.AddRemark(entityInfo);
this.Context.DbMaintenance.AddIndex(entityInfo);
CreateIndex(entityInfo);
this.Context.DbMaintenance.AddDefaultValue(entityInfo);
}
private void CreateIndex(EntityInfo entityInfo)
{
if (entityInfo.Indexs.HasValue())
{
foreach (var item in entityInfo.Indexs)
{
if (!this.Context.DbMaintenance.IsAnyIndex(item.IndexName))
{
var fileds = item.IndexFields
.Select(it =>
{
var dbColumn = entityInfo.Columns.FirstOrDefault(z => z.PropertyName == it.Key);
if (dbColumn == null)
{
Check.ExceptionEasy($"{entityInfo.EntityName} no SugarIndex[ {it.Key} ] found", $"类{entityInfo.EntityName} 索引特性没找到列 {it.Key}");
}
return new KeyValuePair<string, OrderByType>(dbColumn.DbColumnName, it.Value);
})
.Select(it => it.Key + " " + it.Value).ToArray();
this.Context.DbMaintenance.CreateIndex(entityInfo.DbTableName, fileds, item.IndexName, item.IsUnique);
}
}
}
}
public virtual void NoExistLogic(EntityInfo entityInfo)
{
var tableName = GetTableName(entityInfo);

View File

@@ -37,6 +37,11 @@ namespace SqlSugar
result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll;
result.IsDisabledDelete = sugarTable.IsDisabledDelete;
}
var indexs = type.GetCustomAttributes(typeof(SugarIndexAttribute));
if (indexs != null && indexs.Any())
{
result.Indexs = indexs.Select(it => it as SugarIndexAttribute).ToList();
}
if (result.TableDescription.IsNullOrEmpty()) result.TableDescription = GetTableAnnotation(type);
if (this.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService != null)
{

View File

@@ -17,5 +17,6 @@ namespace SqlSugar
public List<EntityColumnInfo> Columns { get; set; }
public bool IsDisabledDelete { get; set; }
public bool IsDisabledUpdateAll { get; set; }
public List<SugarIndexAttribute> Indexs { get; set; }
}
}

View File

@@ -227,74 +227,82 @@ namespace SqlSugar
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class SugarIndexAttribute : Attribute
{
public string IndexName { get; set; }
public Dictionary<string, OrderByType> IndexFields { get; set; }
public SugarIndexAttribute(string indexName,string fieldName,OrderByType type)
public bool IsUnique { get; set; }
public SugarIndexAttribute(string indexName,string fieldName,OrderByType sortType,bool isUnique=false)
{
this.IndexName = indexName;
IndexFields = new Dictionary<string, OrderByType>();
IndexFields.Add(fieldName,type);
IndexFields.Add(fieldName, sortType);
this.IsUnique = isUnique;
}
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType type1, string fieldName2, OrderByType type2)
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType sortType1, string fieldName2, OrderByType sortType2, bool isUnique = false)
{
this.IndexName = indexName;
IndexFields = new Dictionary<string, OrderByType>();
IndexFields.Add(fieldName1, type1);
IndexFields.Add(fieldName2, type2);
IndexFields.Add(fieldName1, sortType1);
IndexFields.Add(fieldName2, sortType2);
this.IsUnique = isUnique;
}
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType type1, string fieldName2, OrderByType type2, string fieldName3, OrderByType type3)
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType sortType1, string fieldName2, OrderByType sortType2, string fieldName3, OrderByType sortType3, bool isUnique = false)
{
this.IndexName = indexName;
IndexFields = new Dictionary<string, OrderByType>();
IndexFields.Add(fieldName1, type1);
IndexFields.Add(fieldName2, type2);
IndexFields.Add(fieldName3, type3);
IndexFields.Add(fieldName1, sortType1);
IndexFields.Add(fieldName2, sortType2);
IndexFields.Add(fieldName3, sortType3);
this.IsUnique = isUnique;
}
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType type1, string fieldName2, OrderByType type2, string fieldName3, OrderByType type3, string fieldName4, OrderByType type4)
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType sortType1, string fieldName2, OrderByType sortType2, string fieldName3, OrderByType sortType3, string fieldName4, OrderByType sortType4, bool isUnique = false)
{
this.IndexName = indexName;
IndexFields = new Dictionary<string, OrderByType>();
IndexFields.Add(fieldName1, type1);
IndexFields.Add(fieldName2, type2);
IndexFields.Add(fieldName3, type3);
IndexFields.Add(fieldName4, type4);
IndexFields.Add(fieldName1, sortType1);
IndexFields.Add(fieldName2, sortType2);
IndexFields.Add(fieldName3, sortType3);
IndexFields.Add(fieldName4, sortType4);
this.IsUnique = isUnique;
}
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType type1, string fieldName2, OrderByType type2, string fieldName3, OrderByType type3, string fieldName4, OrderByType type4,string fieldName5, OrderByType type5)
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType sortType1, string fieldName2, OrderByType sortType2, string fieldName3, OrderByType sortType3, string fieldName4, OrderByType sortType4,string fieldName5, OrderByType sortType5, bool isUnique = false)
{
this.IndexName = indexName;
IndexFields = new Dictionary<string, OrderByType>();
IndexFields.Add(fieldName1, type1);
IndexFields.Add(fieldName2, type2);
IndexFields.Add(fieldName3, type3);
IndexFields.Add(fieldName4, type4);
IndexFields.Add(fieldName5, type5);
IndexFields.Add(fieldName1, sortType1);
IndexFields.Add(fieldName2, sortType2);
IndexFields.Add(fieldName3, sortType3);
IndexFields.Add(fieldName4, sortType4);
IndexFields.Add(fieldName5, sortType5);
this.IsUnique = isUnique;
}
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType type1, string fieldName2, OrderByType type2, string fieldName3, OrderByType type3, string fieldName4, OrderByType type4, string fieldName5, OrderByType type5, string fieldName6, OrderByType type6)
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType sortType1, string fieldName2, OrderByType sortType2, string fieldName3, OrderByType sortType3, string fieldName4, OrderByType sortType4, string fieldName5, OrderByType sortType5, string fieldName6, OrderByType sortType6, bool isUnique = false)
{
this.IndexName = indexName;
IndexFields = new Dictionary<string, OrderByType>();
IndexFields.Add(fieldName1, type1);
IndexFields.Add(fieldName2, type2);
IndexFields.Add(fieldName3, type3);
IndexFields.Add(fieldName4, type4);
IndexFields.Add(fieldName5, type5);
IndexFields.Add(fieldName6, type6);
IndexFields.Add(fieldName1, sortType1);
IndexFields.Add(fieldName2, sortType2);
IndexFields.Add(fieldName3, sortType3);
IndexFields.Add(fieldName4, sortType4);
IndexFields.Add(fieldName5, sortType5);
IndexFields.Add(fieldName6, sortType6);
this.IsUnique = isUnique;
}
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType type1, string fieldName2, OrderByType type2, string fieldName3, OrderByType type3, string fieldName4, OrderByType type4, string fieldName5, OrderByType type5, string fieldName6, OrderByType type6, string fieldName7, OrderByType type7)
public SugarIndexAttribute(string indexName, string fieldName1, OrderByType sortType1, string fieldName2, OrderByType sortType2, string fieldName3, OrderByType sortType3, string fieldName4, OrderByType sortType4, string fieldName5, OrderByType sortType5, string fieldName6, OrderByType sortType6, string fieldName7, OrderByType sortType7, bool isUnique = false)
{
this.IndexName = indexName;
IndexFields = new Dictionary<string, OrderByType>();
IndexFields.Add(fieldName1, type1);
IndexFields.Add(fieldName2, type2);
IndexFields.Add(fieldName3, type3);
IndexFields.Add(fieldName4, type4);
IndexFields.Add(fieldName5, type5);
IndexFields.Add(fieldName6, type6);
IndexFields.Add(fieldName7, type7);
IndexFields.Add(fieldName1, sortType1);
IndexFields.Add(fieldName2, sortType2);
IndexFields.Add(fieldName3, sortType3);
IndexFields.Add(fieldName4, sortType4);
IndexFields.Add(fieldName5, sortType5);
IndexFields.Add(fieldName6, sortType6);
IndexFields.Add(fieldName7, sortType7);
this.IsUnique = isUnique;
}
}