chore: refactor for sonarqube issues
SonarQube Analysis / sonarqube (pull_request) Successful in 6m3s

This commit is contained in:
Thom Lamb
2026-05-22 17:13:31 -05:00
parent 063d5a7ba0
commit 012e693fe1
22 changed files with 522 additions and 461 deletions
@@ -64,10 +64,15 @@ public static class DbContextExtensions
/// <param name="context">The DbContext instance.</param>
/// <param name="id">The ID of the QueryBreakdownEntity to retrieve.</param>
/// <returns>The QueryBreakdownEntity with related entities included, or null if not found.</returns>
public static async Task<QueryBreakdownEntity?> GetQueryBreakdownWithRelatedDataAsync(this DbContext context, int id)
public static Task<QueryBreakdownEntity?> GetQueryBreakdownWithRelatedDataAsync(this DbContext context, int id)
{
ArgumentNullException.ThrowIfNull(context);
return GetQueryBreakdownWithRelatedDataCoreAsync(context, id);
}
private static async Task<QueryBreakdownEntity?> GetQueryBreakdownWithRelatedDataCoreAsync(DbContext context, int id)
{
return await context.Set<QueryBreakdownEntity>()
.FirstOrDefaultAsync(q => q.Id == id);
}
@@ -3,6 +3,7 @@ using System.Text.Json;
using Strata.SqlTools.Breakdowns.SqlServer;
using Strata.SqlTools.EFCore.Abstractions;
using Strata.SqlTools.EFCore.Models;
using Strata.SqlTools.SqlBreakdown.Classes;
namespace Strata.SqlTools.EFCore.Services;
@@ -54,41 +55,12 @@ public class QueryBreakdownMapper : IQueryBreakdownMapper
var queryBreakdown = new QueryBreakdown();
// Set clause properties
if (!string.IsNullOrEmpty(entity.SelectClause))
{
queryBreakdown.SelectClause.Clause = entity.SelectClause;
queryBreakdown.SelectClause.Comment = entity.SelectClauseComment;
}
if (!string.IsNullOrEmpty(entity.FromClause))
{
queryBreakdown.FromClause.Clause = entity.FromClause;
queryBreakdown.FromClause.Comment = entity.FromClauseComment;
}
if (!string.IsNullOrEmpty(entity.WhereClause))
{
queryBreakdown.WhereClause.Clause = entity.WhereClause;
queryBreakdown.WhereClause.Comment = entity.WhereClauseComment;
}
if (!string.IsNullOrEmpty(entity.GroupByClause))
{
queryBreakdown.GroupByClause.Clause = entity.GroupByClause;
queryBreakdown.GroupByClause.Comment = entity.GroupByClauseComment;
}
if (!string.IsNullOrEmpty(entity.HavingClause))
{
queryBreakdown.HavingClause.Clause = entity.HavingClause;
queryBreakdown.HavingClause.Comment = entity.HavingClauseComment;
}
if (!string.IsNullOrEmpty(entity.OrderByClause))
{
queryBreakdown.OrderByClause.Clause = entity.OrderByClause;
queryBreakdown.OrderByClause.Comment = entity.OrderByClauseComment;
}
ApplyClause(queryBreakdown.SelectClause, entity.SelectClause, entity.SelectClauseComment);
ApplyClause(queryBreakdown.FromClause, entity.FromClause, entity.FromClauseComment);
ApplyClause(queryBreakdown.WhereClause, entity.WhereClause, entity.WhereClauseComment);
ApplyClause(queryBreakdown.GroupByClause, entity.GroupByClause, entity.GroupByClauseComment);
ApplyClause(queryBreakdown.HavingClause, entity.HavingClause, entity.HavingClauseComment);
ApplyClause(queryBreakdown.OrderByClause, entity.OrderByClause, entity.OrderByClauseComment);
if (!string.IsNullOrEmpty(entity.WithClause))
{
@@ -194,32 +166,55 @@ public class QueryBreakdownMapper : IQueryBreakdownMapper
? paramEntity.ParameterName
: $"@{paramEntity.ParameterName}";
// Deserialize value if type information is available
object? value = paramEntity.ParameterValue;
if (!string.IsNullOrEmpty(paramEntity.ParameterTypeName) && !string.IsNullOrEmpty(paramEntity.ParameterValue))
{
var type = Type.GetType(paramEntity.ParameterTypeName);
if (type != null)
{
try
{
value = Convert.ChangeType(paramEntity.ParameterValue, type);
}
catch
{
// If conversion fails, use string value
value = paramEntity.ParameterValue;
}
}
}
queryBreakdown.Parameters[key] = value;
queryBreakdown.Parameters[key] = DeserializeParameterValue(paramEntity);
}
}
return queryBreakdown;
}
/// <summary>
/// Applies a stored clause value and comment to a target clause, leaving it untouched when the value is empty.
/// </summary>
private static void ApplyClause(ISqlClause target, string? value, string? comment)
{
if (string.IsNullOrEmpty(value))
{
return;
}
target.Clause = value;
target.Comment = comment;
}
/// <summary>
/// Reconstructs a parameter value from its stored string and type name, falling back to the raw
/// string when no type information is available or the conversion fails.
/// </summary>
private static object? DeserializeParameterValue(QueryParameterEntity paramEntity)
{
if (string.IsNullOrEmpty(paramEntity.ParameterTypeName) || string.IsNullOrEmpty(paramEntity.ParameterValue))
{
return paramEntity.ParameterValue;
}
var type = Type.GetType(paramEntity.ParameterTypeName);
if (type == null)
{
return paramEntity.ParameterValue;
}
try
{
return Convert.ChangeType(paramEntity.ParameterValue, type);
}
catch
{
// If conversion fails, use string value
return paramEntity.ParameterValue;
}
}
#region Helper Methods
internal static string SerializeList(List<string> list)
@@ -90,10 +90,15 @@ public class QueryBreakdownRepository : IQueryBreakdownRepository
/// <summary>
/// Adds a new QueryBreakdown to the repository and saves changes.
/// </summary>
public async Task<int> AddAsync(QueryBreakdown queryBreakdown)
public Task<int> AddAsync(QueryBreakdown queryBreakdown)
{
ArgumentNullException.ThrowIfNull(queryBreakdown);
return AddCoreAsync(queryBreakdown);
}
private async Task<int> AddCoreAsync(QueryBreakdown queryBreakdown)
{
var (entity, parameters, withClauses) = _mapper.MapToEntityWithRelations(queryBreakdown);
// Add the main entity
@@ -159,10 +164,15 @@ public class QueryBreakdownRepository : IQueryBreakdownRepository
/// <summary>
/// Updates an existing QueryBreakdown and saves changes.
/// </summary>
public async Task UpdateAsync(int id, QueryBreakdown queryBreakdown)
public Task UpdateAsync(int id, QueryBreakdown queryBreakdown)
{
ArgumentNullException.ThrowIfNull(queryBreakdown);
return UpdateCoreAsync(id, queryBreakdown);
}
private async Task UpdateCoreAsync(int id, QueryBreakdown queryBreakdown)
{
var entity = await _context.Set<QueryBreakdownEntity>().FirstOrDefaultAsync(e => e.Id == id);
if (entity == null)
{