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)); }
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); }
public static Expression Replace(Expression proj, AliasGenerator aliasGenerator) { AliasProjectionReplacer apr = new AliasProjectionReplacer( root: proj as ProjectionExpression, aliasGenerator: aliasGenerator ); return(apr.Visit(proj)); }
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); }
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; }
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); }
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)); }
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)); }
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)); }
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)); }
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); }
public static CommandExpression Simplify(CommandExpression ce, bool removeSelectRowCount, AliasGenerator aliasGenerator) { return((CommandExpression) new CommandSimplifier { removeSelectRowCount = removeSelectRowCount, aliasGenerator = aliasGenerator }.Visit(ce)); }
public CommandSimplifier(bool removeSelectRowCount, AliasGenerator aliasGenerator) { this.removeSelectRowCount = removeSelectRowCount; this.aliasGenerator = aliasGenerator; }
public DuplicateHistory(AliasGenerator generator) { this.aliasGenerator = generator; }
private ChildProjectionFlattener(AliasGenerator aliasGenerator) { this.aliasGenerator = aliasGenerator; }
public AliasProjectionReplacer(ProjectionExpression?root, AliasGenerator aliasGenerator) { this.root = root; this.aliasGenerator = aliasGenerator; }
public CommandSimplifier(AliasGenerator aliasGenerator) { this.aliasGenerator = aliasGenerator; }
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)); }
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); }