A computed expression. Defined by a VarDefOp
Inheritance: System.Data.Entity.Core.Query.InternalTrees.Var
示例#1
0
        internal virtual ComputedVar CreateComputedVar(TypeUsage type)
        {
            ComputedVar computedVar = new ComputedVar(this.NewVarId(), type);

            this.m_vars.Add((Var)computedVar);
            return(computedVar);
        }
        public void Translate_preserves_column_type()
        {
            var intTypeUsage = 
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));
            var enumTypeUsage = 
                TypeUsage.CreateDefaultTypeUsage(
                    new EnumType("ns", "DayOfWeek", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32), false, DataSpace.CSpace));

            var originalVar = new ComputedVar(42, intTypeUsage);
            var originalColumnMap = new VarRefColumnMap(enumTypeUsage, "dayOfWeek", originalVar);

            var replacementVar = new ComputedVar(911, intTypeUsage);
            var replacementColumnMap = new VarRefColumnMap(intTypeUsage, null, replacementVar);

            var varToColumnMap = new Dictionary<Var, ColumnMap> { { originalVar, replacementColumnMap } };

            var resultColumnMap = ColumnMapTranslator.Translate(originalColumnMap, varToColumnMap);

            Assert.Same(replacementColumnMap, resultColumnMap);
            Assert.Equal(originalColumnMap.Name, resultColumnMap.Name);
            Assert.Equal(originalColumnMap.Type.EdmType, resultColumnMap.Type.EdmType);
        }
        private void FlattenComputedVar(ComputedVar v, Node node, out List<Node> newNodes, out md.TypeUsage newType)
        {
            newNodes = new List<Node>();
            var definingExprNode = node.Child0; // defining expression for the VarDefOp
            newType = null;

            if (TypeUtils.IsCollectionType(v.Type))
            {
                PlanCompiler.Assert(definingExprNode.Op.OpType != OpType.Function, "Flattening of TVF output is not allowed.");
                newType = GetNewType(v.Type);
                Var newVar;
                var newVarDefNode = m_command.CreateVarDefNode(definingExprNode, out newVar);
                newNodes.Add(newVarDefNode);
                m_varInfoMap.CreateCollectionVarInfo(v, newVar);
                return;
            }

            // Get the "new" type for the Var
            var typeInfo = m_typeInfo.GetTypeInfo(v.Type);
            // Get a list of properties that we think are necessary 
            var desiredProperties = m_varPropertyMap[v];
            var newVars = new List<Var>();
            var newProps = new List<md.EdmProperty>();
            newNodes = new List<Node>();
            var hasNullSentinelVar = false;
            foreach (var p in typeInfo.PropertyRefList)
            {
                // do I care for this property?
                if (!desiredProperties.Contains(p))
                {
                    continue;
                }

                var newProperty = typeInfo.GetNewProperty(p);

                //
                // #479467 - Make sure that we build Vars for all properties - if
                // we are asked to produce all properties. This is extremely important
                // for the top-level Vars
                // 
                Node propAccessor = null;
                if (desiredProperties.AllProperties)
                {
                    propAccessor = BuildAccessorWithNulls(definingExprNode, newProperty);
                }
                else
                {
                    propAccessor = BuildAccessor(definingExprNode, newProperty);
                    if (propAccessor == null)
                    {
                        continue;
                    }
                }

                // Add the new property
                newProps.Add(newProperty);

                // Create a new VarDefOp. 
                Var newVar;
                var newVarDefNode = m_command.CreateVarDefNode(propAccessor, out newVar);
                newNodes.Add(newVarDefNode);
                newVars.Add(newVar);

                // Check if it is a null sentinel var
                if (!hasNullSentinelVar
                    && IsNullSentinelPropertyRef(p))
                {
                    hasNullSentinelVar = true;
                }
            }
            m_varInfoMap.CreateStructuredVarInfo(v, typeInfo.FlattenedType, newVars, newProps, hasNullSentinelVar);
            return;
        }
示例#4
0
 // <summary>
 // Creates a computed var (ie) a variable that is computed by an expression
 // </summary>
 // <param name="type"> The type of the result produced by the expression that defines the variable </param>
 // <returns> A new ComputedVar instance with the specified result type </returns>
 internal virtual ComputedVar CreateComputedVar(TypeUsage type)
 {
     var v = new ComputedVar(NewVarId(), type);
     m_vars.Add(v);
     return v;
 }