Skip to content

Commit

Permalink
Code cleanup R#
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cordeiro committed Sep 21, 2023
1 parent 85fbf75 commit bed1321
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion PgBulk.EFCore/ContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static Task BulkInsertAsync<T>(this DbContext dbContext, IEnumerable<T> e
var @operator = new BulkEfOperator(dbContext, timeoutOverride);
return @operator.InsertAsync(entities);
}

public static BulkEfOperator GetBulkOperator(this DbContext dbContext, int? timeoutOverride = 600)
{
return new BulkEfOperator(dbContext, timeoutOverride);
Expand Down
5 changes: 2 additions & 3 deletions PgBulk.Tests/EFCoreTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Bogus;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PgBulk.EFCore;

namespace PgBulk.Tests;
Expand All @@ -12,12 +11,12 @@ public class EFCoreTests
.RuleFor(i => i.Value1, f => f.Address.City())
.RuleFor(i => i.Value2, f => f.Company.CompanyName())
.RuleFor(i => i.Value3, f => f.PickRandom(null, f.Name.Suffix()));

private static MyContext CreateContext()
{
return EntityHelper.CreateContext(Nanoid.Nanoid.Generate(size: 8));
}

[TestMethod]
[DataRow(100)]
[DataRow(1000)]
Expand Down
6 changes: 3 additions & 3 deletions PgBulk.Tests/ManualMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace PgBulk.Tests;
public class ManualMappingTests
{
private ManualTableInformationProvider? ManualTableInformationProvider { get; set; }

private static Faker<TestRow> Faker => new Faker<TestRow>().RuleFor(i => i.Id, f => f.IndexFaker)
.RuleFor(i => i.Value1, f => f.Address.City())
.RuleFor(i => i.Value2, f => f.Company.CompanyName())
.RuleFor(i => i.Value3, f => f.PickRandom(null, f.Name.Suffix()));

[TestInitialize]
public void Setup()
{
Expand All @@ -28,7 +28,7 @@ private async Task<Tuple<ManualBulkOperator, MyContext>> GetOperator()

return new Tuple<ManualBulkOperator, MyContext>(bulkOperator, myContext);
}

[TestMethod]
[DataRow(100)]
[DataRow(1000)]
Expand Down
14 changes: 7 additions & 7 deletions PgBulk.Tests/PgBulk.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0"/>
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10"/>
<PackageReference Include="MSTest.TestFramework" Version="2.2.10"/>
<PackageReference Include="coverlet.collector" Version="3.1.2"/>
<PackageReference Include="Bogus" Version="34.0.2"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand All @@ -25,8 +25,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PgBulk.EFCore\PgBulk.EFCore.csproj" />
<ProjectReference Include="..\PgBulk\PgBulk.csproj" />
<ProjectReference Include="..\PgBulk.EFCore\PgBulk.EFCore.csproj"/>
<ProjectReference Include="..\PgBulk\PgBulk.csproj"/>
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions PgBulk/BulkOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ public async Task<NpgsqlBinaryImporter<T>> CreateBinaryImporterAsync<T>(NpgsqlCo
var tableInformation = await TableInformationProvider.GetTableInformation(typeof(T));
return await CreateBinaryImporterAsync<T>(connection, tableInformation, tableInformation.Name);
}

public async Task<NpgsqlBinaryImporter<T>> CreateBinaryImporterAsync<T>(NpgsqlConnection connection, ITableInformation tableInformation, string tableName)
{
var columns = tableInformation.Columns
.Where(i => !i.ValueGeneratedOnAdd)
.Select(i => $"\"{i.Name}\"")
.Aggregate((x, y) => $"{x}, {y}");

#if NET5_0
return await Task.FromResult(new NpgsqlBinaryImporter<T>(connection.BeginBinaryImport($"COPY \"{tableName}\" ({columns}) FROM STDIN (FORMAT BINARY)"), tableInformation));
#else
Expand Down
32 changes: 16 additions & 16 deletions PgBulk/NpgsqlBinaryImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,38 @@ public NpgsqlBinaryImporter(NpgsqlBinaryImporter binaryImporter, ITableInformati
}

private NpgsqlBinaryImporter BinaryImporter { get; }

private ITableInformation TableInformation { get; }

private ICollection<ITableColumnInformation> WritableColumns { get; }

public async ValueTask DisposeAsync()
{
await BinaryImporter.DisposeAsync();
}

public void Dispose()
{
BinaryImporter.Dispose();
}

public async Task<ulong> WriteToBinaryImporter(IEnumerable<T> entities)
{
ulong inserted = 0;

foreach (var entity in entities)
{
await BinaryImporter.StartRowAsync();
foreach (var columnValue in WritableColumns.Select(i => i.GetValue(entity)))

foreach (var columnValue in WritableColumns.Select(i => i.GetValue(entity)))
await BinaryImporter.WriteAsync(columnValue);

inserted++;
}

return inserted;
}

public void Dispose()
{
BinaryImporter.Dispose();
}

public async ValueTask DisposeAsync()
{
await BinaryImporter.DisposeAsync();
}

public ValueTask<ulong> CompleteAsync()
{
return BinaryImporter.CompleteAsync();
Expand Down

0 comments on commit bed1321

Please sign in to comment.