mirror of
https://github.com/ACEmulator/ACE
synced 2026-08-17 12:26:06 -04:00
* packages updated, fix compiler errors/warnings * scaffold * fix ShardDatabaseOfflineTools * WorldDatabase fix performance * ShardDatabase fix GetCharacters * move comment stuff * extend UseMySQL statements * fix character creation linq parsing * fix exception * fix references
30 lines
1.6 KiB
C#
30 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
using Microsoft.EntityFrameworkCore.Query;
|
|
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
|
|
|
|
|
|
namespace ACE.Database.Extensions
|
|
{
|
|
public static class QueryableExtensions
|
|
{
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public static string ToSql<TEntity>(this IQueryable<TEntity> query)
|
|
{
|
|
var enumerator = query.Provider.Execute<IEnumerable<TEntity>>(query.Expression).GetEnumerator();
|
|
var enumeratorType = enumerator.GetType();
|
|
var selectFieldInfo = enumeratorType.GetField("_selectExpression", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new InvalidOperationException($"cannot find field _selectExpression on type {enumeratorType.Name}");
|
|
var sqlGeneratorFieldInfo = enumeratorType.GetField("_querySqlGeneratorFactory", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new InvalidOperationException($"cannot find field _querySqlGeneratorFactory on type {enumeratorType.Name}");
|
|
var selectExpression = selectFieldInfo.GetValue(enumerator) as SelectExpression ?? throw new InvalidOperationException($"could not get SelectExpression");
|
|
var factory = sqlGeneratorFieldInfo.GetValue(enumerator) as IQuerySqlGeneratorFactory ?? throw new InvalidOperationException($"could not get IQuerySqlGeneratorFactory");
|
|
var sqlGenerator = factory.Create();
|
|
var command = sqlGenerator.GetCommand(selectExpression);
|
|
var sql = command.CommandText;
|
|
|
|
return sql;
|
|
}
|
|
}
|
|
}
|