Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d39d3a9

Browse files
committedMar 7, 2025·
Added CreateFunction to SqlGenerator for SqlServerFunctionStore
1 parent 9d3d5e0 commit d39d3a9

File tree

2 files changed

+66
-34
lines changed

2 files changed

+66
-34
lines changed
 

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,58 @@ DELETE FROM {tablePrefix}_effects
105105
return stringBuilder.ToString();
106106
}
107107

108+
private string? _createFunctionSql;
109+
110+
public string CreateFunction(
111+
StoredId storedId,
112+
FlowInstance humanInstanceId,
113+
byte[]? param,
114+
long leaseExpiration,
115+
long? postponeUntil,
116+
long timestamp,
117+
StoredId? parent,
118+
SqlCommand command,
119+
string? paramPrefix)
120+
{
121+
_createFunctionSql ??= @$"
122+
INSERT INTO {tablePrefix}(
123+
FlowType, FlowInstance,
124+
ParamJson,
125+
Status,
126+
Epoch,
127+
Expires,
128+
Timestamp,
129+
HumanInstanceId,
130+
Parent
131+
)
132+
VALUES
133+
(
134+
@FlowType, @flowInstance,
135+
@ParamJson,
136+
@Status,
137+
0,
138+
@Expires,
139+
@Timestamp,
140+
@HumanInstanceId,
141+
@Parent
142+
)";
143+
144+
var sql = _createFunctionSql;
145+
if (paramPrefix != null)
146+
sql = sql.Replace("@", $"@{paramPrefix}");
147+
148+
command.Parameters.AddWithValue($"@{paramPrefix}FlowType", storedId.Type.Value);
149+
command.Parameters.AddWithValue($"@{paramPrefix}FlowInstance", storedId.Instance.Value);
150+
command.Parameters.AddWithValue($"@{paramPrefix}Status", (int)(postponeUntil == null ? Status.Executing : Status.Postponed));
151+
command.Parameters.AddWithValue($"@{paramPrefix}ParamJson", param == null ? SqlBinary.Null : param);
152+
command.Parameters.AddWithValue($"@{paramPrefix}Expires", postponeUntil ?? leaseExpiration);
153+
command.Parameters.AddWithValue($"@{paramPrefix}HumanInstanceId", humanInstanceId.Value);
154+
command.Parameters.AddWithValue($"@{paramPrefix}Timestamp", timestamp);
155+
command.Parameters.AddWithValue($"@{paramPrefix}Parent", parent?.Serialize() ?? (object)DBNull.Value);
156+
157+
return sql;
158+
}
159+
108160
private string? _succeedFunctionSql;
109161
public string SucceedFunction(
110162
StoredId storedId, byte[]? result, long timestamp, int expectedEpoch,

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

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -146,41 +146,21 @@ public async Task<bool> CreateFunction(
146146
await using var conn = await _connFunc();
147147

148148
try
149-
{
150-
_createFunctionSql ??= @$"
151-
INSERT INTO {_tableName}(
152-
FlowType, FlowInstance,
153-
ParamJson,
154-
Status,
155-
Epoch,
156-
Expires,
157-
Timestamp,
158-
HumanInstanceId,
159-
Parent
160-
)
161-
VALUES
162-
(
163-
@FlowType, @flowInstance,
164-
@ParamJson,
165-
@Status,
166-
0,
167-
@Expires,
168-
@Timestamp,
169-
@HumanInstanceId,
170-
@Parent
171-
)";
172-
173-
await using var command = new SqlCommand(_createFunctionSql, conn);
149+
{
150+
await using var command = new SqlCommand();
151+
command.Connection = conn;
152+
command.CommandText = _sqlGenerator.CreateFunction(
153+
storedId,
154+
humanInstanceId,
155+
param,
156+
leaseExpiration,
157+
postponeUntil,
158+
timestamp,
159+
parent,
160+
command,
161+
paramPrefix: null
162+
);
174163

175-
command.Parameters.AddWithValue("@FlowType", storedId.Type.Value);
176-
command.Parameters.AddWithValue("@FlowInstance", storedId.Instance.Value);
177-
command.Parameters.AddWithValue("@Status", (int) (postponeUntil == null ? Status.Executing : Status.Postponed));
178-
command.Parameters.AddWithValue("@ParamJson", param == null ? SqlBinary.Null : param);
179-
command.Parameters.AddWithValue("@Expires", postponeUntil ?? leaseExpiration);
180-
command.Parameters.AddWithValue("@HumanInstanceId", humanInstanceId.Value);
181-
command.Parameters.AddWithValue("@Timestamp", timestamp);
182-
command.Parameters.AddWithValue("@Parent", parent?.Serialize() ?? (object) DBNull.Value);
183-
184164
await command.ExecuteNonQueryAsync();
185165
}
186166
catch (SqlException sqlException) when (sqlException.Number == SqlError.UNIQUENESS_VIOLATION)

0 commit comments

Comments
 (0)
Please sign in to comment.