2022-01-17 00:04:50 +08:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Query;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Query.Internal;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
|
|
|
using ShardingCore;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
using ShardingCore.Core.RuntimeContexts;
|
2022-03-04 15:35:30 +08:00
|
|
|
|
using ShardingCore.Core.UnionAllMergeShardingProviders.Abstractions;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
using ShardingCore.Exceptions;
|
2022-03-04 15:35:30 +08:00
|
|
|
|
using ShardingCore.Sharding.Abstractions;
|
2022-01-17 00:04:50 +08:00
|
|
|
|
|
2022-03-04 15:35:30 +08:00
|
|
|
|
namespace Sample.SqlServer.UnionAllMerge
|
2022-01-17 00:04:50 +08:00
|
|
|
|
{
|
2022-03-04 15:35:30 +08:00
|
|
|
|
public class UnionAllMergeQueryCompiler : QueryCompiler, IUnionAllMergeQueryCompiler
|
2022-01-17 00:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly IQueryContextFactory _queryContextFactory;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
private readonly IShardingRuntimeContext _shardingRuntimeContext;
|
2022-01-17 00:04:50 +08:00
|
|
|
|
private readonly IDatabase _database;
|
|
|
|
|
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;
|
|
|
|
|
private readonly IModel _model;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
private readonly IUnionAllMergeManager _unionAllMergeManager;
|
2022-01-17 00:04:50 +08:00
|
|
|
|
|
2022-07-03 16:52:03 +08:00
|
|
|
|
public UnionAllMergeQueryCompiler(IQueryContextFactory queryContextFactory,IShardingRuntimeContext shardingRuntimeContext, ICompiledQueryCache compiledQueryCache, ICompiledQueryCacheKeyGenerator compiledQueryCacheKeyGenerator, IDatabase database, IDiagnosticsLogger<DbLoggerCategory.Query> logger, ICurrentDbContext currentContext, IEvaluatableExpressionFilter evaluatableExpressionFilter, IModel model) : base(queryContextFactory, compiledQueryCache, compiledQueryCacheKeyGenerator, database, logger, currentContext, evaluatableExpressionFilter, model)
|
2022-01-17 00:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
_queryContextFactory = queryContextFactory;
|
2022-07-03 16:52:03 +08:00
|
|
|
|
_shardingRuntimeContext = shardingRuntimeContext;
|
|
|
|
|
_unionAllMergeManager=_shardingRuntimeContext.GetRequiredService<IUnionAllMergeManager>();
|
2022-01-17 00:04:50 +08:00
|
|
|
|
_database = database;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_model = model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TResult Execute<TResult>(Expression query)
|
|
|
|
|
{
|
2022-07-03 16:52:03 +08:00
|
|
|
|
if (_unionAllMergeManager?.Current != null)
|
2022-01-17 00:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
return NotSupportShardingExecute<TResult>(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.Execute<TResult>(query);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// use no compiler
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TResult"></typeparam>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private TResult NotSupportShardingExecute<TResult>(Expression query)
|
|
|
|
|
{
|
|
|
|
|
var queryContext = _queryContextFactory.Create();
|
|
|
|
|
|
|
|
|
|
query = ExtractParameters(query, queryContext, _logger);
|
|
|
|
|
|
|
|
|
|
var compiledQuery
|
|
|
|
|
= CompileQueryCore<TResult>(_database, query, _model, false);
|
|
|
|
|
|
|
|
|
|
return compiledQuery(queryContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TResult ExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken = new CancellationToken())
|
|
|
|
|
{
|
2022-07-03 16:52:03 +08:00
|
|
|
|
if (_unionAllMergeManager?.Current != null)
|
2022-01-17 00:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
var result = NotSupportShardingExecuteAsync<TResult>(query, cancellationToken);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.ExecuteAsync<TResult>(query, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TResult NotSupportShardingExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken = new CancellationToken())
|
|
|
|
|
{
|
|
|
|
|
var queryContext = _queryContextFactory.Create();
|
|
|
|
|
|
|
|
|
|
queryContext.CancellationToken = cancellationToken;
|
|
|
|
|
|
|
|
|
|
query = ExtractParameters(query, queryContext, _logger);
|
|
|
|
|
|
|
|
|
|
var compiledQuery
|
|
|
|
|
= CompileQueryCore<TResult>(_database, query, _model, true);
|
|
|
|
|
|
|
|
|
|
return compiledQuery(queryContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|