Skip to content

Commit

Permalink
SaveAllAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
say25 committed Jun 9, 2019
1 parent 2d7b43e commit 39a7a67
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Threading.Tasks;
using Npgsql;

namespace PostgreSQLCopyHelper
{
public interface IPostgreSQLCopyHelper<TEntity>
{
ulong SaveAll(NpgsqlConnection connection, IEnumerable<TEntity> entities);
Task<ulong> SaveAllAsync(NpgsqlConnection connection, IEnumerable<TEntity> entities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Npgsql;
using NpgsqlTypes;
using PostgreSQLCopyHelper.Model;
Expand Down Expand Up @@ -37,13 +38,16 @@ public PostgreSQLCopyHelper(string schemaName, string tableName)
_columns = new List<ColumnDefinition<TEntity>>();
}

public ulong SaveAll(NpgsqlConnection connection, IEnumerable<TEntity> entities)
public ulong SaveAll(NpgsqlConnection connection, IEnumerable<TEntity> entities) =>
SaveAllAsync(connection, entities).GetAwaiter().GetResult();

public async Task<ulong> SaveAllAsync(NpgsqlConnection connection, IEnumerable<TEntity> entities)
{
using (var binaryCopyWriter = connection.BeginBinaryImport(GetCopyCommand()))
{
WriteToStream(binaryCopyWriter, entities);
await WriteToStream(binaryCopyWriter, entities);

return binaryCopyWriter.Complete();
return await binaryCopyWriter.Complete(async: true);
}
}

Expand Down Expand Up @@ -77,11 +81,11 @@ public PostgreSQLCopyHelper<TEntity> MapNullable<TProperty>(string columnName, F
});
}

private void WriteToStream(NpgsqlBinaryImporter writer, IEnumerable<TEntity> entities)
private async Task WriteToStream(NpgsqlBinaryImporter writer, IEnumerable<TEntity> entities)
{
foreach (var entity in entities)
{
writer.StartRow();
await writer.StartRow(async: true);

foreach (var columnDefinition in _columns)
{
Expand Down

0 comments on commit 39a7a67

Please sign in to comment.