diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9b170e9..7f471a3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,7 +23,7 @@ jobs: run: dotnet tool install dotnet-ilverify --global --version 8.0.0 - name: Run build script - run: dotnet-script build/build.csx + run: dotnet-script build/build.csx --disable-isolated-load-context env: # Or as an environment variable GITHUB_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} IS_SECURE_BUILDENVIRONMENT: ${{ secrets.IS_SECURE_BUILDENVIRONMENT }} diff --git a/build/build.csx b/build/build.csx index 70d0a88..6ffe2a0 100644 --- a/build/build.csx +++ b/build/build.csx @@ -1,4 +1,4 @@ -#load "nuget:Dotnet.Build, 0.24.0" +#load "nuget:Dotnet.Build, 0.26.0" #load "nuget:dotnet-steps, 0.0.2" Console.WriteLine($"Building with the latest tag {BuildContext.LatestTag}"); diff --git a/src/DbReader.Tests/DbReader.Tests.csproj b/src/DbReader.Tests/DbReader.Tests.csproj index 510aee1..1b25a5a 100644 --- a/src/DbReader.Tests/DbReader.Tests.csproj +++ b/src/DbReader.Tests/DbReader.Tests.csproj @@ -10,21 +10,20 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - + - + runtime; build; native; contentfiles; analyzers all - + net8.0 false - @@ -34,6 +33,4 @@ PreserveNewest - - \ No newline at end of file diff --git a/src/DbReader.Tests/IntegrationTests.cs b/src/DbReader.Tests/IntegrationTests.cs index 6045efd..df89e12 100644 --- a/src/DbReader.Tests/IntegrationTests.cs +++ b/src/DbReader.Tests/IntegrationTests.cs @@ -603,6 +603,18 @@ public async Task ShouldNotCacheCollection() } } + [Fact] + public async Task ShouldReadFirstOrDefaultAsync() + { + using (var connection = CreateConnection()) + { + var customer = await connection.ReadFirstOrDefaultAsync("SELECT * FROM Customers WHERE CustomerID = @CustomerID", new { CustomerID = "ALFKI" }); + customer.ShouldNotBeNull(); + customer.CustomerId.ShouldBe("ALFKI"); + } + } + + public class CustomerId { private string Value { get; set; } diff --git a/src/DbReader.Tracking/DbReader.Tracking.csproj b/src/DbReader.Tracking/DbReader.Tracking.csproj index 4b5784c..8271c27 100644 --- a/src/DbReader.Tracking/DbReader.Tracking.csproj +++ b/src/DbReader.Tracking/DbReader.Tracking.csproj @@ -1,33 +1,29 @@ - - + + net8.0 enable enable false - - + - - - + + - - - + \ No newline at end of file diff --git a/src/DbReader.Tracking/DbReader.Tracking.nuspec b/src/DbReader.Tracking/DbReader.Tracking.nuspec index ac0c472..d415713 100644 --- a/src/DbReader.Tracking/DbReader.Tracking.nuspec +++ b/src/DbReader.Tracking/DbReader.Tracking.nuspec @@ -1,35 +1,34 @@ - + - - - DbReader.Tracking - - 0.0.1 - - Bernhard Richter - - A custom MSBuild task package. - - https://github.com/seesharper/DbReader - - MIT - - DbReader Tracking - - true - - - - - - - - - - - - - - - - + + + DbReader.Tracking + + 0.0.1 + + Bernhard Richter + + A custom MSBuild task package. + + https://github.com/seesharper/DbReader + + MIT + + DbReader Tracking + true + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/DbReader/DbConnectionExtensions.cs b/src/DbReader/DbConnectionExtensions.cs index 0c93a1d..042715a 100644 --- a/src/DbReader/DbConnectionExtensions.cs +++ b/src/DbReader/DbConnectionExtensions.cs @@ -116,9 +116,7 @@ public static IDataReader ExecuteReader(this IDbConnection dbConnection, string /// A function used to configure the underlying . /// public static async Task ExecuteReaderAsync(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) - { - return await dbConnection.ExecuteReaderAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false); - } + => await dbConnection.ExecuteReaderAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false); /// /// Executes the given asynchronously and returns an . @@ -162,8 +160,6 @@ public static IDbCommand CreateCommand(this IDbConnection dbConnection, string q return command; } - - /// /// Executes the given and returns the number of rows affected. /// @@ -189,10 +185,7 @@ public static int Execute(this IDbConnection dbConnection, string query, object /// A function used to configure the underlying . /// The number of rows affected. public static async Task ExecuteAsync(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) - { - return await dbConnection.ExecuteAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false); - } - + => await dbConnection.ExecuteAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false); /// /// Executes the given asynchronously and returns the number of rows affected. @@ -221,16 +214,30 @@ public static async Task ExecuteAsync(this IDbConnection dbConnection, Canc /// /// The type of value to be returned. public static async Task ExecuteScalarAsync(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) + => await dbConnection.ExecuteScalarAsync(CancellationToken.None, query, arguments, configureCommand); + + /// + /// Executes the query asynchronously , and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored. + /// + /// The to be used when executing the query. + /// The token to monitor for cancellation requests. + /// The query to be executed. + /// The argument object that represents the arguments passed to the query. + /// A function used to configure the underlying . + /// + /// The type of value to be returned. + public static async Task ExecuteScalarAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null, Action configureCommand = default) { using (var command = (DbCommand)CreateCommand(dbConnection, query, arguments, configureCommand)) { - using (var reader = await command.ExecuteReaderAsync()) + using (var reader = await command.ExecuteReaderAsync(cancellationToken)) { return ReadScalarValue(reader); } } } + /// /// Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored. /// @@ -251,6 +258,31 @@ public static T ExecuteScalar(this IDbConnection dbConnection, string query, } } + /// + /// Executes the given and returns an instance of {T} for the first row or null if no rows."/> that + /// + /// The type to be projected from the query. + /// The target . + /// The query to be executed. + /// An object that represents the query arguments. + /// A function used to configure the underlying . + /// The first instance of {T}/> that represents the result of the query or null if not found. + public static async Task ReadFirstOrDefaultAsync(this IDbConnection dbConnection, string query, object? arguments = null, Action? configureCommand = null) + => await dbConnection.ReadFirstOrDefaultAsync(CancellationToken.None, query, arguments, configureCommand); + + /// + /// Executes the given and returns an {T} for the first row or null if no rows"/> + /// + /// The type to be projected from the query. + /// The target . + /// The token to monitor for cancellation requests. + /// The query to be executed. + /// An object that represents the query arguments. + /// A function used to configure the underlying . + /// The first instance of {T}/> that represents the result of the query or null if not found. + public static async Task ReadFirstOrDefaultAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object? arguments = null, Action? configureCommand = null) + => (await dbConnection.ReadAsync(cancellationToken, query, arguments, configureCommand)).FirstOrDefault(); + private static T ReadScalarValue(IDataReader reader) { if (!reader.Read() || reader.IsDBNull(0)) diff --git a/src/DbReader/DbReader.csproj b/src/DbReader/DbReader.csproj index 5bcde3a..adc7332 100644 --- a/src/DbReader/DbReader.csproj +++ b/src/DbReader/DbReader.csproj @@ -18,8 +18,6 @@ $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb latest - - all diff --git a/src/DbReader/Nuget/DbReader.nuspec b/src/DbReader/Nuget/DbReader.nuspec index d2a9556..cb6acb4 100644 --- a/src/DbReader/Nuget/DbReader.nuspec +++ b/src/DbReader/Nuget/DbReader.nuspec @@ -7,7 +7,7 @@ Bernhard Richter http://opensource.org/licenses/MIT https://github.com/seesharper/DbReader - false + false A simple and fast database reader for the .Net framework. Bernhard Richter data-access orm sql micro-orm