53 lines
1.9 KiB
C#
53 lines
1.9 KiB
C#
using brecal.model;
|
|
using MySqlConnector;
|
|
using System.Data;
|
|
using System.Runtime.CompilerServices;
|
|
using static brecal.model.IDBManager;
|
|
|
|
namespace brecal.mysql
|
|
{
|
|
public class DBManager : IDBManager
|
|
{
|
|
// TODO: remove this and use certificates instead
|
|
private static readonly string _connectionString = "Server=lager;User ID=ds;Password=HalloWach23;Database=bremen_calling";
|
|
|
|
public async Task<List<DbEntity>> Load(QueryFunc prepareAction, LoadFunc<IDataReader> loadAction, params object[] args)
|
|
{
|
|
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
using MySqlCommand cmd = new();
|
|
cmd.Connection = connection;
|
|
prepareAction(cmd, args);
|
|
MySqlDataReader reader = await cmd.ExecuteReaderAsync();
|
|
List<DbEntity> result = loadAction(reader);
|
|
reader.Close();
|
|
return result;
|
|
}
|
|
|
|
public async Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction)
|
|
{
|
|
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
using MySqlCommand cmd = new();
|
|
cmd.Connection = connection;
|
|
prepareAction(cmd);
|
|
object? result = await cmd.ExecuteScalarAsync();
|
|
return result;
|
|
}
|
|
|
|
public async Task<int> ExecuteNonQuery(Action<IDbCommand> prepareAction)
|
|
{
|
|
await using MySqlConnection connection = new(_connectionString);
|
|
await connection.OpenAsync();
|
|
|
|
using MySqlCommand cmd = new();
|
|
cmd.Connection = connection;
|
|
prepareAction(cmd);
|
|
await cmd.ExecuteNonQueryAsync();
|
|
int result = (int)cmd.LastInsertedId;
|
|
return result;
|
|
}
|
|
|
|
}
|
|
} |