Skip to content

Commit d39e68d

Browse files
committed
Using SqlGenerator for CreateFunction-method for Postgres
1 parent c548994 commit d39e68d

File tree

3 files changed

+75
-24
lines changed

3 files changed

+75
-24
lines changed

Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL/PostgreSqlFunctionStore.cs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ public async Task TruncateTables()
127127
await using var command = new NpgsqlCommand(_truncateTableSql, conn);
128128
await command.ExecuteNonQueryAsync();
129129
}
130-
131-
private string? _createFunctionSql;
130+
132131
public async Task<bool> CreateFunction(
133132
StoredId storedId,
134133
FlowInstance humanInstanceId,
@@ -139,28 +138,16 @@ public async Task<bool> CreateFunction(
139138
StoredId? parent)
140139
{
141140
await using var conn = await CreateConnection();
142-
143-
_createFunctionSql ??= @$"
144-
INSERT INTO {_tableName}
145-
(type, instance, status, param_json, expires, timestamp, human_instance_id, parent)
146-
VALUES
147-
($1, $2, $3, $4, $5, $6, $7, $8)
148-
ON CONFLICT DO NOTHING;";
149-
150-
await using var command = new NpgsqlCommand(_createFunctionSql, conn)
151-
{
152-
Parameters =
153-
{
154-
new() {Value = storedId.Type.Value},
155-
new() {Value = storedId.Instance.Value},
156-
new() {Value = (int) (postponeUntil == null ? Status.Executing : Status.Postponed)},
157-
new() {Value = param == null ? DBNull.Value : param},
158-
new() {Value = postponeUntil ?? leaseExpiration},
159-
new() {Value = timestamp},
160-
new() {Value = humanInstanceId.Value},
161-
new() {Value = parent?.Serialize() ?? (object) DBNull.Value},
162-
}
163-
};
141+
142+
await using var command = _sqlGenerator.CreateFunction(
143+
storedId,
144+
humanInstanceId,
145+
param,
146+
leaseExpiration,
147+
postponeUntil,
148+
timestamp,
149+
parent
150+
).ToNpgsqlCommand(conn);
164151

165152
var affectedRows = await command.ExecuteNonQueryAsync();
166153
return affectedRows == 1;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using Npgsql;
3+
4+
namespace Cleipnir.ResilientFunctions.PostgreSQL;
5+
6+
public class PostgresCommand
7+
{
8+
public required string Sql { get; init; }
9+
public required List<object> Parameters { get; init; }
10+
11+
public NpgsqlCommand ToNpgsqlCommand(NpgsqlConnection conn)
12+
{
13+
var cmd = new NpgsqlCommand(Sql, conn);
14+
foreach (var parameter in Parameters)
15+
cmd.Parameters.Add(new NpgsqlParameter { Value = parameter });
16+
17+
return cmd;
18+
}
19+
20+
public NpgsqlBatchCommand ToNpgsqlBatchCommand()
21+
{
22+
var cmd = new NpgsqlBatchCommand(Sql);
23+
foreach (var parameter in Parameters)
24+
cmd.Parameters.Add(new NpgsqlParameter { Value = parameter });
25+
26+
return cmd;
27+
}
28+
}

Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL/SqlGenerator.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,40 @@ ON CONFLICT (type, instance, id_hash)
9191

9292
return commands;
9393
}
94+
95+
private string? _createFunctionSql;
96+
public PostgresCommand CreateFunction(
97+
StoredId storedId,
98+
FlowInstance humanInstanceId,
99+
byte[]? param,
100+
long leaseExpiration,
101+
long? postponeUntil,
102+
long timestamp,
103+
StoredId? parent)
104+
{
105+
_createFunctionSql ??= @$"
106+
INSERT INTO {tablePrefix}
107+
(type, instance, status, param_json, expires, timestamp, human_instance_id, parent)
108+
VALUES
109+
($1, $2, $3, $4, $5, $6, $7, $8)
110+
ON CONFLICT DO NOTHING;";
111+
112+
113+
var cmd = new PostgresCommand
114+
{
115+
Sql = _createFunctionSql,
116+
Parameters =
117+
[
118+
storedId.Type.Value,
119+
storedId.Instance.Value,
120+
(int)(postponeUntil == null ? Status.Executing : Status.Postponed),
121+
param == null ? DBNull.Value : param,
122+
postponeUntil ?? leaseExpiration,
123+
timestamp,
124+
humanInstanceId.Value,
125+
parent?.Serialize() ?? (object)DBNull.Value
126+
]
127+
};
128+
return cmd;
129+
}
94130
}

0 commit comments

Comments
 (0)