Пример #1
0
        public static Expression Replace(Expression source, AliasGenerator aliasGenerator)
        {
            var aliasMap = DeclaredAliasGatherer.GatherDeclared(source).Reverse().ToDictionary(a => a, aliasGenerator.CloneAlias);

            AliasReplacer ap = new AliasReplacer(aliasMap);

            return(ap.Visit(source));
        }
Пример #2
0
        public static Expression Rewrite(Expression expr, AliasGenerator generator)
        {
            if (!Schema.Current.Settings.IsPostgres)
            {
                return(expr);
            }

            return(new DuplicateHistory(generator).Visit(expr));
        }
 public static Expression Replace(Expression proj, AliasGenerator aliasGenerator)
 {
     AliasProjectionReplacer apr = new AliasProjectionReplacer()
     {
         aliasGenerator = aliasGenerator,
         root = proj as ProjectionExpression,
     };
     return apr.Visit(proj);
 }
Пример #4
0
 public static Expression Replace(Expression proj, AliasGenerator aliasGenerator)
 {
     AliasProjectionReplacer apr = new AliasProjectionReplacer()
     {
         aliasGenerator = aliasGenerator,
         root = proj as ProjectionExpression,
     };
     return apr.Visit(proj);
 }       
Пример #5
0
        public static Expression Replace(Expression proj, AliasGenerator aliasGenerator)
        {
            AliasProjectionReplacer apr = new AliasProjectionReplacer(
                root:  proj as ProjectionExpression,
                aliasGenerator: aliasGenerator
                );

            return(apr.Visit(proj));
        }
Пример #6
0
        public static Expression Replace(Expression source, AliasGenerator aliasGenerator)
        {
            AliasReplacer ap = new AliasReplacer()
            {
                aliasMap = DeclaredAliasGatherer.GatherDeclared(source).Reverse().ToDictionary(a => a, aliasGenerator.CloneAlias)
            };

            return ap.Visit(source);
        }
Пример #7
0
        static internal ProjectionExpression Flatten(ProjectionExpression proj, AliasGenerator aliasGenerator)
        {
            var result = (ProjectionExpression)new ChildProjectionFlattener { aliasGenerator = aliasGenerator }.Visit(proj);
            if (result == proj)
                return result;

            Expression columnCleaned = UnusedColumnRemover.Remove(result);
            Expression subqueryCleaned = RedundantSubqueryRemover.Remove(columnCleaned);

            return (ProjectionExpression)subqueryCleaned; 
        }
Пример #8
0
        static internal ProjectionExpression Flatten(ProjectionExpression proj, AliasGenerator aliasGenerator)
        {
            var result = (ProjectionExpression) new ChildProjectionFlattener(aliasGenerator).Visit(proj);

            if (result == proj)
            {
                return(result);
            }

            Expression columnCleaned   = UnusedColumnRemover.Remove(result);
            Expression subqueryCleaned = RedundantSubqueryRemover.Remove(columnCleaned);

            return((ProjectionExpression)subqueryCleaned);
        }
Пример #9
0
        internal R Insert <R>(IQueryable query, LambdaExpression constructor, ITable table, Func <SqlPreCommandSimple, R> continuation, bool removeSelectRowCount = false)
        {
            AliasGenerator aliasGenerator = new AliasGenerator();

            SqlPreCommandSimple cr;

            using (HeavyProfiler.Log("LINQ"))
                using (var log = HeavyProfiler.LogNoStackTrace("Clean"))
                {
                    Expression cleaned = Clean(query.Expression, true, log) !;
                    var        binder  = new QueryBinder(aliasGenerator);
                    log.Switch("Bind");
                    CommandExpression insert           = binder.BindInsert(cleaned, constructor, table);
                    CommandExpression insertOprimized  = (CommandExpression)Optimize(insert, binder, aliasGenerator, log);
                    CommandExpression insertSimplified = CommandSimplifier.Simplify(insertOprimized, removeSelectRowCount, aliasGenerator);
                    log.Switch("TR");
                    cr = TranslatorBuilder.BuildCommandResult(insertSimplified);
                }
            return(continuation(cr));
        }
Пример #10
0
        internal R Translate <R>(Expression expression, Func <ITranslateResult, R> continuation) //For debugging purposes
        {
            AliasGenerator aliasGenerator = new AliasGenerator();

            ITranslateResult result;

            using (HeavyProfiler.Log("LINQ", () => expression.ToString()))
                using (var log = HeavyProfiler.LogNoStackTrace("Clean"))
                {
                    Expression cleaned = Clean(expression, true, log) !;
                    var        binder  = new QueryBinder(aliasGenerator);
                    log.Switch("Bind");
                    ProjectionExpression binded    = (ProjectionExpression)binder.BindQuery(cleaned);
                    ProjectionExpression optimized = (ProjectionExpression)Optimize(binded, binder, aliasGenerator, log);
                    log.Switch("ChPrjFlatt");
                    ProjectionExpression flat = ChildProjectionFlattener.Flatten(optimized, aliasGenerator);
                    log.Switch("TB");
                    result = TranslatorBuilder.Build(flat);
                }
            return(continuation(result));
        }
Пример #11
0
        internal R Update <R>(IUpdateable updateable, Func <SqlPreCommandSimple, R> continuation, bool removeSelectRowCount = false)
        {
            AliasGenerator aliasGenerator = new AliasGenerator();

            SqlPreCommandSimple cr;

            using (HeavyProfiler.Log("LINQ"))
                using (var log = HeavyProfiler.LogNoStackTrace("Clean"))
                {
                    Expression cleaned = Clean(updateable.Query.Expression, true, log) !;

                    var binder = new QueryBinder(aliasGenerator);
                    log.Switch("Bind");
                    CommandExpression update           = binder.BindUpdate(cleaned, updateable.PartSelector, updateable.SetterExpressions);
                    CommandExpression updateOptimized  = (CommandExpression)Optimize(update, binder, aliasGenerator, log);
                    CommandExpression updateSimplified = CommandSimplifier.Simplify(updateOptimized, removeSelectRowCount, aliasGenerator);
                    log.Switch("TR");
                    cr = TranslatorBuilder.BuildCommandResult(updateSimplified);
                }
            return(continuation(cr));
        }
Пример #12
0
        internal protected virtual R Delete <R>(IQueryable query, Func <SqlPreCommandSimple, R> continuation, bool removeSelectRowCount = false)
        {
            AliasGenerator aliasGenerator = new AliasGenerator();

            SqlPreCommandSimple cr;

            using (HeavyProfiler.Log("LINQ"))
                using (var log = HeavyProfiler.LogNoStackTrace("Clean"))
                {
                    Expression cleaned = Clean(query.Expression, true, log) !;

                    log.Switch("Bind");
                    var binder = new QueryBinder(aliasGenerator);
                    CommandExpression delete           = binder.BindDelete(cleaned);
                    CommandExpression deleteOptimized  = (CommandExpression)Optimize(delete, binder, aliasGenerator, log);
                    CommandExpression deleteSimplified = CommandSimplifier.Simplify(deleteOptimized, removeSelectRowCount, aliasGenerator);

                    cr = TranslatorBuilder.BuildCommandResult(deleteSimplified);
                }
            return(continuation(cr));
        }
Пример #13
0
        internal static Expression Optimize(Expression binded, QueryBinder binder, AliasGenerator aliasGenerator, HeavyProfiler.Tracer?log)
        {
            var isPostgres = Schema.Current.Settings.IsPostgres;


            log.Switch("Aggregate");
            Expression rewriten = AggregateRewriter.Rewrite(binded);

            log.Switch("DupHistory");
            Expression dupHistory = DuplicateHistory.Rewrite(rewriten, aliasGenerator);

            log.Switch("EntityCompleter");
            Expression completed = EntityCompleter.Complete(dupHistory, binder);

            log.Switch("AliasReplacer");
            Expression replaced = AliasProjectionReplacer.Replace(completed, aliasGenerator);

            log.Switch("OrderBy");
            Expression orderRewrited = OrderByRewriter.Rewrite(replaced);

            log.Switch("Rebinder");
            Expression rebinded = QueryRebinder.Rebind(orderRewrited);

            log.Switch("UnusedColumn");
            Expression columnCleaned = UnusedColumnRemover.Remove(rebinded);

            log.Switch("Redundant");
            Expression subqueryCleaned = RedundantSubqueryRemover.Remove(columnCleaned);

            log.Switch("Condition");
            Expression rewriteConditions = isPostgres ? ConditionsRewriterPostgres.Rewrite(subqueryCleaned) : ConditionsRewriter.Rewrite(subqueryCleaned);

            log.Switch("Scalar");
            Expression scalar = ScalarSubqueryRewriter.Rewrite(rewriteConditions);

            return(scalar);
        }
Пример #14
0
 public static CommandExpression Simplify(CommandExpression ce, bool removeSelectRowCount, AliasGenerator aliasGenerator)
 {
     return((CommandExpression) new CommandSimplifier {
         removeSelectRowCount = removeSelectRowCount, aliasGenerator = aliasGenerator
     }.Visit(ce));
 }
Пример #15
0
 public CommandSimplifier(bool removeSelectRowCount, AliasGenerator aliasGenerator)
 {
     this.removeSelectRowCount = removeSelectRowCount;
     this.aliasGenerator       = aliasGenerator;
 }
Пример #16
0
 public DuplicateHistory(AliasGenerator generator)
 {
     this.aliasGenerator = generator;
 }
Пример #17
0
 private ChildProjectionFlattener(AliasGenerator aliasGenerator)
 {
     this.aliasGenerator = aliasGenerator;
 }
Пример #18
0
 public AliasProjectionReplacer(ProjectionExpression?root, AliasGenerator aliasGenerator)
 {
     this.root           = root;
     this.aliasGenerator = aliasGenerator;
 }
Пример #19
0
 public CommandSimplifier(AliasGenerator aliasGenerator)
 {
     this.aliasGenerator = aliasGenerator;
 }
Пример #20
0
        public static CommandExpression Simplify(CommandExpression ce, bool removeSelectRowCount, AliasGenerator aliasGenerator)
        {
            if (removeSelectRowCount)
            {
                ce = (CommandExpression) new SelectRowRemover().Visit(ce);
            }

            return((CommandExpression) new CommandSimplifier(aliasGenerator).Visit(ce));
        }
Пример #21
0
        private static SqlPreCommandSimple UpdateByFkChange(string tn, DiffColumn difCol, IColumn tabCol, Func<ObjectName, ObjectName> changeName)
        {
            if (difCol.ForeignKey == null || tabCol.ReferenceTable == null || tabCol.AvoidForeignKey)
                return null;

            ObjectName oldFk = changeName(difCol.ForeignKey.TargetTable);

            if (oldFk.Equals(tabCol.ReferenceTable.Name))
                return null;

            AliasGenerator ag = new AliasGenerator();

            return new SqlPreCommandSimple(
@"UPDATE {2}
SET {0} =  -- get {5} id from {4}.Id
FROM {1} {2}
JOIN {3} {4} ON {2}.{0} = {4}.Id".FormatWith(tabCol.Name,
                tn, ag.NextTableAlias(tn),
                oldFk, ag.NextTableAlias(oldFk.Name),
                tabCol.ReferenceTable.Name.Name));
        }
 public static CommandExpression Simplify(CommandExpression ce, bool removeSelectRowCount, AliasGenerator aliasGenerator)
 {
     return (CommandExpression)new CommandSimplifier { removeSelectRowCount = removeSelectRowCount, aliasGenerator = aliasGenerator }.Visit(ce);
 }