From 9dd40d9e9cb8e9b3d0c5429d61b6c7b61685ea4e Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Sun, 2 Jun 2024 14:09:58 +0200 Subject: [PATCH 1/2] Added configureCommand --- src/DbReader/DbConnectionExtensions.cs | 60 ++++++++++++++++---------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/src/DbReader/DbConnectionExtensions.cs b/src/DbReader/DbConnectionExtensions.cs index bce765c..280e852 100644 --- a/src/DbReader/DbConnectionExtensions.cs +++ b/src/DbReader/DbConnectionExtensions.cs @@ -38,10 +38,11 @@ static DbConnectionExtensions() /// The target . /// The query to be executed. /// An object that represents the query arguments. + /// A function used to configure the underlying . /// An that represents the result of the query. - public static IEnumerable Read(this IDbConnection dbConnection, string query, object arguments = null) + public static IEnumerable Read(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { - using (var dataReader = dbConnection.ExecuteReader(query, arguments)) + using (var dataReader = dbConnection.ExecuteReader(query, arguments, configureCommand)) { return dataReader.Read(); } @@ -55,13 +56,14 @@ public static IEnumerable Read(this IDbConnection dbConnection, string que /// The target . /// The query to be executed. /// An object that represents the query arguments. + /// A function used to configure the underlying . /// An that represents the result of the query. public static async Task> ReadAsync( this IDbConnection dbConnection, string query, - object arguments = null) + object arguments = null, Action configureCommand = default) { - return await dbConnection.ReadAsync(CancellationToken.None, query, arguments).ConfigureAwait(false); + return await dbConnection.ReadAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false); } /// @@ -73,14 +75,15 @@ public static async Task> ReadAsync( /// 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 . /// An that represents the result of the query. public static async Task> ReadAsync( this IDbConnection dbConnection, CancellationToken cancellationToken, string query, - object arguments = null) + object arguments = null, Action configureCommand = default) { - using (var dataReader = await dbConnection.ExecuteReaderAsync(cancellationToken, query, arguments).ConfigureAwait(false)) + using (var dataReader = await dbConnection.ExecuteReaderAsync(cancellationToken, query, arguments, configureCommand).ConfigureAwait(false)) { SqlStatement.Current = query; return dataReader.Read(); @@ -93,10 +96,11 @@ public static async Task> ReadAsync( /// The to be used when executing the query. /// The query to be executed. /// The argument object that represents the arguments passed to the query. + /// A function used to configure the underlying . /// - public static IDataReader ExecuteReader(this IDbConnection dbConnection, string query, object arguments = null) + public static IDataReader ExecuteReader(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { - using (var command = CreateCommand(dbConnection, query, arguments)) + using (var command = CreateCommand(dbConnection, query, arguments, configureCommand)) { return command.ExecuteReader(); } @@ -108,10 +112,11 @@ public static IDataReader ExecuteReader(this IDbConnection dbConnection, string /// The to be used when executing the query. /// The query to be executed. /// The argument object that represents the arguments passed to the query. + /// A function used to configure the underlying . /// - public static async Task ExecuteReaderAsync(this IDbConnection dbConnection, string query, object arguments = null) + public static async Task ExecuteReaderAsync(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { - return await dbConnection.ExecuteReaderAsync(CancellationToken.None, query, arguments).ConfigureAwait(false); + return await dbConnection.ExecuteReaderAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false); } /// @@ -121,10 +126,11 @@ public static async Task ExecuteReaderAsync(this IDbConnection dbCo /// 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 . /// - public static async Task ExecuteReaderAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null) + public static async Task ExecuteReaderAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null, Action configureCommand = null) { - using (var command = CreateCommand(dbConnection, query, arguments)) + using (var command = CreateCommand(dbConnection, query, arguments, configureCommand)) { return await ((DbCommand)command).ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); } @@ -136,12 +142,14 @@ public static async Task ExecuteReaderAsync(this IDbConnection dbCo /// The to be used when creating the command. /// The query to be executed. /// The argument object that represents the arguments passed to the query. + /// A function used to configure the underlying . /// - public static IDbCommand CreateCommand(this IDbConnection dbConnection, string query, object arguments = null) + public static IDbCommand CreateCommand(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { var command = dbConnection.CreateCommand(); command.CommandText = query; CommandInitializer?.Invoke(command); + configureCommand?.Invoke(command); var queryInfo = ArgumentParser.Parse(query, arguments, () => command.CreateParameter(), command.Parameters.Cast().ToArray()); SqlStatement.Current = queryInfo.Query; @@ -163,10 +171,11 @@ public static IDbCommand CreateCommand(this IDbConnection dbConnection, string q /// The to be used when executing the query. /// The query to be executed. /// The argument object that represents the arguments passed to the query. + /// A function used to configure the underlying . /// The number of rows affected. - public static int Execute(this IDbConnection dbConnection, string query, object arguments = null) + public static int Execute(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { - using (var command = CreateCommand(dbConnection, query, arguments)) + using (var command = CreateCommand(dbConnection, query, arguments, configureCommand)) { return command.ExecuteNonQuery(); } @@ -178,11 +187,13 @@ public static int Execute(this IDbConnection dbConnection, string query, object /// The to be used when executing the query. /// The query to be executed. /// The argument object that represents the arguments passed to the query. + /// 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) + public static async Task ExecuteAsync(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { - return await dbConnection.ExecuteAsync(CancellationToken.None, query, arguments).ConfigureAwait(false); + return await dbConnection.ExecuteAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false); } + /// /// Executes the given asynchronously and returns the number of rows affected. @@ -191,10 +202,11 @@ public static async Task ExecuteAsync(this IDbConnection dbConnection, stri /// 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 number of rows affected. - public static async Task ExecuteAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null) + public static async Task ExecuteAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null, Action configureCommand = default) { - using (var command = (DbCommand)CreateCommand(dbConnection, query, arguments)) + using (var command = (DbCommand)CreateCommand(dbConnection, query, arguments, configureCommand)) { return await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); } @@ -206,11 +218,12 @@ public static async Task ExecuteAsync(this IDbConnection dbConnection, Canc /// The to be used when executing the query. /// 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, string query, object arguments = null) + public static async Task ExecuteScalarAsync(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { - using (var command = (DbCommand)CreateCommand(dbConnection, query, arguments)) + using (var command = (DbCommand)CreateCommand(dbConnection, query, arguments, configureCommand)) { using (var reader = await command.ExecuteReaderAsync()) { @@ -225,11 +238,12 @@ public static async Task ExecuteScalarAsync(this IDbConnection dbConnectio /// The to be used when executing the query. /// 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 T ExecuteScalar(this IDbConnection dbConnection, string query, object arguments = null) + public static T ExecuteScalar(this IDbConnection dbConnection, string query, object arguments = null, Action configureCommand = default) { - using (var command = CreateCommand(dbConnection, query, arguments)) + using (var command = CreateCommand(dbConnection, query, arguments, configureCommand)) { using (var reader = command.ExecuteReader()) { From 701c30c62a123547bceb1f1e30f91fdf508b655c Mon Sep 17 00:00:00 2001 From: Bernhard Richter Date: Sun, 2 Jun 2024 14:23:15 +0200 Subject: [PATCH 2/2] Use .net 8 --- .github/workflows/main.yml | 2 +- global.json | 2 +- src/DbReader.Tests/DbReader.Tests.csproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8bef4ca..45cefd4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: - name: Install .Net Core uses: actions/setup-dotnet@v2.0.0 with: - dotnet-version: "7.0.200" + dotnet-version: 8.0.201 - name: Install dotnet-script run: dotnet tool install dotnet-script --global diff --git a/global.json b/global.json index 1c7274b..501e79a 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.100", + "version": "8.0.100", "rollForward": "latestFeature" } } \ No newline at end of file diff --git a/src/DbReader.Tests/DbReader.Tests.csproj b/src/DbReader.Tests/DbReader.Tests.csproj index e7a53b1..de2f4ea 100644 --- a/src/DbReader.Tests/DbReader.Tests.csproj +++ b/src/DbReader.Tests/DbReader.Tests.csproj @@ -29,7 +29,7 @@ - net7.0 + net8.0 false