示例#1
0
        public string GetQueryPlan(Expression expression)
        {
            Expression plan = this.GetExecutionPlan(expression);

            return(DbExpressionWriter.WriteToString(this.Language, plan));
        }
 public override string ToString()
 {
     return(DbExpressionWriter.WriteToString(this));
 }
示例#3
0
        protected void TestQuery(EntityProvider pro, Expression query, string baselineKey, bool expectedToFail)
        {
            ConsoleColor originalColor = Console.ForegroundColor;

            try
            {
                if (query.NodeType == ExpressionType.Convert && query.Type == typeof(object))
                {
                    query = ((UnaryExpression)query).Operand; // remove box
                }

                if (pro.Log != null)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    DbExpressionWriter.Write(pro.Log, pro.Language, query);
                    pro.Log.WriteLine();
                    pro.Log.WriteLine("==>");
                }

                string queryText = null;
                try
                {
                    queryText = pro.GetQueryText(query);
                    WriteBaseline(baselineKey, queryText);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(string.Format("Query translation failed for {0}", baselineKey));
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(query.ToString());
                    throw new TestFailureException(e.Message);
                }

                if (this.executeQueries)
                {
                    Exception caught = null;
                    try
                    {
                        object      result = pro.Execute(query);
                        IEnumerable seq    = result as IEnumerable;
                        if (seq != null)
                        {
                            // iterate results
                            foreach (var item in seq)
                            {
                            }
                        }
                        else
                        {
                            IDisposable disposable = result as IDisposable;
                            if (disposable != null)
                            {
                                disposable.Dispose();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        caught = e;
                        if (!expectedToFail)
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Query failed to execute:");
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(queryText);
                            throw new TestFailureException(e.Message);
                        }
                    }
                    if (caught == null && expectedToFail)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Query succeeded when expected to fail");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(queryText);
                        throw new TestFailureException(null);
                    }
                }
                else if (pro.Log != null)
                {
                    var text = pro.GetQueryText(query);
                    pro.Log.WriteLine(text);
                    pro.Log.WriteLine();
                }

                string baseline = null;
                if (this.baselines != null && this.baselines.TryGetValue(baselineKey, out baseline))
                {
                    string trimAct  = TrimExtraWhiteSpace(queryText).Trim();
                    string trimBase = TrimExtraWhiteSpace(baseline).Trim();
                    if (trimAct != trimBase)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Query translation does not match baseline:");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine(queryText);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("---- current ----");
                        WriteDifferences(trimAct, trimBase);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("---- baseline ----");
                        WriteDifferences(trimBase, trimAct);
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        throw new TestFailureException("Translation differed from baseline.");
                    }
                }

                if (baseline == null && this.baselines != null)
                {
                    throw new TestFailureException("No baseline");
                }
            }
            finally
            {
                Console.ForegroundColor = originalColor;
            }
        }
示例#4
0
        /// <summary>
        /// Gets text representing the entire query execution plan, including both server-side commands
        /// and client-side project and execution logic. For debugging purposes.
        /// </summary>
        public string GetQueryPlan(Expression expression)
        {
            var plan = GetExecutionPlan(expression);

            return(DbExpressionWriter.WriteToString(Language, plan));
        }