using System.Data;
using Npgsql;

namespace QuantEngine.Infrastructure.Data
{
    public interface IDbConnectionFactory
    {
        IDbConnection CreateConnection();
    }

    public class DbConnectionFactory : IDbConnectionFactory, IDisposable
    {
        private readonly NpgsqlDataSource _dataSource;

        public DbConnectionFactory(string connectionString)
        {
            _dataSource = NpgsqlDataSource.Create(connectionString);
        }

        public DbConnectionFactory(NpgsqlDataSource dataSource)
        {
            _dataSource = dataSource;
        }

        public IDbConnection CreateConnection()
        {
            return _dataSource.CreateConnection();
        }

        public void Dispose()
        {
            _dataSource.Dispose();
        }
    }
}
