forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbDataProtection.cs
More file actions
43 lines (38 loc) · 1.37 KB
/
Copy pathDbDataProtection.cs
File metadata and controls
43 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Xml.Linq;
using Microsoft.AspNetCore.DataProtection.Repositories;
using Npgsql;
using NpgsqlRest;
namespace NpgsqlRestClient;
public class DbDataProtection(
string? connectionString,
string getCommand,
string storeCommand,
RetryStrategy? cmdRetryStrategy,
ILogger? logger)
: IXmlRepository
{
public IReadOnlyCollection<XElement> GetAllElements()
{
var elements = new List<XElement>();
using var connection = new NpgsqlConnection(connectionString);
connection.Open();
using var cmd = new NpgsqlCommand(getCommand, connection);
using var reader = cmd.ExecuteReaderWithRetry(cmdRetryStrategy, logger);
while (reader.Read())
{
elements.Add(XElement.Parse(reader.GetString(0)));
}
return elements;
}
public void StoreElement(XElement element, string friendlyName)
{
using var connection = new NpgsqlConnection(connectionString);
connection.Open();
using var cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = storeCommand;
cmd.Parameters.Add(new NpgsqlParameter() { Value = friendlyName }); // $1
cmd.Parameters.Add(new NpgsqlParameter() { Value = element.ToString(SaveOptions.DisableFormatting) }); // $2
cmd.ExecuteNonQueryWithRetry(cmdRetryStrategy, logger);
}
}