Skip to content

Commit

Permalink
Implement Customize SqlClientFactory with discovery database types (#…
Browse files Browse the repository at this point in the history
…3781)

* Implement Customize SqlClientFactory with discovery database types

* Order usings
  • Loading branch information
mircotamburini authored Mar 11, 2023
1 parent f17b143 commit c25f221
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
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

0 comments on commit c25f221

Please sign in to comment.