Skip to content

Commit

Permalink
Merge pull request #204 from phnx47/refactor/bulk-insert
Browse files Browse the repository at this point in the history
refactor: update BulkInsert interface
  • Loading branch information
phnx47 authored Feb 9, 2022
2 parents 331d585 + fe919c4 commit ce014c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
38 changes: 25 additions & 13 deletions src/MicroOrm.Dapper.Repositories/DapperRepository.BulkInsert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,33 @@ public partial class DapperRepository<TEntity>
where TEntity : class
{
/// <inheritdoc />
public virtual int BulkInsert(IEnumerable<TEntity> instances, IDbTransaction transaction = null)
public virtual int BulkInsert(IEnumerable<TEntity> instances)
{
return BulkInsert(instances, null);
}

/// <inheritdoc />
public virtual int BulkInsert(IEnumerable<TEntity> instances, IDbTransaction transaction)
{
if (SqlGenerator.Provider == SqlProvider.MSSQL)
{
int count = 0;
int totalInstances = instances.Count();
var count = 0;
var totalInstances = instances.Count();

var properties =
(SqlGenerator.IsIdentity
? SqlGenerator.SqlProperties.Where(p => !p.PropertyName.Equals(SqlGenerator.IdentitySqlProperty.PropertyName, StringComparison.OrdinalIgnoreCase))
: SqlGenerator.SqlProperties).ToList();

int exceededTimes = (int) Math.Ceiling(totalInstances * properties.Count / 2099d);
var exceededTimes = (int)Math.Ceiling(totalInstances * properties.Count / 2099d);
if (exceededTimes > 1)
{
int maxAllowedInstancesPerBatch = totalInstances / exceededTimes;
var maxAllowedInstancesPerBatch = totalInstances / exceededTimes;

for (int i = 0; i <= exceededTimes; i++)
for (var i = 0; i <= exceededTimes; i++)
{
var skips = i * maxAllowedInstancesPerBatch;

if (skips >= totalInstances)
break;

Expand All @@ -54,24 +60,30 @@ public virtual int BulkInsert(IEnumerable<TEntity> instances, IDbTransaction tra
}

/// <inheritdoc />
public virtual async Task<int> BulkInsertAsync(IEnumerable<TEntity> instances, IDbTransaction transaction = null)
public virtual Task<int> BulkInsertAsync(IEnumerable<TEntity> instances)
{
return BulkInsertAsync(instances, null);
}

/// <inheritdoc />
public virtual async Task<int> BulkInsertAsync(IEnumerable<TEntity> instances, IDbTransaction transaction)
{
if (SqlGenerator.Provider == SqlProvider.MSSQL)
{
int count = 0;
int totalInstances = instances.Count();
var count = 0;
var totalInstances = instances.Count();

var properties =
(SqlGenerator.IsIdentity
? SqlGenerator.SqlProperties.Where(p => !p.PropertyName.Equals(SqlGenerator.IdentitySqlProperty.PropertyName, StringComparison.OrdinalIgnoreCase))
: SqlGenerator.SqlProperties).ToList();

int exceededTimes = (int) Math.Ceiling(totalInstances * properties.Count / 2099d);
var exceededTimes = (int)Math.Ceiling(totalInstances * properties.Count / 2099d);
if (exceededTimes > 1)
{
int maxAllowedInstancesPerBatch = totalInstances / exceededTimes;
var maxAllowedInstancesPerBatch = totalInstances / exceededTimes;

for (int i = 0; i <= exceededTimes; i++)
for (var i = 0; i <= exceededTimes; i++)
{
var skips = i * maxAllowedInstancesPerBatch;

Expand Down
14 changes: 12 additions & 2 deletions src/MicroOrm.Dapper.Repositories/IDapperRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,22 @@ public interface IDapperRepository<TEntity> : IReadOnlyDapperRepository<TEntity>
/// <summary>
/// Bulk Insert objects to DB
/// </summary>
int BulkInsert(IEnumerable<TEntity> instances, IDbTransaction transaction = null);
int BulkInsert(IEnumerable<TEntity> instances);

/// <summary>
/// Bulk Insert objects to DB
/// </summary>
Task<int> BulkInsertAsync(IEnumerable<TEntity> instances, IDbTransaction transaction = null);
int BulkInsert(IEnumerable<TEntity> instances, IDbTransaction transaction);

/// <summary>
/// Bulk Insert objects to DB
/// </summary>
Task<int> BulkInsertAsync(IEnumerable<TEntity> instances);

/// <summary>
/// Bulk Insert objects to DB
/// </summary>
Task<int> BulkInsertAsync(IEnumerable<TEntity> instances, IDbTransaction transaction);

/// <summary>
/// Delete object from DB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Description>CRUD for Dapper</Description>
<Copyright>2015 © Serge K</Copyright>
<VersionPrefix>1.20.1</VersionPrefix>
<VersionPrefix>1.21.0</VersionPrefix>
<Authors>Serge K and Contributors</Authors>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;net5.0;net6.0</TargetFrameworks>
Expand Down

0 comments on commit ce014c4

Please sign in to comment.