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

Implement Customize SqlClientFactory with discovery database types #3781

Merged
merged 2 commits into from
Mar 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
using Elsa.Attributes;
using Elsa.Design;
using Elsa.Expressions;
using Elsa.Metadata;
using Elsa.Services;
using Elsa.Services.Models;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace Elsa.Activities.Sql.Activities
{
Expand All @@ -18,19 +24,33 @@ namespace Elsa.Activities.Sql.Activities
Description = "Execute given SQL command and returned number of rows affected",
Outcomes = new[] { OutcomeNames.Done }
)]
public class ExecuteSqlCommand : Activity
public class ExecuteSqlCommand : Activity, IActivityPropertyOptionsProvider, IRuntimeSelectListProvider
{
/// <summary>
/// Allowed databases to run SQL
/// </summary>
[ActivityInput(
UIHint = ActivityInputUIHints.Dropdown,
Hint = "Allowed databases to run SQL.",
Options = new[] { "", "MSSQL Server", "PostgreSql" },
SupportedSyntaxes = new[] { SyntaxNames.JavaScript, SyntaxNames.Liquid }
Options = typeof(ExecuteSqlCommand),
DefaultSyntax = SyntaxNames.Literal,
SupportedSyntaxes = new[] { SyntaxNames.Literal, SyntaxNames.JavaScript, SyntaxNames.Liquid }
)]
public string? Database { get; set; }

/// <summary>
/// Return options to be used by the designer. The designer will pass back whatever context is provided here.
/// </summary>
public object GetOptions(PropertyInfo property) => new RuntimeSelectListProviderSettings(GetType(), this._sqlClientFactory.Databases);


public ValueTask<SelectList> GetSelectListAsync(object? context = null, CancellationToken cancellationToken = default)
{
var tList = (List<string>)context!;
var items = tList.Select(x => new SelectListItem(x)).ToList();
return new ValueTask<SelectList>(new SelectList(items));
}

/// <summary>
/// SQl script to execute
/// </summary>
Expand Down Expand Up @@ -62,5 +82,7 @@ private IActivityExecutionResult ExecuteCommand()

return Done();
}


}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System.Data;
using Elsa.Activities.Sql.Factory;
using Elsa.Activities.Sql.Models;
using Elsa.ActivityResults;
using Elsa.Attributes;
using Elsa.Design;
using Elsa.Expressions;
using Elsa.Metadata;
using Elsa.Providers.WorkflowStorage;
using Elsa.Services;
using Elsa.Services.Models;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace Elsa.Activities.Sql.Activities
{
Expand All @@ -20,19 +26,32 @@ namespace Elsa.Activities.Sql.Activities
Description = "Execute given SQL query and returned execution result",
Outcomes = new[] { OutcomeNames.Done }
)]
public class ExecuteSqlQuery : Activity
public class ExecuteSqlQuery : Activity, IActivityPropertyOptionsProvider, IRuntimeSelectListProvider
{
/// <summary>
/// Allowed databases to run SQL
/// </summary>
[ActivityInput(
UIHint = ActivityInputUIHints.Dropdown,
Hint = "Allowed databases to run SQL.",
Options = new[] { "MSSQL Server", "PostgreSql" },
DefaultValue = "MSSQL Server",
SupportedSyntaxes = new[] { SyntaxNames.JavaScript, SyntaxNames.Liquid }
Options = typeof(ExecuteSqlQuery),
DefaultSyntax = SyntaxNames.Literal,
SupportedSyntaxes = new[] { SyntaxNames.Literal, SyntaxNames.JavaScript, SyntaxNames.Liquid }
)]
public string? Database { get; set; } = "MSSQL Server";
public string? Database { get; set; }

/// <summary>
/// Return options to be used by the designer. The designer will pass back whatever context is provided here.
/// </summary>
public object GetOptions(PropertyInfo property) => new RuntimeSelectListProviderSettings(GetType(), this._sqlClientFactory.Databases);


public ValueTask<SelectList> GetSelectListAsync(object? context = null, CancellationToken cancellationToken = default)
{
var tList = (List<string>)context!;
var items = tList.Select(x => new SelectListItem(x)).ToList();
return new ValueTask<SelectList>(new SelectList(items));
}

/// <summary>
/// SQl script to execute
Expand All @@ -58,7 +77,7 @@ public class ExecuteSqlQuery : Activity

private readonly ISqlClientFactory _sqlClientFactory;

public ExecuteSqlQuery(ISqlClientFactory sqlClientFactory) => _sqlClientFactory = sqlClientFactory;
public ExecuteSqlQuery(ISqlClientFactory sqlClientFactory) => _sqlClientFactory = sqlClientFactory;

protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context) => ExecuteQuery();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ public static class ServiceCollectionExtensions
{
public static ElsaOptionsBuilder AddSqlServerActivities(this ElsaOptionsBuilder elsa)
{
elsa.Services.AddSingleton<ISqlClientFactory, SqlClientFactory>();
return AddSqlServerActivities<SqlClientFactory>(elsa);
}

public static ElsaOptionsBuilder AddSqlServerActivities<T>(this ElsaOptionsBuilder elsa) where T : class, ISqlClientFactory
{
elsa.Services.AddSingleton<ISqlClientFactory, T>();
elsa.AddActivity<Activities.ExecuteSqlQuery>();
elsa.AddActivity<Activities.ExecuteSqlCommand>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Elsa.Activities.Sql.Client;
using Elsa.Activities.Sql.Models;
using System.Collections.Generic;

namespace Elsa.Activities.Sql.Factory
{
public interface ISqlClientFactory
{
List<string> Databases { get; }

ISqlClient CreateClient(CreateSqlClientModel createSqlClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using Elsa.Activities.Sql.Client.PostgreSqlClient;
using Elsa.Activities.Sql.Models;
using System;
using System.Collections.Generic;

namespace Elsa.Activities.Sql.Factory
{
public class SqlClientFactory : ISqlClientFactory
{
public List<string> Databases => new List<string>() { "MSSQLServer", "MSSQL Server", "PostgreSql" };

public ISqlClient CreateClient(CreateSqlClientModel createSqlClient)
{
return createSqlClient.Database switch
Expand Down