Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
theandrunique committed Jan 27, 2024
1 parent da42643 commit 3e2d29c
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 236 deletions.
17 changes: 9 additions & 8 deletions UniqModel.Tests/SqliteProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Data.Sqlite;
using NLog;
using NLog.Targets;
using UniqModel;
Expand Down Expand Up @@ -112,10 +113,10 @@ public async void SqliteTests()
Assert.Equal("new description", listProfiles[0].Description);

// an error due to a foreign key constraint
//Assert.Throws<SqliteException>(() =>
//{
// session.Delete(listProfiles[0]);
//});
Assert.Throws<SqliteException>(() =>
{
session.Delete(listProfiles[0]);
});

session.Delete(listLogins[0]);
session.Delete(listProfiles[0]);
Expand Down Expand Up @@ -168,10 +169,10 @@ public async void SqliteTests()
Assert.Equal("new name", listProfiles[0].Name);
Assert.Equal("new description", listProfiles[0].Description);

//await Assert.ThrowsAsync<SqliteException>(async () =>
//{
// await session.Delete(listProfiles[0]);
//});
await Assert.ThrowsAsync<SqliteException>(async () =>
{
await session.Delete(listProfiles[0]);
});

await session.Delete(listLogins[0]);
await session.Delete(listProfiles[0]);
Expand Down
86 changes: 42 additions & 44 deletions UniqModel/AsyncSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,32 @@ namespace UniqModel
{
public class AsyncSession : IDisposable
{
public Core DbCore { get { return dbcore; } }
public IDbConnection Connection { get { return conn; } }
public IDbTransaction Transaction { get { return transaction; } }
Core dbcore;
IDbConnection conn;
IDbTransaction transaction;
public bool Expired { get { return expired; } }
private bool expired;
public IDbConnection Connection { get { return _conn; } }
public IDbTransaction Transaction { get { return _transaction; } }
IDbConnection _conn;
IDbTransaction _transaction;
public bool Expired { get { return _expired; } }
private bool _expired;

List<IDataReader> readerPool = new List<IDataReader>();
public AsyncSession(Core dbcore)
List<IDataReader> _readerPool = new List<IDataReader>();
public AsyncSession() { }
async public static Task<AsyncSession> Create()
{
this.dbcore = dbcore;
}
async public static Task<AsyncSession> Create(Core dbcore)
{
var asyncSession = new AsyncSession(dbcore);
var asyncSession = new AsyncSession();

IDbConnection conn = await dbcore.OpenConnectionAsync();
asyncSession.conn = conn;
asyncSession._conn = await CoreImpl.OpenConnectionAsync();
try
{
Logging.Info($"BEGIN (implicit)");
asyncSession.transaction = await dbcore.BeginTransactionAsync(conn);
asyncSession._transaction = await CoreImpl.BeginTransactionAsync(asyncSession._conn);
}
catch (Exception ex)
{
Logging.Error($"Error occurred while starting the transaction. Details: {ex.Message}");
asyncSession.expired = true;
asyncSession._expired = true;
if (UniqSettings.DropErrors)
throw;
return null;
}
return asyncSession;
}
Expand All @@ -44,30 +41,40 @@ public async void Dispose()
await CloseReaders();
try
{
if (dbcore.AutoCommit)
if (UniqSettings.AutoCommit)
{
await Commit();
}
}
catch (Exception ex)
{
Logging.Info($"ROLLBACK ({ex.Message})");
throw;
if (UniqSettings.DropErrors)
throw;
}
finally
{
await dbcore.CloseConnectionAsync(conn);
expired = true;
try
{
await CoreImpl.CloseConnectionAsync(_conn);
}
catch (Exception ex)
{
Logging.Error($"Error occurred while closing the connection. Details: {ex.Message}");
if (UniqSettings.DropErrors)
throw;
}
_expired = true;
}
}
public async Task Commit()
{
await dbcore.CommitTransactionAsync(transaction);
await CoreImpl.CommitTransactionAsync(_transaction);
Logging.Info($"COMMIT");
}
public async Task RollBack()
{
transaction.Rollback();
_transaction.Rollback();
}
async public Task<T> GetById<T>(int id)
{
Expand All @@ -89,55 +96,46 @@ async public Task Add(object newObject)
{
await Crud.CreateAsync(newObject, this);
}
async public Task ExecuteNonQuery(string query)
async public Task Execute(string query, object param = null)
{
CheckIsExpired();
try
{
await dbcore.ExecuteEmptyQueryAsync(query, conn, transaction);
await CoreImpl.ExecuteAsync(query, param, _conn, _transaction);
}
catch
{
if (dbcore.DropErrors)
if (UniqSettings.DropErrors)
throw;
}
}
async public Task<IDataReader> Execute(string query)
{
CheckIsExpired();
try
{
await CloseReaders();
IDataReader reader = await dbcore.ExecuteQueryAsync(query, conn, transaction);
readerPool.Add(reader);
IDataReader reader = await CoreImpl.ExecuteQueryAsync(query, _conn, _transaction);
_readerPool.Add(reader);
return reader;
}
catch
{
if (dbcore.DropErrors)
if (UniqSettings.DropErrors)
throw;
return null;
}
}
async public Task<bool> ReadAsync(IDataReader reader)
{
return await dbcore.ReadReaderAsync(reader);
}
public void CheckIsExpired()
{
if (expired)
{
throw new Exception("The session is closed or expired due to an exception");
}
return await CoreImpl.ReadReaderAsync(reader);
}
private async Task CloseReaders()
{
foreach (IDataReader item in readerPool)
foreach (IDataReader item in _readerPool)
{
if (!item.IsClosed)
await dbcore.CloseReaderAsync(item);
await CoreImpl.CloseReaderAsync(item);
}
readerPool.Clear();
_readerPool.Clear();
}
}
}
Loading

0 comments on commit 3e2d29c

Please sign in to comment.