You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Data/Sql/DatabaseReader.cs

74 lines
1.4 KiB

2 years ago
using Connected.Data.Storage;
using Connected.Entities.Storage;
using System.Collections.Immutable;
using System.Data;
namespace Connected.Data.Sql;
internal class DatabaseReader<T> : DatabaseCommand, IStorageReader<T>
{
public DatabaseReader(IStorageOperation operation, IStorageConnection connection) : base(operation, connection)
{
}
public async Task<ImmutableList<T>?> Query()
{
if (Connection is null)
return default;
try
{
var result = await Connection.Query<T>(this);
if (Connection.Behavior == StorageConnectionMode.Isolated)
await Connection.Commit();
return result;
}
finally
{
if (Connection.Behavior == StorageConnectionMode.Isolated)
{
await Connection.Close();
await Connection.DisposeAsync();
Connection = null;
}
}
}
public async Task<T?> Select()
{
try
{
if (Connection is null)
return default;
var result = await Connection.Select<T>(this);
if (Connection.Behavior == StorageConnectionMode.Isolated)
await Connection.Commit();
return result;
}
finally
{
if (Connection.Behavior == StorageConnectionMode.Isolated)
{
await Connection.Close();
await Connection.DisposeAsync();
Connection = null;
}
}
}
public async Task<IDataReader> OpenReader()
{
if (Connection is null)
return default;
return await Connection.OpenReader(this);
}
}