Example #1
0
        public void CombinedTest()
        {
            var parameter = new Parameter <int>();

            Assert.AreEqual(null, parameter.ExpectedValue);
            Assert.IsFalse(parameter.IsExpectedValueSet);

            parameter = new Parameter <int>(1);

            Assert.AreEqual(1, parameter.ExpectedValue);

            AssertEx.Throws <Exception>(delegate {
                parameter.Value = 5;
            });

            var firstContext = new ParameterContext();

            using (firstContext.Activate()) {
                AssertEx.Throws <InvalidOperationException>(delegate {
                    int i = parameter.Value;
                });

                parameter.Value = 10;
                Assert.AreEqual(10, parameter.Value);

                using (ParameterContext.ExpectedValues.Activate()) {
                    Assert.AreEqual(1, parameter.Value);
                    AssertEx.ThrowsInvalidOperationException(() => parameter.Value = 1);
                }

                Assert.AreEqual(10, parameter.Value);

                parameter.Value = 15;
                Assert.AreEqual(15, parameter.Value);

                using (new ParameterContext().Activate()) {
                    Assert.AreEqual(15, parameter.Value);

                    parameter.Value = 20;
                    Assert.AreEqual(20, parameter.Value);

                    // Reactivating first context
                    using (firstContext.Activate()) {
                        Assert.AreEqual(15, parameter.Value);
                        parameter.Value = 25;
                    }

                    Assert.AreEqual(20, parameter.Value);
                }

                Assert.AreEqual(25, parameter.Value);
            }

            AssertEx.Throws <Exception>(delegate {
                int i = parameter.Value;
            });
        }
        private static IEnumerable <TResult> SubqueryMaterialize <TResult>(IEnumerable <TResult> materializedSequence, ParameterContext parameterContext)
        {
            ParameterScope scope         = null;
            var            batchSequence = materializedSequence
                                           .Batch(BatchFastFirstCount, BatchMinSize, BatchMaxSize)
                                           .ApplyBeforeAndAfter(
                () => scope = parameterContext.Activate(),
                () => scope.DisposeSafely());

            return(batchSequence.SelectMany(batch => batch));
        }
        private ParameterContext CreateParameterContext <TResult>(ParameterizedQuery <TResult> query)
        {
            var parameterContext = new ParameterContext();

            if (query.QueryParameter != null)
            {
                using (parameterContext.Activate())
                    query.QueryParameter.Value = queryTarget;
            }
            return(parameterContext);
        }
        private QueryTask CreateQueryTask(List <Tuple> currentKeySet)
        {
            var parameterContext = new ParameterContext();

            using (parameterContext.Activate()) {
                includeParameter.Value = currentKeySet;
                object key = new Pair <object, CacheKey>(recordSetCachingRegion, cacheKey);
                Func <object, object> generator = CreateRecordSet;
                var session = manager.Owner.Session;
                Provider = (CompilableProvider)session.StorageNode.InternalQueryCache.GetOrAdd(key, generator);
                var executableProvider = session.Compile(Provider);
                return(new QueryTask(executableProvider, session.GetLifetimeToken(), parameterContext));
            }
        }
        private QueryTask CreateQueryTask()
        {
            var parameterContext = new ParameterContext();

            using (parameterContext.Activate()) {
                ownerParameter.Value = ownerKey.Value;
                if (ItemCountLimit != null)
                {
                    itemCountLimitParameter.Value = ItemCountLimit.Value;
                }
                object key = new Pair <object, CacheKey>(itemsQueryCachingRegion, cacheKey);
                Func <object, object> generator = CreateRecordSetLoadingItems;
                var session = manager.Owner.Session;
                QueryProvider = (CompilableProvider)session.StorageNode.InternalQueryCache.GetOrAdd(key, generator);
                var executableProvider = session.Compile(QueryProvider);
                return(new QueryTask(executableProvider, session.GetLifetimeToken(), parameterContext));
            }
        }
        /// <summary>
        /// Materializes the specified data source.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="dataSource">The data source.</param>
        /// <param name="context">The context.</param>
        /// <param name="parameterContext">The parameter context.</param>
        /// <param name="itemMaterializer">The item materializer.</param>
        /// <param name="tupleParameterBindings">The tuple parameter bindings.</param>
        public static IEnumerable <TResult> Materialize <TResult>(IEnumerable <Tuple> dataSource, MaterializationContext context, ParameterContext parameterContext, Func <Tuple, ItemMaterializationContext, TResult> itemMaterializer, Dictionary <Parameter <Tuple>, Tuple> tupleParameterBindings)
        {
            using (parameterContext.Activate())
                foreach (var tupleParameterBinding in tupleParameterBindings)
                {
                    tupleParameterBinding.Key.Value = tupleParameterBinding.Value;
                }
            var session   = context.Session;
            var recordSet = dataSource as RecordSet;

            if (recordSet != null)
            {
                var enumerationContext = (EnumerationContext)recordSet.Context;
                enumerationContext.MaterializationContext = context;
            }
            var materializedSequence = dataSource
                                       .Select(tuple => itemMaterializer.Invoke(tuple, new ItemMaterializationContext(context, session)));

            return(context.MaterializationQueue == null
        ? BatchMaterialize(materializedSequence, context, parameterContext)
        : SubqueryMaterialize(materializedSequence, parameterContext));
        }
Example #7
0
        // Constructors

// ReSharper disable MemberCanBeProtected.Global
        public SubQuery(ProjectionExpression projectionExpression, TranslatedQuery query, Parameter <Tuple> parameter, Tuple tuple, ItemMaterializationContext context)
// ReSharper restore MemberCanBeProtected.Global
        {
            this.provider = context.Session.Query.Provider;
            var tupleParameterBindings = new Dictionary <Parameter <Tuple>, Tuple>(projectionExpression.TupleParameterBindings);
            var currentTranslatedQuery = ((TranslatedQuery <IEnumerable <TElement> >)query);

            // Gather Parameter<Tuple> values from current ParameterScope for future use.
            parameter.Value = tuple;
            foreach (var tupleParameter in currentTranslatedQuery.TupleParameters)
            {
                var value = tupleParameter.Value;
                tupleParameterBindings[tupleParameter] = value;
            }
            var parameterContext = new ParameterContext();

            using (parameterContext.Activate())
                foreach (var tupleParameter in currentTranslatedQuery.TupleParameters)
                {
                    tupleParameter.Value = tupleParameter.Value;
                }

            this.projectionExpression = new ProjectionExpression(
                projectionExpression.Type,
                projectionExpression.ItemProjector,
                tupleParameterBindings,
                projectionExpression.ResultType);
            var translatedQuery = new TranslatedQuery <IEnumerable <TElement> >(
                query.DataSource,
                (Func <IEnumerable <Tuple>, Session, Dictionary <Parameter <Tuple>, Tuple>, ParameterContext, IEnumerable <TElement> >)query.UntypedMaterializer,
                tupleParameterBindings,
                EnumerableUtils <Parameter <Tuple> > .Empty);

            delayedSequence = new DelayedSequence <TElement>(context.Session, translatedQuery, parameterContext);
            context.Session.RegisterUserDefinedDelayedQuery(delayedSequence.Task);
            context.MaterializationContext.MaterializationQueue.Enqueue(MaterializeSelf);
        }
        /// <summary>
        /// Gets the references to specified entity.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="association">The association.</param>
        /// <returns>References.</returns>
        public virtual IEnumerable <ReferenceInfo> GetReferencesTo(Entity target, AssociationInfo association)
        {
            if (association.IsPaired)
            {
                return(FindReferences(target, association, true));
            }
            object key = new Pair <object, AssociationInfo>(CachingRegion, association);
            Func <object, object> generator = p => BuildReferencingQuery(((Pair <object, AssociationInfo>)p).Second);
            var pair             = (Pair <CompilableProvider, Parameter <Tuple> >)Session.StorageNode.InternalQueryCache.GetOrAdd(key, generator);
            var recordSet        = pair.First;
            var parameter        = pair.Second;
            var parameterContext = new ParameterContext();
            ExecutableProvider executableProvider;

            using (parameterContext.Activate()) {
                parameter.Value    = target.Key.Value;
                executableProvider = Session.Compile(recordSet);
            }
            var queryTask = new QueryTask(executableProvider, Session.GetLifetimeToken(), parameterContext);

            Session.RegisterInternalDelayedQuery(queryTask);

            return(GetReferencesToInternal(association, target, recordSet.Header, queryTask));
        }
 public void Activate()
 {
     scope = parameterContext.Activate();
 }
 /// <summary>
 /// Activates specified <see cref="ParameterContext"/> if it is not null;
 /// otherwise does nothing.
 /// </summary>
 /// <param name="context">The context to activate.</param>
 /// <returns><see cref="ParameterScope"/> if <paramref name="context"/> is not <see langword="null"/>;
 /// otherwise <see langword="null"/>.</returns>
 public static ParameterScope ActivateSafely(this ParameterContext context)
 {
     return(context != null?context.Activate() : null);
 }