添加debug log日志,针对多个skip take进行了判断报错,重写移除skip和take合并为一起

This commit is contained in:
xuejiaming 2022-07-29 09:07:39 +08:00
parent da2c7fe10c
commit 4f6c724636
17 changed files with 121 additions and 100 deletions

View File

@ -27,6 +27,12 @@ namespace ShardingCore.Extensions
private static readonly MethodInfo QueryableTakeMethod = typeof(Queryable).GetMethods().First(
m => m.Name == nameof(Queryable.Take)
&& m.GetParameters().Length == 2 && m.GetParameters()[1].ParameterType == typeof(int));
internal static IQueryable RemoveSkipAndTake(this IQueryable source)
{
var expression = new RemoveSkipAndTakeVisitor().Visit(source.Expression);
return source.Provider.CreateQuery(expression);
}
/// <summary>
/// 删除Skip表达式
/// </summary>

View File

@ -1,52 +0,0 @@
// using System;
// using System.Collections.Generic;
// using System.Diagnostics.Contracts;
// using System.Text;
// using System.Threading;
// using Microsoft.Extensions.Logging;
// using Microsoft.Extensions.Logging.Abstractions;
//
// namespace ShardingCore.Logger
// {
// /// <summary>
// ///
// /// </summary>
// /// Author: xjm
// /// Created: 2022/5/18 10:02:25
// /// Email: 326308290@qq.com
// public sealed class ShardingLoggerFactory
// {
// static ILoggerFactory _defaultFactory;
//
//
// static ILoggerFactory NewDefaultFactory()
// {
// var f = new NullLoggerFactory();
// return f;
// }
//
// /// <summary>
// /// Gets or sets the default factory.
// /// </summary>
// public static ILoggerFactory DefaultFactory
// {
// get
// {
// ILoggerFactory factory = Volatile.Read(ref _defaultFactory);
// if (factory == null)
// {
// factory = NewDefaultFactory();
// ILoggerFactory current = Interlocked.CompareExchange(ref _defaultFactory, factory, null);
// if (current != null)
// {
// return current;
// }
// }
// return factory;
// }
// set => Volatile.Write(ref _defaultFactory, value);
// }
// public static ILogger<T> CreateLogger<T>() => DefaultFactory.CreateLogger<T>();
// public static ILogger CreateLogger(string categoryName) => DefaultFactory.CreateLogger(categoryName);
// }
// }

View File

@ -1,6 +1,5 @@
using System;
using System.Linq;
using ShardingCore.Core.Internal.Visitors.Selects;
namespace ShardingCore.Sharding.MergeContexts
{

View File

@ -13,5 +13,10 @@ namespace ShardingCore.Sharding.MergeContexts
{
return string.Join(",", PropertyOrders);
}
public override string ToString()
{
return string.Join(",", PropertyOrders.Select(o=>$"{o}"));
}
}
}

View File

@ -3,21 +3,57 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Exceptions;
namespace ShardingCore.Sharding.MergeContexts
{
public sealed class PaginationContext
{
public int? Skip { get; set; }
public int? Take { get; set; }
public int? Skip { get; private set; }
public int? Take { get; private set; }
public bool HasSkip()
{
return Skip.HasValue;
}
public bool HasTake()
{
return Take.HasValue;
}
public void AddSkip(int skip)
{
if (Skip.HasValue)
{
throw new ShardingCoreNotSupportException("multi skip");
}
Skip = skip;
}
public void AddTake(int take)
{
if (Take.HasValue)
{
throw new ShardingCoreNotSupportException("multi take");
}
Take = take;
}
/// <summary>
/// 替换为固定的take一般用于first 1 single 2 last 1
/// </summary>
/// <param name="take"></param>
public void ReplaceToFixedTake(int take)
{
Take = take;
}
public override string ToString()
{
return $"{nameof(Skip)}: {Skip}, {nameof(Take)}: {Take}";
}
}
}
}

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Core.Internal.Visitors.Selects;
namespace ShardingCore.Sharding.MergeContexts
{

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.Extensions.Logging;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
@ -20,13 +21,33 @@ namespace ShardingCore.Sharding.MergeContexts
{
public sealed class QueryableRewriteEngine : IQueryableRewriteEngine
{
private readonly ILogger<QueryableRewriteEngine> _logger;
private readonly bool _enableLogDebug;
public QueryableRewriteEngine(ILogger<QueryableRewriteEngine> logger)
{
_logger = logger;
_enableLogDebug=logger.IsEnabled(LogLevel.Debug);
}
public IRewriteResult GetRewriteQueryable(IMergeQueryCompilerContext mergeQueryCompilerContext, IParseResult parseResult)
{
var paginationContext = parseResult.GetPaginationContext();
_logger.LogDebug($"rewrite queryable pagination context:[{paginationContext}]");
var orderByContext = parseResult.GetOrderByContext();
if (_enableLogDebug)
{
_logger.LogDebug($"rewrite queryable order by context:[{orderByContext}]");
}
var groupByContext = parseResult.GetGroupByContext();
if (_enableLogDebug)
{
_logger.LogDebug($"rewrite queryable group by context:[{groupByContext.GroupExpression?.ShardingPrint()}]");
}
var selectContext = parseResult.GetSelectContext();
if (_enableLogDebug)
{
_logger.LogDebug($"rewrite queryable select context:[{selectContext}]");
}
var skip = paginationContext.Skip;
var take = paginationContext.Take;
var orders = orderByContext.PropertyOrders;
@ -34,43 +55,23 @@ namespace ShardingCore.Sharding.MergeContexts
var combineQueryable = mergeQueryCompilerContext.GetQueryCombineResult().GetCombineQueryable();
//去除分页,获取前Take+Skip数量
var reWriteQueryable = combineQueryable;
if (take.HasValue || skip.HasValue)
{
reWriteQueryable = reWriteQueryable.RemoveSkipAndTake();
}
if (take.HasValue)
{
reWriteQueryable = reWriteQueryable.RemoveTake();
}
if (skip.HasValue)
{
reWriteQueryable = reWriteQueryable.RemoveSkip();
}
//如果是first or default
var fixedTake = mergeQueryCompilerContext.GetFixedTake();
if (fixedTake.HasValue)
{
if (skip.HasValue)
{
reWriteQueryable = reWriteQueryable.ReSkip(0).ReTake(fixedTake.Value + skip.GetValueOrDefault());
reWriteQueryable = reWriteQueryable.ReSkip(0).ReTake(take.Value + skip.GetValueOrDefault());
}
else
{
reWriteQueryable = reWriteQueryable.ReTake(fixedTake.Value);
reWriteQueryable = reWriteQueryable.ReTake(take.Value + skip.GetValueOrDefault());
}
}
else
{
if (take.HasValue)
{
if (skip.HasValue)
{
reWriteQueryable = reWriteQueryable.ReSkip(0).ReTake(take.Value + skip.GetValueOrDefault());
}
else
{
reWriteQueryable = reWriteQueryable.ReTake(take.Value + skip.GetValueOrDefault());
}
}
}
}
//包含group by
if (groupByContext.GroupExpression != null)
{

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using ShardingCore.Sharding.Visitors.Selects;
namespace ShardingCore.Core.Internal.Visitors.Selects
namespace ShardingCore.Sharding.MergeContexts
{
/*
* @Author: xjm
@ -24,5 +24,10 @@ namespace ShardingCore.Core.Internal.Visitors.Selects
{
return SelectProperties.Any(o=>o is SelectCountProperty);
}
public override string ToString()
{
return String.Join(",",SelectProperties.Select(o=>$"{o}"));
}
}
}

View File

@ -1,6 +1,5 @@
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core;
using ShardingCore.Core.Internal.Visitors.Selects;
using ShardingCore.Core.ShardingConfigurations.Abstractions;
using ShardingCore.Core.TrackerManagers;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;

View File

@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using ShardingCore.Core.Internal.Visitors.Selects;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.MergeContexts;
using ShardingCore.Sharding.Visitors.Selects;

View File

@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ShardingCore.Core.Internal.Visitors.Selects;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.MergeContexts;
using ShardingCore.Sharding.Visitors.Selects;
namespace ShardingCore.Core.Internal.Visitors

View File

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ShardingCore.Core.Internal.Visitors.Selects;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.MergeContexts;
@ -44,7 +43,7 @@ namespace ShardingCore.Core.Internal.Visitors
var fixedTake = _mergeQueryCompilerContext.GetFixedTake();
if (fixedTake.HasValue)
{
_paginationContext.Take = fixedTake.Value;
_paginationContext.ReplaceToFixedTake(fixedTake.Value);
}
return _paginationContext;
}
@ -60,13 +59,15 @@ namespace ShardingCore.Core.Internal.Visitors
{
if (_paginationContext.HasSkip())
throw new ShardingCoreInvalidOperationException("more than one skip found");
_paginationContext.Skip = (int)GetExpressionValue(node.Arguments[1]);
var skip = (int)GetExpressionValue(node.Arguments[1]);
_paginationContext.AddSkip(skip);
}
else if (node.Method.Name == nameof(Queryable.Take))
{
if (_paginationContext.HasTake())
throw new ShardingCoreInvalidOperationException("more than one take found");
_paginationContext.Take = (int)GetExpressionValue(node.Arguments[1]);
var take = (int)GetExpressionValue(node.Arguments[1]);
_paginationContext.AddTake(take);
}
else if (method.Name == nameof(Queryable.OrderBy) || method.Name == nameof(Queryable.OrderByDescending) || method.Name == nameof(Queryable.ThenBy) || method.Name == nameof(Queryable.ThenByDescending))
{

View File

@ -0,0 +1,18 @@
using System.Linq;
using System.Linq.Expressions;
namespace ShardingCore.Sharding.Visitors
{
internal class RemoveSkipAndTakeVisitor: ExpressionVisitor
{
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.Name == nameof(Queryable.Skip))
return base.Visit(node.Arguments[0]);
if (node.Method.Name == nameof(Queryable.Take))
return base.Visit(node.Arguments[0]);
return base.VisitMethodCall(node);
}
}
}

View File

@ -4,7 +4,6 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Core.Internal.Visitors.Selects;
namespace ShardingCore.Sharding.Visitors.Selects
{

View File

@ -4,7 +4,6 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ShardingCore.Core.Internal.Visitors.Selects;
namespace ShardingCore.Sharding.Visitors.Selects
{

View File

@ -1,14 +1,13 @@
using System;
using System.Reflection;
using ShardingCore.Sharding.Visitors.Selects;
/*
* @Author: xjm
* @Description:select属性
* @Description:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>select<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @Date: Tuesday, 02 February 2021 08:17:48
* @Email: 326308290@qq.com
*/
namespace ShardingCore.Core.Internal.Visitors.Selects
namespace ShardingCore.Sharding.Visitors.Selects
{
public class SelectOwnerProperty: SelectProperty
{
@ -20,5 +19,10 @@ namespace ShardingCore.Core.Internal.Visitors.Selects
public Type OwnerType { get; }
public PropertyInfo Property { get; }
public string PropertyName => Property.Name;
public override string ToString()
{
return $"{nameof(OwnerType)}: {OwnerType}, {nameof(Property)}: {Property}, {nameof(PropertyName)}: {PropertyName}";
}
}
}

View File

@ -37,6 +37,9 @@
<Compile Update="..\..\src\ShardingCore\Sharding\MergeEngines\Executors\Enumerators\FirstOrDefaultEnumeratorExecutor.cs">
<Link>Sharding\MergeEngines\Executors\Enumerators\FirstOrDefaultEnumeratorExecutor.cs</Link>
</Compile>
<Compile Update="..\..\src\ShardingCore\Sharding\MergeContexts\Paginations\PaginationItem.cs">
<Link>Sharding\MergeContexts\Paginations\PaginationItem.cs</Link>
</Compile>
</ItemGroup>