Compare commits
No commits in common. "61151bef0ce03c47e183e6211cdfc3c03e78107f" and "94e0d54c31b94772e29c448eef5a0ed86ba182b1" have entirely different histories.
61151bef0c
...
94e0d54c31
@ -1,27 +0,0 @@
|
||||
using HermesSocketServer.Services;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace HermesSocketServer.Requests
|
||||
{
|
||||
public class DeletePolicy : IRequest
|
||||
{
|
||||
public string Name => "delete_policy";
|
||||
public string[] RequiredKeys => ["id"];
|
||||
private ChannelManager _channels;
|
||||
private ILogger _logger;
|
||||
|
||||
public DeletePolicy(ChannelManager channels, ILogger logger)
|
||||
{
|
||||
_channels = channels;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<RequestResult> Grant(string sender, IDictionary<string, object>? data)
|
||||
{
|
||||
var channel = _channels.Get(sender);
|
||||
channel.Policies.Remove(data!["id"].ToString());
|
||||
_logger.Information($"Deleted a policy by id [policy id: {data["id"]}]");
|
||||
return RequestResult.Successful(null);
|
||||
}
|
||||
}
|
||||
}
|
@ -84,11 +84,6 @@ s.AddSingleton<VoiceStore>();
|
||||
s.AddSingleton<UserStore>();
|
||||
|
||||
// Request handlers
|
||||
s.AddSingleton<IRequest, CreatePolicy>();
|
||||
s.AddSingleton<IRequest, CreateTTSUser>();
|
||||
s.AddSingleton<IRequest, CreateTTSVoice>();
|
||||
s.AddSingleton<IRequest, DeletePolicy>();
|
||||
s.AddSingleton<IRequest, DeleteTTSVoice>();
|
||||
s.AddSingleton<IRequest, GetChatterIds>();
|
||||
s.AddSingleton<IRequest, GetConnections>();
|
||||
s.AddSingleton<IRequest, GetDefaultTTSVoice>();
|
||||
@ -101,6 +96,10 @@ s.AddSingleton<IRequest, GetPolicies>();
|
||||
s.AddSingleton<IRequest, GetTTSUsers>();
|
||||
s.AddSingleton<IRequest, GetTTSVoices>();
|
||||
s.AddSingleton<IRequest, GetTTSWordFilters>();
|
||||
s.AddSingleton<IRequest, CreatePolicy>();
|
||||
s.AddSingleton<IRequest, CreateTTSUser>();
|
||||
s.AddSingleton<IRequest, CreateTTSVoice>();
|
||||
s.AddSingleton<IRequest, DeleteTTSVoice>();
|
||||
s.AddSingleton<IRequest, UpdateTTSUser>();
|
||||
s.AddSingleton<IRequest, UpdateTTSVoice>();
|
||||
s.AddSingleton<IRequest, UpdateDefaultTTSVoice>();
|
||||
|
@ -1,4 +1,3 @@
|
||||
using System.Collections.Immutable;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketServer.Models;
|
||||
|
||||
@ -24,7 +23,7 @@ namespace HermesSocketServer.Store
|
||||
{ "ttsVoiceId", "VoiceId" },
|
||||
{ "userId", "UserId" },
|
||||
};
|
||||
_generator = new GroupSaveSqlGenerator<ChatterVoice>(ctp, _logger);
|
||||
_generator = new GroupSaveSqlGenerator<ChatterVoice>(ctp);
|
||||
}
|
||||
|
||||
public override async Task Load()
|
||||
@ -56,53 +55,46 @@ namespace HermesSocketServer.Store
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task Save()
|
||||
public override async Task<bool> Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
ImmutableList<string>? list = null;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _added.ToImmutableList();
|
||||
count = _added.Count;
|
||||
sql = _generator.GenerateInsertSql("TtsChatVoice", _added.Select(a => _store[a]), ["userId", "chatterId", "ttsVoiceId"]);
|
||||
_added.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedInsertSql("TtsChatVoice", count, ["userId", "chatterId", "ttsVoiceId"]);
|
||||
|
||||
_logger.Debug($"User - Adding {count} rows to database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
_logger.Debug($"TtsChatVoice - Adding {count} rows to database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _modified.ToImmutableList();
|
||||
count = _modified.Count;
|
||||
sql = _generator.GenerateUpdateSql("TtsChatVoice", _modified.Select(m => _store[m]), ["userId", "chatterId"], ["ttsVoiceId"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedUpdateSql("TtsChatVoice", count, ["userId", "chatterId"], ["ttsVoiceId"]);
|
||||
|
||||
_logger.Debug($"User - Modifying {count} rows in database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
_logger.Debug($"TtsChatVoice - Modifying {count} rows in database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _deleted.ToImmutableList();
|
||||
count = _deleted.Count;
|
||||
sql = _generator.GenerateDeleteSql("TtsChatVoice", _deleted, ["userId", "chatterId"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedDeleteSql("TtsChatVoice", count, ["userId", "chatterId"]);
|
||||
|
||||
_logger.Debug($"User - Deleting {count} rows from database: {sql}");
|
||||
await _generator.DoPreparedStatement(_database, sql, list, ["id"]);
|
||||
}
|
||||
_logger.Debug($"TtsChatVoice - Deleting {count} rows from database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +1,15 @@
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using HermesSocketLibrary.db;
|
||||
|
||||
namespace HermesSocketServer.Store
|
||||
{
|
||||
public class GroupSaveSqlGenerator<T>
|
||||
{
|
||||
private readonly IDictionary<string, PropertyInfo?> columnPropertyRelations;
|
||||
private readonly Serilog.ILogger _logger;
|
||||
|
||||
public GroupSaveSqlGenerator(IDictionary<string, string> columnsToProperties, Serilog.ILogger logger)
|
||||
public GroupSaveSqlGenerator(IDictionary<string, string> columnsToProperties)
|
||||
{
|
||||
columnPropertyRelations = columnsToProperties.ToDictionary(p => p.Key, p => typeof(T).GetProperty(p.Value));
|
||||
_logger = logger;
|
||||
|
||||
var nullProperties = columnPropertyRelations.Where(p => p.Value == null)
|
||||
.Select(p => columnsToProperties[p.Key]);
|
||||
@ -20,24 +17,6 @@ namespace HermesSocketServer.Store
|
||||
throw new ArgumentException("Some properties do not exist on the values given: " + string.Join(", ", nullProperties));
|
||||
}
|
||||
|
||||
public async Task DoPreparedStatement<V>(Database database, string sql, IEnumerable<V> values, string[] columns)
|
||||
{
|
||||
await database.Execute(sql, (c) =>
|
||||
{
|
||||
var valueCounter = 0;
|
||||
foreach (var value in values)
|
||||
{
|
||||
foreach (var column in columns)
|
||||
{
|
||||
var propValue = columnPropertyRelations[column]!.GetValue(value);
|
||||
var propType = columnPropertyRelations[column]!.PropertyType;
|
||||
c.Parameters.AddWithValue(column.ToLower() + valueCounter, propValue ?? DBNull.Value);
|
||||
}
|
||||
valueCounter++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string GenerateInsertSql(string table, IEnumerable<T> values, IEnumerable<string> columns)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(table))
|
||||
@ -61,7 +40,7 @@ namespace HermesSocketServer.Store
|
||||
{
|
||||
var propValue = columnPropertyRelations[column]!.GetValue(value);
|
||||
var propType = columnPropertyRelations[column]!.PropertyType;
|
||||
WriteValue(sb, propValue ?? DBNull.Value, propType);
|
||||
WriteValue(sb, propValue, propType);
|
||||
sb.Append(",");
|
||||
}
|
||||
sb.Remove(sb.Length - 1, 1)
|
||||
@ -72,37 +51,6 @@ namespace HermesSocketServer.Store
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public string GeneratePreparedInsertSql(string table, int rows, IEnumerable<string> columns)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(table))
|
||||
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
||||
if (columns == null)
|
||||
throw new ArgumentNullException(nameof(columns));
|
||||
if (!columns.Any())
|
||||
throw new ArgumentException("Empty list given.", nameof(columns));
|
||||
|
||||
var ctp = columns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
||||
var sb = new StringBuilder();
|
||||
var columnsLower = columns.Select(c => c.ToLower());
|
||||
sb.Append($"INSERT INTO \"{table}\" (\"{string.Join("\", \"", columns)}\") VALUES ");
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
sb.Append("(");
|
||||
foreach (var column in columnsLower)
|
||||
{
|
||||
sb.Append('@')
|
||||
.Append(column)
|
||||
.Append(row)
|
||||
.Append(", ");
|
||||
}
|
||||
sb.Remove(sb.Length - 2, 2)
|
||||
.Append("),");
|
||||
}
|
||||
sb.Remove(sb.Length - 1, 1)
|
||||
.Append(';');
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public string GenerateUpdateSql(string table, IEnumerable<T> values, IEnumerable<string> keyColumns, IEnumerable<string> updateColumns)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(table))
|
||||
@ -145,44 +93,6 @@ namespace HermesSocketServer.Store
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public string GeneratePreparedUpdateSql(string table, int rows, IEnumerable<string> keyColumns, IEnumerable<string> updateColumns)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(table))
|
||||
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
||||
if (keyColumns == null)
|
||||
throw new ArgumentNullException(nameof(keyColumns));
|
||||
if (!keyColumns.Any())
|
||||
throw new ArgumentException("Empty list given.", nameof(keyColumns));
|
||||
if (updateColumns == null)
|
||||
throw new ArgumentNullException(nameof(updateColumns));
|
||||
if (!updateColumns.Any())
|
||||
throw new ArgumentException("Empty list given.", nameof(updateColumns));
|
||||
|
||||
var columns = keyColumns.Union(updateColumns);
|
||||
var ctp = columns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
||||
var sb = new StringBuilder();
|
||||
sb.Append($"UPDATE \"{table}\" as t SET {string.Join(", ", updateColumns.Select(c => "\"" + c + "\" = c.\"" + c + "\""))} FROM (VALUES ");
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
sb.Append("(");
|
||||
foreach (var column in columns)
|
||||
{
|
||||
sb.Append('@')
|
||||
.Append(column)
|
||||
.Append(row)
|
||||
.Append(", ");
|
||||
}
|
||||
sb.Remove(sb.Length - 2, 2)
|
||||
.Append("),");
|
||||
}
|
||||
sb.Remove(sb.Length - 1, 1)
|
||||
.Append($") AS c(\"{string.Join("\", \"", columns)}\") WHERE ")
|
||||
.Append(string.Join(" AND ", keyColumns.Select(c => "t.\"" + c + "\" = c.\"" + c + "\"")))
|
||||
.Append(";");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public string GenerateDeleteSql(string table, IEnumerable<string> keys, IEnumerable<string> keyColumns)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(table))
|
||||
@ -217,37 +127,6 @@ namespace HermesSocketServer.Store
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public string GeneratePreparedDeleteSql(string table, int rows, IEnumerable<string> keyColumns)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(table))
|
||||
throw new ArgumentException("Value is either null or whitespace-filled.", nameof(table));
|
||||
if (keyColumns == null)
|
||||
throw new ArgumentNullException(nameof(keyColumns));
|
||||
if (!keyColumns.Any())
|
||||
throw new ArgumentException("Empty list given.", nameof(keyColumns));
|
||||
|
||||
var ctp = keyColumns.ToDictionary(c => c, c => columnPropertyRelations[c]);
|
||||
var sb = new StringBuilder();
|
||||
sb.Append($"DELETE FROM \"{table}\" WHERE (\"{string.Join("\", \"", keyColumns)}\") IN (");
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
sb.Append("(");
|
||||
foreach (var column in keyColumns)
|
||||
{
|
||||
sb.Append('@')
|
||||
.Append(column)
|
||||
.Append(row)
|
||||
.Append(", ");
|
||||
}
|
||||
sb.Remove(sb.Length - 2, 2)
|
||||
.Append("),");
|
||||
}
|
||||
sb.Remove(sb.Length - 1, 1)
|
||||
.Append(");");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void WriteValue(StringBuilder sb, object? value, Type type)
|
||||
{
|
||||
if (type == typeof(string))
|
||||
@ -255,9 +134,9 @@ namespace HermesSocketServer.Store
|
||||
.Append(value)
|
||||
.Append("'");
|
||||
else if (type == typeof(Guid))
|
||||
sb.Append("uuid('")
|
||||
sb.Append("'")
|
||||
.Append(value?.ToString())
|
||||
.Append("')");
|
||||
.Append("'");
|
||||
else if (type == typeof(TimeSpan))
|
||||
sb.Append(((TimeSpan)value).TotalMilliseconds);
|
||||
else
|
||||
|
@ -28,7 +28,7 @@ namespace HermesSocketServer.Store
|
||||
protected abstract void OnInitialAdd(K key, V value);
|
||||
protected abstract void OnInitialModify(K key, V value);
|
||||
protected abstract void OnInitialRemove(K key);
|
||||
public abstract Task Save();
|
||||
public abstract Task<bool> Save();
|
||||
|
||||
public V? Get(K key)
|
||||
{
|
||||
|
@ -7,7 +7,7 @@ namespace HermesSocketServer.Store
|
||||
Task Load();
|
||||
bool Modify(K? key, Action<V> action);
|
||||
void Remove(K? key);
|
||||
Task Save();
|
||||
Task<bool> Save();
|
||||
bool Set(K? key, V? value);
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
using System.Collections.Immutable;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketServer.Models;
|
||||
|
||||
@ -27,7 +26,7 @@ namespace HermesSocketServer.Store
|
||||
{ "count", "Usage" },
|
||||
{ "timespan", "Span" },
|
||||
};
|
||||
_generator = new GroupSaveSqlGenerator<PolicyMessage>(ctp, _logger);
|
||||
_generator = new GroupSaveSqlGenerator<PolicyMessage>(ctp);
|
||||
}
|
||||
|
||||
public override async Task Load()
|
||||
@ -62,53 +61,46 @@ namespace HermesSocketServer.Store
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task Save()
|
||||
public override async Task<bool> Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
ImmutableList<string>? list = null;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _added.ToImmutableList();
|
||||
count = _added.Count;
|
||||
sql = _generator.GenerateInsertSql("GroupPermissionPolicy", _added.Select(a => _store[a]), ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
_added.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedInsertSql("GroupPermissionPolicy", count, ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
|
||||
_logger.Debug($"GroupPermissionPolicy - Adding {count} rows to database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _modified.ToImmutableList();
|
||||
count = _modified.Count;
|
||||
sql = _generator.GenerateUpdateSql("GroupPermissionPolicy", _modified.Select(m => _store[m]), ["id"], ["userId", "groupId", "path", "count", "timespan"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedUpdateSql("GroupPermissionPolicy", count, ["id"], ["userId", "groupId", "path", "count", "timespan"]);
|
||||
|
||||
_logger.Debug($"GroupPermissionPolicy - Modifying {count} rows in database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "userId", "groupId", "path", "count", "timespan"]);
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _deleted.ToImmutableList();
|
||||
count = _deleted.Count;
|
||||
sql = _generator.GenerateDeleteSql("GroupPermissionPolicy", _deleted, ["id"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedDeleteSql("GroupPermissionPolicy", count, ["id"]);
|
||||
|
||||
_logger.Debug($"GroupPermissionPolicy - Deleting {count} rows from database: {sql}");
|
||||
await _generator.DoPreparedStatement(_database, sql, list, ["id"]);
|
||||
}
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
using System.Collections.Immutable;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketServer.Models;
|
||||
|
||||
@ -24,7 +23,7 @@ namespace HermesSocketServer.Store
|
||||
{ "role", "Role" },
|
||||
{ "ttsDefaultVoice", "DefaultVoice" }
|
||||
};
|
||||
_generator = new GroupSaveSqlGenerator<User>(ctp, _logger);
|
||||
_generator = new GroupSaveSqlGenerator<User>(ctp);
|
||||
}
|
||||
|
||||
public override async Task Load()
|
||||
@ -57,53 +56,45 @@ namespace HermesSocketServer.Store
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task Save()
|
||||
public override async Task<bool> Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
ImmutableList<string>? list = null;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _added.ToImmutableList();
|
||||
count = _added.Count;
|
||||
sql = _generator.GenerateInsertSql("User", _added.Select(a => _store[a]), ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
_added.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedInsertSql("User", count, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
|
||||
_logger.Debug($"User - Adding {count} rows to database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _modified.ToImmutableList();
|
||||
count = _modified.Count;
|
||||
sql = _generator.GenerateUpdateSql("User", _modified.Select(m => _store[m]), ["id"], ["name", "email", "role", "ttsDefaultVoice"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedUpdateSql("User", count, ["id"], ["name", "email", "role", "ttsDefaultVoice"]);
|
||||
|
||||
_logger.Debug($"User - Modifying {count} rows in database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _deleted.ToImmutableList();
|
||||
count = _deleted.Count;
|
||||
sql = _generator.GenerateDeleteSql("User", _deleted, ["id"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedDeleteSql("User", count, ["id"]);
|
||||
|
||||
_logger.Debug($"User - Deleting {count} rows from database: {sql}");
|
||||
await _generator.DoPreparedStatement(_database, sql, list, ["id"]);
|
||||
}
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
using System.Collections.Immutable;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketServer.Models;
|
||||
using HermesSocketServer.Validators;
|
||||
@ -26,7 +25,7 @@ namespace HermesSocketServer.Store
|
||||
{ "id", "Id" },
|
||||
{ "name", "Name" }
|
||||
};
|
||||
_generator = new GroupSaveSqlGenerator<Voice>(ctp, _logger);
|
||||
_generator = new GroupSaveSqlGenerator<Voice>(ctp);
|
||||
}
|
||||
|
||||
public override async Task Load()
|
||||
@ -59,53 +58,46 @@ namespace HermesSocketServer.Store
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task Save()
|
||||
public override async Task<bool> Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
ImmutableList<string>? list = null;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _added.ToImmutableList();
|
||||
count = _added.Count;
|
||||
sql = _generator.GenerateInsertSql("TtsVoice", _added.Select(a => _store[a]), ["id", "name"]);
|
||||
_added.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedInsertSql("TtsVoice", count, ["id", "name"]);
|
||||
|
||||
_logger.Debug($"User - Adding {count} rows to database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
_logger.Debug($"TtsVoice - Adding {count} rows to database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _modified.ToImmutableList();
|
||||
count = _modified.Count;
|
||||
sql = _generator.GenerateUpdateSql("TtsVoice", _modified.Select(m => _store[m]), ["id"], ["name"]);
|
||||
_modified.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedUpdateSql("TtsVoice", count, ["id"], ["name"]);
|
||||
|
||||
_logger.Debug($"User - Modifying {count} rows in database: {sql}");
|
||||
var values = list.Select(id => _store[id]).Where(v => v != null);
|
||||
await _generator.DoPreparedStatement(_database, sql, values, ["id", "name", "email", "role", "ttsDefaultVoice"]);
|
||||
_logger.Debug($"TtsVoice - Modifying {count} rows in database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
list = _deleted.ToImmutableList();
|
||||
count = _deleted.Count;
|
||||
sql = _generator.GenerateDeleteSql("TtsVoice", _deleted, ["id"]);
|
||||
_deleted.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedDeleteSql("TtsVoice", count, ["id"]);
|
||||
|
||||
_logger.Debug($"User - Deleting {count} rows from database: {sql}");
|
||||
await _generator.DoPreparedStatement(_database, sql, list, ["id"]);
|
||||
}
|
||||
_logger.Debug($"TtsVoice - Deleting {count} rows from database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -69,18 +69,6 @@ namespace HermesSocketLibrary.db
|
||||
return await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task<int> ExecuteTransaction(string sql, Action<NpgsqlCommand> prepare)
|
||||
{
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var transaction = await connection.BeginTransactionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection, transaction);
|
||||
prepare(command);
|
||||
await command.PrepareAsync();
|
||||
var results = await command.ExecuteNonQueryAsync();
|
||||
await transaction.CommitAsync();
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<object?> ExecuteScalar(string sql, IDictionary<string, object>? values = null)
|
||||
{
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
|
Loading…
Reference in New Issue
Block a user