Skip to content

Commit

Permalink
more fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Feb 7, 2025
1 parent 810789e commit 83c5a4c
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 46 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.100",
"version": "9.0.102",
"rollForward": "latestMajor"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,11 @@ static string BuildParameterMap(in ParseState ctx, IInvocationOperation op, stri

internal static class FeatureKeys
{
public const string InterceptorsPreviewNamespaces = nameof(InterceptorsPreviewNamespaces),
public const string InterceptorsNamespaces = nameof(InterceptorsNamespaces),
InterceptorsPreviewNamespaces = nameof(InterceptorsPreviewNamespaces),
CodegenNamespace = "Dapper.AOT";
public static KeyValuePair<string, string> InterceptorsPreviewNamespacePair => new(InterceptorsPreviewNamespaces, CodegenNamespace);
public static KeyValuePair<string, string> InterceptorsNamespacePair => new(InterceptorsNamespaces, CodegenNamespace);
}

private static bool CheckPrerequisites(in GenerateState ctx)
Expand Down
24 changes: 24 additions & 0 deletions src/Dapper.AOT/CommandT.Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -222,6 +223,7 @@ private int ExecuteMultiBatch(ReadOnlySpan<TArgs> source, int batchSize) // TODO
{
Debug.Assert(source.Length > 1);
UnifiedCommand batch = default;
bool closeConnection = false;
try
{
foreach (var arg in source)
Expand All @@ -231,6 +233,12 @@ private int ExecuteMultiBatch(ReadOnlySpan<TArgs> source, int batchSize) // TODO
}
if (!batch.HasBatch) return 0;

if (connection.State != ConnectionState.Open)
{
connection.Open();
closeConnection = true;
}

var result = batch.AssertBatch.ExecuteNonQuery();

if (commandFactory.RequirePostProcess)
Expand All @@ -241,9 +249,14 @@ private int ExecuteMultiBatch(ReadOnlySpan<TArgs> source, int batchSize) // TODO
}
finally
{
if (closeConnection)
{
connection.Close();
}
batch.Cleanup();
}
}

private int ExecuteMultiBatch(IEnumerable<TArgs> source, int batchSize) // TODO: sub-batching
{
if (commandFactory.RequirePostProcess)
Expand All @@ -253,6 +266,7 @@ private int ExecuteMultiBatch(IEnumerable<TArgs> source, int batchSize) // TODO:
}

UnifiedCommand batch = default;
bool closeConnection = false;
try
{
foreach (var arg in source)
Expand All @@ -262,6 +276,12 @@ private int ExecuteMultiBatch(IEnumerable<TArgs> source, int batchSize) // TODO:
}
if (!batch.HasBatch) return 0;

if (connection.State != ConnectionState.Open)
{
connection.Open();
closeConnection = true;
}

var result = batch.AssertBatch.ExecuteNonQuery();
if (commandFactory.RequirePostProcess)
{
Expand All @@ -271,6 +291,10 @@ private int ExecuteMultiBatch(IEnumerable<TArgs> source, int batchSize) // TODO:
}
finally
{
if (closeConnection)
{
connection.Close();
}
batch.Cleanup();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dapper.AOT/Internal/SyncCommandState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void OnBeforeExecute(DbCommand command)
Debug.Assert(command?.Connection is not null);
Command = command!;
connection = command!.Connection;

if (connection.State != ConnectionState.Open)
{
connection.Open();
Expand Down
4 changes: 3 additions & 1 deletion src/Dapper.AOT/UnifiedCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Dapper;
Expand All @@ -16,8 +18,8 @@ namespace Dapper;
#pragma warning restore IDE0079

public readonly struct UnifiedCommand

{

/// <summary>
/// The <see cref="System.Data.Common.DbCommand"/> associated with the current operation; this may be <c>null</c> for batch commands.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract class IntegrationTestsBase
.WithFeatures(new[]
{
new KeyValuePair<string, string>("InterceptorsPreviewNamespaces", "$(InterceptorsPreviewNamespaces);Dapper.AOT"),
new KeyValuePair<string, string>("InterceptorsNamespaces", "$(InterceptorsNamespaces);Dapper.AOT"),
});

protected readonly IDbConnection DbConnection;
Expand Down
4 changes: 2 additions & 2 deletions test/Dapper.AOT.Test/Dapper.AOT.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" />
<PackageReference Include="Oracle.ManagedDataAccess" Condition="'$(TargetFramework)'=='net48'" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Condition="'$(TargetFramework)'!='net48'" />
<PackageReference Include="System.ComponentModel.Annotations" />
<PackageReference Include="System.Data.Common" />
<PackageReference Include="System.Data.SqlClient" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="xunit" />
Expand All @@ -61,6 +59,8 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net48'">
<PackageReference Include="Npgsql" VersionOverride="8.0.6" />
<PackageReference Include="System.Data.Common" />
<PackageReference Include="System.ComponentModel.Annotations" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions test/Dapper.AOT.Test/DapperApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public void DiscoveredMethodsAreExpected()
var names = string.Join(",", methods);
Log.WriteLine(names);

Assert.Equal("AsList<,AsTableValuedParameter,AsTableValuedParameter<,Execute,ExecuteAsync,ExecuteReader,ExecuteReaderAsync,ExecuteScalar,ExecuteScalar<,ExecuteScalarAsync,ExecuteScalarAsync<,GetRowParser,GetRowParser<,GetTypeName,Parse,Parse<,Query,Query<,QueryAsync,QueryAsync<,QueryFirst,QueryFirst<,QueryFirstAsync,QueryFirstAsync<,QueryFirstOrDefault,QueryFirstOrDefault<,QueryFirstOrDefaultAsync,QueryFirstOrDefaultAsync<,QueryMultiple,QueryMultipleAsync,QuerySingle,QuerySingle<,QuerySingleAsync,QuerySingleAsync<,QuerySingleOrDefault,QuerySingleOrDefault<,QuerySingleOrDefaultAsync,QuerySingleOrDefaultAsync<" + (IsNetFx ? "" : ",QueryUnbufferedAsync,QueryUnbufferedAsync<") + ",ReplaceLiterals,SetTypeName", names);
Assert.Equal("AsList<,AsTableValuedParameter,AsTableValuedParameter<,Execute,ExecuteAsync,ExecuteReader,ExecuteReaderAsync,ExecuteScalar,ExecuteScalar<,ExecuteScalarAsync,ExecuteScalarAsync<,GetRowParser,GetRowParser<,GetTypeName,Parse,Parse<,Query,Query<,QueryAsync,QueryAsync<,QueryFirst,QueryFirst<,QueryFirstAsync,QueryFirstAsync<,QueryFirstOrDefault,QueryFirstOrDefault<,QueryFirstOrDefaultAsync,QueryFirstOrDefaultAsync<,QueryMultiple,QueryMultipleAsync,QuerySingle,QuerySingle<,QuerySingleAsync,QuerySingleAsync<,QuerySingleOrDefault,QuerySingleOrDefault<,QuerySingleOrDefaultAsync,QuerySingleOrDefaultAsync<,QueryUnbufferedAsync,QueryUnbufferedAsync<,ReplaceLiterals,SetTypeName", names);

var candidates = string.Join(",", methods.Where(DapperInterceptorGenerator.IsCandidate));
Log.WriteLine(candidates);
Assert.Equal("Execute,ExecuteAsync,ExecuteReader,ExecuteReaderAsync,ExecuteScalar,ExecuteScalar<,ExecuteScalarAsync,ExecuteScalarAsync<,GetRowParser,GetRowParser<,Query,Query<,QueryAsync,QueryAsync<,QueryFirst,QueryFirst<,QueryFirstAsync,QueryFirstAsync<,QueryFirstOrDefault,QueryFirstOrDefault<,QueryFirstOrDefaultAsync,QueryFirstOrDefaultAsync<,QueryMultiple,QueryMultipleAsync,QuerySingle,QuerySingle<,QuerySingleAsync,QuerySingleAsync<,QuerySingleOrDefault,QuerySingleOrDefault<,QuerySingleOrDefaultAsync,QuerySingleOrDefaultAsync<" + (IsNetFx ? "" : ",QueryUnbufferedAsync,QueryUnbufferedAsync<"), candidates);
Assert.Equal("Execute,ExecuteAsync,ExecuteReader,ExecuteReaderAsync,ExecuteScalar,ExecuteScalar<,ExecuteScalarAsync,ExecuteScalarAsync<,GetRowParser,GetRowParser<,Query,Query<,QueryAsync,QueryAsync<,QueryFirst,QueryFirst<,QueryFirstAsync,QueryFirstAsync<,QueryFirstOrDefault,QueryFirstOrDefault<,QueryFirstOrDefaultAsync,QueryFirstOrDefaultAsync<,QueryMultiple,QueryMultipleAsync,QuerySingle,QuerySingle<,QuerySingleAsync,QuerySingleAsync<,QuerySingleOrDefault,QuerySingleOrDefault<,QuerySingleOrDefaultAsync,QuerySingleOrDefaultAsync<,QueryUnbufferedAsync,QueryUnbufferedAsync<", candidates);
}
}
12 changes: 9 additions & 3 deletions test/Dapper.AOT.Test/Helpers/TestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ internal static class TestFramework
Enum.GetValues<Net>()
#endif
.Select(static x => x.ToString())
.ToHashSet();
.ToHashSet(StringComparer.OrdinalIgnoreCase);

public static Net DetermineNetVersion()
{
#if NET6_0_OR_GREATER
#if NET9_0_OR_GREATER
return Net.Net9;
#elif NET8_0_OR_GREATER
return Net.Net8;
#elif NET6_0_OR_GREATER
return Net.Net6;
#else
return Net.Net48;
Expand All @@ -27,7 +31,9 @@ public static Net DetermineNetVersion()
public enum Net
{
Net48,
Net6
Net6,
Net8,
Net9,
}
}
}
6 changes: 5 additions & 1 deletion test/Dapper.AOT.Test/InterceptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public static IEnumerable<object[]> GetFiles()
if (TestFramework.NetVersions.Contains(fileNetVersionStr))
{
// it has to be the same or greater version
var fileNetVersion = (TestFramework.Net)Enum.Parse(typeof(TestFramework.Net), fileNetVersionStr);
#if NET48
var fileNetVersion = (TestFramework.Net)Enum.Parse(typeof(TestFramework.Net), fileNetVersionStr, true);
#else
var fileNetVersion = Enum.Parse<TestFramework.Net>(fileNetVersionStr, true);
#endif
if (currentNetVersion < fileNetVersion)
{
// skip if current version is lower than specified in the input file name
Expand Down
2 changes: 1 addition & 1 deletion test/Dapper.AOT.Test/TSqlParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ from Customers

var args = parser.GetParameters(out var errors);
Assert.Equal("@id", Assert.Single(args));
Assert.Equal("Incorrect syntax near garbage. (#46010) L5 C27", Assert.Single(errors));
Assert.Equal("Incorrect syntax near 'garbage'. (#46010) L5 C27", Assert.Single(errors));
}

[Fact]
Expand Down
4 changes: 3 additions & 1 deletion test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ internal static class RoslynTestHelpers
"RELEASE",
#endif
})
.WithFeatures([DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespacePair]);
.WithFeatures([
DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespacePair,
DapperInterceptorGenerator.FeatureKeys.InterceptorsNamespacePair]);

public static Compilation CreateCompilation(string source, string name, string fileName)
=> CSharpCompilation.Create(name,
Expand Down
2 changes: 1 addition & 1 deletion test/Dapper.AOT.Test/Verifiers/DAP206.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public class DAP206 : Verifier<DapperAnalyzer>
[Fact]
public Task ParseError() => SqlVerifyAsync("{|#0:|}111 invalid",
Diagnostic(Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111."));
.WithLocation(0).WithArguments(46010, "Incorrect syntax near '111'."));
}
20 changes: 10 additions & 10 deletions test/Dapper.AOT.Test/Verifiers/SqlDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Foo(DbConnection conn)
}
}
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111.")]);
.WithLocation(0).WithArguments(46010, "Incorrect syntax near '111'.")]);

[Fact]
public Task CSVerifyQuestionMarkInQuery_LikePseudoPositional() => CSVerifyAsync("""
Expand Down Expand Up @@ -79,9 +79,9 @@ public void Foo(DbCommand cmd)
public string Blap {get;set;}
}
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111."),
.WithLocation(0).WithArguments(46010, "Incorrect syntax near '111'."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1).WithArguments(46010, "Incorrect syntax near 444.")]);
.WithLocation(1).WithArguments(46010, "Incorrect syntax near '444'.")]);

[Fact]
public Task CSViaParam() => CSVerifyAsync("""
Expand All @@ -104,10 +104,10 @@ public void Usage()
""", DefaultConfig,
[Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0)
.WithArguments(46010, "Incorrect syntax near 111."),
.WithArguments(46010, "Incorrect syntax near '111'."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1)
.WithArguments(46010, "Incorrect syntax near 333.")]);
.WithArguments(46010, "Incorrect syntax near '333'.")]);

[Fact]
public Task VBViaDapper() => VBVerifyAsync("""
Expand All @@ -122,7 +122,7 @@ Public Sub Foo(conn As DbConnection)
End Sub
End Class
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111.")]);
.WithLocation(0).WithArguments(46010, "Incorrect syntax near '111'.")]);

[Fact]
public Task VBViaProperty() => VBVerifyAsync("""
Expand All @@ -145,9 +145,9 @@ Public Property Bar As String
Public Property Blap As String
End Class
""", DefaultConfig, [Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0).WithArguments(46010, "Incorrect syntax near 111."),
.WithLocation(0).WithArguments(46010, "Incorrect syntax near '111'."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1).WithArguments(46010, "Incorrect syntax near 444.")]);
.WithLocation(1).WithArguments(46010, "Incorrect syntax near '444'.")]);

[Fact]
public Task VBViaParam() => VBVerifyAsync("""
Expand All @@ -171,10 +171,10 @@ End Class
""", DefaultConfig,
[Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(0)
.WithArguments(46010, "Incorrect syntax near 111."),
.WithArguments(46010, "Incorrect syntax near '111'."),
Diagnostic(DapperAnalyzer.Diagnostics.ParseError)
.WithLocation(1)
.WithArguments(46010, "Incorrect syntax near 333.")]);
.WithArguments(46010, "Incorrect syntax near '333'.")]);

[Fact]
public Task CSharpSmokeTestVanilla() => CSVerifyAsync(""""
Expand Down
Loading

0 comments on commit 83c5a4c

Please sign in to comment.