Update Core

This commit is contained in:
sunkaixuan
2018-10-08 08:44:16 +08:00
parent efcd26374e
commit ebcb1d74fa
13 changed files with 90 additions and 20 deletions

View File

@@ -194,6 +194,17 @@ namespace SqlSugar
} }
return result; return result;
} }
public Task<DbResult<bool>> UseTranAsync(Action action)
{
Task<DbResult<bool>> result = new Task<DbResult<bool>>(() =>
{
return UseTran(action);
});
TaskStart(result);
return result;
}
public DbResult<T> UseTran<T>(Func<T> action) public DbResult<T> UseTran<T>(Func<T> action)
{ {
var result = new DbResult<T>(); var result = new DbResult<T>();
@@ -214,6 +225,17 @@ namespace SqlSugar
} }
return result; return result;
} }
public Task<DbResult<T>> UseTranAsync<T>(Func<T> action)
{
Task<DbResult<T>> result = new Task<DbResult<T>>(() =>
{
return UseTran(action);
});
TaskStart(result);
return result;
}
public void UseStoredProcedure(Action action) public void UseStoredProcedure(Action action)
{ {
var oldCommandType = this.CommandType; var oldCommandType = this.CommandType;
@@ -624,6 +646,14 @@ namespace SqlSugar
#endregion #endregion
#region Helper #region Helper
private void TaskStart<Type>(Task<Type> result)
{
if (this.Context.CurrentConnectionConfig.IsShardSameThread)
{
Check.Exception(true, "IsShardSameThread=true can't be used async method");
}
result.Start();
}
private void ExecuteProcessingSQL(ref string sql, SugarParameter[] parameters) private void ExecuteProcessingSQL(ref string sql, SugarParameter[] parameters)
{ {
var result = this.ProcessingEventStartingSQL(sql, parameters); var result = this.ProcessingEventStartingSQL(sql, parameters);

View File

@@ -62,6 +62,10 @@ namespace SqlSugar
IsAs = true; IsAs = true;
OldMappingTableList = this.Context.MappingTables; OldMappingTableList = this.Context.MappingTables;
this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables); this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
if (this.Context.MappingTables.Any(it => it.EntityName == entityName))
{
this.Context.MappingTables.Add(this.Context.MappingTables.First(it => it.EntityName == entityName).DbTableName, tableName);
}
this.Context.MappingTables.Add(entityName, tableName); this.Context.MappingTables.Add(entityName, tableName);
return this; ; return this; ;
} }

View File

@@ -145,6 +145,10 @@ namespace SqlSugar
IsAs = true; IsAs = true;
OldMappingTableList = this.Context.MappingTables; OldMappingTableList = this.Context.MappingTables;
this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables); this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
if (this.Context.MappingTables.Any(it => it.EntityName == entityName))
{
this.Context.MappingTables.Add(this.Context.MappingTables.First(it => it.EntityName == entityName).DbTableName, tableName);
}
this.Context.MappingTables.Add(entityName, tableName); this.Context.MappingTables.Add(entityName, tableName);
return this; ; return this; ;
} }

View File

@@ -628,7 +628,7 @@ namespace SqlSugar
} }
public ISugarQueryable<T> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue) public ISugarQueryable<T> WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue)
{ {
if (IsCache) if (isCache)
{ {
this.IsCache = true; this.IsCache = true;
this.CacheTime = cacheDurationInSeconds; this.CacheTime = cacheDurationInSeconds;
@@ -1019,6 +1019,10 @@ namespace SqlSugar
IsAs = true; IsAs = true;
OldMappingTableList = this.Context.MappingTables; OldMappingTableList = this.Context.MappingTables;
this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables); this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
if (this.Context.MappingTables.Any(it => it.EntityName == entityName))
{
this.Context.MappingTables.Add(this.Context.MappingTables.First(it => it.EntityName == entityName).DbTableName, tableName);
}
this.Context.MappingTables.Add(entityName, tableName); this.Context.MappingTables.Add(entityName, tableName);
this.QueryableMappingTableList = this.Context.MappingTables; this.QueryableMappingTableList = this.Context.MappingTables;
return this; return this;
@@ -1212,6 +1216,8 @@ namespace SqlSugar
asyncQueryableBuilder.TableWithString = this.QueryBuilder.TableWithString; asyncQueryableBuilder.TableWithString = this.QueryBuilder.TableWithString;
asyncQueryableBuilder.GroupByValue = this.QueryBuilder.GroupByValue; asyncQueryableBuilder.GroupByValue = this.QueryBuilder.GroupByValue;
asyncQueryableBuilder.OrderByValue = this.QueryBuilder.OrderByValue; asyncQueryableBuilder.OrderByValue = this.QueryBuilder.OrderByValue;
asyncQueryableBuilder.IsDisabledGobalFilter = this.QueryBuilder.IsDisabledGobalFilter;
asyncQueryableBuilder.PartitionByValue = this.QueryBuilder.PartitionByValue;
return asyncQueryable; return asyncQueryable;
} }
#endregion #endregion

View File

@@ -64,6 +64,9 @@ namespace SqlSugar
IsAs = true; IsAs = true;
OldMappingTableList = this.Context.MappingTables; OldMappingTableList = this.Context.MappingTables;
this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables); this.Context.MappingTables = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
if (this.Context.MappingTables.Any(it => it.EntityName == entityName)) {
this.Context.MappingTables.Add(this.Context.MappingTables.First(it => it.EntityName == entityName).DbTableName, tableName);
}
this.Context.MappingTables.Add(entityName, tableName); this.Context.MappingTables.Add(entityName, tableName);
return this; ; return this; ;
} }
@@ -107,7 +110,7 @@ namespace SqlSugar
{ {
var moreSetts = this.Context.CurrentConnectionConfig.MoreSettings; var moreSetts = this.Context.CurrentConnectionConfig.MoreSettings;
var extService = this.Context.CurrentConnectionConfig.ConfigureExternalServices; var extService = this.Context.CurrentConnectionConfig.ConfigureExternalServices;
if (moreSetts != null && moreSetts.IsAutoRemoveDataCache && extService != null && extService.DataInfoCacheService != null) if (moreSetts != null && moreSetts.IsAutoRemoveDataCache && extService!=null&& extService.DataInfoCacheService!=null)
{ {
this.RemoveDataCache(); this.RemoveDataCache();
} }
@@ -147,13 +150,12 @@ namespace SqlSugar
return this; return this;
} }
public IUpdateable<T> UpdateColumns(Expression<Func<T, bool>> columns) public IUpdateable<T> UpdateColumns(Expression<Func<T, bool>> columns) {
{
var binaryExp = columns.Body as BinaryExpression; var binaryExp = columns.Body as BinaryExpression;
Check.Exception(!binaryExp.NodeType.IsIn(ExpressionType.Equal), "No support {0}", columns.ToString()); Check.Exception(!binaryExp.NodeType.IsIn(ExpressionType.Equal), "No support {0}", columns.ToString());
Check.Exception(!(binaryExp.Left is MemberExpression) && !(binaryExp.Left is UnaryExpression), "No support {0}", columns.ToString()); Check.Exception(!(binaryExp.Left is MemberExpression)&& !(binaryExp.Left is UnaryExpression), "No support {0}", columns.ToString());
Check.Exception(ExpressionTool.IsConstExpression(binaryExp.Left as MemberExpression), "No support {0}", columns.ToString()); Check.Exception(ExpressionTool.IsConstExpression(binaryExp.Left as MemberExpression), "No support {0}", columns.ToString());
var expResult = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.WhereSingle).GetResultString().Replace("))", ") )").Replace("((", "( (").Trim().TrimStart('(').TrimEnd(')'); var expResult = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.WhereSingle).GetResultString().Replace("))",") )").Replace("((", "( (").Trim().TrimStart('(').TrimEnd(')');
string key = SqlBuilder.GetNoTranslationColumnName(expResult); string key = SqlBuilder.GetNoTranslationColumnName(expResult);
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(key), expResult)); UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(key), expResult));
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList(); this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
@@ -187,7 +189,7 @@ namespace SqlSugar
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(key), item)); UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(key), item));
} }
} }
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList(); this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName,StringComparison.CurrentCultureIgnoreCase)|| SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName,StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
return this; return this;
} }
[Obsolete("Use IUpdateable<T> IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false);")] [Obsolete("Use IUpdateable<T> IgnoreColumns(bool ignoreAllNullColumns, bool isOffIdentity = false);")]
@@ -209,22 +211,20 @@ namespace SqlSugar
public IUpdateable<T> Where(string whereSql, object parameters = null) public IUpdateable<T> Where(string whereSql, object parameters = null)
{ {
if (whereSql.HasValue()) if (whereSql.HasValue()) {
{
UpdateBuilder.WhereValues.Add(whereSql); UpdateBuilder.WhereValues.Add(whereSql);
} }
if (parameters != null) if (parameters != null) {
{
UpdateBuilder.Parameters.AddRange(Context.Ado.GetParameters(parameters)); UpdateBuilder.Parameters.AddRange(Context.Ado.GetParameters(parameters));
} }
return this; return this;
} }
public IUpdateable<T> Where(string fieldName, string conditionalType, object fieldValue) public IUpdateable<T> Where(string fieldName,string conditionalType, object fieldValue)
{ {
var whereSql = this.SqlBuilder.GetWhere(fieldName, conditionalType, 0); var whereSql=this.SqlBuilder.GetWhere(fieldName, conditionalType,0);
this.Where(whereSql); this.Where(whereSql);
string parameterName = this.SqlBuilder.SqlParameterKeyWord + fieldName + "0"; string parameterName = this.SqlBuilder.SqlParameterKeyWord + fieldName+ "0";
this.UpdateBuilder.Parameters.Add(new SugarParameter(parameterName, fieldValue)); this.UpdateBuilder.Parameters.Add(new SugarParameter(parameterName, fieldValue));
return this; return this;
} }
@@ -259,7 +259,7 @@ namespace SqlSugar
foreach (var item in UpdateObjs) foreach (var item in UpdateObjs)
{ {
List<DbColumnInfo> updateItem = new List<DbColumnInfo>(); List<DbColumnInfo> updateItem = new List<DbColumnInfo>();
var isDic = item is Dictionary<string, object>; var isDic = item is Dictionary<string,object>;
if (isDic) if (isDic)
{ {
SetUpdateItemByDic(i, item, updateItem); SetUpdateItemByDic(i, item, updateItem);
@@ -273,12 +273,12 @@ namespace SqlSugar
} }
private void SetUpdateItemByDic(int i, T item, List<DbColumnInfo> updateItem) private void SetUpdateItemByDic(int i, T item, List<DbColumnInfo> updateItem)
{ {
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 = UtilMethods.GetUnderType(column.Value.GetType()), PropertyType = UtilMethods.GetUnderType(column.Value.GetType()),
TableId = i TableId = i
@@ -331,8 +331,7 @@ namespace SqlSugar
foreach (var item in this.UpdateBuilder.DbColumnInfoList) foreach (var item in this.UpdateBuilder.DbColumnInfoList)
{ {
if (this.UpdateBuilder.Parameters == null) this.UpdateBuilder.Parameters = new List<SugarParameter>(); if (this.UpdateBuilder.Parameters == null) this.UpdateBuilder.Parameters = new List<SugarParameter>();
if (this.UpdateBuilder.SetValues.Any(it => this.SqlBuilder.GetNoTranslationColumnName(it.Key) == item.PropertyName)) if (this.UpdateBuilder.SetValues.Any(it =>this.SqlBuilder.GetNoTranslationColumnName(it.Key) == item.PropertyName)) {
{
continue; continue;
} }
this.UpdateBuilder.Parameters.Add(new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType)); this.UpdateBuilder.Parameters.Add(new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType));

View File

@@ -325,6 +325,11 @@ namespace SqlSugar
return "GETDATE()"; return "GETDATE()";
} }
public virtual string GetRandom()
{
return "NEWID()";
}
public virtual string CaseWhen(List<KeyValuePair<string, string>> sqls) public virtual string CaseWhen(List<KeyValuePair<string, string>> sqls)
{ {
StringBuilder reslut = new StringBuilder(); StringBuilder reslut = new StringBuilder();

View File

@@ -59,5 +59,6 @@ namespace SqlSugar
string Pack(string sql); string Pack(string sql);
string Null(); string Null();
string GetDate(); string GetDate();
string GetRandom();
} }
} }

View File

@@ -110,6 +110,7 @@ namespace SqlSugar
/// <returns></returns> /// <returns></returns>
public static TResult GetSelfAndAutoFill<TResult>(TResult value) { throw new NotSupportedException("Can only be used in expressions"); } public static TResult GetSelfAndAutoFill<TResult>(TResult value) { throw new NotSupportedException("Can only be used in expressions"); }
public static DateTime GetDate() { throw new NotSupportedException("Can only be used in expressions"); } public static DateTime GetDate() { throw new NotSupportedException("Can only be used in expressions"); }
public static string GetRandom() { throw new NotSupportedException("Can only be used in expressions"); }
/// <summary> /// <summary>
/// Subquery /// Subquery
/// </summary> /// </summary>

View File

@@ -504,6 +504,8 @@ namespace SqlSugar
return this.Context.DbMehtods.GetSelfAndAutoFill(model.Args[0].MemberValue.ObjToString(), this.Context.IsSingle); return this.Context.DbMehtods.GetSelfAndAutoFill(model.Args[0].MemberValue.ObjToString(), this.Context.IsSingle);
case "GetDate": case "GetDate":
return this.Context.DbMehtods.GetDate(); return this.Context.DbMehtods.GetDate();
case "GetRandom":
return this.Context.DbMehtods.GetRandom();
default: default:
break; break;
} }

View File

@@ -86,6 +86,9 @@ namespace SqlSugar
DbResult<bool> UseTran(Action action); DbResult<bool> UseTran(Action action);
DbResult<T> UseTran<T>(Func<T> action); DbResult<T> UseTran<T>(Func<T> action);
Task<DbResult<bool>> UseTranAsync(Action action);
Task<DbResult<T>> UseTranAsync<T>(Func<T> action);
void UseStoredProcedure(Action action); void UseStoredProcedure(Action action);
T UseStoredProcedure<T>(Func<T> action); T UseStoredProcedure<T>(Func<T> action);
IAdo UseStoredProcedure(); IAdo UseStoredProcedure();

View File

@@ -133,5 +133,10 @@ namespace SqlSugar
{ {
return "NOW()"; return "NOW()";
} }
public override string GetRandom()
{
return "rand()";
}
} }
} }

View File

@@ -180,5 +180,10 @@ namespace SqlSugar
{ {
return "sysdate"; return "sysdate";
} }
public override string GetRandom()
{
return "dbms_random.value";
}
} }
} }

View File

@@ -157,7 +157,7 @@ namespace SqlSugar
var parameter = model.Args[0].MemberName; var parameter = model.Args[0].MemberName;
var parameter2 = model.Args[1].MemberName; var parameter2 = model.Args[1].MemberName;
int time = 1; int time = 1;
return string.Format(" Cast((JulianDay({0}) - JulianDay({1})) *{2} As INTEGER)", parameter, parameter2, time); return string.Format(" Cast((JulianDay({0}) - JulianDay({1})) *{2} As INTEGER)=0 ", parameter, parameter2, time);
} }
public override string DateIsSameByType(MethodCallExpressionModel model) public override string DateIsSameByType(MethodCallExpressionModel model)
{ {
@@ -208,5 +208,10 @@ namespace SqlSugar
{ {
return "DATETIME('now')"; return "DATETIME('now')";
} }
public override string GetRandom()
{
return "RANDOM()";
}
} }
} }