Added policy messages for WS. Fixed DB changes via stores. Updated chat voices messages via WS to use stores.
This commit is contained in:
141
db/Database.cs
141
db/Database.cs
@@ -5,9 +5,7 @@ namespace HermesSocketLibrary.db
|
||||
{
|
||||
public class Database
|
||||
{
|
||||
private NpgsqlDataSource _source;
|
||||
private ServerConfiguration _configuration;
|
||||
|
||||
private readonly NpgsqlDataSource _source;
|
||||
public NpgsqlDataSource DataSource { get => _source; }
|
||||
|
||||
|
||||
@@ -19,111 +17,96 @@ namespace HermesSocketLibrary.db
|
||||
|
||||
public async Task Execute(string sql, IDictionary<string, object>? values, Action<NpgsqlDataReader> reading)
|
||||
{
|
||||
using (var connection = await _source.OpenConnectionAsync())
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection);
|
||||
if (values != null)
|
||||
{
|
||||
using (var command = new NpgsqlCommand(sql, connection))
|
||||
{
|
||||
if (values != null)
|
||||
{
|
||||
foreach (var entry in values)
|
||||
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
||||
}
|
||||
await command.PrepareAsync();
|
||||
foreach (var entry in values)
|
||||
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
||||
}
|
||||
await command.PrepareAsync();
|
||||
|
||||
using (var reader = await command.ExecuteReaderAsync())
|
||||
{
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
reading(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
await using var reader = await command.ExecuteReaderAsync();
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
reading(reader);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Execute(string sql, Action<NpgsqlCommand> action, Action<NpgsqlDataReader> reading)
|
||||
{
|
||||
using (var connection = await _source.OpenConnectionAsync())
|
||||
{
|
||||
using (var command = new NpgsqlCommand(sql, connection))
|
||||
{
|
||||
action(command);
|
||||
await command.PrepareAsync();
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection);
|
||||
action(command);
|
||||
await command.PrepareAsync();
|
||||
|
||||
using (var reader = await command.ExecuteReaderAsync())
|
||||
{
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
reading(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
await using var reader = await command.ExecuteReaderAsync();
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
reading(reader);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> Execute(string sql, IDictionary<string, object>? values)
|
||||
{
|
||||
using (var connection = await _source.OpenConnectionAsync())
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection);
|
||||
if (values != null)
|
||||
{
|
||||
using (var command = new NpgsqlCommand(sql, connection))
|
||||
{
|
||||
if (values != null)
|
||||
{
|
||||
foreach (var entry in values)
|
||||
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
||||
}
|
||||
await command.PrepareAsync();
|
||||
|
||||
return await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
foreach (var entry in values)
|
||||
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
||||
}
|
||||
await command.PrepareAsync();
|
||||
return await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task<int> Execute(string sql, Action<NpgsqlCommand> prepare)
|
||||
{
|
||||
using (var connection = await _source.OpenConnectionAsync())
|
||||
{
|
||||
using (var command = new NpgsqlCommand(sql, connection))
|
||||
{
|
||||
prepare(command);
|
||||
await command.PrepareAsync();
|
||||
|
||||
return await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection);
|
||||
prepare(command);
|
||||
await command.PrepareAsync();
|
||||
return await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task<object?> ExecuteScalar(string sql, IDictionary<string, object>? values = null)
|
||||
{
|
||||
using (var connection = await _source.OpenConnectionAsync())
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection);
|
||||
if (values != null)
|
||||
{
|
||||
using (var command = new NpgsqlCommand(sql, connection))
|
||||
{
|
||||
if (values != null)
|
||||
{
|
||||
foreach (var entry in values)
|
||||
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
||||
}
|
||||
|
||||
await command.PrepareAsync();
|
||||
|
||||
return await command.ExecuteScalarAsync();
|
||||
}
|
||||
foreach (var entry in values)
|
||||
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
||||
}
|
||||
|
||||
await command.PrepareAsync();
|
||||
return await command.ExecuteScalarAsync();
|
||||
}
|
||||
|
||||
public async Task<object?> ExecuteScalarTransaction(string sql, IDictionary<string, object>? values = null)
|
||||
{
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var transaction = await connection.BeginTransactionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection, transaction);
|
||||
if (values != null)
|
||||
{
|
||||
foreach (var entry in values)
|
||||
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
||||
}
|
||||
|
||||
await command.PrepareAsync();
|
||||
var results = await command.ExecuteScalarAsync();
|
||||
await transaction.CommitAsync();
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<object?> ExecuteScalar(string sql, Action<NpgsqlCommand> action)
|
||||
{
|
||||
using (var connection = await _source.OpenConnectionAsync())
|
||||
{
|
||||
using (var command = new NpgsqlCommand(sql, connection))
|
||||
{
|
||||
action(command);
|
||||
await command.PrepareAsync();
|
||||
|
||||
return await command.ExecuteScalarAsync();
|
||||
}
|
||||
}
|
||||
await using var connection = await _source.OpenConnectionAsync();
|
||||
await using var command = new NpgsqlCommand(sql, connection);
|
||||
action(command);
|
||||
await command.PrepareAsync();
|
||||
return await command.ExecuteScalarAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user