Browse Source

optimization

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
6fc9181ecf
  1. 2
      Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs
  2. 60
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

2
Src/Asp.Net/SqlServerTest/UnitTest/DataTest.cs

@ -67,6 +67,8 @@ namespace OrmTest.UnitTest
{ {
throw new Exception("DataTest Error"); throw new Exception("DataTest Error");
} }
db.Insertable(new DataTestInfo2() { }).ExecuteCommand();
} }
} }
} }

60
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -35,14 +35,24 @@ namespace SqlSugar
#region Core #region Core
public virtual int ExecuteCommand() public virtual int ExecuteCommand()
{ {
if (InsertBuilder.DbColumnInfoList.HasValue())
{
var pks = GetPrimaryKeys();
foreach (var item in InsertBuilder.DbColumnInfoList)
{
if (item.IsPrimarykey==true||(pks.Any(y=>y.Equals(item.DbColumnName,StringComparison.CurrentCultureIgnoreCase)))&&item.PropertyType == UtilConstants.GuidType&&item.Value.ObjToString()==Guid.Empty.ToString()) {
item.Value = Guid.NewGuid();
}
}
}
InsertBuilder.IsReturnIdentity = false; InsertBuilder.IsReturnIdentity = false;
PreToSql(); PreToSql();
AutoRemoveDataCache(); AutoRemoveDataCache();
string sql = InsertBuilder.ToSqlString(); string sql = InsertBuilder.ToSqlString();
RestoreMapping(); RestoreMapping();
Before(sql); Before(sql);
var result= Ado.ExecuteCommand(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()); var result = Ado.ExecuteCommand(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
After(sql,null); After(sql, null);
return result; return result;
} }
@ -63,8 +73,8 @@ namespace SqlSugar
string sql = InsertBuilder.ToSqlString(); string sql = InsertBuilder.ToSqlString();
RestoreMapping(); RestoreMapping();
Before(sql); Before(sql);
var result= Ado.GetInt(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()); var result = Ado.GetInt(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray());
After(sql,result); After(sql, result);
return result; return result;
} }
public virtual long ExecuteReturnBigIdentity() public virtual long ExecuteReturnBigIdentity()
@ -75,8 +85,8 @@ namespace SqlSugar
string sql = InsertBuilder.ToSqlString(); string sql = InsertBuilder.ToSqlString();
RestoreMapping(); RestoreMapping();
Before(sql); Before(sql);
var result= Convert.ToInt64( Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray())); var result = Convert.ToInt64(Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray()));
After(sql,result); After(sql, result);
return result; return result;
} }
public virtual T ExecuteReturnEntity() public virtual T ExecuteReturnEntity()
@ -92,13 +102,13 @@ namespace SqlSugar
var idValue = ExecuteReturnBigIdentity(); var idValue = ExecuteReturnBigIdentity();
Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys"); Check.Exception(identityKeys.Count > 1, "ExecuteCommandIdentityIntoEntity does not support multiple identity keys");
var identityKey = identityKeys.First(); var identityKey = identityKeys.First();
object setValue= 0; object setValue = 0;
if (idValue > int.MaxValue) if (idValue > int.MaxValue)
setValue = idValue; setValue = idValue;
else else
setValue = Convert.ToInt32(idValue); setValue = Convert.ToInt32(idValue);
this.Context.EntityMaintenance.GetProperty<T>(identityKey).SetValue(result,setValue, null); this.Context.EntityMaintenance.GetProperty<T>(identityKey).SetValue(result, setValue, null);
return idValue>0; return idValue > 0;
} }
public Task<int> ExecuteCommandAsync() public Task<int> ExecuteCommandAsync()
{ {
@ -181,7 +191,7 @@ namespace SqlSugar
public IInsertable<T> InsertColumns(Expression<Func<T, object>> columns) public IInsertable<T> InsertColumns(Expression<Func<T, object>> columns)
{ {
var ignoreColumns = InsertBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList(); var ignoreColumns = InsertBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList();
this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => ignoreColumns.Any(ig=>ig.Equals(it.PropertyName,StringComparison.CurrentCultureIgnoreCase))).ToList(); this.InsertBuilder.DbColumnInfoList = this.InsertBuilder.DbColumnInfoList.Where(it => ignoreColumns.Any(ig => ig.Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase))).ToList();
return this; return this;
} }
@ -314,14 +324,14 @@ namespace SqlSugar
} }
private void SetInsertItemByDic(int i, T item, List<DbColumnInfo> insertItem) private void SetInsertItemByDic(int i, T item, List<DbColumnInfo> insertItem)
{ {
foreach (var column in item as Dictionary<string,object>) foreach (var column in item as Dictionary<string, object>)
{ {
var columnInfo = new DbColumnInfo() var columnInfo = new DbColumnInfo()
{ {
Value = column.Value, Value = column.Value,
DbColumnName = column.Key, DbColumnName = column.Key,
PropertyName = column.Key, PropertyName = column.Key,
PropertyType = column.Value == null? DBNull.Value.GetType(): UtilMethods.GetUnderType(column.Value.GetType()), PropertyType = column.Value == null ? DBNull.Value.GetType() : UtilMethods.GetUnderType(column.Value.GetType()),
TableId = i TableId = i
}; };
if (columnInfo.PropertyType.IsEnum()) if (columnInfo.PropertyType.IsEnum())
@ -428,19 +438,20 @@ namespace SqlSugar
return asyncInsertable; return asyncInsertable;
} }
private void After(string sql,long? result) private void After(string sql, long? result)
{ {
if (this.IsEnableDiffLogEvent) if (this.IsEnableDiffLogEvent)
{ {
var parameters = InsertBuilder.Parameters; var parameters = InsertBuilder.Parameters;
if (parameters == null) if (parameters == null)
parameters = new List<SugarParameter>(); parameters = new List<SugarParameter>();
diffModel.AfterData = GetDiffTable(sql,result); diffModel.AfterData = GetDiffTable(sql, result);
diffModel.Time = this.Context.Ado.SqlExecutionTime; diffModel.Time = this.Context.Ado.SqlExecutionTime;
if (this.Context.Ado.DiffLogEvent != null) if (this.Context.Ado.DiffLogEvent != null)
this.Context.Ado.DiffLogEvent(diffModel); this.Context.Ado.DiffLogEvent(diffModel);
} }
if (this.RemoveCacheFunc != null) { if (this.RemoveCacheFunc != null)
{
this.RemoveCacheFunc(); this.RemoveCacheFunc();
} }
} }
@ -456,22 +467,24 @@ namespace SqlSugar
diffModel.Parameters = parameters.ToArray(); diffModel.Parameters = parameters.ToArray();
} }
} }
private List<DiffLogTableInfo> GetDiffTable(string sql,long? identity) private List<DiffLogTableInfo> GetDiffTable(string sql, long? identity)
{ {
List<SugarParameter> parameters = new List<SugarParameter>(); List<SugarParameter> parameters = new List<SugarParameter>();
List<DiffLogTableInfo> result = new List<DiffLogTableInfo>(); List<DiffLogTableInfo> result = new List<DiffLogTableInfo>();
var whereSql = string.Empty; var whereSql = string.Empty;
List<IConditionalModel> cons = new List<IConditionalModel>(); List<IConditionalModel> cons = new List<IConditionalModel>();
if (identity != null&&identity>0&& GetIdentityKeys().HasValue()) if (identity != null && identity > 0 && GetIdentityKeys().HasValue())
{ {
var fieldName = GetIdentityKeys().Last(); var fieldName = GetIdentityKeys().Last();
cons.Add(new ConditionalModel() { ConditionalType=ConditionalType.Equal, FieldName= fieldName, FieldValue=identity.ToString() }); cons.Add(new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = fieldName, FieldValue = identity.ToString() });
} }
else { else
foreach(var item in this.EntityInfo.Columns.Where(it => it.IsIgnore == false&&GetPrimaryKeys().Any(pk=>pk.Equals(it.DbColumnName,StringComparison.CurrentCultureIgnoreCase)))) { {
foreach (var item in this.EntityInfo.Columns.Where(it => it.IsIgnore == false && GetPrimaryKeys().Any(pk => pk.Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase))))
{
var fielddName = item.DbColumnName; var fielddName = item.DbColumnName;
var fieldValue = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName == item.PropertyName).PropertyInfo.GetValue(this.InsertObjs.Last(), null).ObjToString(); var fieldValue = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName == item.PropertyName).PropertyInfo.GetValue(this.InsertObjs.Last(), null).ObjToString();
cons.Add(new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = fielddName, FieldValue =fieldValue }); cons.Add(new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = fielddName, FieldValue = fieldValue });
} }
} }
Check.Exception(cons.IsNullOrEmpty(), "Insertable.EnableDiffLogEvent need primary key"); Check.Exception(cons.IsNullOrEmpty(), "Insertable.EnableDiffLogEvent need primary key");
@ -499,7 +512,8 @@ namespace SqlSugar
} }
return result; return result;
} }
else { else
{
DiffLogTableInfo diffTable = new DiffLogTableInfo(); DiffLogTableInfo diffTable = new DiffLogTableInfo();
diffTable.TableName = this.EntityInfo.DbTableName; diffTable.TableName = this.EntityInfo.DbTableName;
diffTable.TableDescription = this.EntityInfo.TableDescription; diffTable.TableDescription = this.EntityInfo.TableDescription;
@ -511,7 +525,7 @@ namespace SqlSugar
}).ToList(); }).ToList();
return new List<DiffLogTableInfo>() { diffTable }; return new List<DiffLogTableInfo>() { diffTable };
} }
} }
#endregion #endregion
} }

Loading…
Cancel
Save