20230818
This commit is contained in:
122
anbs_cpdb/Classes/MySqlEngine.cs
Normal file
122
anbs_cpdb/Classes/MySqlEngine.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using anbs_cp.Database.Interfaces;
|
||||
using anbs_cp.Structs;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using MySqlConnector;
|
||||
|
||||
namespace anbs_cp.Database.Classes;
|
||||
|
||||
public class MySqlEngine: IDbEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="connectionString">Строка подключения базы данных</param>
|
||||
public MySqlEngine (string connectionString) => ConnectionString = connectionString;
|
||||
|
||||
/// <summary>
|
||||
/// Строка подключения базы данных
|
||||
/// </summary>
|
||||
public string ConnectionString { get; set; }
|
||||
|
||||
#region Небезопасные методы
|
||||
/// <summary>
|
||||
/// Выполняем команду
|
||||
/// </summary>
|
||||
/// <param name="sql">Запрос</param>
|
||||
/// <param name="model">Данные запроса</param>
|
||||
/// <returns>Успешно ли (есть ли хотя бы одна затронутая строки) выполнена команда</returns>
|
||||
public async Task<bool> ExecuteAsync (string sql, object model)
|
||||
{
|
||||
// Подключаемся к БД
|
||||
await using MySqlConnection connection = new(ConnectionString);
|
||||
|
||||
// Открываем соединение
|
||||
await connection.OpenAsync();
|
||||
|
||||
// Выполняем команду и выводим результат
|
||||
return await connection.ExecuteAsync(sql, model) > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Запрос на получение одной строки
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Тип передаваемого значения</typeparam>
|
||||
/// <param name="sql">Запрос</param>
|
||||
/// <param name="model">Данные запроса</param>
|
||||
/// <returns>Возвращает тип <see cref="T"/> или значение по умолчанию</returns>
|
||||
public async Task<T> QueryRowAsync<T> (string sql, object model)
|
||||
{
|
||||
// Подключаемся к БД
|
||||
await using MySqlConnection connection = new(ConnectionString);
|
||||
|
||||
// Открываем соединение
|
||||
await connection.OpenAsync();
|
||||
|
||||
// Выполняем запрос и выводим результат
|
||||
return await connection.QueryFirstOrDefaultAsync<T>(sql, model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Запрос
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Тип передаваемого значения</typeparam>
|
||||
/// <param name="sql">Запрос</param>
|
||||
/// <param name="model">Данные запроса</param>
|
||||
/// <returns>Возвращает массив типа <see cref="T"/> или значение по умолчанию</returns>
|
||||
public async Task<IEnumerable<T>> QueryAsync<T> (string sql, object model)
|
||||
{
|
||||
// Подключаемся к БД
|
||||
await using MySqlConnection? connection = new(ConnectionString);
|
||||
|
||||
// Открываем соединение
|
||||
await connection.OpenAsync();
|
||||
|
||||
// Выполняем запрос и выводим результат
|
||||
return await connection.QueryAsync<T>(sql, model);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Получение
|
||||
public async Task<IEnumerable<T>> SelectAsync<T> (string table, string[] columns, List<KeyValue<string, string>> where, string? orderBy = null)
|
||||
{
|
||||
string sql = $@"SELECT
|
||||
{(columns.Any() ? string.Join(", ", columns) : "*")}
|
||||
FROM {table} ";
|
||||
|
||||
List<string> whereCondition = new();
|
||||
|
||||
List<string> paramsValues = new();
|
||||
|
||||
if (where.Any())
|
||||
{
|
||||
whereCondition.AddRange(where.Select(static whereItem => $"{whereItem.Key}=@{whereItem.Key}"));
|
||||
paramsValues.AddRange(where.Select(static whereItem => $"@{whereItem.Key}={whereItem.Value}"));
|
||||
}
|
||||
|
||||
if (whereCondition.Any())
|
||||
sql += $" WHERE {string.Join(", ", whereCondition.ToArray())}";
|
||||
|
||||
if (orderBy != null)
|
||||
sql += $" ORDER BY {orderBy}";
|
||||
|
||||
return await QueryAsync<T>(sql, paramsValues.ToArray());
|
||||
}
|
||||
|
||||
public async Task<T> SelectRowAsync<T> (string table, List<string> columns, KeyValue<string, string> where, string? orderBy = null) => throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
public async Task<int> CountAsync (string table) => throw new NotImplementedException();
|
||||
|
||||
public async Task<TPit> InsertAsync<T, TPit> (string table, T entity, KeyValue<string, TPit> primaryIndex) => throw new NotImplementedException();
|
||||
|
||||
public async Task<bool> InsertUpdateAsync<T> (string table, T entity) => throw new NotImplementedException();
|
||||
|
||||
public async Task<bool> UpdateAsync<T> (string table, T entity, KeyValue<string, string> where) => throw new NotImplementedException();
|
||||
|
||||
public async Task<bool> DeleteAsync<T> (string table, KeyValue<string, string> where) => throw new NotImplementedException();
|
||||
|
||||
public async Task<bool> DeleteRowAsync<T> (string table, KeyValue<string, string> where) => throw new NotImplementedException();
|
||||
}}
|
Reference in New Issue
Block a user