The ExecuteDynamicSqlAsync method now returns an IEnumerable of dictionaries representing query result rows, instead of a JSON string. This allows consumers to work directly with the data in its native .NET object form.
61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using ReC.Domain.QueryOutput;
|
|
using ReC.Domain.Views;
|
|
using System.Text.Json;
|
|
|
|
namespace ReC.Application.Common.Interfaces;
|
|
|
|
public interface IRecDbContext
|
|
{
|
|
#region DbSets
|
|
public DbSet<RecActionView> RecActionViews { get; set; }
|
|
|
|
public DbSet<ProfileView> ProfileViews { get; set; }
|
|
|
|
public DbSet<ResultView> RecResultViews { get; set; }
|
|
|
|
public DbSet<HeaderQueryResult> HeaderQueryResults { get; set; }
|
|
|
|
public DbSet<BodyQueryResult> BodyQueryResults { get; set; }
|
|
|
|
public DbSet<InsertObjectResult> RecResults { get; set; }
|
|
#endregion DbSets
|
|
|
|
public Task<int> SaveChangesAsync(CancellationToken cancel = default);
|
|
|
|
public DatabaseFacade Database { get; }
|
|
}
|
|
|
|
public static class RecDbContextSaveExtensions
|
|
{
|
|
//TODO: Once it is finalized, move it to Common.Infrastructure
|
|
public static async Task<IEnumerable<Dictionary<string, object?>>> ExecuteDynamicSqlAsync(this IRecDbContext context, string sql, CancellationToken cancel = default)
|
|
{
|
|
var result = new List<Dictionary<string, object?>>();
|
|
|
|
using var command = context.Database.GetDbConnection().CreateCommand();
|
|
|
|
command.CommandText = sql;
|
|
|
|
await context.Database.OpenConnectionAsync(cancel);
|
|
|
|
using var reader = await command.ExecuteReaderAsync(cancel);
|
|
while (await reader.ReadAsync(cancel))
|
|
{
|
|
var row = new Dictionary<string, object?>();
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
{
|
|
var columnName = reader.GetName(i);
|
|
var value = reader.IsDBNull(i) ? null : reader.GetValue(i);
|
|
|
|
row[columnName] = value;
|
|
}
|
|
|
|
result.Add(row);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
} |