Esempio n. 1
0
            private SqlJoin GetLeftOuterWithUnreferencedSingletonOnLeft(SqlSource source)
            {
                SqlAlias alias = source as SqlAlias;

                if (alias != null)
                {
                    SqlSelect node = alias.Node as SqlSelect;
                    if ((((node != null) && (node.Where == null)) && ((node.Top == null) && (node.GroupBy.Count == 0))) &&
                        (node.OrderBy.Count == 0))
                    {
                        return(this.GetLeftOuterWithUnreferencedSingletonOnLeft(node.From));
                    }
                }
                SqlJoin join = source as SqlJoin;

                if ((join == null) || (join.JoinType != SqlJoinType.LeftOuter))
                {
                    return(null);
                }
                if (!this.IsSingletonSelect(join.Left))
                {
                    return(null);
                }
                Dictionary <SqlAlias, bool> dictionary = SqlGatherProducedAliases.Gather(join.Left);

                foreach (SqlAlias alias2 in SqlGatherConsumedAliases.Gather(join.Right).Keys)
                {
                    if (dictionary.ContainsKey(alias2))
                    {
                        return(null);
                    }
                }
                return(join);
            }
Esempio n. 2
0
            internal static SqlExpression Lift(SqlSource source, Dictionary <SqlAlias, bool> aliasesForLifting)
            {
                Visitor visitor = new Visitor(true, aliasesForLifting, null);

                visitor.VisitSource(source);
                return(visitor.lifted);
            }
Esempio n. 3
0
            // Methods
            internal static List <SqlColumn> GatherColumns(SqlSource source)
            {
                List <SqlColumn> columns = new List <SqlColumn>();

                new Visitor(columns).Visit(source);
                return(columns);
            }
Esempio n. 4
0
            internal override SqlSource VisitJoin(SqlJoin join)
            {
                SqlSource left  = this.VisitSource(join.Left);
                SqlSource right = this.VisitSource(join.Right);

                return(new SqlJoin(join.JoinType, left, right, (SqlExpression)this.Visit(join.Condition), join.SourceExpression));
            }
Esempio n. 5
0
 // Methods
 internal SqlJoin(SqlJoinType type, SqlSource left, SqlSource right, SqlExpression cond, Expression sourceExpression)
     : base(SqlNodeType.Join, sourceExpression)
 {
     this.JoinType  = type;
     this.Left      = left;
     this.Right     = right;
     this.Condition = cond;
 }
Esempio n. 6
0
            internal static List <List <SqlColumn> > Lift(SqlSource source, Dictionary <SqlAlias, bool> aliasesForLifting,
                                                          Dictionary <SqlExpression, bool> liftedExpressions)
            {
                Visitor visitor = new Visitor(true, aliasesForLifting, liftedExpressions);

                visitor.VisitSource(source);
                return(visitor.lifted);
            }
Esempio n. 7
0
            // Methods
            internal static bool CanLift(SqlSource source, Dictionary <SqlAlias, bool> aliasesForLifting,
                                         Dictionary <SqlExpression, bool> liftedExpressions)
            {
                Visitor visitor = new Visitor(false, aliasesForLifting, liftedExpressions);

                visitor.VisitSource(source);
                return(visitor.canLiftAll);
            }
Esempio n. 8
0
            private bool HasTrivialSource(SqlSource node)
            {
                SqlJoin join = node as SqlJoin;

                if (join == null)
                {
                    return(node is SqlAlias);
                }
                return(this.HasTrivialSource(join.Left) && this.HasTrivialSource(join.Right));
            }
Esempio n. 9
0
            private bool HasTrivialSource(SqlSource node)
            {
                SqlAlias alias = node as SqlAlias;

                if (alias == null)
                {
                    return(false);
                }
                return(alias.Node is SqlSelect);
            }
Esempio n. 10
0
 // Methods
 internal SqlSelect(SqlExpression selection, SqlSource from, Expression sourceExpression)
     : base(SqlNodeType.Select, sourceExpression)
 {
     this.Row          = new SqlRow(sourceExpression);
     this.Selection    = selection;
     this.From         = from;
     this.GroupBy      = new List <SqlExpression>();
     this.OrderBy      = new List <SqlOrderExpression>();
     this.OrderingType = SqlOrderingType.Default;
 }
Esempio n. 11
0
            private SqlSource PushSourceDown(SqlSource sqlSource, List <SqlColumn> cols)
            {
                SqlSelect node = new SqlSelect(
                    new SqlNop(cols[0].ClrType, cols[0].SqlType, sqlSource.SourceExpression), sqlSource,
                    sqlSource.SourceExpression);

                //node.Row.Columns.AddRange(cols);
                foreach (var col in cols)
                {
                    node.Row.Columns.Add(col);
                }
                return(new SqlAlias(node));
            }
Esempio n. 12
0
            // Methods
            private SqlUnion GetUnion(SqlSource source)
            {
                SqlAlias alias = source as SqlAlias;

                if (alias != null)
                {
                    SqlUnion node = alias.Node as SqlUnion;
                    if (node != null)
                    {
                        return(node);
                    }
                }
                return(null);
            }
Esempio n. 13
0
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                SqlSource            from       = this.VisitSource(select.From);
                List <SqlExpression> collection = null;

                if (select.GroupBy.Count > 0)
                {
                    collection = new List <SqlExpression>(select.GroupBy.Count);
                    foreach (SqlExpression expression in select.GroupBy)
                    {
                        collection.Add((SqlExpression)this.Visit(expression));
                    }
                }
                SqlExpression             expression2 = (SqlExpression)this.Visit(select.Having);
                List <SqlOrderExpression> list2       = null;

                if (select.OrderBy.Count > 0)
                {
                    list2 = new List <SqlOrderExpression>(select.OrderBy.Count);
                    foreach (SqlOrderExpression expression3 in select.OrderBy)
                    {
                        SqlOrderExpression item = new SqlOrderExpression(expression3.OrderType, (SqlExpression)this.Visit(expression3.Expression));
                        list2.Add(item);
                    }
                }
                SqlExpression expression5 = (SqlExpression)this.Visit(select.Top);
                SqlExpression expression6 = (SqlExpression)this.Visit(select.Where);
                SqlRow        row         = (SqlRow)this.Visit(select.Row);
                SqlSelect     select2     = new SqlSelect(this.VisitExpression(select.Selection), from, select.SourceExpression);

                if (collection != null)
                {
                    select2.GroupBy.AddRange(collection);
                }
                select2.Having = expression2;
                if (list2 != null)
                {
                    select2.OrderBy.AddRange(list2);
                }
                select2.OrderingType = select.OrderingType;
                select2.Row          = row;
                select2.Top          = expression5;
                select2.IsDistinct   = select.IsDistinct;
                select2.IsPercent    = select.IsPercent;
                select2.Where        = expression6;
                select2.DoNotOutput  = select.DoNotOutput;
                return(select2);
            }
Esempio n. 14
0
            private bool HasEmptySource(SqlSource node)
            {
                SqlAlias alias = node as SqlAlias;

                if (alias == null)
                {
                    return(false);
                }
                SqlSelect select = alias.Node as SqlSelect;

                if (select == null)
                {
                    return(false);
                }
                return(((((select.Row.Columns.Count == 0) && (select.From == null)) && ((select.Where == null) && (select.GroupBy.Count == 0))) && (select.Having == null)) && (select.OrderBy.Count == 0));
            }
Esempio n. 15
0
            internal override SqlSource VisitSource(SqlSource node)
            {
                node = (SqlSource)this.Visit(node);
                SqlAlias alias = node as SqlAlias;

                if (alias != null)
                {
                    SqlSelect select = alias.Node as SqlSelect;
                    if ((select != null) && this.IsTrivialSelect(select))
                    {
                        this.removedMap[alias] = alias;
                        node = select.From;
                    }
                }
                return(node);
            }
Esempio n. 16
0
 private void GetSelectionsBeforeJoin(SqlSource source, List <List <SqlColumn> > selections)
 {
     if (!(source is SqlJoin))
     {
         SqlAlias alias = source as SqlAlias;
         if (alias != null)
         {
             SqlSelect node = alias.Node as SqlSelect;
             if (node != null)
             {
                 this.GetSelectionsBeforeJoin(node.From, selections);
                 //selections.Add(node.Row.Columns);
                 List <SqlColumn> columns = new List <SqlColumn>(node.Row.Columns);
                 selections.Add(columns);
             }
         }
     }
 }
Esempio n. 17
0
            private bool IsSingletonSelect(SqlSource source)
            {
                SqlAlias alias = source as SqlAlias;

                if (alias == null)
                {
                    return(false);
                }
                SqlSelect node = alias.Node as SqlSelect;

                if (node == null)
                {
                    return(false);
                }
                if (node.From != null)
                {
                    return(false);
                }
                return(true);
            }
Esempio n. 18
0
            internal override SqlSource VisitJoin(SqlJoin join)
            {
                if (join.JoinType != SqlJoinType.CrossApply)
                {
                    return(base.VisitJoin(join));
                }
                join.Left      = this.VisitSource(join.Left);
                join.Condition = this.VisitExpression(join.Condition);
                SelectScope expressionSink = this.expressionSink;

                this.expressionSink = new SelectScope();
                this.expressionSink.LeftProduction = SqlGatherProducedAliases.Gather(join.Left).Keys;
                join.Right = this.VisitSource(join.Right);
                SqlSource sqlSource = join;

                foreach (List <SqlColumn> list in this.expressionSink.Lifted)
                {
                    sqlSource = this.PushSourceDown(sqlSource, list);
                }
                this.expressionSink = expressionSink;
                return(sqlSource);
            }
Esempio n. 19
0
            private static bool IsTableAlias(SqlSource src)
            {
                SqlAlias alias = src as SqlAlias;

                return((alias != null) && (alias.Node is SqlTable));
            }
Esempio n. 20
0
 internal virtual SqlSource VisitSource(SqlSource source)
 {
     return((SqlSource)this.Visit(source));
 }
Esempio n. 21
0
            internal override SqlSource VisitSource(SqlSource source)
            {
                source = base.VisitSource(source);
                SqlJoin node = source as SqlJoin;

                if (node != null)
                {
                    if (node.JoinType == SqlJoinType.OuterApply)
                    {
                        Dictionary <SqlAlias, bool>      aliasesForLifting = SqlGatherProducedAliases.Gather(node.Left);
                        Dictionary <SqlExpression, bool> liftedExpressions = new Dictionary <SqlExpression, bool>();
                        if (
                            (SqlOuterApplyReducer.SqlPredicateLifter.CanLift(node.Right, aliasesForLifting,
                                                                             liftedExpressions) &&
                             SqlOuterApplyReducer.SqlSelectionLifter.CanLift(node.Right, aliasesForLifting,
                                                                             liftedExpressions)) &&
                            !SqlOuterApplyReducer.SqlAliasDependencyChecker.IsDependent(node.Right, aliasesForLifting,
                                                                                        liftedExpressions))
                        {
                            SqlExpression expression = SqlOuterApplyReducer.SqlPredicateLifter.Lift(node.Right,
                                                                                                    aliasesForLifting);
                            List <List <SqlColumn> > list = SqlOuterApplyReducer.SqlSelectionLifter.Lift(node.Right,
                                                                                                         aliasesForLifting,
                                                                                                         liftedExpressions);
                            node.JoinType  = SqlJoinType.LeftOuter;
                            node.Condition = expression;
                            if (list != null)
                            {
                                foreach (List <SqlColumn> list2 in list)
                                {
                                    source = this.PushSourceDown(source, list2);
                                }
                            }
                        }
                        else
                        {
                            this.AnnotateSqlIncompatibility(node,
                                                            new SqlProvider.ProviderMode[]
                                                            { SqlProvider.ProviderMode.Sql2000 });
                        }
                    }
                    else if (node.JoinType == SqlJoinType.CrossApply)
                    {
                        SqlJoin leftOuterWithUnreferencedSingletonOnLeft =
                            this.GetLeftOuterWithUnreferencedSingletonOnLeft(node.Right);
                        if (leftOuterWithUnreferencedSingletonOnLeft != null)
                        {
                            Dictionary <SqlAlias, bool>      dictionary3 = SqlGatherProducedAliases.Gather(node.Left);
                            Dictionary <SqlExpression, bool> dictionary4 = new Dictionary <SqlExpression, bool>();
                            if (
                                (SqlOuterApplyReducer.SqlPredicateLifter.CanLift(
                                     leftOuterWithUnreferencedSingletonOnLeft.Right, dictionary3, dictionary4) &&
                                 SqlOuterApplyReducer.SqlSelectionLifter.CanLift(
                                     leftOuterWithUnreferencedSingletonOnLeft.Right, dictionary3, dictionary4)) &&
                                !SqlOuterApplyReducer.SqlAliasDependencyChecker.IsDependent(
                                    leftOuterWithUnreferencedSingletonOnLeft.Right, dictionary3, dictionary4))
                            {
                                SqlExpression right =
                                    SqlOuterApplyReducer.SqlPredicateLifter.Lift(
                                        leftOuterWithUnreferencedSingletonOnLeft.Right, dictionary3);
                                List <List <SqlColumn> > selections =
                                    SqlOuterApplyReducer.SqlSelectionLifter.Lift(
                                        leftOuterWithUnreferencedSingletonOnLeft.Right, dictionary3, dictionary4);
                                this.GetSelectionsBeforeJoin(node.Right, selections);
                                foreach (
                                    List <SqlColumn> list4 in
                                    selections.Where <List <SqlColumn> >(
                                        delegate(List <SqlColumn> s) { return(s.Count > 0); }))
                                {
                                    source = this.PushSourceDown(source, list4);
                                }
                                node.JoinType  = SqlJoinType.LeftOuter;
                                node.Condition =
                                    this.factory.AndAccumulate(leftOuterWithUnreferencedSingletonOnLeft.Condition, right);
                                node.Right = leftOuterWithUnreferencedSingletonOnLeft.Right;
                            }
                            else
                            {
                                this.AnnotateSqlIncompatibility(node,
                                                                new SqlProvider.ProviderMode[]
                                                                { SqlProvider.ProviderMode.Sql2000 });
                            }
                        }
                    }
                    while (node.JoinType == SqlJoinType.LeftOuter)
                    {
                        SqlJoin join3 = this.GetLeftOuterWithUnreferencedSingletonOnLeft(node.Left);
                        if (join3 == null)
                        {
                            return(source);
                        }
                        List <List <SqlColumn> > list5 = new List <List <SqlColumn> >();
                        this.GetSelectionsBeforeJoin(node.Left, list5);
                        foreach (List <SqlColumn> list6 in list5)
                        {
                            source = this.PushSourceDown(source, list6);
                        }
                        SqlSource     source2   = node.Right;
                        SqlExpression condition = node.Condition;
                        node.Left       = join3.Left;
                        node.Right      = join3;
                        node.Condition  = join3.Condition;
                        join3.Left      = join3.Right;
                        join3.Right     = source2;
                        join3.Condition = condition;
                    }
                }
                return(source);
            }
Esempio n. 22
0
 internal void BuildEqivalenceMap(SqlSource scope)
 {
     this.map = new Dictionary <SqlColumn, SqlColumn>();
     this.Visit(scope);
 }