feat: Corregge la logica di rilevamento database specificato nella connection string
- Modifica IsDatabaseSpecifiedInConnectionString per verificare prima il campo DatabaseName della credenziale - Aggiunge logging dettagliato per debugging del processo di connessione database - Corregge il flusso di connessione per evitare il modale quando il database è già specificato - Migliora la gestione degli errori nel caricamento tabelle dal database specificato - Rimuove codice non raggiungibile nella logica di connessione database Il bug precedente mostrava sempre il modale di selezione database anche quando il database era specificato nel campo DatabaseName della credenziale, ora la verifica segue la logica corretta: 1. Controlla se DatabaseName è valorizzato nella credenziale 2. Solo se vuoto, verifica i parametri Database=/Initial Catalog= nella connection string
This commit is contained in:
@@ -142,7 +142,8 @@ public class EFCoreDatabaseManager : IDatabaseManager
|
||||
{
|
||||
return await _context.Database.ExecuteSqlRawAsync(sql, parameters);
|
||||
}
|
||||
public async Task<IDictionary<string, IEnumerable<DbColumnInfo>>> GetDatabaseSchemaAsync()
|
||||
|
||||
public async Task<IDictionary<string, IEnumerable<DbColumnInfo>>> GetDatabaseSchemaAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -166,7 +167,91 @@ public class EFCoreDatabaseManager : IDatabaseManager
|
||||
Console.WriteLine($"Errore nel recupero dello schema del database: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
} public async Task<IEnumerable<Dictionary<string, object>>> GetAllRecordsAsync(string tableName)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ottiene la lista dei database disponibili sul server
|
||||
/// </summary>
|
||||
/// <returns>Lista dei nomi dei database disponibili</returns>
|
||||
public async Task<List<string>> GetAvailableDatabasesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Usa la factory per ottenere il provider appropriato in base al tipo di database
|
||||
var schemaProvider = DatabaseSchemaProviderFactory.CreateProvider(_options.DatabaseType);
|
||||
|
||||
// Usa il provider per ottenere la lista dei database
|
||||
var connectionString = _context.Database.GetConnectionString();
|
||||
if (connectionString == null)
|
||||
throw new InvalidOperationException("Connection string is null");
|
||||
|
||||
var result = await schemaProvider.GetAvailableDatabasesAsync(connectionString);
|
||||
|
||||
return result.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore nel recupero della lista dei database: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ottiene solo la lista dei nomi delle tabelle disponibili (senza dettagli delle colonne)
|
||||
/// </summary>
|
||||
/// <returns>Lista dei nomi delle tabelle</returns>
|
||||
public async Task<IEnumerable<string>> GetTableNamesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Usa la factory per ottenere il provider appropriato in base al tipo di database
|
||||
var schemaProvider = DatabaseSchemaProviderFactory.CreateProvider(_options.DatabaseType);
|
||||
|
||||
// Usa il provider per ottenere la lista delle tabelle
|
||||
var connectionString = _context.Database.GetConnectionString();
|
||||
if (connectionString == null)
|
||||
throw new InvalidOperationException("Connection string is null");
|
||||
|
||||
var result = await schemaProvider.GetTableNamesAsync(connectionString);
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore nel recupero della lista delle tabelle: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ottiene i dettagli delle colonne per una specifica tabella
|
||||
/// </summary>
|
||||
/// <param name="tableName">Nome della tabella (con schema se necessario)</param>
|
||||
/// <returns>Lista delle informazioni sulle colonne</returns>
|
||||
public async Task<IEnumerable<DbColumnInfo>> GetTableSchemaAsync(string tableName)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Usa la factory per ottenere il provider appropriato in base al tipo di database
|
||||
var schemaProvider = DatabaseSchemaProviderFactory.CreateProvider(_options.DatabaseType);
|
||||
|
||||
// Usa il provider per ottenere lo schema della tabella
|
||||
var connectionString = _context.Database.GetConnectionString();
|
||||
if (connectionString == null)
|
||||
throw new InvalidOperationException("Connection string is null");
|
||||
|
||||
var result = await schemaProvider.GetTableSchemaAsync(connectionString, tableName);
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore nel recupero dello schema della tabella {tableName}: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Dictionary<string, object>>> GetAllRecordsAsync(string tableName)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -226,47 +311,6 @@ public class EFCoreDatabaseManager : IDatabaseManager
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetAvailableDatabasesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var connectionString = _context.Database.GetConnectionString();
|
||||
if (connectionString == null)
|
||||
throw new InvalidOperationException("Connection string is null");
|
||||
|
||||
// Crea una connessione al server (senza specificare il database)
|
||||
var serverConnectionString = GetServerConnectionString(connectionString);
|
||||
|
||||
using var connection = CreateConnection(serverConnectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
|
||||
// Query per ottenere i database disponibili (esclude quelli di sistema)
|
||||
command.CommandText = @"
|
||||
SELECT name
|
||||
FROM sys.databases
|
||||
WHERE state_desc = 'ONLINE'
|
||||
AND name NOT IN ('master', 'tempdb', 'model', 'msdb', 'distribution')
|
||||
ORDER BY name";
|
||||
|
||||
var databases = new List<string>();
|
||||
using var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
databases.Add(reader.GetString(0));
|
||||
}
|
||||
|
||||
return databases;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore nell'ottenere la lista dei database: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ChangeDatabaseAsync(string databaseName)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user