diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/ArrayTypesTest.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/ArrayTypesTest.cs index 1949a5b..48614ff 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/ArrayTypesTest.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/ArrayTypesTest.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/BasicDataTypesTests.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/BasicDataTypesTests.cs index 52ccad2..ac07840 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/BasicDataTypesTests.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/BasicDataTypesTests.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Extensions/NpgsqlExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Extensions/NpgsqlExtensions.cs index bfeea61..7f1630f 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Extensions/NpgsqlExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Extensions/NpgsqlExtensions.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; using Npgsql; namespace PostgreSQLCopyHelper.Test.Extensions diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue1_QuotingTest.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue1_QuotingTest.cs index e3e77be..d7cf17f 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue1_QuotingTest.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue1_QuotingTest.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using Npgsql; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue2_MixedCaseEntity_Test.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue2_MixedCaseEntity_Test.cs index 11db545..9028dde 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue2_MixedCaseEntity_Test.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/Issues/Issue2_MixedCaseEntity_Test.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using Npgsql; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/NullableBasicDataTypesTest.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/NullableBasicDataTypesTest.cs index 33cfe7a..99dc852 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/NullableBasicDataTypesTest.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/NullableBasicDataTypesTest.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/PostgreSQLCopyHelper.Test.csproj b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/PostgreSQLCopyHelper.Test.csproj index cc1f7e2..8887a5b 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/PostgreSQLCopyHelper.Test.csproj +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/PostgreSQLCopyHelper.Test.csproj @@ -8,7 +8,7 @@ - + diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/TransactionalTestBase.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/TransactionalTestBase.cs index b31ca03..b233090 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/TransactionalTestBase.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.Test/TransactionalTestBase.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using Npgsql; using NUnit.Framework; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/ArrayTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/ArrayTypeExtensions.cs index 8ccdc97..c5f7a54 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/ArrayTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/ArrayTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BinaryDataTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BinaryDataTypeExtensions.cs index 9e5bf60..f128b6f 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BinaryDataTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BinaryDataTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BitStringTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BitStringTypeExtensions.cs index aef3752..b2b8287 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BitStringTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BitStringTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BooleanTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BooleanTypeExtensions.cs index be74c78..e75533c 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BooleanTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/BooleanTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/CharacterTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/CharacterTypeExtensions.cs index b869703..816f491 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/CharacterTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/CharacterTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/DateTimeTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/DateTimeTypeExtensions.cs index 2cc8510..ee9da13 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/DateTimeTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/DateTimeTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/JsonTypeExtension.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/JsonTypeExtension.cs index fa262c1..ca78ffb 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/JsonTypeExtension.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/JsonTypeExtension.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/MonetaryTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/MonetaryTypeExtensions.cs index 0e66f64..e6f73dc 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/MonetaryTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/MonetaryTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NetworkAddressTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NetworkAddressTypeExtensions.cs index 153f2e1..3885315 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NetworkAddressTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NetworkAddressTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Net; @@ -20,4 +19,4 @@ public static PostgreSQLCopyHelper MapMacAddress(this PostgreS return helper.Map(columnName, propertyGetter, NpgsqlDbType.MacAddr); } } -} \ No newline at end of file +} diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NumericTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NumericTypeExtensions.cs index 20b3d63..010a68c 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NumericTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/NumericTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; @@ -68,4 +67,4 @@ public static PostgreSQLCopyHelper MapDouble(this PostgreSQLCo return helper.MapNullable(columnName, propertyGetter, NpgsqlDbType.Double); } } -} \ No newline at end of file +} diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/UUIDTypeExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/UUIDTypeExtensions.cs index 56182c0..214a2f2 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/UUIDTypeExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Extensions/UUIDTypeExtensions.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using NpgsqlTypes; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/IPostgreSQLCopyHelper.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/IPostgreSQLCopyHelper.cs index 0959282..8b90770 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/IPostgreSQLCopyHelper.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/IPostgreSQLCopyHelper.cs @@ -1,7 +1,8 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; using Npgsql; namespace PostgreSQLCopyHelper @@ -9,5 +10,6 @@ namespace PostgreSQLCopyHelper public interface IPostgreSQLCopyHelper { ulong SaveAll(NpgsqlConnection connection, IEnumerable entities); + ValueTask SaveAllAsync(NpgsqlConnection connection, IEnumerable entities, CancellationToken cancellationToken = default); } } diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/ColumnDefinition.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/ColumnDefinition.cs index 6baf89d..94bc9e6 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/ColumnDefinition.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/ColumnDefinition.cs @@ -1,7 +1,7 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Threading.Tasks; using Npgsql; namespace PostgreSQLCopyHelper.Model @@ -10,7 +10,7 @@ internal class ColumnDefinition { public string ColumnName { get; set; } - public Action Write { get; set; } + public Func Write { get; set; } public override string ToString() { diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/TableDefinition.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/TableDefinition.cs index b28bdc6..485015a 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/TableDefinition.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Model/TableDefinition.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using PostgreSQLCopyHelper.Utils; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.cs index 24e8007..3b13976 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.cs @@ -1,9 +1,10 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Npgsql; using NpgsqlTypes; using PostgreSQLCopyHelper.Model; @@ -37,13 +38,29 @@ public PostgreSQLCopyHelper(string schemaName, string tableName) _columns = new List>(); } - public ulong SaveAll(NpgsqlConnection connection, IEnumerable entities) + public ulong SaveAll(NpgsqlConnection connection, IEnumerable entities) => + DoSaveAllAsync(connection, entities).GetAwaiter().GetResult(); + + public ValueTask SaveAllAsync(NpgsqlConnection connection, IEnumerable entities, CancellationToken cancellationToken = default) + { + if (cancellationToken.IsCancellationRequested) + { + return new ValueTask(Task.FromCanceled(cancellationToken)); + } + + using (NoSynchronizationContextScope.Enter()) + { + return DoSaveAllAsync(connection, entities); + } + } + + private async ValueTask DoSaveAllAsync(NpgsqlConnection connection, IEnumerable entities) { using (var binaryCopyWriter = connection.BeginBinaryImport(GetCopyCommand())) { - WriteToStream(binaryCopyWriter, entities); + await WriteToStream(binaryCopyWriter, entities); - return binaryCopyWriter.Complete(); + return await binaryCopyWriter.CompleteAsync(); } } @@ -56,41 +73,41 @@ public PostgreSQLCopyHelper UsePostgresQuoting(bool enabled = true) public PostgreSQLCopyHelper Map(string columnName, Func propertyGetter, NpgsqlDbType type) { - return AddColumn(columnName, (writer, entity) => writer.Write(propertyGetter(entity), type)); + return AddColumn(columnName, (writer, entity) => writer.WriteAsync(propertyGetter(entity), type)); } public PostgreSQLCopyHelper MapNullable(string columnName, Func propertyGetter, NpgsqlDbType type) where TProperty : struct { - return AddColumn(columnName, (writer, entity) => + return AddColumn(columnName, async (writer, entity) => { var val = propertyGetter(entity); if (!val.HasValue) { - writer.WriteNull(); + await writer.WriteNullAsync(); } else { - writer.Write(val.Value, type); + await writer.WriteAsync(val.Value, type); } }); } - private void WriteToStream(NpgsqlBinaryImporter writer, IEnumerable entities) + private async Task WriteToStream(NpgsqlBinaryImporter writer, IEnumerable entities) { foreach (var entity in entities) { - writer.StartRow(); + await writer.StartRowAsync(); foreach (var columnDefinition in _columns) { - columnDefinition.Write(writer, entity); + await columnDefinition.Write(writer, entity); } } } - private PostgreSQLCopyHelper AddColumn(string columnName, Action action) + private PostgreSQLCopyHelper AddColumn(string columnName, Func action) { _columns.Add(new ColumnDefinition { diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.csproj b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.csproj index a3b5008..599af68 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.csproj +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper.csproj @@ -21,7 +21,11 @@ - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NoSynchronizationContextScope.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NoSynchronizationContextScope.cs new file mode 100644 index 0000000..3ccc433 --- /dev/null +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NoSynchronizationContextScope.cs @@ -0,0 +1,40 @@ +// Code taken from https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/NoSynchronizationContextScope.cs + +using System; +using System.Threading; + +namespace PostgreSQLCopyHelper.Utils +{ + /// + /// This mechanism is used to temporarily set the current synchronization context to null while + /// executing Npgsql code, making all await continuations execute on the thread pool. This replaces + /// the need to place ConfigureAwait(false) everywhere, and should be used in all surface async methods, + /// without exception. + /// + /// Warning: do not use this directly in async methods, use it in sync wrappers of async methods + /// (see https://github.com/npgsql/npgsql/issues/1593) + /// + /// + /// http://stackoverflow.com/a/28307965/640325 + /// + internal static class NoSynchronizationContextScope + { + internal static Disposable Enter() + { + var sc = SynchronizationContext.Current; + SynchronizationContext.SetSynchronizationContext(null); + return new Disposable(sc); + } + + internal struct Disposable : IDisposable + { + private readonly SynchronizationContext _synchronizationContext; + + internal Disposable(SynchronizationContext synchronizationContext) + => _synchronizationContext = synchronizationContext; + + public void Dispose() + => SynchronizationContext.SetSynchronizationContext(_synchronizationContext); + } + } +} diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NpgsqlUtils.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NpgsqlUtils.cs index 803f754..7fa4192 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NpgsqlUtils.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/NpgsqlUtils.cs @@ -1,5 +1,4 @@ -// Copyright (c) Philipp Wagner. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Linq; diff --git a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/StringExtensions.cs b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/StringExtensions.cs index 40d0a39..39c34b5 100644 --- a/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/StringExtensions.cs +++ b/PostgreSQLCopyHelper/PostgreSQLCopyHelper/PostgreSQLCopyHelper/Utils/StringExtensions.cs @@ -1,4 +1,6 @@ -namespace PostgreSQLCopyHelper.Utils +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace PostgreSQLCopyHelper.Utils { public static class StringExtensions {