7346db3b63
feat: Add SqlServerSchemaProvider for extracting database schema information from SQL Server feat: Introduce DatabaseType and NamingStrategy enums for better database management and naming conventions feat: Create IDatabaseDiscovery and IDatabaseManager interfaces for database operations and metadata retrieval feat: Develop REST service client architecture with BaseRestServiceClient and SAP Business One specific implementation feat: Implement REST service discovery page with UI for connecting to SAP Business One Service Layer and displaying discovered entities
132 lines
3.8 KiB
C#
132 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
using DataConnection.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
|
|
namespace DataConnection.EF;
|
|
|
|
/// <summary>
|
|
/// Implementazione di IExistingDatabaseManager basata su Entity Framework Core
|
|
/// </summary>
|
|
public class EFCoreDatabaseManager : IDatabaseManager
|
|
{
|
|
private readonly DbManagerOptions _options;
|
|
private ExistingDatabaseContext _context;
|
|
|
|
public EFCoreDatabaseManager(DbManagerOptions options)
|
|
{
|
|
_options = options ?? throw new ArgumentNullException(nameof(options));
|
|
InitializeContext();
|
|
}
|
|
|
|
private void InitializeContext()
|
|
{
|
|
var optionsBuilder = new DbContextOptionsBuilder<ExistingDatabaseContext>();
|
|
_options.DbContextConfigurator(optionsBuilder);
|
|
|
|
_context = new ExistingDatabaseContext(
|
|
optionsBuilder.Options,
|
|
_options.ModelConfigurator,
|
|
_options.EnableAutoDiscovery,
|
|
_options.EntityAssembly,
|
|
_options.EntityNamespace,
|
|
_options.NamingStrategy);
|
|
}
|
|
|
|
public async Task<bool> TestConnectionAsync()
|
|
{
|
|
try
|
|
{
|
|
return await _context.Database.CanConnectAsync();
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<T>> GetAsync<T>(
|
|
Expression<Func<T, bool>> filter = null,
|
|
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
|
|
string includeProperties = "",
|
|
int? skip = null,
|
|
int? take = null) where T : class
|
|
{
|
|
IQueryable<T> query = _context.Set<T>();
|
|
|
|
if (filter != null)
|
|
{
|
|
query = query.Where(filter);
|
|
}
|
|
|
|
foreach (var includeProperty in includeProperties.Split
|
|
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
query = query.Include(includeProperty);
|
|
}
|
|
|
|
if (orderBy != null)
|
|
{
|
|
query = orderBy(query);
|
|
}
|
|
|
|
if (skip.HasValue)
|
|
{
|
|
query = query.Skip(skip.Value);
|
|
}
|
|
|
|
if (take.HasValue)
|
|
{
|
|
query = query.Take(take.Value);
|
|
}
|
|
|
|
return await query.ToListAsync();
|
|
}
|
|
|
|
public async Task<T> GetByIdAsync<T>(object id) where T : class
|
|
{
|
|
return await _context.Set<T>().FindAsync(id);
|
|
}
|
|
|
|
public async Task<IEnumerable<T>> ExecuteQueryAsync<T>(string sql, params object[] parameters) where T : class
|
|
{
|
|
return await _context.Set<T>().FromSqlRaw(sql, parameters).ToListAsync();
|
|
}
|
|
|
|
public async Task<int> ExecuteCommandAsync(string sql, params object[] parameters)
|
|
{
|
|
return await _context.Database.ExecuteSqlRawAsync(sql, parameters);
|
|
}
|
|
|
|
public async Task<IDictionary<string, IEnumerable<DbColumnInfo>>> GetDatabaseSchemaAsync()
|
|
{
|
|
try
|
|
{
|
|
// Assicurarsi che il contesto sia connesso
|
|
await _context.Database.OpenConnectionAsync();
|
|
|
|
// 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
|
|
return await schemaProvider.GetDatabaseSchemaAsync(_context.Database.GetConnectionString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Errore nel recupero dello schema del database: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_context?.Dispose();
|
|
}
|
|
}
|