Exemple #1
0
            private void GatherUnionExpressions(SqlNode node, IList <SqlExpression> exprs)
            {
                SqlUnion union = node as SqlUnion;

                if (union != null)
                {
                    this.GatherUnionExpressions(union.Left, exprs);
                    this.GatherUnionExpressions(union.Right, exprs);
                }
                else
                {
                    SqlSelect select = node as SqlSelect;
                    if (select != null)
                    {
                        SqlAliasRef selection = select.Selection as SqlAliasRef;
                        if (selection != null)
                        {
                            this.GatherUnionExpressions(selection.Alias.Node, exprs);
                        }
                        else
                        {
                            exprs.Add(select.Selection);
                        }
                    }
                }
            }
Exemple #2
0
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                if (!this.IsTrivialSelect(select))
                {
                    return(select);
                }
                SqlSelect node = (SqlSelect)((SqlAlias)select.From).Node;
                Dictionary <SqlColumn, SqlColumnRef> map = new Dictionary <SqlColumn, SqlColumnRef>();

                foreach (SqlColumn column in select.Row.Columns)
                {
                    SqlColumnRef expression;

                    //if (column is SqlDynamicColumn)
                    //{
                    //    expression = new SqlColumnRef(column);
                    //}
                    //else
                    //{
                    expression = (SqlColumnRef)column.Expression;
                    //}

                    map.Add(column, expression);
                    if (!string.IsNullOrEmpty(column.Name))
                    {
                        expression.Column.Name = column.Name;
                    }
                }
                node.Selection = new ColumnMapper(map).VisitExpression(select.Selection);
                return(node);
            }
Exemple #3
0
            internal override SqlNode VisitUnion(SqlUnion su)
            {
                bool      changed = false;
                bool      containsLongExpressions = false;
                SqlSelect left = su.Left as SqlSelect;

                if (left != null)
                {
                    this.ConvertColumnsToMax(left, out changed, out containsLongExpressions);
                }
                bool      flag3 = false;
                bool      flag4 = false;
                SqlSelect right = su.Right as SqlSelect;

                if (right != null)
                {
                    this.ConvertColumnsToMax(right, out flag3, out flag4);
                }
                if (!su.All && (containsLongExpressions || flag4))
                {
                    this.annotations.Add(su,
                                         new SqlServerCompatibilityAnnotation(
                                             Strings.TextNTextAndImageCannotOccurInUnion(su.SourceExpression),
                                             new SqlProvider.ProviderMode[]
                                             { SqlProvider.ProviderMode.Sql2000, SqlProvider.ProviderMode.SqlCE }));
                }
                return(base.VisitUnion(su));
            }
 internal override SqlSelect VisitSelect(SqlSelect select)
 {
     this.VisitSource(select.From);
     if (((select.Top != null) || (select.GroupBy.Count > 0)) ||
         (this.aggregateChecker.HasAggregates(select) || select.IsDistinct))
     {
         this.canLiftAll = false;
     }
     if ((this.canLiftAll && (select.Where != null)) && this.aliases.ReferencesAny(select.Where))
     {
         if (this.liftedExpressions != null)
         {
             this.liftedExpressions[select.Where] = true;
         }
         if (!this.doLifting)
         {
             return(select);
         }
         if (this.lifted != null)
         {
             this.lifted = new SqlBinary(SqlNodeType.And, this.lifted.ClrType, this.lifted.SqlType,
                                         this.lifted, select.Where);
         }
         else
         {
             this.lifted = select.Where;
         }
         select.Where = null;
     }
     return(select);
 }
            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);
            }
Exemple #6
0
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                bool topLevel = this.topLevel;

                this.topLevel = false;
                select.From   = (SqlSource)Visit(select.From);
                select.Where  = VisitExpression(select.Where);
                int num   = 0;
                int count = select.GroupBy.Count;

                while (num < count)
                {
                    select.GroupBy[num] = VisitExpression(select.GroupBy[num]);
                    num++;
                }
                select.Having = this.VisitExpression(select.Having);
                int num3 = 0;
                int num4 = select.OrderBy.Count;

                while (num3 < num4)
                {
                    select.OrderBy[num3].Expression = this.VisitExpression(select.OrderBy[num3].Expression);
                    num3++;
                }
                select.Top       = this.VisitExpression(select.Top);
                select.Row       = (SqlRow)this.Visit(select.Row);
                this.topLevel    = topLevel;
                select.Selection = this.VisitExpression(select.Selection);
                return(select);
            }
            // Methods
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                int index = 0;
                IList <SqlOrderExpression> orderBy = select.OrderBy;

                while (index < orderBy.Count)
                {
                    SqlExpression discriminator = orderBy[index].Expression;
                    while (discriminator.NodeType == SqlNodeType.DiscriminatedType)
                    {
                        discriminator = ((SqlDiscriminatedType)discriminator).Discriminator;
                    }
                    switch (discriminator.NodeType)
                    {
                    case SqlNodeType.Parameter:
                    case SqlNodeType.Value:
                    {
                        orderBy.RemoveAt(index);
                        continue;
                    }
                    }
                    index++;
                }
                return(base.VisitSelect(select));
            }
Exemple #8
0
            internal override SqlExpression VisitAliasRef(SqlAliasRef aref)
            {
                SqlNode node = aref.Alias.Node;

                if ((node is SqlTable) || (node is SqlTableValuedFunctionCall))
                {
                    return(aref);
                }
                SqlUnion union = node as SqlUnion;

                if (union != null)
                {
                    return(this.ExpandUnion(union));
                }
                SqlSelect select = node as SqlSelect;

                if (select != null)
                {
                    return(this.VisitExpression(select.Selection));
                }
                SqlExpression exp = node as SqlExpression;

                if (exp == null)
                {
                    throw Error.CouldNotHandleAliasRef(node.NodeType);
                }
                return(this.VisitExpression(exp));
            }
Exemple #9
0
        private static IProviderType GetSqlType(SqlNode node)
        {
            SqlExpression expression = node as SqlExpression;

            if (expression != null)
            {
                return(expression.SqlType);
            }
            SqlSelect select = node as SqlSelect;

            if (select != null)
            {
                return(select.Selection.SqlType);
            }
            SqlTable table = node as SqlTable;

            if (table != null)
            {
                return(table.SqlRowType);
            }
            SqlUnion union = node as SqlUnion;

            if (union == null)
            {
                throw Error.UnexpectedNode(node.NodeType);
            }
            return(union.GetSqlType());
        }
        internal override SqlSelect VisitSelect(SqlSelect select)
        {
            if (select.IsDistinct)
            {
                this.IsDistinct = true;
                this.AddIdentityMembers(select.Selection.ClrType.GetProperties());
                return(select);
            }
            select.From = (SqlSource)base.Visit(select.From);
            if ((this.IdentityMembers == null) || (this.IdentityMembers.Count == 0))
            {
                throw Error.SkipRequiresSingleTableQueryWithPKs();
            }
            SqlNodeType nodeType = select.Selection.NodeType;

            if (nodeType <= SqlNodeType.ColumnRef)
            {
                switch (nodeType)
                {
                case SqlNodeType.Column:
                case SqlNodeType.ColumnRef:
                    goto Label_009E;

                case SqlNodeType.AliasRef:
                    goto Label_00DD;
                }
                goto Label_00F1;
            }
            if (nodeType != SqlNodeType.Member)
            {
                switch (nodeType)
                {
                case SqlNodeType.Treat:
                case SqlNodeType.TypeCase:
                    return(select);

                case SqlNodeType.New:
                    goto Label_00DD;
                }
                goto Label_00F1;
            }
Label_009E:
            if (this.IdentityMembers.Count == 1)
            {
                MemberInfo column = this.IdentityMembers[0];
                this.IsValid &= IsColumnMatch(column, select.Selection);
                return(select);
            }
            this.IsValid = false;
            return(select);

Label_00DD:
            select.Selection = this.VisitExpression(select.Selection);
            return(select);

Label_00F1:
            this.IsValid = false;
            return(select);
        }
Exemple #11
0
 internal override SqlSelect VisitSelect(SqlSelect select)
 {
     foreach (SqlColumn column in select.Row.Columns)
     {
         this.columns.Add(column);
     }
     return(select);
 }
Exemple #12
0
            private void PushDown(SqlColumn column)
            {
                SqlSelect node = new SqlSelect(new SqlNop(column.ClrType, column.SqlType, column.SourceExpression),
                                               this.currentSelect.From, this.currentSelect.SourceExpression);

                this.currentSelect.From = new SqlAlias(node);
                node.Row.Columns.Add(column);
            }
Exemple #13
0
 private bool IsTrivialSelect(SqlSelect select)
 {
     if ((((select.OrderBy.Count != 0) || (select.GroupBy.Count != 0)) || ((select.Having != null) || (select.Top != null))) || (select.IsDistinct || (select.Where != null)))
     {
         return(false);
     }
     return(this.HasTrivialSource(select.From) && this.HasTrivialProjection(select));
 }
Exemple #14
0
 internal override SqlSelect VisitSelect(SqlSelect select)
 {
     if (this.isTopLevel)
     {
         select.Selection = this.sDeflator.VisitExpression(select.Selection);
     }
     return(select);
 }
Exemple #15
0
 internal override SqlSelect VisitSelect(SqlSelect select)
 {
     base.VisitSelect(select);
     if (select.Where != null)
     {
         this.CheckJoinCondition(select.Where);
     }
     return(select);
 }
Exemple #16
0
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                base.VisitSelect(select);
                SqlUnion union = this.GetUnion(select.From);

                if (union != null)
                {
                    SqlSelect left  = union.Left as SqlSelect;
                    SqlSelect right = union.Right as SqlSelect;
                    if (!((left != null) & (right != null)))
                    {
                        return(select);
                    }
                    int num   = 0;
                    int count = left.Row.Columns.Count;
                    while (num < count)
                    {
                        left.Row.Columns[num].Ordinal = select.Row.Columns.Count + num;
                        num++;
                    }
                    int num3 = 0;
                    int num4 = right.Row.Columns.Count;
                    while (num3 < num4)
                    {
                        right.Row.Columns[num3].Ordinal = select.Row.Columns.Count + num3;
                        num3++;
                    }
                    int num5 = 0;
                    int num6 = select.Row.Columns.Count;
                    while (num5 < num6)
                    {
                        SqlExprSet expression = select.Row.Columns[num5].Expression as SqlExprSet;
                        if (expression != null)
                        {
                            int num7 = 0;
                            int num8 = expression.Expressions.Count;
                            while (num7 < num8)
                            {
                                SqlColumnRef ref2 = expression.Expressions[num7] as SqlColumnRef;
                                if ((ref2 != null) && (num7 >= select.Row.Columns.Count))
                                {
                                    ref2.Column.Ordinal = num5;
                                }
                                num7++;
                            }
                        }
                        num5++;
                    }
                    Comparison <SqlColumn> comparison = delegate(SqlColumn x, SqlColumn y)
                    {
                        return(x.Ordinal - y.Ordinal);
                    };
                    left.Row.Columns.Sort(comparison);
                    right.Row.Columns.Sort(comparison);
                }
                return(select);
            }
Exemple #17
0
 private bool HasTrivialProjection(SqlSelect select)
 {
     foreach (SqlColumn column in select.Row.Columns)
     {
         if ((column.Expression != null) && (column.Expression.NodeType != SqlNodeType.ColumnRef))
         {
             return(false);
         }
     }
     return(true);
 }
Exemple #18
0
            internal override SqlStatement VisitUpdate(SqlUpdate su)
            {
                SqlSelect        select      = (SqlSelect)this.Visit(su.Select);
                List <SqlAssign> assignments = new List <SqlAssign>(su.Assignments.Count);

                foreach (SqlAssign assign in su.Assignments)
                {
                    assignments.Add((SqlAssign)this.Visit(assign));
                }
                return(new SqlUpdate(select, assignments, su.SourceExpression));
            }
Exemple #19
0
            // Methods
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                select.From  = this.VisitSource(select.From);
                select.Where = this.VisitExpression(select.Where);
                int num   = 0;
                int count = select.GroupBy.Count;

                while (num < count)
                {
                    select.GroupBy[num] = this.VisitExpression(select.GroupBy[num]);
                    num++;
                }
                for (int i = select.GroupBy.Count - 1; i >= 0; i--)
                {
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (SqlComparer.AreEqual(select.GroupBy[i], select.GroupBy[j]))
                        {
                            select.GroupBy.RemoveAt(i);
                            break;
                        }
                    }
                }
                select.Having = this.VisitExpression(select.Having);
                int num5 = 0;
                int num6 = select.OrderBy.Count;

                while (num5 < num6)
                {
                    select.OrderBy[num5].Expression = this.VisitExpression(select.OrderBy[num5].Expression);
                    num5++;
                }
                if (select.OrderBy.Count > 0)
                {
                    this.equalizer.BuildEqivalenceMap(select.From);
                    for (int k = select.OrderBy.Count - 1; k >= 0; k--)
                    {
                        for (int m = k - 1; m >= 0; m--)
                        {
                            if (this.equalizer.AreEquivalent(select.OrderBy[k].Expression, select.OrderBy[m].Expression))
                            {
                                select.OrderBy.RemoveAt(k);
                                break;
                            }
                        }
                    }
                }
                select.Top       = this.VisitExpression(select.Top);
                select.Row       = (SqlRow)this.Visit(select.Row);
                select.Selection = this.VisitExpression(select.Selection);
                return(select);
            }
Exemple #20
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));
            }
Exemple #21
0
            private void ConvertColumnsToMax(SqlSelect select, out bool changed, out bool containsLongExpressions)
            {
                SqlRow row = select.Row;

                changed = false;
                containsLongExpressions = false;
                foreach (SqlColumn column in row.Columns)
                {
                    bool flag;
                    containsLongExpressions = containsLongExpressions || column.SqlType.IsLargeType;
                    column.Expression       = this.ConvertToMax(column.Expression, out flag);
                    changed = changed || flag;
                }
            }
Exemple #22
0
 // Methods
 internal SqlSubSelect(SqlNodeType nt, Type clrType, IProviderType sqlType, SqlSelect select)
     : base(nt, clrType, sqlType, select.SourceExpression)
 {
     switch (nt)
     {
     case SqlNodeType.Element:
     case SqlNodeType.Exists:
     case SqlNodeType.Multiset:
     case SqlNodeType.ScalarSubSelect:
         this.Select = select;
         return;
     }
     throw Error.UnexpectedNode(nt);
 }
Exemple #23
0
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                SelectScope expressionSink = this.expressionSink;

                if (select.Top != null)
                {
                    this.expressionSink = null;
                }
                if ((select.GroupBy.Count > 0) || this.aggregateChecker.HasAggregates(select))
                {
                    this.expressionSink = null;
                }
                if (select.IsDistinct)
                {
                    this.expressionSink = null;
                }
                if (this.expressionSink != null)
                {
                    List <SqlColumn> collection = new List <SqlColumn>();
                    List <SqlColumn> item       = new List <SqlColumn>();
                    foreach (SqlColumn column in select.Row.Columns)
                    {
                        bool flag = SqlAliasesReferenced.ReferencesAny(column.Expression,
                                                                       this.expressionSink.LeftProduction);
                        bool flag2 = this.expressionSink.ReferencedExpressions.ContainsKey(column);
                        if (flag && !flag2)
                        {
                            item.Add(column);
                        }
                        else
                        {
                            collection.Add(column);
                        }
                    }
                    select.Row.Columns.Clear();
                    //select.Row.Columns.AddRange(collection);
                    foreach (var column in collection)
                    {
                        select.Row.Columns.Add(column);
                    }
                    if (item.Count > 0)
                    {
                        this.expressionSink.Lifted.Push(item);
                    }
                }
                SqlSelect select2 = base.VisitSelect(select);

                this.expressionSink = expressionSink;
                return(select2);
            }
Exemple #24
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);
            }
Exemple #25
0
            internal override SqlSelect VisitSelectCore(SqlSelect select)
            {
                SqlSelect select2;
                bool      isTopLevel = this.isTopLevel;

                this.isTopLevel = false;
                try
                {
                    select2 = base.VisitSelectCore(select);
                }
                finally
                {
                    this.isTopLevel = isTopLevel;
                }
                return(select2);
            }
Exemple #26
0
        internal IProviderType GetSqlType()
        {
            SqlExpression left = this.Left as SqlExpression;

            if (left != null)
            {
                return(left.SqlType);
            }
            SqlSelect select = this.Left as SqlSelect;

            if (select == null)
            {
                throw Error.CouldNotGetSqlType();
            }
            return(select.Selection.SqlType);
        }
Exemple #27
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);
            }
Exemple #28
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));
            }
Exemple #29
0
            internal override SqlSelect VisitSelect(SqlSelect select)
            {
                bool forceReferenceAll = this.forceReferenceAll;

                this.forceReferenceAll = false;
                bool isTopLevel = this.isTopLevel;

                try
                {
                    if (this.isTopLevel)
                    {
                        select.Selection = this.VisitExpression(select.Selection);
                    }
                    this.isTopLevel = false;
                    for (int i = select.Row.Columns.Count - 1; i >= 0; i--)
                    {
                        SqlColumn key = select.Row.Columns[i];
                        if (((!forceReferenceAll && !this.referenceMap.ContainsKey(key)) && !select.IsDistinct) &&
                            ((select.GroupBy.Count != 0) || !this.aggregateChecker.HasAggregates(key.Expression)))
                        {
                            select.Row.Columns.RemoveAt(i);
                        }
                        else
                        {
                            this.VisitExpression(key.Expression);
                        }
                    }
                    select.Top = this.VisitExpression(select.Top);
                    for (int j = select.OrderBy.Count - 1; j >= 0; j--)
                    {
                        select.OrderBy[j].Expression = this.VisitExpression(select.OrderBy[j].Expression);
                    }
                    select.Having = this.VisitExpression(select.Having);
                    for (int k = select.GroupBy.Count - 1; k >= 0; k--)
                    {
                        select.GroupBy[k] = this.VisitExpression(select.GroupBy[k]);
                    }
                    select.Where = this.VisitExpression(select.Where);
                    select.From  = this.VisitSource(select.From);
                }
                finally
                {
                    this.isTopLevel        = isTopLevel;
                    this.forceReferenceAll = forceReferenceAll;
                }
                return(select);
            }
Exemple #30
0
 internal override SqlSelect VisitSelect(SqlSelect select)
 {
     if (select.IsDistinct)
     {
         bool flag;
         bool flag2;
         this.ConvertColumnsToMax(select, out flag, out flag2);
         if (flag2)
         {
             this.annotations.Add(select,
                                  new SqlServerCompatibilityAnnotation(
                                      Strings.TextNTextAndImageCannotOccurInDistinct(select.SourceExpression),
                                      new SqlProvider.ProviderMode[]
                                      { SqlProvider.ProviderMode.Sql2000, SqlProvider.ProviderMode.SqlCE }));
         }
     }
     return(base.VisitSelect(select));
 }