diff --git a/Src/Asp.Net/SqlServerTest/Config.cs b/Src/Asp.Net/SqlServerTest/Config.cs index 7d87d0ee6..43f3df924 100644 --- a/Src/Asp.Net/SqlServerTest/Config.cs +++ b/Src/Asp.Net/SqlServerTest/Config.cs @@ -8,8 +8,8 @@ namespace OrmTest { public class Config { - public static string ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest"; - public static string ConnectionString2 = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST"; - public static string ConnectionString3 = "server=.;uid=sa;pwd=sasa;database=sqlsugar4xtest"; + public static string ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest"; + public static string ConnectionString2 = "server=.;uid=sa;pwd=@jhl85661501;database=SQLSUGAR4XTEST"; + public static string ConnectionString3 = "server=.;uid=sa;pwd=@jhl85661501;database=sqlsugar4xtest"; } } diff --git a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs index 1eab9ce24..a2bc06e95 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs @@ -194,6 +194,27 @@ namespace OrmTest.Demo db.Ado.RollbackTran(); throw; } + + + + //async tran + var asyncResult = db.Ado.UseTranAsync(() => + { + + var beginCount = db.Queryable().ToList(); + db.Ado.ExecuteCommand("delete student"); + var endCount = db.Queryable().Count(); + throw new Exception("error haha"); + }); + asyncResult.Wait(); + var asyncCount = db.Queryable().Count(); + + //async + var asyncResult2 = db.Ado.UseTranAsync>(() => + { + return db.Queryable().ToList(); + }); + asyncResult2.Wait(); } private static void Group() { @@ -244,6 +265,8 @@ namespace OrmTest.Demo var db = GetInstance(); var dbTime = db.GetDate(); var getAll = db.Queryable().Select("*").ToList(); + var getAll2 = db.Queryable().ToList(); + var getRandomList = db.Queryable().OrderBy(it => SqlFunc.GetRandom()).ToList(); var getAllOrder = db.Queryable().OrderBy(it => it.Id).OrderBy(it => it.Name, OrderByType.Desc).ToList(); var getId = db.Queryable().Select(it => it.Id).ToList(); var getNew = db.Queryable().Where(it => it.Id == 1).Select(it => new { id = SqlFunc.IIF(it.Id == 0, 1, it.Id), it.Name, it.SchoolId }).ToList(); diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index f89c140d3..a9817f6ec 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -194,6 +194,17 @@ namespace SqlSugar } return result; } + + public Task> UseTranAsync(Action action) + { + Task> result = new Task>(() => + { + return UseTran(action); + }); + TaskStart(result); + return result; + } + public DbResult UseTran(Func action) { var result = new DbResult(); @@ -214,6 +225,17 @@ namespace SqlSugar } return result; } + + public Task> UseTranAsync(Func action) + { + Task> result = new Task>(() => + { + return UseTran(action); + }); + TaskStart(result); + return result; + } + public void UseStoredProcedure(Action action) { var oldCommandType = this.CommandType; @@ -624,6 +646,14 @@ namespace SqlSugar #endregion #region Helper + private void TaskStart(Task 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) { var result = this.ProcessingEventStartingSQL(sql, parameters); diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index 5ca27c5a0..06b966cef 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -62,6 +62,10 @@ namespace SqlSugar IsAs = true; OldMappingTableList = 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); return this; ; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index bc11b2d30..e8594d9b5 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -145,6 +145,10 @@ namespace SqlSugar IsAs = true; OldMappingTableList = 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); return this; ; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 450f65c02..955663236 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -628,7 +628,7 @@ namespace SqlSugar } public ISugarQueryable WithCacheIF(bool isCache, int cacheDurationInSeconds = int.MaxValue) { - if (IsCache) + if (isCache) { this.IsCache = true; this.CacheTime = cacheDurationInSeconds; @@ -1019,6 +1019,10 @@ namespace SqlSugar IsAs = true; OldMappingTableList = 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.QueryableMappingTableList = this.Context.MappingTables; return this; @@ -1212,6 +1216,7 @@ namespace SqlSugar asyncQueryableBuilder.TableWithString = this.QueryBuilder.TableWithString; asyncQueryableBuilder.GroupByValue = this.QueryBuilder.GroupByValue; asyncQueryableBuilder.OrderByValue = this.QueryBuilder.OrderByValue; + asyncQueryableBuilder.IsDisabledGobalFilter = this.QueryBuilder.IsDisabledGobalFilter; return asyncQueryable; } #endregion diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index 5aa45ef1a..2e14edc01 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -64,6 +64,9 @@ namespace SqlSugar IsAs = true; OldMappingTableList = 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); return this; ; } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index 6b0624d93..b91e925a4 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -325,6 +325,11 @@ namespace SqlSugar return "GETDATE()"; } + public virtual string GetRandom() + { + return "NEWID()"; + } + public virtual string CaseWhen(List> sqls) { StringBuilder reslut = new StringBuilder(); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index 0e800a6ef..5e6c1b752 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs @@ -59,5 +59,6 @@ namespace SqlSugar string Pack(string sql); string Null(); string GetDate(); + string GetRandom(); } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index 265e3f157..e33cf1b69 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs @@ -110,6 +110,7 @@ namespace SqlSugar /// public static TResult GetSelfAndAutoFill(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 string GetRandom() { throw new NotSupportedException("Can only be used in expressions"); } /// /// Subquery /// diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index a56e5695a..91f9eda82 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -504,6 +504,8 @@ namespace SqlSugar return this.Context.DbMehtods.GetSelfAndAutoFill(model.Args[0].MemberValue.ObjToString(), this.Context.IsSingle); case "GetDate": return this.Context.DbMehtods.GetDate(); + case "GetRandom": + return this.Context.DbMehtods.GetRandom(); default: break; } diff --git a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs index cc34f9e74..b1efa74a6 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs @@ -86,6 +86,9 @@ namespace SqlSugar DbResult UseTran(Action action); DbResult UseTran(Func action); + Task> UseTranAsync(Action action); + Task> UseTranAsync(Func action); + void UseStoredProcedure(Action action); T UseStoredProcedure(Func action); IAdo UseStoredProcedure(); diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs index 879bba742..7b79c616a 100644 --- a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlExpressionContext.cs @@ -133,5 +133,10 @@ namespace SqlSugar { return "NOW()"; } + + public override string GetRandom() + { + return "rand()"; + } } } diff --git a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs index e39e4621e..51013c28a 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Oracle/SqlBuilder/OracleExpressionContext.cs @@ -180,5 +180,10 @@ namespace SqlSugar { return "sysdate"; } + + public override string GetRandom() + { + return "dbms_random.value"; + } } } diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs index f8844eba8..cd62aa0e9 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/SqlBuilder/SqliteExpressionContext.cs @@ -208,5 +208,10 @@ namespace SqlSugar { return "DATETIME('now')"; } + + public override string GetRandom() + { + return "RANDOM()"; + } } }