diff --git a/.github/conventions.yml b/.github/conventions.yml index 5e5cf36..8e1acfd 100644 --- a/.github/conventions.yml +++ b/.github/conventions.yml @@ -1,10 +1,6 @@ # applied automatically by https://github.com/Faithlife/RepoConventionsApplier (DO NOT REMOVE THIS LINE) conventions: - - path: Faithlife/CodingGuidelines/conventions/faithlife-auto-apply-conventions - - path: Faithlife/CodingGuidelines/conventions/dotnet-common - - path: Faithlife/CodingGuidelines/conventions/faithlife-dotnet-library-build - - path: Faithlife/CodingGuidelines/conventions/faithlife-dotnet-library-workflow - - path: Faithlife/CodingGuidelines/conventions/faithlife-license-mit + - path: Faithlife/CodingGuidelines/conventions/faithlife-dotnet-library pull-request: reviewers: diff --git a/.gitignore b/.gitignore index a2ad192..7ec75c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,21 @@ -.vs/ -.idea/ +# DO NOT EDIT: gitignore-common convention +.DS_Store +Thumbs.db +*.log +# END DO NOT EDIT + +# DO NOT EDIT: gitignore-dotnet convention artifacts/ bin/ obj/ release/ +# END DO NOT EDIT +# DO NOT EDIT: gitignore-ide convention +.vs/ +.idea/ *.cache -*.log -*.ncrunchproject -*.ncrunchsolution *.user -launchSettings.json -nCrunchTemp* +*.userprefs _ReSharper* -.DS_Store +# END DO NOT EDIT diff --git a/DapperUtility.slnx b/DapperUtility.slnx index f6f69a4..416345c 100644 --- a/DapperUtility.slnx +++ b/DapperUtility.slnx @@ -16,9 +16,11 @@ + + diff --git a/Directory.Build.props b/Directory.Build.props index bbf312a..696a49c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -3,23 +3,25 @@ 2.0.0 2.0.0 - 12.0 - disable + $(NoWarn);1591;1998;NU1507;NU5105 + Faithlife + DapperUtility + + + + + 14.0 + enable enable true - $(NoWarn);1591;1998;NU1507;NU5105 en-US embedded - Faithlife - DapperUtility MIT https://github.com/$(GitHubOrganization)/$(RepositoryName) https://github.com/$(GitHubOrganization)/$(RepositoryName)/blob/master/ReleaseNotes.md - https://github.com/$(GitHubOrganization)/$(RepositoryName).git + https://github.com/$(GitHubOrganization)/$(RepositoryName) Faithlife Copyright $(Authors) - true - true true latest-all true @@ -31,11 +33,10 @@ true true true + true + all + low - - - $(VersionPrefix).$(BuildNumber) - true - + diff --git a/Directory.Packages.props b/Directory.Packages.props index 1bae07e..c1c4231 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,7 +1,11 @@ + true + true + true + @@ -10,9 +14,11 @@ + - - - + + + + \ No newline at end of file diff --git a/nuget.config b/nuget.config index 6873eb9..95e879e 100644 --- a/nuget.config +++ b/nuget.config @@ -1,6 +1,6 @@ - + diff --git a/src/Faithlife.Utility.Dapper/BulkInsertUtility.cs b/src/Faithlife.Utility.Dapper/BulkInsertUtility.cs index 42c215c..f814185 100644 --- a/src/Faithlife.Utility.Dapper/BulkInsertUtility.cs +++ b/src/Faithlife.Utility.Dapper/BulkInsertUtility.cs @@ -16,15 +16,15 @@ public static class BulkInsertUtility /// /// Efficiently inserts multiple rows, in batches as necessary. /// - public static int BulkInsert(this IDbConnection connection, string sql, IEnumerable insertParams, IDbTransaction transaction = null, int? batchSize = null) + public static int BulkInsert(this IDbConnection connection, string sql, IEnumerable insertParams, IDbTransaction? transaction = null, int? batchSize = null) { - return connection.BulkInsert(sql, (object) null, insertParams, transaction, batchSize); + return connection.BulkInsert(sql, (object?) null, insertParams, transaction, batchSize); } /// /// Efficiently inserts multiple rows, in batches as necessary. /// - public static int BulkInsert(this IDbConnection connection, string sql, TCommon commonParam, IEnumerable insertParams, IDbTransaction transaction = null, int? batchSize = null) + public static int BulkInsert(this IDbConnection connection, string sql, TCommon? commonParam, IEnumerable insertParams, IDbTransaction? transaction = null, int? batchSize = null) { int rowCount = 0; foreach (var commandDefinition in GetBulkInsertCommands(sql, commonParam, insertParams, transaction, batchSize)) @@ -35,15 +35,15 @@ public static int BulkInsert(this IDbConnection connection, st /// /// Efficiently inserts multiple rows, in batches as necessary. /// - public static Task BulkInsertAsync(this IDbConnection connection, string sql, IEnumerable insertParams, IDbTransaction transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) + public static Task BulkInsertAsync(this IDbConnection connection, string sql, IEnumerable insertParams, IDbTransaction? transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) { - return connection.BulkInsertAsync(sql, (object) null, insertParams, transaction, batchSize, cancellationToken); + return connection.BulkInsertAsync(sql, (object?) null, insertParams, transaction, batchSize, cancellationToken); } /// /// Efficiently inserts multiple rows, in batches as necessary. /// - public static async Task BulkInsertAsync(this IDbConnection connection, string sql, TCommon commonParam, IEnumerable insertParams, IDbTransaction transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) + public static async Task BulkInsertAsync(this IDbConnection connection, string sql, TCommon? commonParam, IEnumerable insertParams, IDbTransaction? transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) { int rowCount = 0; foreach (var commandDefinition in GetBulkInsertCommands(sql, commonParam, insertParams, transaction, batchSize, cancellationToken)) @@ -54,15 +54,15 @@ public static int BulkInsert(this IDbConnection connection, st /// /// Gets the Dapper CommandDefinitions used by BulkInsert and BulkInsertAsync. /// - public static IEnumerable GetBulkInsertCommands(string sql, IEnumerable insertParams, IDbTransaction transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) + public static IEnumerable GetBulkInsertCommands(string sql, IEnumerable insertParams, IDbTransaction? transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) { - return GetBulkInsertCommands(sql, (object) null, insertParams, transaction, batchSize, cancellationToken); + return GetBulkInsertCommands(sql, (object?) null, insertParams, transaction, batchSize, cancellationToken); } /// /// Gets the Dapper CommandDefinitions used by BulkInsert and BulkInsertAsync. /// - public static IEnumerable GetBulkInsertCommands(string sql, TCommon commonParam, IEnumerable insertParams, IDbTransaction transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) + public static IEnumerable GetBulkInsertCommands(string sql, TCommon? commonParam, IEnumerable insertParams, IDbTransaction? transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) { if (sql == null) throw new ArgumentNullException(nameof(sql)); @@ -81,7 +81,7 @@ public static int BulkInsert(this IDbConnection connection, st return YieldBulkInsertCommands(valuesClauseMatches[0], sql, commonParam, insertParams, transaction, batchSize, cancellationToken); } - private static IEnumerable YieldBulkInsertCommands(Match valuesClauseMatch, string sql, TCommon commonParam, IEnumerable insertParams, IDbTransaction transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) + private static IEnumerable YieldBulkInsertCommands(Match valuesClauseMatch, string sql, TCommon? commonParam, IEnumerable insertParams, IDbTransaction? transaction = null, int? batchSize = null, CancellationToken cancellationToken = default(CancellationToken)) { // identify SQL parts Group tupleMatch = valuesClauseMatch.Groups[1]; @@ -91,7 +91,7 @@ public static int BulkInsert(this IDbConnection connection, st // get common names and values string[] commonNames = ParamExtractor.GetNames(); - object[] commonValues = ParamExtractor.GetValues(commonParam); + object?[] commonValues = ParamExtractor.GetValues(commonParam); // get insert names and find insert parameters in tuple string[] insertNames = ParamExtractor.GetNames(); @@ -106,9 +106,9 @@ public static int BulkInsert(this IDbConnection connection, st Math.Max(1, (maxParamsPerBatch - commonNames.Length) / Math.Max(1, insertNames.Length)); // insert one batch at a time - string batchSql = null; + string? batchSql = null; int lastBatchCount = 0; - StringBuilder batchSqlBuilder = null; + StringBuilder? batchSqlBuilder = null; foreach (var insertParamBatch in EnumerateBatches(insertParams, actualBatchSize)) { // build the SQL for the batch @@ -167,9 +167,9 @@ private static class ParamExtractor { public static string[] GetNames() => s_names; - public static object[] GetValues(T param) + public static object?[] GetValues(T? param) { - var values = new object[s_getters.Length]; + var values = new object?[s_getters.Length]; if (param != null) { for (int index = 0; index < values.Length; index++) @@ -183,7 +183,7 @@ static ParamExtractor() #pragma warning restore CA1810 { var names = new List(); - var getters = new List>(); + var getters = new List>(); foreach (var property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) { var getter = TryCreateGetter(property); @@ -197,7 +197,7 @@ static ParamExtractor() s_getters = [.. getters]; } - private static Func TryCreateGetter(PropertyInfo property) + private static Func? TryCreateGetter(PropertyInfo property) { var getMethod = property.GetGetMethod(); var ownerType = property.DeclaringType; @@ -215,11 +215,11 @@ private static Func TryCreateGetter(PropertyInfo property) generator.Emit(OpCodes.Box, property.PropertyType); generator.Emit(OpCodes.Ret); - return (Func) dynamicGetMethod.CreateDelegate(typeof(Func)); + return (Func) dynamicGetMethod.CreateDelegate(typeof(Func)); } private static readonly string[] s_names; - private static readonly Func[] s_getters; + private static readonly Func[] s_getters; } private static IEnumerable> EnumerateBatches(IEnumerable items, int batchSize) diff --git a/src/Faithlife.Utility.Dapper/Faithlife.Utility.Dapper.csproj b/src/Faithlife.Utility.Dapper/Faithlife.Utility.Dapper.csproj index 0e80ac7..5f601fb 100644 --- a/src/Faithlife.Utility.Dapper/Faithlife.Utility.Dapper.csproj +++ b/src/Faithlife.Utility.Dapper/Faithlife.Utility.Dapper.csproj @@ -7,6 +7,7 @@ true true README.md + true diff --git a/tests/Faithlife.Utility.Dapper.Tests/BulkInsertUtilityTests.cs b/tests/Faithlife.Utility.Dapper.Tests/BulkInsertUtilityTests.cs index 443df4a..09d88cb 100644 --- a/tests/Faithlife.Utility.Dapper.Tests/BulkInsertUtilityTests.cs +++ b/tests/Faithlife.Utility.Dapper.Tests/BulkInsertUtilityTests.cs @@ -11,7 +11,7 @@ public void NullSql_Throws() { Assert.Throws(() => { - BulkInsertUtility.GetBulkInsertCommands(null, new[] { new { foo = 1 } }); + BulkInsertUtility.GetBulkInsertCommands(null!, new[] { new { foo = 1 } }); }); } @@ -29,7 +29,7 @@ public void NullInsertParams_Throws() { Assert.Throws(() => { - BulkInsertUtility.GetBulkInsertCommands("VALUES (@foo)...", default(object[])); + BulkInsertUtility.GetBulkInsertCommands("VALUES (@foo)...", default(object[])!); }); } @@ -84,7 +84,7 @@ public void MinimalInsert() var commands = BulkInsertUtility.GetBulkInsertCommands("INSERT INTO t (foo)VALUES(@foo)...;", new[] { new { foo = 1 } }).ToList(); commands.Count.Should().Be(1); commands[0].CommandText.Should().Be("INSERT INTO t (foo)VALUES(@foo_0);"); - var parameters = (DynamicParameters) commands[0].Parameters; + var parameters = (DynamicParameters) commands[0].Parameters!; parameters.ParameterNames.Single().Should().Be("foo_0"); ((SqlMapper.IParameterLookup) parameters)["foo_0"].Should().Be(1); } @@ -112,7 +112,7 @@ public void CommonAndInsertedParameters() var commands = BulkInsertUtility.GetBulkInsertCommands("VALUES (@a, @b, @c, @d)...", new { a = 1, b = 2 }, new[] { new { c = 3, d = 4 }, new { c = 5, d = 6 } }).ToList(); commands.Count.Should().Be(1); commands[0].CommandText.Should().Be("VALUES (@a, @b, @c_0, @d_0),(@a, @b, @c_1, @d_1)"); - var parameters = (SqlMapper.IParameterLookup) commands[0].Parameters; + var parameters = (SqlMapper.IParameterLookup) commands[0].Parameters!; parameters["a"].Should().Be(1); parameters["b"].Should().Be(2); parameters["c_0"].Should().Be(3); @@ -129,7 +129,7 @@ public void EightRowsInThreeBatches() commands[0].CommandText.Should().Be("VALUES(@foo_0),(@foo_1),(@foo_2)"); commands[1].CommandText.Should().Be("VALUES(@foo_0),(@foo_1),(@foo_2)"); commands[2].CommandText.Should().Be("VALUES(@foo_0),(@foo_1)"); - ((SqlMapper.IParameterLookup) commands[2].Parameters)["foo_1"].Should().Be(7); + ((SqlMapper.IParameterLookup) commands[2].Parameters!)["foo_1"].Should().Be(7); } [Fact] @@ -177,7 +177,7 @@ public void NoParameterNameValidation() var commands = BulkInsertUtility.GetBulkInsertCommands("VALUES (@a, @b, @c, @d)...", new { e = 1, f = 2 }, new[] { new { g = 3, h = 4 }, new { g = 5, h = 6 } }).ToList(); commands.Count.Should().Be(1); commands[0].CommandText.Should().Be("VALUES (@a, @b, @c, @d),(@a, @b, @c, @d)"); - var parameters = (SqlMapper.IParameterLookup) commands[0].Parameters; + var parameters = (SqlMapper.IParameterLookup) commands[0].Parameters!; parameters["e"].Should().Be(1); parameters["f"].Should().Be(2); parameters["g_0"].Should().Be(3); @@ -192,7 +192,7 @@ public void ComplexValues() var commands = BulkInsertUtility.GetBulkInsertCommands("VALUES (@a + (@d * @c) -\r\n\t@d)...", new { a = 1, b = 2 }, new[] { new { c = 3, d = 4 }, new { c = 5, d = 6 } }).ToList(); commands.Count.Should().Be(1); commands[0].CommandText.Should().Be("VALUES (@a + (@d_0 * @c_0) -\r\n\t@d_0),(@a + (@d_1 * @c_1) -\r\n\t@d_1)"); - var parameters = (SqlMapper.IParameterLookup) commands[0].Parameters; + var parameters = (SqlMapper.IParameterLookup) commands[0].Parameters!; parameters["a"].Should().Be(1); parameters["b"].Should().Be(2); parameters["c_0"].Should().Be(3);