public string GenerateQuery(QueryModel queryModel, out ScalarResultBehavior scalarResultBehavior)
        {
            // If ITypeSerializer is an IExtendedTypeSerializer, use it as the member name resolver
            // Otherwise fallback to the legacy behavior which assumes we're using Newtonsoft.Json
            // Note that DefaultSerializer implements IExtendedTypeSerializer, but has the same logic as JsonNetMemberNameResolver

            var serializer = Serializer as IExtendedTypeSerializer;

#pragma warning disable CS0618 // Type or member is obsolete
            var memberNameResolver = serializer != null ?
                                     (IMemberNameResolver) new ExtendedTypeSerializerMemberNameResolver(serializer) :
                                     (IMemberNameResolver) new JsonNetMemberNameResolver(JsonConvert.DefaultSettings().ContractResolver);
#pragma warning restore CS0618 // Type or member is obsolete

            var methodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider();

            var queryGenerationContext = new N1QlQueryGenerationContext
            {
                MemberNameResolver           = memberNameResolver,
                MethodCallTranslatorProvider = methodCallTranslatorProvider,
                Serializer = serializer,
                // TODO: Don't use .Result
                ClusterVersion = _clusterVersionProvider.GetVersionAsync().Result ?? FeatureVersions.DefaultVersion,
                LoggerFactory  = _cluster.ClusterServices.GetRequiredService <ILoggerFactory>()
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);
            visitor.VisitQueryModel(queryModel);

            var query = visitor.GetQuery();
            _logger.LogDebug("Generated query: {0}", query);

            scalarResultBehavior = visitor.ScalarResultBehavior;
            return(query);
        }
        public string GenerateQuery(QueryModel queryModel, bool selectDocumentMetadata, out ScalarResultBehavior scalarResultBehavior)
        {
            // If ITypeSerializer is an IExtendedTypeSerializer, use it as the member name resolver
            // Otherwise fallback to the legacy behavior which assumes we're using Newtonsoft.Json
            // Note that DefaultSerializer implements IExtendedTypeSerializer, but has the same logic as JsonNetMemberNameResolver

            var serializer = Serializer as IExtendedTypeSerializer;

#pragma warning disable CS0618 // Type or member is obsolete
            var memberNameResolver = serializer != null ?
                                     (IMemberNameResolver) new ExtendedTypeSerializerMemberNameResolver(serializer) :
                                     (IMemberNameResolver) new JsonNetMemberNameResolver(_configuration.SerializationSettings.ContractResolver);
#pragma warning restore CS0618 // Type or member is obsolete

            var methodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider();

            var queryGenerationContext = new N1QlQueryGenerationContext
            {
                MemberNameResolver           = memberNameResolver,
                MethodCallTranslatorProvider = methodCallTranslatorProvider,
                Serializer             = serializer,
                SelectDocumentMetadata = selectDocumentMetadata,
                ClusterVersion         = VersionProvider.Current.GetVersion(_bucket)
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);
            visitor.VisitQueryModel(queryModel);

            var query = visitor.GetQuery();
            _log.Debug("Generated query: {0}", query);

            scalarResultBehavior = visitor.ScalarResultBehavior;
            return(query);
        }
예제 #3
0
        public void StringCompare_EqualToNeg1_ReturnsLessThan()
        {
            var queryGenerationContext = new N1QlQueryGenerationContext()
            {
                MemberNameResolver =
                    new JsonNetMemberNameResolver(new Newtonsoft.Json.Serialization.DefaultContractResolver()),
                MethodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider(),
                Serializer = new Core.Serialization.DefaultSerializer()
            };

            var visitor = new Mock <N1QlExpressionTreeVisitor>(queryGenerationContext)
            {
                CallBase = true
            };

            var string1 = Expression.Constant("A");
            var string2 = Expression.Constant("B");

            BinaryExpression expression = Expression.Equal(
                Expression.Call(
                    typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) }),
                    string1,
                    string2
                    ),
                Expression.Constant(-1));

            var newExpression = visitor.Object.ConvertStringCompareExpression(expression) as StringComparisonExpression;

            Assert.NotNull(newExpression);
            Assert.AreEqual(ExpressionType.LessThan, newExpression.Operation);
            Assert.AreEqual(string1, newExpression.Left);
            Assert.AreEqual(string2, newExpression.Right);
        }
예제 #4
0
        internal string CreateN1QlQuery(IBucket bucket, Expression expression, ClusterVersion clusterVersion,
                                        bool selectDocumentMetadata, out ScalarResultBehavior resultBehavior)
        {
            var serializer = new Core.Serialization.DefaultSerializer();

            var bucketContext = new Mock <IBucketContext>();

            bucketContext.SetupGet(p => p.Bucket).Returns(bucket);
            bucketContext.SetupGet(p => p.Configuration).Returns(new ClientConfiguration
            {
                Serializer = () => serializer
            });

            var queryModel = QueryParserHelper.CreateQueryParser(bucketContext.Object).GetParsedQuery(expression);

            var queryGenerationContext = new N1QlQueryGenerationContext()
            {
                MemberNameResolver           = MemberNameResolver,
                MethodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider(),
                Serializer             = serializer,
                SelectDocumentMetadata = selectDocumentMetadata,
                ClusterVersion         = clusterVersion
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);

            visitor.VisitQueryModel(queryModel);

            resultBehavior = visitor.ScalarResultBehavior;
            return(visitor.GetQuery());
        }
예제 #5
0
        internal string CreateN1QlQuery(IBucket bucket, Expression expression, ClusterVersion clusterVersion,
                                        bool selectDocumentMetadata, out ScalarResultBehavior resultBehavior)
        {
            var mockCluster = new Mock <ICluster>();

            mockCluster
            .Setup(p => p.ClusterServices)
            .Returns(ServiceProvider);

            var queryModel = QueryParserHelper.CreateQueryParser(mockCluster.Object).GetParsedQuery(expression);

            var queryGenerationContext = new N1QlQueryGenerationContext()
            {
                MemberNameResolver           = MemberNameResolver,
                MethodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider(),
                Serializer             = ServiceProvider.GetRequiredService <ITypeSerializer>(),
                SelectDocumentMetadata = selectDocumentMetadata,
                ClusterVersion         = clusterVersion,
                LoggerFactory          = LoggerFactory
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);

            visitor.VisitQueryModel(queryModel);

            resultBehavior = visitor.ScalarResultBehavior;
            return(visitor.GetQuery());
        }
        /// <summary>
        /// Tests an <see cref="Expression"/> returning a DateTime to see if it is serialized as
        /// Unix milliseconds using the <see cref="N1QlQueryGenerationContext.DateTimeSerializationFormatProvider"/>.
        /// </summary>
        /// <param name="context">The <see cref="N1QlQueryGenerationContext"/>.</param>
        /// <param name="expression">Expression which returns a DateTime.</param>
        /// <returns>True if the member is serialized as Unix milliseconds.</returns>
        public static bool IsUnixMillisecondsMember(this N1QlQueryGenerationContext context, Expression expression)
        {
            var memberInfo = ExtractMemberInfo(expression);

            if (memberInfo != null)
            {
                return(context.DateTimeSerializationFormatProvider.GetDateTimeSerializationFormat(memberInfo) ==
                       DateTimeSerializationFormat.UnixMilliseconds);
            }

            return(false);
        }
        public string ExecuteCollection(QueryModel queryModel)
        {
            var queryGenerationContext = new N1QlQueryGenerationContext()
            {
                MemberNameResolver           = Test.MemberNameResolver,
                MethodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider(),
                Serializer = new Core.Serialization.DefaultSerializer()
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);

            visitor.VisitQueryModel(queryModel);
            return(visitor.GetQuery());
        }
예제 #8
0
        protected string CreateN1QlQuery(IBucket bucket, Expression expression)
        {
            var queryModel = QueryParserHelper.CreateQueryParser().GetParsedQuery(expression);

            var queryGenerationContext = new N1QlQueryGenerationContext()
            {
                MemberNameResolver           = new JsonNetMemberNameResolver(_contractResolver),
                MethodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider(),
                Serializer = new Core.Serialization.DefaultSerializer()
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);

            visitor.VisitQueryModel(queryModel);
            return(visitor.GetQuery());
        }
예제 #9
0
        protected string CreateN1QlQuery(IBucket bucket, Expression expression, Version clusterVersion, bool selectDocumentMetadata)
        {
            var queryModel = QueryParserHelper.CreateQueryParser().GetParsedQuery(expression);

            var queryGenerationContext = new N1QlQueryGenerationContext()
            {
                MemberNameResolver           = MemberNameResolver,
                MethodCallTranslatorProvider = new DefaultMethodCallTranslatorProvider(),
                Serializer             = new Core.Serialization.DefaultSerializer(),
                SelectDocumentMetadata = selectDocumentMetadata,
                ClusterVersion         = clusterVersion
            };

            var visitor = new N1QlQueryModelVisitor(queryGenerationContext);

            visitor.VisitQueryModel(queryModel);
            return(visitor.GetQuery());
        }