Changes of DB implementation

This commit is contained in:
Serraniel 2022-11-18 19:51:33 +01:00
parent 91eb7fea58
commit 2a239dba58
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
5 changed files with 41 additions and 36 deletions

View file

@ -4,18 +4,6 @@
<name>SweetLib.Storage</name> <name>SweetLib.Storage</name>
</assembly> </assembly>
<members> <members>
<member name="T:SweetLib.Storage.Database.IConnectionProvider`1">
<summary>
Provider for database connection.
</summary>
<typeparam name="T">Specific type of the database connection. Must inherit <see cref="T:System.Data.Common.DbConnection" />.</typeparam>
</member>
<member name="M:SweetLib.Storage.Database.IConnectionProvider`1.GetConnection">
<summary>
Gets an instance of a database connection.
</summary>
<returns></returns>
</member>
<member name="T:SweetLib.Storage.Database.IDatabaseObject`1"> <member name="T:SweetLib.Storage.Database.IDatabaseObject`1">
<summary> <summary>
Object to store in database. Object to store in database.
@ -48,6 +36,17 @@
</summary> </summary>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:SweetLib.Storage.IConnectionProvider`1">
<summary>
Provider for database connection.
</summary>
</member>
<member name="M:SweetLib.Storage.IConnectionProvider`1.GetConnection">
<summary>
Gets an instance of a database connection.
</summary>
<returns></returns>
</member>
<member name="T:SweetLib.Storage.IIdentified`1"> <member name="T:SweetLib.Storage.IIdentified`1">
<summary> <summary>
Interface for identified objects. Interface for identified objects.

View file

@ -2,7 +2,6 @@
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using SweetLib.Storage.Database; using SweetLib.Storage.Database;
using SweetLib.Storage.MySql.Helper; using SweetLib.Storage.MySql.Helper;
@ -10,7 +9,7 @@ namespace SweetLib.Storage.MySql;
public abstract class CustomMySqlObject<T> : IDatabaseObject<T> public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
{ {
protected CustomMySqlObject(IConnectionProvider<MySqlConnection> connectionProvider, protected CustomMySqlObject(IDatabaseConnectionProvider connectionProvider,
IIdGenerator<T> idGenerator) IIdGenerator<T> idGenerator)
{ {
ConnectionProvider = connectionProvider; ConnectionProvider = connectionProvider;
@ -18,7 +17,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
Id = UnidentifiedId(); Id = UnidentifiedId();
} }
protected IConnectionProvider<MySqlConnection> ConnectionProvider { get; } protected IDatabaseConnectionProvider ConnectionProvider { get; }
protected IIdGenerator<T> IdGenerator { get; } protected IIdGenerator<T> IdGenerator { get; }
public T Id { get; set; } public T Id { get; set; }
@ -50,7 +49,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
} }
finally finally
{ {
await connection.CloseAsync(); connection.Close();
} }
} }
@ -59,7 +58,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
return Id.Equals(UnidentifiedId()); return Id.Equals(UnidentifiedId());
} }
public IEnumerable<string> AssignFieldNames() public virtual IEnumerable<string> AssignFieldNames()
{ {
var result = new List<string>(); var result = new List<string>();
result.Add(IdFieldName()); result.Add(IdFieldName());
@ -67,15 +66,16 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
return result; return result;
} }
public async Task AssignToAsync(DbParameterCollection parameters) public virtual async Task AssignToAsync(DbParameterCollection parameters)
{ {
if (IsNew()) Id = await IdGenerator.GenerateNewIdAsync(); if (IsNew()) Id = await IdGenerator.GenerateNewIdAsync();
parameters.AddMySqlParameterWithValue("@Id", Id); parameters.AddMySqlParameterWithValue("@Id", Id);
} }
public async Task AssignFromAsync(DbDataReader reader) public virtual async Task AssignFromAsync(DbDataReader reader)
{ {
await reader.ReadAsync();
Id = (T)reader["Id"]; Id = (T)reader["Id"];
} }
@ -89,7 +89,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
{ {
var command = connection.CreateCommand(); var command = connection.CreateCommand();
command.CommandText = $"SELECT * FROM {TableName()} WHERE Id=@id"; command.CommandText = $"SELECT * FROM {TableName()} WHERE Id=@id";
command.Parameters.AddWithValue("@id", identifier); command.Parameters.AddMySqlParameterWithValue("@id", identifier);
var reader = await command.ExecuteReaderAsync(); var reader = await command.ExecuteReaderAsync();
if (!reader.HasRows) if (!reader.HasRows)
@ -100,7 +100,7 @@ public abstract class CustomMySqlObject<T> : IDatabaseObject<T>
} }
finally finally
{ {
await connection.CloseAsync(); connection.Close();
} }
} }

View file

@ -1,17 +1,16 @@
using System.Data.Common; using System.Data.Common;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
namespace SweetLib.Storage.MySql.Helper namespace SweetLib.Storage.MySql.Helper;
{
public static class MySqlDbParameterExtensions
{
public static MySqlParameter AddMySqlParameterWithValue(this DbParameterCollection parameterCollection,
string name, object value)
{
var param = new MySqlParameter(name, value);
parameterCollection.Add(param);
return param; public static class MySqlDbParameterExtensions
} {
public static MySqlParameter AddMySqlParameterWithValue(this DbParameterCollection parameterCollection,
string name, object value)
{
var param = new MySqlParameter(name, value);
parameterCollection.Add(param);
return param;
} }
} }

View file

@ -0,0 +1,8 @@
using System.Data.Common;
namespace SweetLib.Storage.Database
{
public interface IDatabaseConnectionProvider : IConnectionProvider<DbConnection>
{
}
}

View file

@ -1,12 +1,11 @@
using System.Data.Common; using System.Data;
namespace SweetLib.Storage.Database namespace SweetLib.Storage
{ {
/// <summary> /// <summary>
/// Provider for database connection. /// Provider for database connection.
/// </summary> /// </summary>
/// <typeparam name="T">Specific type of the database connection. Must inherit <see cref="DbConnection" />.</typeparam> public interface IConnectionProvider<out T> where T : IDbConnection
public interface IConnectionProvider<T> where T : DbConnection
{ {
/// <summary> /// <summary>
/// Gets an instance of a database connection. /// Gets an instance of a database connection.