using System.Data; using Dapper; using Elsa.Dapper.Models; namespace Elsa.Dapper.Extensions; /// /// Provides extension methods for to execute queries. /// public static class ParameterizedQueryExtensions { /// /// Execute a query asynchronously using Task. /// /// The query to execute. /// The connection to query on. /// The transaction to use, if any. /// The command timeout (in seconds). /// The type of command to execute. /// The type to return. /// A single row of the query result. public static async Task SingleAsync(this ParameterizedQuery query, IDbConnection connection, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return await connection.QuerySingleAsync(query.Sql.ToString(), query.Parameters, transaction, commandTimeout, commandType); } /// /// Execute a single-row query asynchronously using Task. /// /// The type to return. /// The query to execute. /// The connection to query on. /// The transaction to use, if any. /// The command timeout (in seconds). /// The type of command to execute. /// A single row of the query result. public static async Task SingleOrDefaultAsync(this ParameterizedQuery query, IDbConnection connection, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return await connection.QuerySingleOrDefaultAsync(query.Sql.ToString(), query.Parameters, transaction, commandTimeout, commandType); } /// /// Execute a single-row query asynchronously using Task. /// /// The query to execute. /// The connection to query on. /// The transaction to use, if any. /// The command timeout (in seconds). /// The type of command to execute. /// The type to return. /// A single row of the query result. public static async Task FirstOrDefaultAsync(this ParameterizedQuery query, IDbConnection connection, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return await connection.QueryFirstOrDefaultAsync(query.Sql.ToString(), query.Parameters, transaction, commandTimeout, commandType); } /// /// Execute a query asynchronously using Task. /// /// The query to execute. /// The connection to query on. /// The transaction to use, if any. /// The command timeout (in seconds). /// The type of command to execute. /// The type to return. /// A sequence of data of the supplied type. public static async Task> QueryAsync(this ParameterizedQuery query, IDbConnection connection, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return await connection.QueryAsync(query.Sql.ToString(), query.Parameters, transaction, commandTimeout, commandType); } /// /// Execute a command asynchronously using Task. /// /// The query to execute. /// The connection to query on. /// The transaction to use, if any. /// The command timeout (in seconds). /// The type of command to execute. public static async Task ExecuteAsync(this ParameterizedQuery query, IDbConnection connection, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return await connection.ExecuteAsync(query.Sql.ToString(), query.Parameters, transaction, commandTimeout, commandType); } }