2024-06-24 18:21:59 -04:00
|
|
|
using HermesSocketServer;
|
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
namespace HermesSocketLibrary.db
|
|
|
|
{
|
|
|
|
public class Database
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
private readonly NpgsqlDataSource _source;
|
2024-06-24 18:21:59 -04:00
|
|
|
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)
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var connection = await _source.OpenConnectionAsync();
|
|
|
|
await using var command = new NpgsqlCommand(sql, connection);
|
|
|
|
if (values != null)
|
2024-06-24 18:21:59 -04:00
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
foreach (var entry in values)
|
|
|
|
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
|
|
|
}
|
|
|
|
await command.PrepareAsync();
|
2024-06-24 18:21:59 -04:00
|
|
|
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var reader = await command.ExecuteReaderAsync();
|
|
|
|
while (await reader.ReadAsync())
|
|
|
|
{
|
|
|
|
reading(reader);
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute(string sql, Action<NpgsqlCommand> action, Action<NpgsqlDataReader> reading)
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var connection = await _source.OpenConnectionAsync();
|
|
|
|
await using var command = new NpgsqlCommand(sql, connection);
|
|
|
|
action(command);
|
|
|
|
await command.PrepareAsync();
|
2024-06-24 18:21:59 -04:00
|
|
|
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var reader = await command.ExecuteReaderAsync();
|
|
|
|
while (await reader.ReadAsync())
|
|
|
|
{
|
|
|
|
reading(reader);
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<int> Execute(string sql, IDictionary<string, object>? values)
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var connection = await _source.OpenConnectionAsync();
|
|
|
|
await using var command = new NpgsqlCommand(sql, connection);
|
|
|
|
if (values != null)
|
2024-06-24 18:21:59 -04:00
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
foreach (var entry in values)
|
|
|
|
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
2024-10-21 16:44:20 -04:00
|
|
|
await command.PrepareAsync();
|
|
|
|
return await command.ExecuteNonQueryAsync();
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
|
2024-10-17 15:07:00 -04:00
|
|
|
public async Task<int> Execute(string sql, Action<NpgsqlCommand> prepare)
|
2024-06-24 18:21:59 -04:00
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var connection = await _source.OpenConnectionAsync();
|
|
|
|
await using var command = new NpgsqlCommand(sql, connection);
|
|
|
|
prepare(command);
|
|
|
|
await command.PrepareAsync();
|
|
|
|
return await command.ExecuteNonQueryAsync();
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
|
2024-10-29 08:13:38 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:21:59 -04:00
|
|
|
public async Task<object?> ExecuteScalar(string sql, IDictionary<string, object>? values = null)
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var connection = await _source.OpenConnectionAsync();
|
|
|
|
await using var command = new NpgsqlCommand(sql, connection);
|
|
|
|
if (values != null)
|
2024-06-24 18:21:59 -04:00
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
foreach (var entry in values)
|
|
|
|
command.Parameters.AddWithValue(entry.Key, entry.Value);
|
|
|
|
}
|
2024-06-24 18:21:59 -04:00
|
|
|
|
2024-10-21 16:44:20 -04:00
|
|
|
await command.PrepareAsync();
|
|
|
|
return await command.ExecuteScalarAsync();
|
|
|
|
}
|
2024-06-24 18:21:59 -04:00
|
|
|
|
2024-10-21 16:44:20 -04:00
|
|
|
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);
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
2024-10-21 16:44:20 -04:00
|
|
|
|
|
|
|
await command.PrepareAsync();
|
|
|
|
var results = await command.ExecuteScalarAsync();
|
|
|
|
await transaction.CommitAsync();
|
|
|
|
return results;
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<object?> ExecuteScalar(string sql, Action<NpgsqlCommand> action)
|
|
|
|
{
|
2024-10-21 16:44:20 -04:00
|
|
|
await using var connection = await _source.OpenConnectionAsync();
|
|
|
|
await using var command = new NpgsqlCommand(sql, connection);
|
|
|
|
action(command);
|
|
|
|
await command.PrepareAsync();
|
|
|
|
return await command.ExecuteScalarAsync();
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|