From 2a239dba585b1bb1ff2c5bb7ffb6127ab0b4e9c1 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 18 Nov 2022 19:51:33 +0100 Subject: [PATCH] Changes of DB implementation --- Docs/SweetLib.Storage | 23 +++++++++---------- SweetLib.Storage.MySql/CustomMySqlObject.cs | 18 +++++++-------- .../Helper/DbParameterExtensions.cs | 21 ++++++++--------- .../Database/IDatabaseConnectionProvider.cs | 8 +++++++ .../{Database => }/IConnectionProvider.cs | 7 +++--- 5 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 SweetLib.Storage/Database/IDatabaseConnectionProvider.cs rename SweetLib.Storage/{Database => }/IConnectionProvider.cs (50%) diff --git a/Docs/SweetLib.Storage b/Docs/SweetLib.Storage index 9256e24..bd24768 100644 --- a/Docs/SweetLib.Storage +++ b/Docs/SweetLib.Storage @@ -4,18 +4,6 @@ SweetLib.Storage - - - Provider for database connection. - - Specific type of the database connection. Must inherit . - - - - Gets an instance of a database connection. - - - Object to store in database. @@ -48,6 +36,17 @@ + + + Provider for database connection. + + + + + Gets an instance of a database connection. + + + Interface for identified objects. diff --git a/SweetLib.Storage.MySql/CustomMySqlObject.cs b/SweetLib.Storage.MySql/CustomMySqlObject.cs index 2db43a2..c1f7f06 100644 --- a/SweetLib.Storage.MySql/CustomMySqlObject.cs +++ b/SweetLib.Storage.MySql/CustomMySqlObject.cs @@ -2,7 +2,6 @@ using System.Data.Common; using System.Linq; using System.Threading.Tasks; -using MySql.Data.MySqlClient; using SweetLib.Storage.Database; using SweetLib.Storage.MySql.Helper; @@ -10,7 +9,7 @@ namespace SweetLib.Storage.MySql; public abstract class CustomMySqlObject : IDatabaseObject { - protected CustomMySqlObject(IConnectionProvider connectionProvider, + protected CustomMySqlObject(IDatabaseConnectionProvider connectionProvider, IIdGenerator idGenerator) { ConnectionProvider = connectionProvider; @@ -18,7 +17,7 @@ public abstract class CustomMySqlObject : IDatabaseObject Id = UnidentifiedId(); } - protected IConnectionProvider ConnectionProvider { get; } + protected IDatabaseConnectionProvider ConnectionProvider { get; } protected IIdGenerator IdGenerator { get; } public T Id { get; set; } @@ -50,7 +49,7 @@ public abstract class CustomMySqlObject : IDatabaseObject } finally { - await connection.CloseAsync(); + connection.Close(); } } @@ -59,7 +58,7 @@ public abstract class CustomMySqlObject : IDatabaseObject return Id.Equals(UnidentifiedId()); } - public IEnumerable AssignFieldNames() + public virtual IEnumerable AssignFieldNames() { var result = new List(); result.Add(IdFieldName()); @@ -67,15 +66,16 @@ public abstract class CustomMySqlObject : IDatabaseObject return result; } - public async Task AssignToAsync(DbParameterCollection parameters) + public virtual async Task AssignToAsync(DbParameterCollection parameters) { if (IsNew()) Id = await IdGenerator.GenerateNewIdAsync(); parameters.AddMySqlParameterWithValue("@Id", Id); } - public async Task AssignFromAsync(DbDataReader reader) + public virtual async Task AssignFromAsync(DbDataReader reader) { + await reader.ReadAsync(); Id = (T)reader["Id"]; } @@ -89,7 +89,7 @@ public abstract class CustomMySqlObject : IDatabaseObject { var command = connection.CreateCommand(); command.CommandText = $"SELECT * FROM {TableName()} WHERE Id=@id"; - command.Parameters.AddWithValue("@id", identifier); + command.Parameters.AddMySqlParameterWithValue("@id", identifier); var reader = await command.ExecuteReaderAsync(); if (!reader.HasRows) @@ -100,7 +100,7 @@ public abstract class CustomMySqlObject : IDatabaseObject } finally { - await connection.CloseAsync(); + connection.Close(); } } diff --git a/SweetLib.Storage.MySql/Helper/DbParameterExtensions.cs b/SweetLib.Storage.MySql/Helper/DbParameterExtensions.cs index 6bbc2f0..1b31a6a 100644 --- a/SweetLib.Storage.MySql/Helper/DbParameterExtensions.cs +++ b/SweetLib.Storage.MySql/Helper/DbParameterExtensions.cs @@ -1,17 +1,16 @@ using System.Data.Common; using MySql.Data.MySqlClient; -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); +namespace SweetLib.Storage.MySql.Helper; - 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; } } \ No newline at end of file diff --git a/SweetLib.Storage/Database/IDatabaseConnectionProvider.cs b/SweetLib.Storage/Database/IDatabaseConnectionProvider.cs new file mode 100644 index 0000000..e88c09b --- /dev/null +++ b/SweetLib.Storage/Database/IDatabaseConnectionProvider.cs @@ -0,0 +1,8 @@ +using System.Data.Common; + +namespace SweetLib.Storage.Database +{ + public interface IDatabaseConnectionProvider : IConnectionProvider + { + } +} \ No newline at end of file diff --git a/SweetLib.Storage/Database/IConnectionProvider.cs b/SweetLib.Storage/IConnectionProvider.cs similarity index 50% rename from SweetLib.Storage/Database/IConnectionProvider.cs rename to SweetLib.Storage/IConnectionProvider.cs index c5caa30..bdf7967 100644 --- a/SweetLib.Storage/Database/IConnectionProvider.cs +++ b/SweetLib.Storage/IConnectionProvider.cs @@ -1,12 +1,11 @@ -using System.Data.Common; +using System.Data; -namespace SweetLib.Storage.Database +namespace SweetLib.Storage { /// /// Provider for database connection. /// - /// Specific type of the database connection. Must inherit . - public interface IConnectionProvider where T : DbConnection + public interface IConnectionProvider where T : IDbConnection { /// /// Gets an instance of a database connection.