public override Expression Visit(Expression node)
        {
            switch (node)
            {
            case MemberExpression member:
                VisitMember(member);
                break;

            case MethodCallExpression methodCall:
                VisitMethodCall(methodCall);
                break;

            case ConstantExpression constant:
                VisitConstant(constant);
                break;

            case BinaryExpression binary:
                VisitBinary(binary);
                break;

            case UnaryExpression unary:
                VisitUnary(unary);
                break;

            case ConditionalExpression conditional:
                VisitConditional(conditional);
                break;

            case QuerySourceReferenceExpression reference:
                if ((reference.ReferencedQuerySource as IFromClause)?.FromExpression is SubQueryExpression subQuery)
                {
                    Visit(subQuery.QueryModel.SelectClause.Selector);
                }
                break;

            case SubQueryExpression query:
                Writer.Write("(");
                var visitor = new MySqlQueryModelVisitor(Writer);
                visitor.Render(query.QueryModel);
                Writer.Write(")");
                break;

            default:
                throw new NotSupportedException($"Can't translate expression of type '{node.NodeType}' ({node.Type})");
            }

            return(node);
        }
Пример #2
0
        public async Task <bool> TestAsync(IQueryable <bool> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var test = query.Select(condition => condition ? 1 : Sleep(Options.InjectedWaitTime.TotalSeconds)).Take(1);

            using (var writer = WriterPool.Get())
            {
                var visitor = new MySqlQueryModelVisitor(writer);
                var sql     = visitor.Render(test);

                while (true)
                {
                    try
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();

                        Logger.LogDebug(sql);
                        await Injector.InjectAsync(sql, CancellationToken.None);

                        stopwatch.Stop();
                        WriterPool.Return(writer);

                        return(stopwatch.Elapsed < Options.SuccessfulTime);
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("timed out") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("unrecognized response") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (OperationCanceledException)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                }
            }
        }
        public async Task <bool> TestAsync(IQueryable <bool> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var expr = ErrorExpressionProvider.GetErrorExpression();

            var p    = Expression.Parameter(typeof(bool), "condition");
            var next = query.Provider.CreateQuery <long>(
                Expression.Call(
                    typeof(Queryable).GetMethods().Where(m => m.Name == nameof(System.Linq.Queryable.Select)).Single(m => !m.GetParameters()[1].ParameterType.ToString().Contains("Int32"))
                    .MakeGenericMethod(typeof(bool), typeof(long)),
                    query.Expression,
                    Expression.Lambda(
                        Expression.Condition(
                            p,
                            expr,
                            Expression.Constant(1L)
                            ),
                        p
                        )
                    )
                ).Take(1);

            using (var writer = WriterPool.Get())
            {
                var visitor = new MySqlQueryModelVisitor(writer);
                var sql     = visitor.Render(next);

                Logger.LogDebug(sql);

                while (true)
                {
                    try
                    {
                        using (var response = await Injector.InjectAsync(sql, CancellationToken.None))
                        {
                            WriterPool.Return(writer);
                            return(await ErrorDetector.ContainsErrorAsync(response));
                        }
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("timed out") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("unrecognized response") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (OperationCanceledException)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                }
            }
        }