using System.Runtime.CompilerServices;
using Elsa.Extensions;
using Elsa.Sql.Contracts;
using Elsa.Sql.UIHints;
using Elsa.Workflows;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Models;
using Elsa.Workflows.UIHints;
namespace Elsa.Sql.Activities;
///
/// Execute given SQL command and returns the number of rows affected.
///
[Activity("Elsa", "SQL", "Execute given SQL command and returns the number of rows affected.", DisplayName = "SQL Command", Kind = ActivityKind.Task)]
public class SqlCommand : Activity
{
///
///
///
public SqlCommand([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base (source, line)
{
}
///
/// Database client to connect with.
///
[Input(
Description = "Database client.",
UIHint = InputUIHints.DropDown,
UIHandler = typeof(SqlClientsDropDownProvider))]
public Input Client { get; set; } = default!;
///
/// Connection string.
///
[Input(
Description = "Connection string.",
CanContainSecrets = true)]
public Input ConnectionString { get; set; } = default!;
///
/// Command to run against the database.
///
[Input(
Description = "Command to run against the database.",
UIHint = InputUIHints.CodeEditor,
UIHandler = typeof(SqlCodeOptionsProvider)
)]
public Input Command { get; set; } = default!;
///
/// The number of affected rows.
///
[Output(
Description = "The number of rows affected.")]
public Output Result { get; set; } = default!;
///
///
///
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var factory = context.GetRequiredService();
var client = factory.CreateClient(Client.GetOrDefault(context), ConnectionString.GetOrDefault(context));
var result = await client.ExecuteCommandAsync(Command.GetOrDefault(context));
context.Set(Result, result);
await CompleteAsync(context);
}
}