Ejemplo 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);
            }
Ejemplo n.º 2
0
            internal override SqlAlias VisitAlias(SqlAlias a)
            {
                if (!IsTableAlias(a) || !this.addPrimaryKeys)
                {
                    return(base.VisitAlias(a));
                }
                SqlTable node = (SqlTable)a.Node;
                List <SqlOrderExpression> exprs = new List <SqlOrderExpression>();

                foreach (MetaDataMember member in node.RowType.IdentityMembers)
                {
                    string    mappedName = member.MappedName;
                    SqlColumn item       = node.Find(mappedName);
                    if (item == null)
                    {
                        item = new SqlColumn(member.MemberAccessor.Type,
                                             this.typeProvider.From(member.MemberAccessor.Type), mappedName, member,
                                             null, node.SourceExpression);
                        item.Alias = a;
                        node.Columns.Add(item);
                    }
                    exprs.Add(new SqlOrderExpression(SqlOrderType.Ascending, new SqlColumnRef(item)));
                }
                this.PrependOrderExpressions(exprs);
                return(a);
            }
Ejemplo n.º 3
0
            internal override SqlAlias VisitAlias(SqlAlias sqlAlias)
            {
                SqlAlias alias = this.alias;

                this.alias    = sqlAlias;
                sqlAlias.Node = this.Visit(sqlAlias.Node);
                this.alias    = alias;
                return(sqlAlias);
            }
Ejemplo n.º 4
0
 // Methods
 internal SqlAliasRef(SqlAlias alias)
     : base(SqlNodeType.AliasRef, GetClrType(alias.Node), alias.SourceExpression)
 {
     if (alias == null)
     {
         throw Error.ArgumentNull("alias");
     }
     this.alias = alias;
 }
Ejemplo n.º 5
0
            internal override SqlAlias VisitAlias(SqlAlias a)
            {
                SqlAlias alias = new SqlAlias(a.Node);

                this.nodeMap[a] = alias;
                alias.Node      = this.Visit(a.Node);
                alias.Name      = a.Name;
                return(alias);
            }
Ejemplo n.º 6
0
            private bool HasTrivialSource(SqlSource node)
            {
                SqlAlias alias = node as SqlAlias;

                if (alias == null)
                {
                    return(false);
                }
                return(alias.Node is SqlSelect);
            }
Ejemplo n.º 7
0
            internal override SqlExpression VisitAliasRef(SqlAliasRef aref)
            {
                SqlExpression expression = base.VisitAliasRef(aref);

                if ((expression != null) && (expression == aref))
                {
                    SqlAlias alias = aref.Alias;
                    return(new SqlAliasRef(new SqlAlias(new SqlNop(aref.ClrType, aref.SqlType, null))));
                }
                return(expression);
            }
Ejemplo n.º 8
0
            internal override SqlExpression VisitAliasRef(SqlAliasRef aref)
            {
                SqlAlias alias2;
                SqlAlias key = aref.Alias;

                if (this.removedMap.TryGetValue(key, out alias2))
                {
                    throw Error.InvalidReferenceToRemovedAliasDuringDeflation();
                }
                return(aref);
            }
Ejemplo n.º 9
0
        internal SqlSelect BuildDefaultQuery(MetaType rowType, bool allowDeferred, SqlLink link, Expression source)
        {
            if (rowType.HasInheritance && (rowType.InheritanceRoot != rowType))
            {
                throw Error.ArgumentWrongValue("rowType");
            }
            SqlTable node  = sql.Table(rowType.Table, rowType, source);
            var      alias = new SqlAlias(node);
            var      item  = new SqlAliasRef(alias);

            return(new SqlSelect(BuildProjection(item, node.RowType, allowDeferred, link, source), alias, source));
        }
Ejemplo n.º 10
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);
            }
Ejemplo n.º 11
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));
            }
Ejemplo n.º 12
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);
            }
Ejemplo n.º 13
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);
             }
         }
     }
 }
Ejemplo n.º 14
0
 internal SqlAlias VisitAliasConsumed(SqlAlias a)
 {
     if (a != null)
     {
         bool flag = false;
         foreach (SqlAlias alias in this.aliases)
         {
             if (alias == a)
             {
                 flag = true;
                 break;
             }
         }
         if (flag)
         {
             this.referencesAnyMatchingAliases = true;
         }
     }
     return(a);
 }
Ejemplo n.º 15
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);
            }
Ejemplo n.º 16
0
 internal override SqlExpression VisitMultiset(SqlSubSelect sms)
 {
     if (((((this.options & SqlMultiplexer.Options.EnableBigJoin) != SqlMultiplexer.Options.None) && !this.hasBigJoin) &&
          (this.canJoin && this.isTopLevel)) && (((this.outerSelect != null) && !MultisetChecker.HasMultiset(sms.Select.Selection)) &&
                                                 BigJoinChecker.CanBigJoin(sms.Select)))
     {
         sms.Select = this.VisitSelect(sms.Select);
         SqlAlias right = new SqlAlias(sms.Select);
         SqlJoin  join  = new SqlJoin(SqlJoinType.OuterApply, this.outerSelect.From, right, null, sms.SourceExpression);
         this.outerSelect.From         = join;
         this.outerSelect.OrderingType = SqlOrderingType.Always;
         SqlExpression expression = (SqlExpression)SqlDuplicator.Copy(sms.Select.Selection);
         SqlSelect     node       = (SqlSelect)SqlDuplicator.Copy(sms.Select);
         SqlAlias      from       = new SqlAlias(node);
         SqlSelect     select     = new SqlSelect(this.sql.Unary(SqlNodeType.Count, null, sms.SourceExpression), from, sms.SourceExpression);
         select.OrderingType = SqlOrderingType.Never;
         SqlExpression       count   = this.sql.SubSelect(SqlNodeType.ScalarSubSelect, select);
         SqlJoinedCollection joineds = new SqlJoinedCollection(sms.ClrType, sms.SqlType, expression, count, sms.SourceExpression);
         this.hasBigJoin = true;
         return(joineds);
     }
     return(QueryExtractor.Extract(sms, this.parentParameters, sqlIdentity));
 }
Ejemplo n.º 17
0
            internal override SqlSource VisitJoin(SqlJoin join)
            {
                base.VisitJoin(join);
                switch (join.JoinType)
                {
                case SqlJoinType.Cross:
                case SqlJoinType.Inner:
                    return(join);

                case SqlJoinType.LeftOuter:
                case SqlJoinType.CrossApply:
                case SqlJoinType.OuterApply:
                {
                    if (!this.HasEmptySource(join.Right))
                    {
                        return(join);
                    }
                    SqlAlias right = (SqlAlias)join.Right;
                    this.removedMap[right] = right;
                    return(join.Left);
                }
                }
                return(join);
            }
Ejemplo n.º 18
0
 // Methods
 internal void VisitAliasConsumed(SqlAlias a)
 {
     this.Consumed[a] = true;
 }
Ejemplo n.º 19
0
 internal virtual SqlAlias VisitAlias(SqlAlias a)
 {
     a.Node = this.Visit(a.Node);
     return(a);
 }
Ejemplo n.º 20
0
        internal SqlExpression TranslateEquals(SqlBinary expr)
        {
            IList <SqlExpression> keyExpressions;
            IList <SqlExpression> identityExpressions;
            SqlExpression         left  = expr.Left;
            SqlExpression         right = expr.Right;

            if (right.NodeType == SqlNodeType.Element)
            {
                var select    = (SqlSubSelect)right;
                var alias     = new SqlAlias(select.Select);
                var selection = new SqlAliasRef(alias);
                var select2   = new SqlSelect(selection, alias, expr.SourceExpression)
                {
                    Where = sql.Binary(expr.NodeType, sql.DoNotVisitExpression(left), selection)
                };
                return(sql.SubSelect(SqlNodeType.Exists, select2));
            }
            if (left.NodeType == SqlNodeType.Element)
            {
                var select3 = (SqlSubSelect)left;
                var alias2  = new SqlAlias(select3.Select);
                var ref3    = new SqlAliasRef(alias2);
                var select4 = new SqlSelect(ref3, alias2, expr.SourceExpression)
                {
                    Where = sql.Binary(expr.NodeType, sql.DoNotVisitExpression(right), ref3)
                };
                return(sql.SubSelect(SqlNodeType.Exists, select4));
            }
            var sourceMetaType = TypeSource.GetSourceMetaType(left, services.Model);
            var type           = TypeSource.GetSourceMetaType(right, services.Model);

            if (left.NodeType == SqlNodeType.TypeCase)
            {
                left = BestIdentityNode((SqlTypeCase)left);
            }
            if (right.NodeType == SqlNodeType.TypeCase)
            {
                right = BestIdentityNode((SqlTypeCase)right);
            }
            if ((sourceMetaType.IsEntity && type.IsEntity) && (sourceMetaType.Table != type.Table))
            {
                throw Error.CannotCompareItemsAssociatedWithDifferentTable();
            }
            if (((!sourceMetaType.IsEntity && !type.IsEntity) && ((left.NodeType != SqlNodeType.New) || left.SqlType.CanBeColumn)) && ((right.NodeType != SqlNodeType.New) || right.SqlType.CanBeColumn))
            {
                if ((expr.NodeType == SqlNodeType.EQ2V) || (expr.NodeType == SqlNodeType.NE2V))
                {
                    return(TranslateEqualsOp(expr.NodeType, sql.DoNotVisitExpression(expr.Left),
                                             sql.DoNotVisitExpression(expr.Right), false));
                }
                return(expr);
            }
            if ((sourceMetaType != type) && (sourceMetaType.InheritanceRoot != type.InheritanceRoot))
            {
                return(this.sql.Binary(SqlNodeType.EQ, sql.ValueFromObject(0, expr.SourceExpression), this.sql.ValueFromObject(1, expr.SourceExpression)));
            }
            var link = left as SqlLink;

            if (((link != null) && link.Member.IsAssociation) && link.Member.Association.IsForeignKey)
            {
                keyExpressions = link.KeyExpressions;
            }
            else
            {
                keyExpressions = this.GetIdentityExpressions(sourceMetaType, this.sql.DoNotVisitExpression(left));
            }
            var link2 = right as SqlLink;

            if (((link2 != null) && link2.Member.IsAssociation) && link2.Member.Association.IsForeignKey)
            {
                identityExpressions = link2.KeyExpressions;
            }
            else
            {
                identityExpressions = this.GetIdentityExpressions(type, sql.DoNotVisitExpression(right));
            }
            SqlExpression expression3 = null;
            SqlNodeType   op          = ((expr.NodeType == SqlNodeType.EQ2V) || (expr.NodeType == SqlNodeType.NE2V)) ? SqlNodeType.EQ2V : SqlNodeType.EQ;
            int           num         = 0;
            int           count       = keyExpressions.Count;

            while (num < count)
            {
                SqlExpression expression4 = this.TranslateEqualsOp(op, keyExpressions[num], identityExpressions[num], !sourceMetaType.IsEntity);
                if (expression3 == null)
                {
                    expression3 = expression4;
                }
                else
                {
                    expression3 = this.sql.Binary(SqlNodeType.And, expression3, expression4);
                }
                num++;
            }
            if ((expr.NodeType != SqlNodeType.NE) && (expr.NodeType != SqlNodeType.NE2V))
            {
                return(expression3);
            }
            return(this.sql.Unary(SqlNodeType.Not, expression3, expression3.SourceExpression));
        }
Ejemplo n.º 21
0
 // Methods
 internal override SqlAlias VisitAlias(SqlAlias a)
 {
     this.Produced[a] = true;
     return(base.VisitAlias(a));
 }
Ejemplo n.º 22
0
            private static bool IsTableAlias(SqlSource src)
            {
                SqlAlias alias = src as SqlAlias;

                return((alias != null) && (alias.Node is SqlTable));
            }