using System.Runtime.CompilerServices; using Elsa.Expressions.Models; 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 return a single result. /// [Activity("Elsa", "SQL", "Execute given SQL command and return a single result.", DisplayName = "SQL Single Value", Kind = ActivityKind.Task)] public class SqlSingleValue : Activity { /// /// /// public SqlSingleValue([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) { } /// /// Database client to connect with. /// [Input( Description = "Database client.", UIHint = InputUIHints.DropDown, UIHandler = typeof(SqlClientsDropDownProvider))] public Input Client { get; set; } = null!; /// /// Connection string. /// [Input( Description = "Connection string.", CanContainSecrets = true)] public Input ConnectionString { get; set; } = null!; /// /// Command to run against the database. /// [Input( Description = "Query to run against the database.", DefaultSyntax = "Sql", UIHint = InputUIHints.CodeEditor, UIHandler = typeof(SqlCodeOptionsProvider) )] public Input Command { get; set; } = null!; /// /// Command result. /// [Output( Description = "Command result.")] public Output Result { get; set; } = null!; /// /// /// protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) { var command = Command.GetOrDefault(context); // If no command was specified, there's nothing to do. if (string.IsNullOrWhiteSpace(command)) return; // Get and execute the SQL evaluator. var evaluator = context.GetRequiredService(); var evaluatedQuery = await evaluator.EvaluateAsync(command, context.ExpressionExecutionContext, new ExpressionEvaluatorOptions(), context.CancellationToken); // Create client var factory = context.GetRequiredService(); var client = factory.CreateClient(Client.GetOrDefault(context), ConnectionString.GetOrDefault(context)); // Execute command var result = await client.ExecuteScalarAsync(evaluatedQuery); context.Set(Result, result); await CompleteAsync(context); } }