Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configureCommand #68

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Install .Net Core
uses: actions/[email protected]
with:
dotnet-version: "7.0.200"
dotnet-version: 8.0.201
- name: Install dotnet-script
run: dotnet tool install dotnet-script --global

Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.100",
"version": "8.0.100",
"rollForward": "latestFeature"
}
}
2 changes: 1 addition & 1 deletion src/DbReader.Tests/DbReader.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="ILVerifier" Version="0.0.1" />
</ItemGroup>
<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
Expand Down
60 changes: 37 additions & 23 deletions src/DbReader/DbConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ static DbConnectionExtensions()
/// <param name="dbConnection">The target <see cref="IDbConnection"/>.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">An object that represents the query arguments.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that represents the result of the query.</returns>
public static IEnumerable<T> Read<T>(this IDbConnection dbConnection, string query, object arguments = null)
public static IEnumerable<T> Read<T>(this IDbConnection dbConnection, string query, object arguments = null, Action<IDbCommand> configureCommand = default)
{
using (var dataReader = dbConnection.ExecuteReader(query, arguments))
using (var dataReader = dbConnection.ExecuteReader(query, arguments, configureCommand))
{
return dataReader.Read<T>();
}
Expand All @@ -55,13 +56,14 @@ public static IEnumerable<T> Read<T>(this IDbConnection dbConnection, string que
/// <param name="dbConnection">The target <see cref="IDbConnection"/>.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">An object that represents the query arguments.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that represents the result of the query.</returns>
public static async Task<IEnumerable<T>> ReadAsync<T>(
this IDbConnection dbConnection,
string query,
object arguments = null)
object arguments = null, Action<IDbCommand> configureCommand = default)
{
return await dbConnection.ReadAsync<T>(CancellationToken.None, query, arguments).ConfigureAwait(false);
return await dbConnection.ReadAsync<T>(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -73,14 +75,15 @@ public static async Task<IEnumerable<T>> ReadAsync<T>(
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">An object that represents the query arguments.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that represents the result of the query.</returns>
public static async Task<IEnumerable<T>> ReadAsync<T>(
this IDbConnection dbConnection,
CancellationToken cancellationToken,
string query,
object arguments = null)
object arguments = null, Action<IDbCommand> 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<T>();
Expand All @@ -93,10 +96,11 @@ public static async Task<IEnumerable<T>> ReadAsync<T>(
/// <param name="dbConnection">The <see cref="IDbConnection"/> to be used when executing the query.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns><see cref="IDataReader"/></returns>
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<IDbCommand> configureCommand = default)
{
using (var command = CreateCommand(dbConnection, query, arguments))
using (var command = CreateCommand(dbConnection, query, arguments, configureCommand))
{
return command.ExecuteReader();
}
Expand All @@ -108,10 +112,11 @@ public static IDataReader ExecuteReader(this IDbConnection dbConnection, string
/// <param name="dbConnection">The <see cref="IDbConnection"/> to be used when executing the query.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns><see cref="IDataReader"/></returns>
public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection dbConnection, string query, object arguments = null)
public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection dbConnection, string query, object arguments = null, Action<IDbCommand> configureCommand = default)
{
return await dbConnection.ExecuteReaderAsync(CancellationToken.None, query, arguments).ConfigureAwait(false);
return await dbConnection.ExecuteReaderAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -121,10 +126,11 @@ public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection dbCo
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns><see cref="IDataReader"/></returns>
public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null)
public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null, Action<IDbCommand> 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);
}
Expand All @@ -136,12 +142,14 @@ public static async Task<IDataReader> ExecuteReaderAsync(this IDbConnection dbCo
/// <param name="dbConnection">The <see cref="IDbConnection"/> to be used when creating the command.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns><see cref="IDbCommand"/></returns>
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<IDbCommand> 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<IDataParameter>().ToArray());

SqlStatement.Current = queryInfo.Query;
Expand All @@ -163,10 +171,11 @@ public static IDbCommand CreateCommand(this IDbConnection dbConnection, string q
/// <param name="dbConnection">The <see cref="IDbConnection"/> to be used when executing the query.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns>The number of rows affected.</returns>
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<IDbCommand> configureCommand = default)
{
using (var command = CreateCommand(dbConnection, query, arguments))
using (var command = CreateCommand(dbConnection, query, arguments, configureCommand))
{
return command.ExecuteNonQuery();
}
Expand All @@ -178,11 +187,13 @@ public static int Execute(this IDbConnection dbConnection, string query, object
/// <param name="dbConnection">The <see cref="IDbConnection"/> to be used when executing the query.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns>The number of rows affected.</returns>
public static async Task<int> ExecuteAsync(this IDbConnection dbConnection, string query, object arguments = null)
public static async Task<int> ExecuteAsync(this IDbConnection dbConnection, string query, object arguments = null, Action<IDbCommand> configureCommand = default)
{
return await dbConnection.ExecuteAsync(CancellationToken.None, query, arguments).ConfigureAwait(false);
return await dbConnection.ExecuteAsync(CancellationToken.None, query, arguments, configureCommand).ConfigureAwait(false);
}


/// <summary>
/// Executes the given <paramref name="query"/> asynchronously and returns the number of rows affected.
Expand All @@ -191,10 +202,11 @@ public static async Task<int> ExecuteAsync(this IDbConnection dbConnection, stri
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <returns>The number of rows affected.</returns>
public static async Task<int> ExecuteAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null)
public static async Task<int> ExecuteAsync(this IDbConnection dbConnection, CancellationToken cancellationToken, string query, object arguments = null, Action<IDbCommand> 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);
}
Expand All @@ -206,11 +218,12 @@ public static async Task<int> ExecuteAsync(this IDbConnection dbConnection, Canc
/// <param name="dbConnection">The <see cref="IDbConnection"/> to be used when executing the query.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <typeparam name="T"></typeparam>
/// <returns>The type of value to be returned.</returns>
public static async Task<T> ExecuteScalarAsync<T>(this IDbConnection dbConnection, string query, object arguments = null)
public static async Task<T> ExecuteScalarAsync<T>(this IDbConnection dbConnection, string query, object arguments = null, Action<IDbCommand> 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())
{
Expand All @@ -225,11 +238,12 @@ public static async Task<T> ExecuteScalarAsync<T>(this IDbConnection dbConnectio
/// <param name="dbConnection">The <see cref="IDbConnection"/> to be used when executing the query.</param>
/// <param name="query">The query to be executed.</param>
/// <param name="arguments">The argument object that represents the arguments passed to the query.</param>
/// <param name="configureCommand">A function used to configure the underlying <see cref="IDbCommand"/>.</param>
/// <typeparam name="T"></typeparam>
/// <returns>The type of value to be returned.</returns>
public static T ExecuteScalar<T>(this IDbConnection dbConnection, string query, object arguments = null)
public static T ExecuteScalar<T>(this IDbConnection dbConnection, string query, object arguments = null, Action<IDbCommand> configureCommand = default)
{
using (var command = CreateCommand(dbConnection, query, arguments))
using (var command = CreateCommand(dbConnection, query, arguments, configureCommand))
{
using (var reader = command.ExecuteReader())
{
Expand Down
Loading