1
+ using System . Text ;
1
2
using Cleipnir . ResilientFunctions . Domain ;
2
3
using Cleipnir . ResilientFunctions . Helpers ;
3
4
using Cleipnir . ResilientFunctions . Storage ;
5
+ using Cleipnir . ResilientFunctions . Storage . Utils ;
6
+ using MySqlConnector ;
4
7
5
8
namespace Cleipnir . ResilientFunctions . MariaDb ;
6
9
@@ -34,4 +37,59 @@ ELSE expires
34
37
35
38
return sql ;
36
39
}
40
+
41
+ public static string UpdateEffects ( MySqlCommand command , IReadOnlyList < StoredEffectChange > changes , string tablePrefix )
42
+ {
43
+ var stringBuilder = new StringBuilder ( capacity : 2 ) ;
44
+ var upserts = changes
45
+ . Where ( c => c . Operation == CrudOperation . Upsert )
46
+ . Select ( c => new
47
+ {
48
+ Type = c . StoredId . Type . Value ,
49
+ Instance = c . StoredId . Instance . Value ,
50
+ IdHash = c . EffectId . Value ,
51
+ WorkStatus = ( int ) c . StoredEffect ! . WorkStatus ,
52
+ Result = c . StoredEffect ! . Result ,
53
+ Exception = c . StoredEffect ! . StoredException ,
54
+ EffectId = c . StoredEffect ! . EffectId
55
+ } )
56
+ . ToList ( ) ;
57
+
58
+ var setSql = $@ "
59
+ INSERT INTO { tablePrefix } _effects
60
+ (type, instance, id_hash, status, result, exception, effect_id)
61
+ VALUES
62
+ { "(?, ?, ?, ?, ?, ?, ?)" . Replicate ( upserts . Count ) . StringJoin ( ", " ) }
63
+ ON DUPLICATE KEY UPDATE
64
+ status = VALUES(status), result = VALUES(result), exception = VALUES(exception);" ;
65
+ if ( upserts . Any ( ) )
66
+ stringBuilder . AppendLine ( setSql ) ;
67
+ foreach ( var a in upserts )
68
+ {
69
+ command . Parameters . Add ( new MySqlParameter ( name : null , a . Type ) ) ;
70
+ command . Parameters . Add ( new MySqlParameter ( name : null , a . Instance . ToString ( "N" ) ) ) ;
71
+ command . Parameters . Add ( new MySqlParameter ( name : null , a . IdHash . ToString ( "N" ) ) ) ;
72
+ command . Parameters . Add ( new MySqlParameter ( name : null , a . WorkStatus ) ) ;
73
+ command . Parameters . Add ( new MySqlParameter ( name : null , a . Result ?? ( object ) DBNull . Value ) ) ;
74
+ command . Parameters . Add ( new MySqlParameter ( name : null , JsonHelper . ToJson ( a . Exception ) ?? ( object ) DBNull . Value ) ) ;
75
+ command . Parameters . Add ( new MySqlParameter ( name : null , a . EffectId . Serialize ( ) ) ) ;
76
+ }
77
+
78
+ var removes = changes
79
+ . Where ( c => c . Operation == CrudOperation . Delete )
80
+ . Select ( c => new { Type = c . StoredId . Type . Value , Instance = c . StoredId . Instance . Value , IdHash = c . EffectId . Value } )
81
+ . GroupBy ( a => new { a . Type , a . Instance } , a => a . IdHash )
82
+ . ToList ( ) ;
83
+ var predicates = removes
84
+ . Select ( r =>
85
+ $ "(type = { r . Key . Type } AND instance = '{ r . Key . Instance : N} ' AND id_hash IN ({ r . Select ( id => $ "'{ id : N} '") . StringJoin ( ", " ) } ))")
86
+ . StringJoin ( $ " OR { Environment . NewLine } ") ;
87
+ var removeSql = @$ "
88
+ DELETE FROM { tablePrefix } _effects
89
+ WHERE { predicates } " ;
90
+ if ( removes . Any ( ) )
91
+ stringBuilder . AppendLine ( removeSql ) ;
92
+
93
+ return stringBuilder . ToString ( ) ;
94
+ }
37
95
}
0 commit comments