From 90357bb9e99836726092a6f5741150d0cfc2b7cb Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 30 Mar 2023 18:39:32 +0800 Subject: [PATCH] Add overload --- .../InsertableProvider/InsertableProvider.cs | 62 +++++++++++++------ Src/Asp.Net/SqlSugar/Interface/Insertable.cs | 6 ++ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index 969d2a80b..9e804d703 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace SqlSugar @@ -36,7 +37,7 @@ namespace SqlSugar #region Core public void AddQueue() { - if (this.InsertObjs!=null&&this.InsertObjs.Length > 0&& this.InsertObjs[0]!=null) + if (this.InsertObjs != null && this.InsertObjs.Length > 0 && this.InsertObjs[0] != null) { var sqlObj = this.ToSql(); this.Context.Queues.Add(sqlObj.Key, sqlObj.Value); @@ -71,17 +72,17 @@ namespace SqlSugar RestoreMapping(); return new KeyValuePair>(sql, InsertBuilder.Parameters); } - public async Task> ExecuteReturnPkListAsync() + public async Task> ExecuteReturnPkListAsync() { return await Task.Run(() => ExecuteReturnPkList()); } - public virtual List ExecuteReturnPkList() + public virtual List ExecuteReturnPkList() { - var pkInfo= this.EntityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey == true); - Check.ExceptionEasy(pkInfo==null,"ExecuteReturnPkList need primary key", "ExecuteReturnPkList需要主键"); - Check.ExceptionEasy(this.EntityInfo.Columns.Count(it => it.IsPrimarykey == true)>1, "ExecuteReturnPkList ,Only support technology single primary key", "ExecuteReturnPkList只支技单主键"); - var isIdEntity = pkInfo.IsIdentity|| (pkInfo.OracleSequenceName.HasValue()&&this.Context.CurrentConnectionConfig.DbType==DbType.Oracle); - if (isIdEntity&&this.InsertObjs.Length==1) + var pkInfo = this.EntityInfo.Columns.FirstOrDefault(it => it.IsPrimarykey == true); + Check.ExceptionEasy(pkInfo == null, "ExecuteReturnPkList need primary key", "ExecuteReturnPkList需要主键"); + Check.ExceptionEasy(this.EntityInfo.Columns.Count(it => it.IsPrimarykey == true) > 1, "ExecuteReturnPkList ,Only support technology single primary key", "ExecuteReturnPkList只支技单主键"); + var isIdEntity = pkInfo.IsIdentity || (pkInfo.OracleSequenceName.HasValue() && this.Context.CurrentConnectionConfig.DbType == DbType.Oracle); + if (isIdEntity && this.InsertObjs.Length == 1) { return InsertPkListIdentityCount1(pkInfo); } @@ -114,7 +115,7 @@ namespace SqlSugar if (InsertBuilder.IsOleDb) { var isAuto = false; - if (this.Context.Ado.IsAnyTran() == false && this.Context.CurrentConnectionConfig.IsAutoCloseConnection) + if (this.Context.Ado.IsAnyTran() == false && this.Context.CurrentConnectionConfig.IsAutoCloseConnection) { isAuto = this.Context.CurrentConnectionConfig.IsAutoCloseConnection; this.Context.CurrentConnectionConfig.IsAutoCloseConnection = false; @@ -146,7 +147,7 @@ namespace SqlSugar if (InsertBuilder.IsOleDb) { var isAuto = false; - if (this.Context.Ado.IsAnyTran()==false&&this.Context.CurrentConnectionConfig.IsAutoCloseConnection) + if (this.Context.Ado.IsAnyTran() == false && this.Context.CurrentConnectionConfig.IsAutoCloseConnection) { isAuto = this.Context.CurrentConnectionConfig.IsAutoCloseConnection; this.Context.CurrentConnectionConfig.IsAutoCloseConnection = false; @@ -161,25 +162,25 @@ namespace SqlSugar } else { - result= Convert.ToInt64(Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray())); + result = Convert.ToInt64(Ado.GetScalar(sql, InsertBuilder.Parameters == null ? null : InsertBuilder.Parameters.ToArray())); } After(sql, result); return result; } - public virtual long ExecuteReturnSnowflakeId() + public virtual long ExecuteReturnSnowflakeId() { - if (this.InsertObjs.Length > 1) + if (this.InsertObjs.Length > 1) { return this.ExecuteReturnSnowflakeIdList().First(); } var id = SnowFlakeSingle.instance.getID(); var entity = this.Context.EntityMaintenance.GetEntityInfo(); - var snowProperty=entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); - Check.Exception(snowProperty==null, "The entity sets the primary key and is long"); + var snowProperty = entity.Columns.FirstOrDefault(it => it.IsPrimarykey && it.PropertyInfo.PropertyType == UtilConstants.LongType); + Check.Exception(snowProperty == null, "The entity sets the primary key and is long"); Check.Exception(snowProperty.IsIdentity == true, "SnowflakeId IsIdentity can't true"); - foreach (var item in this.InsertBuilder.DbColumnInfoList.Where(it=>it.PropertyName==snowProperty.PropertyName)) + foreach (var item in this.InsertBuilder.DbColumnInfoList.Where(it => it.PropertyName == snowProperty.PropertyName)) { item.Value = id; snowProperty?.PropertyInfo.SetValue(this.InsertObjs.First(), id); @@ -187,7 +188,7 @@ namespace SqlSugar this.ExecuteCommand(); return id; } - public List ExecuteReturnSnowflakeIdList() + public List ExecuteReturnSnowflakeIdList() { List result = new List(); var entity = this.Context.EntityMaintenance.GetEntityInfo(); @@ -208,6 +209,11 @@ namespace SqlSugar this.ExecuteCommand(); return result; } + public Task ExecuteReturnSnowflakeIdAsync(CancellationToken token) + { + this.Context.Ado.CancellationToken= token; + return ExecuteReturnSnowflakeIdAsync(); + } public async Task ExecuteReturnSnowflakeIdAsync() { var id = SnowFlakeSingle.instance.getID(); @@ -245,6 +251,12 @@ namespace SqlSugar return result; } + public Task> ExecuteReturnSnowflakeIdListAsync(CancellationToken token) + { + this.Ado.CancellationToken= token; + return ExecuteReturnSnowflakeIdListAsync(); + } + public virtual T ExecuteReturnEntity() { ExecuteCommandIdentityIntoEntity(); @@ -302,7 +314,11 @@ namespace SqlSugar this.Context.EntityMaintenance.GetProperty(identityKey).SetValue(result, setValue, null); return idValue > 0; } - + public Task ExecuteCommandAsync(CancellationToken token) + { + this.Context.Ado.CancellationToken= token; + return ExecuteCommandAsync(); + } public async Task ExecuteCommandAsync() { if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null) @@ -315,6 +331,11 @@ namespace SqlSugar if (result == -1) return this.InsertObjs.Count(); return result; } + public Task ExecuteReturnIdentityAsync(CancellationToken token) + { + this.Ado.CancellationToken= token; + return ExecuteReturnIdentityAsync(); + } public virtual async Task ExecuteReturnIdentityAsync() { if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null) @@ -404,6 +425,11 @@ namespace SqlSugar this.Context.EntityMaintenance.GetProperty(identityKey).SetValue(result, setValue, null); return idValue > 0; } + public Task ExecuteReturnBigIdentityAsync(CancellationToken token) + { + this.Context.Ado.CancellationToken= token; + return ExecuteReturnBigIdentityAsync(); + } public virtual async Task ExecuteReturnBigIdentityAsync() { if (this.InsertObjs.Count() == 1 && this.InsertObjs.First() == null) diff --git a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs index ef1dd0b33..10d34169f 100644 --- a/Src/Asp.Net/SqlSugar/Interface/Insertable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/Insertable.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace SqlSugar @@ -12,20 +13,25 @@ namespace SqlSugar InsertBuilder InsertBuilder { get; set; } int ExecuteCommand(); Task ExecuteCommandAsync(); + Task ExecuteCommandAsync(CancellationToken token); List ExecuteReturnPkList(); Task> ExecuteReturnPkListAsync(); long ExecuteReturnSnowflakeId(); List ExecuteReturnSnowflakeIdList(); Task ExecuteReturnSnowflakeIdAsync(); + Task ExecuteReturnSnowflakeIdAsync(CancellationToken token); Task> ExecuteReturnSnowflakeIdListAsync(); + Task> ExecuteReturnSnowflakeIdListAsync(CancellationToken token); int ExecuteReturnIdentity(); Task ExecuteReturnIdentityAsync(); + Task ExecuteReturnIdentityAsync(CancellationToken token); T ExecuteReturnEntity(); Task ExecuteReturnEntityAsync(); bool ExecuteCommandIdentityIntoEntity(); Task ExecuteCommandIdentityIntoEntityAsync(); long ExecuteReturnBigIdentity(); Task ExecuteReturnBigIdentityAsync(); + Task ExecuteReturnBigIdentityAsync(CancellationToken token); IInsertable AS(string tableName); IInsertable AsType(Type tableNameType); IInsertable With(string lockString);