Added & using prepared statements for stores
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Immutable;
|
||||
using HermesSocketLibrary.db;
|
||||
using HermesSocketServer.Models;
|
||||
|
||||
@@ -23,7 +24,7 @@ namespace HermesSocketServer.Store
|
||||
{ "ttsVoiceId", "VoiceId" },
|
||||
{ "userId", "UserId" },
|
||||
};
|
||||
_generator = new GroupSaveSqlGenerator<ChatterVoice>(ctp);
|
||||
_generator = new GroupSaveSqlGenerator<ChatterVoice>(ctp, _logger);
|
||||
}
|
||||
|
||||
public override async Task Load()
|
||||
@@ -55,46 +56,53 @@ namespace HermesSocketServer.Store
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<bool> Save()
|
||||
public override async Task Save()
|
||||
{
|
||||
int count = 0;
|
||||
string sql = string.Empty;
|
||||
ImmutableList<string>? list = null;
|
||||
|
||||
if (_added.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
count = _added.Count;
|
||||
sql = _generator.GenerateInsertSql("TtsChatVoice", _added.Select(a => _store[a]), ["userId", "chatterId", "ttsVoiceId"]);
|
||||
list = _added.ToImmutableList();
|
||||
_added.Clear();
|
||||
}
|
||||
count = list.Count;
|
||||
sql = _generator.GeneratePreparedInsertSql("TtsChatVoice", count, ["userId", "chatterId", "ttsVoiceId"]);
|
||||
|
||||
_logger.Debug($"TtsChatVoice - Adding {count} rows to database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
_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"]);
|
||||
}
|
||||
if (_modified.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
count = _modified.Count;
|
||||
sql = _generator.GenerateUpdateSql("TtsChatVoice", _modified.Select(m => _store[m]), ["userId", "chatterId"], ["ttsVoiceId"]);
|
||||
list = _modified.ToImmutableList();
|
||||
_modified.Clear();
|
||||
}
|
||||
_logger.Debug($"TtsChatVoice - Modifying {count} rows in database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
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"]);
|
||||
}
|
||||
if (_deleted.Any())
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
count = _deleted.Count;
|
||||
sql = _generator.GenerateDeleteSql("TtsChatVoice", _deleted, ["userId", "chatterId"]);
|
||||
list = _deleted.ToImmutableList();
|
||||
_deleted.Clear();
|
||||
}
|
||||
_logger.Debug($"TtsChatVoice - Deleting {count} rows from database: {sql}");
|
||||
await _database.ExecuteScalarTransaction(sql);
|
||||
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"]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user