150 lines
5.7 KiB
C#
150 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using DataConnection.Interfaces;
|
|
using DataConnection.EF;
|
|
using DataConnection.EF.DatabaseDiscovery;
|
|
using DataConnection.Enums;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Data_Coupler.Data
|
|
{
|
|
public class DatabaseConnectionService
|
|
{
|
|
private readonly IDatabaseDiscovery _sqlServerDiscovery;
|
|
private readonly IDatabaseDiscovery _mySqlDiscovery;
|
|
private readonly DbManagerOptions _dbManagerOptions;
|
|
private IDatabaseManager _databaseManager;
|
|
|
|
public DatabaseType SelectedDatabaseType { get; set; } = DatabaseType.SqlServer;
|
|
public string ConnectionString { get; set; }
|
|
public List<string> AvailableDatabases { get; private set; } = new List<string>();
|
|
public Dictionary<string, DatabaseInfo> DatabasesInfo { get; private set; } = new Dictionary<string, DatabaseInfo>();
|
|
public string SelectedDatabase { get; set; }
|
|
public bool IsConnected { get; private set; }
|
|
public string ErrorMessage { get; private set; }
|
|
|
|
public DatabaseConnectionService(DbManagerOptions dbManagerOptions)
|
|
{
|
|
_sqlServerDiscovery = new SqlServerDatabaseDiscovery();
|
|
_dbManagerOptions = dbManagerOptions ?? new DbManagerOptions();
|
|
// Se in futuro verrà implementata la discovery MySQL, potremmo aggiungerla qui
|
|
// _mySqlDiscovery = new MySqlDatabaseDiscovery();
|
|
}
|
|
|
|
public IDatabaseDiscovery GetDatabaseDiscovery()
|
|
{
|
|
return SelectedDatabaseType switch
|
|
{
|
|
DatabaseType.SqlServer => _sqlServerDiscovery,
|
|
// DatabaseType.MySql => _mySqlDiscovery,
|
|
_ => throw new NotSupportedException($"Tipo di database non supportato: {SelectedDatabaseType}")
|
|
};
|
|
}
|
|
|
|
public async Task<bool> TestConnectionAsync()
|
|
{
|
|
try
|
|
{
|
|
var discovery = GetDatabaseDiscovery();
|
|
AvailableDatabases = await discovery.GetAvailableDatabasesAsync(ConnectionString, true);
|
|
IsConnected = AvailableDatabases.Any();
|
|
ErrorMessage = IsConnected ? null : "Nessun database trovato sul server.";
|
|
return IsConnected;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
IsConnected = false;
|
|
ErrorMessage = $"Errore di connessione: {ex.Message}";
|
|
if (ex.InnerException != null)
|
|
{
|
|
ErrorMessage += $" - {ex.InnerException.Message}";
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<Dictionary<string, DatabaseInfo>> GetDatabasesInfoAsync()
|
|
{
|
|
try
|
|
{
|
|
var discovery = GetDatabaseDiscovery();
|
|
DatabasesInfo = await discovery.GetDatabasesInfoAsync(ConnectionString, false);
|
|
return DatabasesInfo;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorMessage = $"Errore nel recupero delle informazioni: {ex.Message}";
|
|
return new Dictionary<string, DatabaseInfo>();
|
|
}
|
|
}
|
|
|
|
public string BuildConnectionStringWithDatabase()
|
|
{
|
|
if (string.IsNullOrEmpty(SelectedDatabase))
|
|
return ConnectionString;
|
|
|
|
// Per SQL Server
|
|
if (SelectedDatabaseType == DatabaseType.SqlServer)
|
|
{
|
|
if (ConnectionString.Contains("Initial Catalog=") || ConnectionString.Contains("Database="))
|
|
{
|
|
// Sostituisci il database esistente
|
|
var modifiedString = System.Text.RegularExpressions.Regex.Replace(
|
|
ConnectionString,
|
|
@"(Initial Catalog|Database)=([^;]*)",
|
|
$"$1={SelectedDatabase}");
|
|
|
|
return modifiedString;
|
|
}
|
|
else
|
|
{
|
|
// Aggiungi il database
|
|
return ConnectionString + $";Database={SelectedDatabase}";
|
|
}
|
|
}
|
|
|
|
// Per altri tipi di database, implementare la logica appropriata
|
|
return ConnectionString;
|
|
}
|
|
|
|
public IDatabaseManager GetDatabaseManager()
|
|
{
|
|
if (_databaseManager != null)
|
|
return _databaseManager;
|
|
|
|
// Configura le opzioni del database manager
|
|
_dbManagerOptions.ServerConnectionString = ConnectionString;
|
|
_dbManagerOptions.DatabaseName = SelectedDatabase;
|
|
|
|
// Configura il contesto in base al tipo di database
|
|
_dbManagerOptions.DbContextConfigurator = options =>
|
|
{
|
|
switch (SelectedDatabaseType)
|
|
{
|
|
case DatabaseType.SqlServer:
|
|
options.UseSqlServer(BuildConnectionStringWithDatabase(),
|
|
sqlOptions => sqlOptions.CommandTimeout(_dbManagerOptions.CommandTimeout));
|
|
break;
|
|
// Aggiungi altri tipi di database quando implementati
|
|
default:
|
|
throw new NotSupportedException($"Tipo di database non supportato: {SelectedDatabaseType}");
|
|
}
|
|
};
|
|
|
|
// Crea il database manager
|
|
_databaseManager = new EFCoreDatabaseManager(_dbManagerOptions);
|
|
return _databaseManager;
|
|
}
|
|
|
|
public void DisposeDatabaseManager()
|
|
{
|
|
if (_databaseManager != null)
|
|
{
|
|
_databaseManager.Dispose();
|
|
_databaseManager = null;
|
|
}
|
|
}
|
|
}
|
|
} |