@@ -17,36 +17,42 @@ namespace Cleipnir.ResilientFunctions.SqlServer;
1717
1818public class SqlGenerator ( string tablePrefix )
1919{
20+ private string ? _interruptSql ;
2021 public StoreCommand Interrupt ( IEnumerable < StoredId > storedIds )
2122 {
22- var sql = @$ "
23+ _interruptSql ?? = @$ "
2324 UPDATE { tablePrefix }
24- SET
25+ SET
2526 Interrupted = 1,
26- Status =
27- CASE
27+ Status =
28+ CASE
2829 WHEN Status = { ( int ) Status . Suspended } THEN { ( int ) Status . Postponed }
2930 ELSE Status
3031 END,
31- Expires =
32+ Expires =
3233 CASE
3334 WHEN Status = { ( int ) Status . Postponed } THEN 0
3435 WHEN Status = { ( int ) Status . Suspended } THEN 0
3536 ELSE Expires
3637 END
37- WHERE Id IN ({ storedIds . Select ( id => $ "' { id . AsGuid } '" ) . StringJoin ( ", " ) } );" ;
38+ WHERE Id IN (SELECT CAST(value AS UNIQUEIDENTIFIER) FROM STRING_SPLIT(@Ids, ',') );" ;
3839
39- return StoreCommand . Create ( sql ) ;
40+ var command = StoreCommand . Create ( _interruptSql ) ;
41+ command . AddParameter ( "@Ids" , storedIds . ToCommaSeparatedIds ( ) ) ;
42+ return command ;
4043 }
4144
45+ private string ? _resetInterruptedSql ;
4246 public StoreCommand ResetInterrupted ( IEnumerable < StoredId > storedIds )
4347 {
44- var sql = @$ "
48+ _resetInterruptedSql ?? = @$ "
4549 UPDATE { tablePrefix }
4650 SET Interrupted = 0
47- WHERE Id IN ({ storedIds . Select ( id => $ "' { id . AsGuid } '" ) . StringJoin ( ", " ) } );" ;
51+ WHERE Id IN (SELECT CAST(value AS UNIQUEIDENTIFIER) FROM STRING_SPLIT(@Ids, ',') );" ;
4852
49- return StoreCommand . Create ( sql ) ;
53+ var command = StoreCommand . Create ( _resetInterruptedSql ) ;
54+ command . AddParameter ( "@Ids" , storedIds . ToCommaSeparatedIds ( ) ) ;
55+ return command ;
5056 }
5157
5258 public StoreCommand InsertEffects ( StoredId storedId , IReadOnlyList < StoredEffectChange > changes , SnapshotStorageSession session , string paramPrefix )
@@ -118,14 +124,16 @@ public async Task<StoredEffectsWithSession> ReadEffects(SqlDataReader reader, Re
118124 return new StoredEffectsWithSession ( effects , session ) ;
119125 }
120126
127+ private string ? _getEffectsBulkSql ;
121128 public StoreCommand GetEffects ( IEnumerable < StoredId > storedIds )
122129 {
123- var sql = @$ "
130+ _getEffectsBulkSql ?? = @$ "
124131 SELECT Id, Effects
125132 FROM { tablePrefix }
126- WHERE Id IN ({ storedIds . InClause ( ) } )" ;
133+ WHERE Id IN (SELECT CAST(value AS UNIQUEIDENTIFIER) FROM STRING_SPLIT(@Ids, ',') )" ;
127134
128- var command = StoreCommand . Create ( sql ) ;
135+ var command = StoreCommand . Create ( _getEffectsBulkSql ) ;
136+ command . AddParameter ( "@Ids" , storedIds . ToCommaSeparatedIds ( ) ) ;
129137 return command ;
130138 }
131139
@@ -467,11 +475,11 @@ public StoreCommand RestartExecutions(IReadOnlyList<StoredId> storedIds, Replica
467475 inserted.Parent,
468476 inserted.Owner,
469477 inserted.Effects
470- WHERE Id IN ({{0}} ) AND Owner IS NULL;" ;
478+ WHERE Id IN (SELECT CAST(value AS UNIQUEIDENTIFIER) FROM STRING_SPLIT(@Ids, ',') ) AND Owner IS NULL;" ;
471479
472- var sql = string . Format ( _restartExecutionsSql , storedIds . InClause ( ) ) ;
473- var storeCommand = StoreCommand . Create ( sql ) ;
480+ var storeCommand = StoreCommand . Create ( _restartExecutionsSql ) ;
474481 storeCommand . AddParameter ( "@Owner" , replicaId . AsGuid ) ;
482+ storeCommand . AddParameter ( "@Ids" , storedIds . ToCommaSeparatedIds ( ) ) ;
475483
476484 return storeCommand ;
477485 }
@@ -637,15 +645,17 @@ public StoreCommand GetMessages(StoredId storedId, IReadOnlyList<long> skipPosit
637645 return messages ;
638646 }
639647
648+ private string ? _getMessagesBulkSql ;
640649 public StoreCommand GetMessages ( IEnumerable < StoredId > storedIds )
641650 {
642- var sql = @$ "
651+ _getMessagesBulkSql ?? = @$ "
643652 SELECT Id, Position, Content
644653 FROM { tablePrefix } _Messages
645- WHERE Id IN ({ storedIds . InClause ( ) } )
654+ WHERE Id IN (SELECT CAST(value AS UNIQUEIDENTIFIER) FROM STRING_SPLIT(@Ids, ',') )
646655 ORDER BY Position;" ;
647656
648- var command = StoreCommand . Create ( sql ) ;
657+ var command = StoreCommand . Create ( _getMessagesBulkSql ) ;
658+ command . AddParameter ( "@Ids" , storedIds . ToCommaSeparatedIds ( ) ) ;
649659 return command ;
650660 }
651661
0 commit comments