hermes-server/db/Database.cs

124 lines
4.8 KiB
C#

using HermesSocketServer;
using Npgsql;
namespace HermesSocketLibrary.db
{
public class Database
{
private readonly NpgsqlDataSource _source;
public NpgsqlDataSource DataSource { get => _source; }
public Database(ServerConfiguration configuration)
{
NpgsqlDataSourceBuilder builder = new NpgsqlDataSourceBuilder(configuration.Database.ConnectionString);
_source = builder.Build();
}
public async Task Execute(string sql, IDictionary<string, object>? values, Action<NpgsqlDataReader> reading)
{
await using var connection = await _source.OpenConnectionAsync();
await 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();
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)
{
await using var connection = await _source.OpenConnectionAsync();
await using var command = new NpgsqlCommand(sql, connection);
action(command);
await command.PrepareAsync();
await using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
reading(reader);
}
}
public async Task<int> Execute(string sql, IDictionary<string, object>? values)
{
await using var connection = await _source.OpenConnectionAsync();
await 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();
}
public async Task<int> Execute(string sql, Action<NpgsqlCommand> prepare)
{
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<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();
await 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();
}
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)
{
await using var connection = await _source.OpenConnectionAsync();
await using var command = new NpgsqlCommand(sql, connection);
action(command);
await command.PrepareAsync();
return await command.ExecuteScalarAsync();
}
}
}