Пример #1
0
        internal static SqlNode Reduce(SqlNode node, SqlNodeAnnotations annotations)
        {
            Reducer r = new Reducer();

            r.Annotations = annotations;
            return(r.Visit(node));
        }
Пример #2
0
        internal static HashSet <SqlAlias> Gather(SqlNode node)
        {
            Gatherer g = new Gatherer();

            g.Visit(node);
            return(g.Consumed);
        }
Пример #3
0
            internal override SqlNode Visit(SqlNode node)
            {
                SqlExpression expression = node as SqlExpression;

                if (expression != null)
                {
                    bool saveIsBlocked = this.isBlocked;
                    this.isBlocked = false;
                    if (CanRecurseColumnize(expression))
                    {
                        base.Visit(expression);
                    }
                    if (!this.isBlocked)
                    {
                        if (CanBeColumn(expression))
                        {
                            this.candidates.Add(expression);
                        }
                        else
                        {
                            this.isBlocked = true;
                        }
                    }
                    this.isBlocked |= saveIsBlocked;
                }
                return(node);
            }
Пример #4
0
        internal SqlNode TranslateLink(SqlLink link, List <SqlExpression> keyExpressions, bool asExpression)
        {
            var member = link.Member;

            if (member.IsAssociation)
            {
                var        otherType   = member.Association.OtherType;
                var        type        = otherType.InheritanceRoot.Type;
                var        table       = services.Context.GetTable(type);
                Expression otherSource = new LinkedTableExpression(link, table, typeof(IQueryable <>).MakeGenericType(otherType.Type));
                var        array       = new Expression[keyExpressions.Count];
                for (var i = 0; i < keyExpressions.Count; i++)
                {
                    var metaDataMember = member.Association.OtherKey[i];
                    var memberType     = TypeSystem.GetMemberType(metaDataMember.Member);
                    array[i] = InternalExpression.Known(keyExpressions[i], memberType);
                }
                var     thisInstance   = (link.Expression != null) ? ((Expression)InternalExpression.Known(link.Expression)) : ((Expression)Expression.Constant(null, link.Member.Member.DeclaringType));
                var     node           = TranslateAssociation(services.Context, member.Association, otherSource, array, thisInstance);
                var     queryConverter = new QueryConverter(services, typeProvider, this, sql);
                var     sqlSelect      = (SqlSelect)queryConverter.ConvertInner(node, link.SourceExpression);
                SqlNode result         = sqlSelect;
                if (asExpression)
                {
                    result = ((!member.Association.IsMany) ? new SqlSubSelect(SqlNodeType.Element, link.ClrType, link.SqlType, sqlSelect) : new SqlSubSelect(SqlNodeType.Multiset, link.ClrType, link.SqlType, sqlSelect));
                }
                return(result);
            }
            return(link.Expansion);
        }
Пример #5
0
        private List <SqlParameterInfo> ParameterizeInternal(SqlNode node)
        {
            Visitor v = new Visitor(this);

            v.Visit(node);
            return(new List <SqlParameterInfo>(v.currentParams));
        }
Пример #6
0
 /// <summary>
 /// Execute each current validator.
 /// </summary>
 internal void Validate(SqlNode node)
 {
     foreach (SqlVisitor validator in this.validators)
     {
         validator.Visit(node);
     }
 }
Пример #7
0
            private void GatherUnionExpressions(SqlNode node, List <SqlExpression> exprs)
            {
                SqlUnion union = node as SqlUnion;

                if (union != null)
                {
                    this.GatherUnionExpressions(union.Left, exprs);
                    this.GatherUnionExpressions(union.Right, exprs);
                }
                else
                {
                    SqlSelect sel = node as SqlSelect;
                    if (sel != null)
                    {
                        SqlAliasRef aref = sel.Selection as SqlAliasRef;
                        if (aref != null)
                        {
                            this.GatherUnionExpressions(aref.Alias.Node, exprs);
                        }
                        else
                        {
                            exprs.Add(sel.Selection);
                        }
                    }
                }
            }
Пример #8
0
 internal SqlColumn BubbleUp(SqlColumn col, SqlNode source)
 {
     this.match = this.GetOriginatingColumn(col);
     this.found = null;
     this.Visit(source);
     return(this.found);
 }
Пример #9
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 ss = node as SqlSelect;

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

                if (exp != null)
                {
                    return(this.VisitExpression(exp));
                }
                throw Error.CouldNotHandleAliasRef(node.NodeType);
            }
Пример #10
0
            internal static bool IsDependent(SqlNode node, HashSet <SqlAlias> aliasesToCheck, HashSet <SqlExpression> ignoreExpressions)
            {
                Visitor v = new Visitor(aliasesToCheck, ignoreExpressions);

                v.Visit(node);
                return(v.hasDependency);
            }
Пример #11
0
            internal static HashSet <SqlColumn> Gather(SqlNode node, HashSet <SqlColumn> columns)
            {
                Visitor v = new Visitor(columns);

                v.Visit(node);
                return(columns);
            }
        internal static SqlNode Lift(SqlNode node)
        {
            ColumnLifter cl = new ColumnLifter();

            node = cl.Visit(node);
            return(node);
        }
        /// <summary>
        /// Gets the annotations for the given node. Null if none.
        /// </summary>
        internal List <SqlNodeAnnotation> Get(SqlNode node)
        {
            List <SqlNodeAnnotation> list = null;

            this.annotationMap.TryGetValue(node, out list);
            return(list);
        }
Пример #14
0
 internal SqlUnion(SqlNode left, SqlNode right, bool all)
     : base(SqlNodeType.Union, right.SourceExpression)
 {
     Left  = left;
     Right = right;
     All   = all;
 }
Пример #15
0
        /// <summary>
        /// Returns true iff the given node references any aliases the list of 'aliases'.
        /// </summary>
        internal static bool ReferencesAny(SqlNode node, IEnumerable <SqlAlias> aliases)
        {
            Visitor visitor = new Visitor();

            visitor.aliases = aliases;
            visitor.Visit(node);
            return(visitor.referencesAnyMatchingAliases);
        }
 /// <summary>
 /// Whether the given node has annotations.
 /// </summary>
 internal bool NodeIsAnnotated(SqlNode node)
 {
     if (node == null)
     {
         return(false);
     }
     return(this.annotationMap.ContainsKey(node));
 }
Пример #17
0
 internal SqlNode Deflate(SqlNode node)
 {
     node = this.vDeflator.Visit(node);
     node = this.cDeflator.Visit(node);
     node = this.aDeflator.Visit(node);
     node = this.tsDeflator.Visit(node);
     node = this.dupColumnDeflator.Visit(node);
     return(node);
 }
Пример #18
0
 internal override SqlNode Visit(SqlNode node)
 {
     // Short-circuit when the answer is alreading known
     if (this.referencesAnyMatchingAliases)
     {
         return(node);
     }
     return(base.Visit(node));
 }
        /// <summary>
        /// Get a MetaType that represents the dynamic type of the given node.
        /// </summary>
        internal static MetaType GetSourceMetaType(SqlNode node, MetaModel model)
        {
            Visitor v = new Visitor();

            v.Visit(node);
            Type type = v.sourceType;

            type = TypeSystem.GetNonNullableType(type); // Emulate CLR's behavior: strip nullability from type.
            return(model.GetMetaType(type));
        }
Пример #20
0
            internal override SqlNode VisitUnion(SqlUnion su)
            {
                Scope save = this.current;

                this.current = null;
                SqlNode result = base.VisitUnion(su);

                this.current = save;
                return(result);
            }
Пример #21
0
                internal override SqlNode Visit(SqlNode node)
                {
                    SqlExpression e = node as SqlExpression;

                    if (this.hasDependency)
                    {
                        return(node);
                    }
                    if (e != null && this.ignoreExpressions.Contains(e))
                    {
                        return(node);
                    }
                    return(base.Visit(node));
                }
Пример #22
0
        internal static SqlNode Copy(SqlNode node)
        {
            if (node == null)
            {
                return(null);
            }
            var nodeType = node.NodeType;

            if (nodeType == SqlNodeType.ColumnRef || nodeType == SqlNodeType.Parameter || (uint)(nodeType - 92) <= 1u)
            {
                return(node);
            }
            return(new SqlDuplicator().Duplicate(node));
        }
        /// <summary>
        /// Add an annotation to the given node.
        /// </summary>
        internal void Add(SqlNode node, SqlNodeAnnotation annotation)
        {
            List <SqlNodeAnnotation> list = null;

            if (!this.annotationMap.TryGetValue(node, out list))
            {
                list = new List <SqlNodeAnnotation>();
                this.annotationMap[node] = list;
            }

            uniqueTypes[annotation.GetType()] = String.Empty;

            list.Add(annotation);
        }
Пример #24
0
        internal SqlNode TranslateLink(SqlLink link, List <SqlExpression> keyExpressions, bool asExpression)
        {
            MetaDataMember mm = link.Member;

            if (mm.IsAssociation)
            {
                // Create the row source.
                MetaType   otherType = mm.Association.OtherType;
                Type       tableType = otherType.InheritanceRoot.Type;
                ITable     table     = this.services.Context.GetTable(tableType);
                Expression source    = new LinkedTableExpression(link, table, typeof(IQueryable <>).MakeGenericType(otherType.Type));
                // Build key expression nodes.
                Expression[] keyExprs = new Expression[keyExpressions.Count];
                for (int i = 0; i < keyExpressions.Count; ++i)
                {
                    MetaDataMember metaMember = mm.Association.OtherKey[i];
                    Type           memberType = TypeSystem.GetMemberType(metaMember.Member);
                    keyExprs[i] = InternalExpression.Known(keyExpressions[i], memberType);
                }
                Expression lex = link.Expression != null
                    ? (Expression)InternalExpression.Known(link.Expression)
                    : (Expression)Expression.Constant(null, link.Member.Member.DeclaringType);
                Expression expr = TranslateAssociation(this.services.Context, mm.Association, source, keyExprs, lex);
                // Convert
                QueryConverter qc  = new QueryConverter(this.services, this.typeProvider, this, this.sql);
                SqlSelect      sel = (SqlSelect)qc.ConvertInner(expr, link.SourceExpression);
                // Turn it into an expression is necessary
                SqlNode result = sel;
                if (asExpression)
                {
                    if (mm.Association.IsMany)
                    {
                        result = new SqlSubSelect(SqlNodeType.Multiset, link.ClrType, link.SqlType, sel);
                    }
                    else
                    {
                        result = new SqlSubSelect(SqlNodeType.Element, link.ClrType, link.SqlType, sel);
                    }
                }
                return(result);
            }
            else
            {
                System.Diagnostics.Debug.Assert(link.Expansion != null);
                System.Diagnostics.Debug.Assert(link.KeyExpressions == keyExpressions);
                // deferred expression already defined...
                return(link.Expansion);
            }
        }
 internal override SqlNode Visit(SqlNode node)
 {
     if (annotations.NodeIsAnnotated(node))
     {
         foreach (SqlNodeAnnotation annotation in annotations.Get(node))
         {
             SqlServerCompatibilityAnnotation ssca = annotation as SqlServerCompatibilityAnnotation;
             if (ssca != null && ssca.AppliesTo(provider))
             {
                 reasons.Add(annotation.Message);
             }
         }
     }
     return(base.Visit(node));
 }
Пример #26
0
            internal override SqlNode Visit(SqlNode node)
            {
                if (node == null)
                {
                    return(null);
                }
                SqlNode value = null;

                if (nodeMap.TryGetValue(node, out value))
                {
                    return(value);
                }
                value         = base.Visit(node);
                nodeMap[node] = value;
                return(value);
            }
        /// <summary>
        /// Checks whether the given node is supported on the given server.
        /// </summary>
        internal static void ThrowIfUnsupported(SqlNode node, SqlNodeAnnotations annotations, SqlProvider.ProviderMode provider)
        {
            // Check to see whether there's at least one SqlServerCompatibilityAnnotation.
            if (annotations.HasAnnotationType(typeof(SqlServerCompatibilityAnnotation)))
            {
                Visitor visitor = new Visitor(provider);
                visitor.annotations = annotations;
                visitor.Visit(node);

                // If any messages were recorded, then throw an exception.
                if (visitor.reasons.Count > 0)
                {
                    throw Error.ExpressionNotSupportedForSqlServerVersion(visitor.reasons);
                }
            }
        }
            internal override SqlNode Visit(SqlNode node)
            {
                if (node == null)
                {
                    return(null);
                }
                SqlNode result = null;

                if (this.nodeMap.TryGetValue(node, out result))
                {
                    return(result);
                }
                result             = base.Visit(node);
                this.nodeMap[node] = result;
                return(result);
            }
        internal static SqlNode Copy(SqlNode node)
        {
            if (node == null)
            {
                return(null);
            }
            switch (node.NodeType)
            {
            case SqlNodeType.ColumnRef:
            case SqlNodeType.Value:
            case SqlNodeType.Parameter:
            case SqlNodeType.Variable:
                return(node);

            default:
                return(new SqlDuplicator().Duplicate(node));
            }
        }
Пример #30
0
        internal ReadOnlyCollection <ReadOnlyCollection <SqlParameterInfo> > ParameterizeBlock(SqlBlock block)
        {
            SqlParameterInfo rowStatus =
                new SqlParameterInfo(
                    new SqlParameter(typeof(int), typeProvider.From(typeof(int)), "@ROWCOUNT", block.SourceExpression)
                    );
            List <ReadOnlyCollection <SqlParameterInfo> > list = new List <ReadOnlyCollection <SqlParameterInfo> >();

            for (int i = 0, n = block.Statements.Count; i < n; i++)
            {
                SqlNode statement = block.Statements[i];
                List <SqlParameterInfo> parameters = this.ParameterizeInternal(statement);
                if (i > 0)
                {
                    parameters.Add(rowStatus);
                }
                list.Add(parameters.AsReadOnly());
            }
            return(list.AsReadOnly());
        }