Skip to content

Commit bbde9b3

Browse files
committed
Refactored SqlServer's SqlGenerator into instance-type
1 parent 58059c5 commit bbde9b3

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer/SqlGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111

1212
namespace Cleipnir.ResilientFunctions.SqlServer;
1313

14-
internal static class SqlGenerator
14+
public class SqlGenerator(string tablePrefix)
1515
{
16-
public static string Interrupt(IEnumerable<StoredId> storedIds, string tableName)
16+
public string Interrupt(IEnumerable<StoredId> storedIds)
1717
{
1818
var conditionals = storedIds
1919
.GroupBy(id => id.Type.Value, id => id.Instance.Value)
2020
.Select(group => $"(FlowType = {group.Key} AND FlowInstance IN ({group.Select(i => $"'{i}'").StringJoin(", ")}))")
2121
.StringJoin(" OR ");
2222

2323
var sql = @$"
24-
UPDATE {tableName}
24+
UPDATE {tablePrefix}
2525
SET
2626
Interrupted = 1,
2727
Status =
@@ -40,7 +40,7 @@ ELSE Expires
4040
return sql;
4141
}
4242

43-
public static string UpdateEffects(SqlCommand command, IReadOnlyList<StoredEffectChange> changes, string tablePrefix, string paramPrefix)
43+
public string UpdateEffects(SqlCommand command, IReadOnlyList<StoredEffectChange> changes, string paramPrefix)
4444
{
4545
var stringBuilder = new StringBuilder(capacity: 2);
4646
var upserts = changes

Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer/SqlServerEffectsStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Cleipnir.ResilientFunctions.SqlServer;
1414

15-
public class SqlServerEffectsStore(string connectionString, string tablePrefix = "") : IEffectsStore
15+
public class SqlServerEffectsStore(string connectionString, SqlGenerator sqlGenerator, string tablePrefix = "") : IEffectsStore
1616
{
1717
private string? _initializeSql;
1818
public async Task Initialize()
@@ -79,7 +79,7 @@ public async Task SetEffectResults(StoredId storedId, IReadOnlyList<StoredEffect
7979
await using var conn = await CreateConnection();
8080
await using var command = new SqlCommand();
8181
command.Connection = conn;
82-
command.CommandText = SqlGenerator.UpdateEffects(command, changes, tablePrefix, paramPrefix: "");
82+
command.CommandText = sqlGenerator.UpdateEffects(command, changes, paramPrefix: "");
8383

8484
await command.ExecuteNonQueryAsync();
8585
}

Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer/SqlServerFunctionStore.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ public class SqlServerFunctionStore : IFunctionStore
3636
public ISemaphoreStore SemaphoreStore => _semaphoreStore;
3737

3838
private readonly SqlServerUnderlyingRegister _underlyingRegister;
39+
40+
private readonly SqlGenerator _sqlGenerator;
3941

4042
public SqlServerFunctionStore(string connectionString, string tablePrefix = "")
4143
{
4244
_tableName = tablePrefix == "" ? "RFunctions" : tablePrefix;
45+
_sqlGenerator = new SqlGenerator(_tableName);
4346

4447
_connFunc = CreateConnection(connectionString);
45-
_messageStore = new SqlServerMessageStore(connectionString, _tableName);
48+
_messageStore = new SqlServerMessageStore(connectionString, _sqlGenerator, _tableName);
4649
_timeoutStore = new SqlServerTimeoutStore(connectionString, _tableName);
4750
_underlyingRegister = new SqlServerUnderlyingRegister(connectionString, _tableName);
48-
_effectsStore = new SqlServerEffectsStore(connectionString, _tableName);
51+
_effectsStore = new SqlServerEffectsStore(connectionString, _sqlGenerator, _tableName);
4952
_correlationStore = new SqlServerCorrelationsStore(connectionString, _tableName);
5053
_semaphoreStore = new SqlServerSemaphoreStore(connectionString, _tableName);
5154
_typeStore = new SqlServerTypeStore(connectionString, _tableName);
@@ -554,7 +557,7 @@ public async Task Interrupt(IEnumerable<StoredId> storedIds)
554557
{
555558
await using var conn = await _connFunc();
556559
await using var cmd = new SqlCommand(
557-
SqlGenerator.Interrupt(storedIds, _tableName),
560+
_sqlGenerator.Interrupt(storedIds),
558561
conn
559562
);
560563
await cmd.ExecuteNonQueryAsync();

Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer/SqlServerMessageStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Cleipnir.ResilientFunctions.SqlServer;
1212

13-
public class SqlServerMessageStore(string connectionString, string tablePrefix = "") : IMessageStore
13+
public class SqlServerMessageStore(string connectionString, SqlGenerator sqlGenerator, string tablePrefix = "") : IMessageStore
1414
{
1515
private string? _initializeSql;
1616
public async Task Initialize()
@@ -55,7 +55,7 @@ public async Task AppendMessages(IReadOnlyList<StoredIdAndMessage> messages, boo
5555
storedIds: messages.Select(msg => msg.StoredId).Distinct().ToList()
5656
);
5757
var storedIds = messages.Select(m => m.StoredId).Distinct();
58-
var interuptsSql = SqlGenerator.Interrupt(storedIds, tablePrefix);
58+
var interuptsSql = sqlGenerator.Interrupt(storedIds);
5959

6060
await using var conn = await CreateConnection();
6161
var sql = @$"

0 commit comments

Comments
 (0)