Exemple #1
0
        /// <summary>
        /// create in expression ( operator "=in=" )
        /// </summary>
        /// <returns></returns>
        public static Expression <Func <T, bool> > GetInExpression <T>(ParameterExpression parameter,
                                                                       QueryParser.ComparisonContext context,
                                                                       NamingStrategy namingStrategy = null)
        {
            if (parameter == null)
            {
                throw new ArgumentException(nameof(parameter));
            }
            if (context == null)
            {
                throw new ArgumentException(nameof(context));
            }
            var expressionValue = ExpressionValue.Parse <T>(parameter, context.selector().GetText(), namingStrategy);

            if (!EqOrNeqOrInOrOutAutorizedType.Contains(expressionValue.Property.PropertyType))
            {
                throw new QueryComparisonInvalidComparatorSelectionException(context);
            }
            var values = QueryGetValueHelper.GetValues(expressionValue.Property.PropertyType, context.arguments());

            if (values == null || values.Count == 0)
            {
                throw new QueryComparisonNotEnoughtArgumentException(context);
            }

            var methodContainsInfo =
                QueryReflectionHelper.GetOrRegistryContainsMethodInfo(expressionValue.Property.PropertyType);

            return(Expression.Lambda <Func <T, bool> >(
                       Expression.Call(Expression.Constant(methodContainsInfo.Convert(values)),
                                       methodContainsInfo.ContainsMethod,
                                       expressionValue.Expression), parameter));
        }
Exemple #2
0
        /// <summary>
        /// create like expression
        /// </summary>
        /// <returns></returns>
        private static Expression <Func <T, bool> > GetLkExpression <T>(ParameterExpression parameter,
                                                                        QueryParser.ComparisonContext context,
                                                                        NamingStrategy namingStrategy = null)
        {
            var expressionValue = ExpressionValue.Parse <T>(parameter, context.selector().GetText(), namingStrategy);

            if (expressionValue.Property.PropertyType != typeof(string))
            {
                throw new QueryComparisonInvalidComparatorSelectionException(context);
            }
            var values = QueryGetValueHelper.GetValues(expressionValue.Property.PropertyType, context.arguments());

            if (values == null || values.Count == 0)
            {
                throw new QueryComparisonNotEnoughtArgumentException(context);
            }
            if (values.Count > 1)
            {
                throw new QueryComparisonTooManyArgumentException(context);
            }

            var criteria = Convert.ToString(values[0]);
            var maskStar = "{" + Guid.NewGuid().ToString() + "}";

            criteria = criteria.Replace(@"\*", maskStar);
            MethodInfo method;

            if (criteria.IndexOf('*') == -1)
            {
                criteria = criteria + '*';
            }
            if (criteria.StartsWith("*") && criteria.EndsWith("*"))
            {
                method = QueryReflectionHelper.MethodStringContains;
            }
            else if (criteria.StartsWith("*"))
            {
                method = QueryReflectionHelper.MethodStringEndsWith;
            }
            else
            {
                method = QueryReflectionHelper.MethodStringStartsWith;
            }
            criteria = criteria.Replace("*", "").Replace(maskStar, "*");
            return(Expression.Lambda <Func <T, bool> >(Expression.Call(expressionValue.Expression,
                                                                       method,
                                                                       Expression.Constant(criteria, expressionValue.Property.PropertyType)), parameter));
        }
Exemple #3
0
        /// <summary>
        /// create is null expression ( operator "=is-null=" or "=nil=" )
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="context"></param>
        /// <param name="namingStrategy"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Expression <Func <T, bool> > GetIsNullExpression <T>(ParameterExpression parameter,
                                                                           QueryParser.ComparisonContext context,
                                                                           NamingStrategy namingStrategy = null)
        {
            if (parameter == null)
            {
                throw new ArgumentException(nameof(parameter));
            }
            if (context == null)
            {
                throw new ArgumentException(nameof(context));
            }
            var expressionValue = ExpressionValue.Parse <T>(parameter, context.selector().GetText(), namingStrategy);

            if (expressionValue.Property.PropertyType.IsValueType &&
                !(expressionValue.Property.PropertyType.IsGenericType &&
                  expressionValue.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                throw new QueryComparisonInvalidComparatorSelectionException(context);
            }

            var values = QueryGetValueHelper.GetValues(typeof(bool), context.arguments());

            if (values == null || values.Count == 0)
            {
                throw new QueryComparisonNotEnoughtArgumentException(context);
            }
            if (values.Count > 1)
            {
                throw new QueryComparisonTooManyArgumentException(context);
            }

            var result = Expression.Lambda <Func <T, bool> >(Expression.Equal(
                                                                 expressionValue.Expression,
                                                                 Expression.Constant(null, typeof(object))), parameter);

            if ((bool)values[0])
            {
                return(result);
            }
            var body = Expression.Not(result.Body);

            result = Expression.Lambda <Func <T, bool> >(body, parameter);
            return(result);
        }
Exemple #4
0
        private static object GetUniqueValue <T>(ParameterExpression parameter, ExpressionValue expressionValue,
                                                 QueryParser.ComparisonContext context,
                                                 NamingStrategy namingStrategy = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var value = context.arguments().value();

            if (value.Length == 0)
            {
                throw new QueryComparisonNotEnoughtArgumentException(context);
            }
            if (value.Length > 1)
            {
                throw new QueryComparisonNotEnoughtArgumentException(context);
            }
            return(QueryGetValueHelper.GetValue <T>(parameter, expressionValue, context, namingStrategy));
        }