Update Core

This commit is contained in:
sunkaixuan 2022-05-04 11:43:30 +08:00
parent d14f0aed2f
commit 170a4c1848
6 changed files with 153 additions and 8 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

@ -861,6 +861,10 @@ namespace SqlSugar
{
return GetDiffTableByEntity();
}
else if (GetIdentityKeys().IsNullOrEmpty())
{
return GetDiffTableByEntity();
}
else
{
return GetDiffTableBySql(identity);

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

@ -224,5 +224,86 @@ namespace SqlSugar
this.NavigatType = NavigateType.ManyToMany;
}
}
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class SugarIndexAttribute : Attribute
{
public string IndexName { get; set; }
public Dictionary<string, OrderByType> IndexFields { get; set; }
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, sortType);
this.IsUnique = isUnique;
}
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, sortType1);
IndexFields.Add(fieldName2, sortType2);
this.IsUnique = isUnique;
}
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, sortType1);
IndexFields.Add(fieldName2, sortType2);
IndexFields.Add(fieldName3, sortType3);
this.IsUnique = isUnique;
}
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, sortType1);
IndexFields.Add(fieldName2, sortType2);
IndexFields.Add(fieldName3, sortType3);
IndexFields.Add(fieldName4, sortType4);
this.IsUnique = isUnique;
}
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, 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 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, 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 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, 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;
}
}
}

View File

@ -87,6 +87,7 @@ namespace SqlSugar
}
public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
{
sql = ReplaceKeyWordParameterName(sql, parameters);
OracleCommand sqlCommand = new OracleCommand(sql, (OracleConnection)this.Connection);
sqlCommand.BindByName = true;
sqlCommand.CommandType = this.CommandType;
@ -104,6 +105,32 @@ namespace SqlSugar
CheckConnection();
return sqlCommand;
}
private static string ReplaceKeyWordParameterName(string sql, SugarParameter[] parameters)
{
if (parameters.HasValue())
{
foreach (var Parameter in parameters)
{
if (Parameter.ParameterName != null && Parameter.ParameterName.ToLower().IsIn("@user", "@level", ":user", ":level"))
{
if (parameters.Count(it => it.ParameterName.StartsWith(Parameter.ParameterName)) == 1)
{
var newName = Parameter.ParameterName + "_01";
sql = sql.Replace(Parameter.ParameterName, newName);
Parameter.ParameterName = newName;
}
else
{
Check.ExceptionEasy($" {Parameter.ParameterName} is key word", $"{Parameter.ParameterName}Êǹؼü´Ê");
}
}
}
}
return sql;
}
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
{
((MyOracleDataAdapter)dataAdapter).SelectCommand = (OracleCommand)command;