internal RecordState(RecordStateFactory recordStateFactory, CoordinatorFactory coordinatorFactory)
 {
     RecordStateFactory = recordStateFactory;
     CoordinatorFactory = coordinatorFactory;
     CurrentColumnValues = new object[RecordStateFactory.ColumnCount];
     PendingColumnValues = new object[RecordStateFactory.ColumnCount];
 }
        protected CoordinatorFactory(
            int depth, int stateSlot, Func<Shaper, bool> hasData, Func<Shaper, bool> setKeys, Func<Shaper, bool> checkKeys,
            CoordinatorFactory[] nestedCoordinators, RecordStateFactory[] recordStateFactories)
        {
            Depth = depth;
            StateSlot = stateSlot;

            // figure out if there are any nested coordinators
            IsLeafResult = 0 == nestedCoordinators.Length;

            // if there is no explicit 'has data' discriminator, it means all rows contain data for the coordinator
            if (hasData == null)
            {
                HasData = _alwaysTrue;
            }
            else
            {
                HasData = hasData;
            }

            // if there is no explicit set key delegate, just return true (the value is not used anyways)
            if (setKeys == null)
            {
                SetKeys = _alwaysTrue;
            }
            else
            {
                SetKeys = setKeys;
            }

            // If there are no keys, it means different things depending on whether we are a leaf
            // coordinator or an inner (or 'driving') coordinator. For a leaf coordinator, it means
            // that every row is a new result. For an inner coordinator, it means that there is no
            // key to check. This should only occur where there is a SingleRowTable (in other words,
            // all rows are elements of a single child collection).
            if (checkKeys == null)
            {
                if (IsLeafResult)
                {
                    CheckKeys = _alwaysFalse; // every row is a new result (the keys don't match)
                }
                else
                {
                    CheckKeys = _alwaysTrue; // every row belongs to a single child collection
                }
            }
            else
            {
                CheckKeys = checkKeys;
            }
            NestedCoordinators = new ReadOnlyCollection<CoordinatorFactory>(nestedCoordinators);
            RecordStateFactories = new ReadOnlyCollection<RecordStateFactory>(recordStateFactories);

            // Determines whether this coordinator can be handled by a 'simple' enumerator. See IsSimple for details.
            IsSimple = IsLeafResult && null == checkKeys && null == hasData;
        }
        internal RecordStateFactory Compile()
        {
            RecordStateFactory[] nestedRecordStateFactories = new RecordStateFactory[_nestedRecordStateScratchpads.Count];
            for (int i = 0; i < nestedRecordStateFactories.Length; i++)
            {
                nestedRecordStateFactories[i] = _nestedRecordStateScratchpads[i].Compile();
            }

            RecordStateFactory result = (RecordStateFactory)Activator.CreateInstance(typeof(RecordStateFactory), new object[] {
                                                            this.StateSlotNumber, 
                                                            this.ColumnCount,
                                                            nestedRecordStateFactories,
                                                            this.DataRecordInfo,
                                                            this.GatherData,
                                                            this.PropertyNames,
                                                            this.TypeUsages
                                                            });
            return result;
        }
        internal RecordStateFactory Compile()
        {
            var nestedRecordStateFactories = new RecordStateFactory[_nestedRecordStateScratchpads.Count];
            for (var i = 0; i < nestedRecordStateFactories.Length; i++)
            {
                nestedRecordStateFactories[i] = _nestedRecordStateScratchpads[i].Compile();
            }

            var result = (RecordStateFactory)Activator.CreateInstance(
                typeof(RecordStateFactory), new object[]
                    {
                        StateSlotNumber,
                        ColumnCount,
                        nestedRecordStateFactories,
                        DataRecordInfo,
                        GatherData,
                        PropertyNames,
                        TypeUsages
                    });
            return result;
        }
        public RecordStateFactory(int stateSlotNumber, int columnCount, RecordStateFactory[] nestedRecordStateFactories, DataRecordInfo dataRecordInfo, Expression gatherData, string[] propertyNames, TypeUsage[] typeUsages)
        {
            this.StateSlotNumber = stateSlotNumber;
            this.ColumnCount = columnCount;
            this.NestedRecordStateFactories = new System.Collections.ObjectModel.ReadOnlyCollection<RecordStateFactory>(nestedRecordStateFactories);
            this.DataRecordInfo = dataRecordInfo;
            this.GatherData = Translator.Compile<bool>(gatherData);
            this.Description = gatherData.ToString();
            this.ColumnNames = new System.Collections.ObjectModel.ReadOnlyCollection<string>(propertyNames);
            this.TypeUsages = new System.Collections.ObjectModel.ReadOnlyCollection<TypeUsage>(typeUsages);

            this.FieldNameLookup = new FieldNameLookup(this.ColumnNames, -1);

            // pre-compute the nested objects from typeUsage, for performance
            bool[] isColumnNested = new bool[columnCount];

            for (int ordinal = 0; ordinal < columnCount; ordinal++)
            {
                switch (typeUsages[ordinal].EdmType.BuiltInTypeKind)
                {
                    case BuiltInTypeKind.EntityType:
                    case BuiltInTypeKind.ComplexType:
                    case BuiltInTypeKind.RowType:
                    case BuiltInTypeKind.CollectionType:
                        isColumnNested[ordinal] = true;
                        this.HasNestedColumns = true;
                        break;
                    default:
                        isColumnNested[ordinal] = false;
                        break;
                }
            }
            this.IsColumnNested = new System.Collections.ObjectModel.ReadOnlyCollection<bool>(isColumnNested);
        }
        internal CoordinatorFactory Compile()
        {
            RecordStateFactory[] recordStateFactories;
            if (null != _recordStateScratchpads)
            {
                recordStateFactories = new RecordStateFactory[_recordStateScratchpads.Count];
                for (var i = 0; i < recordStateFactories.Length; i++)
                {
                    recordStateFactories[i] = _recordStateScratchpads[i].Compile();
                }
            }
            else
            {
                recordStateFactories = new RecordStateFactory[0];
            }

            var nestedCoordinators = new CoordinatorFactory[_nestedCoordinatorScratchpads.Count];
            for (var i = 0; i < nestedCoordinators.Length; i++)
            {
                nestedCoordinators[i] = _nestedCoordinatorScratchpads[i].Compile();
            }

            // compile inline delegates
            var replacementVisitor = new ReplacementExpressionVisitor(null, _inlineDelegates);
            var element = new SecurityBoundaryExpressionVisitor().Visit(replacementVisitor.Visit(Element));

            // substitute expressions that have error handlers into a new expression (used
            // when a more detailed exception message is needed)
            replacementVisitor = new ReplacementExpressionVisitor(_expressionWithErrorHandlingMap, _inlineDelegates);
            var elementWithErrorHandling = new SecurityBoundaryExpressionVisitor().Visit(replacementVisitor.Visit(Element));

            var result =
                (CoordinatorFactory)Activator.CreateInstance(
                    typeof(CoordinatorFactory<>).MakeGenericType(_elementType), new object[]
                        {
                            Depth,
                            StateSlotNumber,
                            HasData,
                            SetKeys,
                            CheckKeys,
                            nestedCoordinators,
                            element,
                            elementWithErrorHandling,
                            InitializeCollection,
                            recordStateFactories
                        });
            return result;
        }
        public RecordStateFactory(
            int stateSlotNumber, int columnCount, RecordStateFactory[] nestedRecordStateFactories, DataRecordInfo dataRecordInfo,
            Expression<Func<Shaper, bool>> gatherData, string[] propertyNames, TypeUsage[] typeUsages, bool[] isColumnNested)
        {
            StateSlotNumber = stateSlotNumber;
            ColumnCount = columnCount;
            NestedRecordStateFactories = new ReadOnlyCollection<RecordStateFactory>(nestedRecordStateFactories);
            DataRecordInfo = dataRecordInfo;
            GatherData = gatherData.Compile();
            Description = gatherData.ToString();
            ColumnNames = new ReadOnlyCollection<string>(propertyNames);
            TypeUsages = new ReadOnlyCollection<TypeUsage>(typeUsages);

            FieldNameLookup = new FieldNameLookup(ColumnNames, -1);

            // pre-compute the nested objects from typeUsage, for performance
            if (isColumnNested == null)
            {
                isColumnNested = new bool[columnCount];

                for (var ordinal = 0; ordinal < columnCount; ordinal++)
                {
                    switch (typeUsages[ordinal].EdmType.BuiltInTypeKind)
                    {
                        case BuiltInTypeKind.EntityType:
                        case BuiltInTypeKind.ComplexType:
                        case BuiltInTypeKind.RowType:
                        case BuiltInTypeKind.CollectionType:
                            isColumnNested[ordinal] = true;
                            HasNestedColumns = true;
                            break;
                        default:
                            isColumnNested[ordinal] = false;
                            break;
                    }
                }
            }
            IsColumnNested = new ReadOnlyCollection<bool>(isColumnNested);
        }
 public RecordStateFactory(
     int stateSlotNumber, int columnCount, RecordStateFactory[] nestedRecordStateFactories, DataRecordInfo dataRecordInfo,
     Expression gatherData, string[] propertyNames, TypeUsage[] typeUsages)
     : this(stateSlotNumber, columnCount, nestedRecordStateFactories, dataRecordInfo,
         CodeGenEmitter.BuildShaperLambda<bool>(gatherData), propertyNames, typeUsages, isColumnNested: null)
 {
 }