123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SqlSugar
- {
- public class InsertablePage<T> where T:class,new()
- {
- public int PageSize { get; set; }
- public SqlSugarProvider Context { get; set; }
- public T[] DataList { get; set; }
- public string TableName { get; internal set; }
- public List<string> InsertColumns { get; internal set; }
- public bool IsEnableDiffLogEvent { get; internal set; }
- public DiffLogModel DiffModel { get; internal set; }
- public bool IsOffIdentity { get; internal set; }
- public bool IsInsertColumnsNull { get; internal set; }
- public int ExecuteCommand()
- {
- if (DataList.Count() == 1 && DataList.First() == null)
- {
- return 0;
- }
- if (PageSize == 0) { PageSize = 1000; }
- var result = 0;
- var isNoTran = this.Context.Ado.IsNoTran();
- try
- {
- if (isNoTran)
- {
- this.Context.Ado.BeginTran();
- }
- this.Context.Utilities.PageEach(DataList, PageSize, pageItem =>
- {
- result += this.Context.Insertable(pageItem).AS(TableName).IgnoreColumnsNull(this.IsInsertColumnsNull).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteCommand();
- });
- if (isNoTran)
- {
- this.Context.Ado.CommitTran();
- }
- }
- catch (Exception)
- {
- if (isNoTran)
- {
- this.Context.Ado.RollbackTran();
- }
- throw;
- }
- return result;
- }
- public async Task<int> ExecuteCommandAsync()
- {
- if (DataList.Count() == 1 && DataList.First() == null)
- {
- return 0;
- }
- if (PageSize == 0) { PageSize = 1000; }
- var result = 0;
- var isNoTran = this.Context.Ado.IsNoTran();
- try
- {
- if (isNoTran)
- {
- await this.Context.Ado.BeginTranAsync();
- }
- await this.Context.Utilities.PageEachAsync(DataList, PageSize, async pageItem =>
- {
- result +=await this.Context.Insertable(pageItem).AS(TableName).IgnoreColumnsNull(this.IsInsertColumnsNull).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteCommandAsync();
- });
- if (isNoTran)
- {
- await this.Context.Ado.CommitTranAsync();
- }
- }
- catch (Exception)
- {
- if (isNoTran)
- {
- await this.Context.Ado.RollbackTranAsync();
- }
- throw;
- }
- return result;
- }
- public List<long> ExecuteReturnSnowflakeIdList()
- {
- if (DataList.Count() == 1 && DataList.First() == null)
- {
- return new List<long>();
- }
- if (PageSize == 0) { PageSize = 1000; }
- var result = new List<long>();
- var isNoTran = this.Context.Ado.IsNoTran();
- try
- {
- if (isNoTran)
- {
- this.Context.Ado.BeginTran();
- }
- this.Context.Utilities.PageEach(DataList, PageSize, pageItem =>
- {
- result.AddRange(this.Context.Insertable(pageItem).AS(TableName).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteReturnSnowflakeIdList());
- });
- if (isNoTran)
- {
- this.Context.Ado.CommitTran();
- }
- }
- catch (Exception)
- {
- if (isNoTran)
- {
- this.Context.Ado.RollbackTran();
- }
- throw;
- }
- return result;
- }
- public async Task<List<long>> ExecuteReturnSnowflakeIdListAsync()
- {
- if (DataList.Count() == 1 && DataList.First() == null)
- {
- return new List<long>();
- }
- if (PageSize == 0) { PageSize = 1000; }
- var result = new List<long>();
- var isNoTran = this.Context.Ado.IsNoTran();
- try
- {
- if (isNoTran)
- {
- await this.Context.Ado.BeginTranAsync();
- }
- await this.Context.Utilities.PageEachAsync(DataList, PageSize, async pageItem =>
- {
- result.AddRange(await this.Context.Insertable(pageItem).AS(TableName).OffIdentity(IsOffIdentity).EnableDiffLogEventIF(IsEnableDiffLogEvent, DiffModel).InsertColumns(InsertColumns.ToArray()).ExecuteReturnSnowflakeIdListAsync());
- });
- if (isNoTran)
- {
- await this.Context.Ado.CommitTranAsync();
- }
- }
- catch (Exception)
- {
- if (isNoTran)
- {
- await this.Context.Ado.RollbackTranAsync();
- }
- throw;
- }
- return result;
- }
- public InsertablePage<T> IgnoreColumnsNull(bool isIgnoreNull = true)
- {
- this.PageSize = 1;
- this.IsInsertColumnsNull = isIgnoreNull;
- return this;
- }
- }
- }
|