Ejemplo n.º 1
0
        private static object GetBoolean <T>(ParameterExpression parameter, RSqlQueryParser.ValueContext valueContext,
                                             JsonNamingPolicy jsonNamingPolicy = null)
        {
            if (bool.TryParse(valueContext.GetText(), out var result))
            {
                return(result);
            }

            if (ExpressionValue.TryParse <T>(parameter, valueContext.GetText(), jsonNamingPolicy, out var value))
            {
                return(value);
            }

            throw new InvalidConversionException(valueContext, typeof(bool));
        }
Ejemplo n.º 2
0
        private static object GetDecimal <T>(ParameterExpression parameter, RSqlQueryParser.ValueContext valueContext,
                                             JsonNamingPolicy jsonNamingPolicy = null)
        {
            if (decimal.TryParse(
                    valueContext.GetText().Replace(".",
                                                   Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator), out var result))
            {
                return(result);
            }

            if (ExpressionValue.TryParse <T>(parameter, valueContext.GetText(), jsonNamingPolicy, out var value))
            {
                return(value);
            }

            throw new InvalidConversionException(valueContext, typeof(decimal));
        }
Ejemplo n.º 3
0
        private static object GetDateTimeOffset <T>(ParameterExpression parameter, RSqlQueryParser.ValueContext valueContext,
                                                    JsonNamingPolicy jsonNamingPolicy = null)
        {
            try
            {
                return(DateTimeOffset.Parse(valueContext.GetText(), CultureInfo.InvariantCulture,
                                            DateTimeStyles.RoundtripKind));
            }
            catch
            {
                if (ExpressionValue.TryParse <T>(parameter, valueContext.GetText(), jsonNamingPolicy, out var value))
                {
                    return(value);
                }

                throw new InvalidConversionException(valueContext, typeof(DateTimeOffset));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// try to convert value context to enum values
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="valueContext"></param>
        /// <param name="enumType"></param>
        /// <param name="jsonNamingPolicy"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="InvalidConversionException"></exception>
        private static object GetEnum <T>(
            ParameterExpression parameter,
            RSqlQueryParser.ValueContext valueContext,
            Type enumType,
            JsonNamingPolicy jsonNamingPolicy = null)
        {
            if (Enum.TryParse(enumType.IsGenericType ? enumType.GetGenericArguments()[0] : enumType,
                              valueContext.GetText(), true, out var result))
            {
                return(result);
            }

            if (ExpressionValue.TryParse <T>(parameter, valueContext.GetText(), jsonNamingPolicy, out var expression))
            {
                return(expression);
            }

            throw new InvalidConversionException(valueContext, enumType);
        }
Ejemplo n.º 5
0
        private static object GetString <T>(ParameterExpression parameter,
                                            RSqlQueryParser.ValueContext valueContext,
                                            JsonNamingPolicy jsonNamingPolicy = null)
        {
            if (valueContext.single_quote() != null || valueContext.double_quote() != null)
            {
                var replace = valueContext.single_quote() != null ? "'" : "\"";
                var value   = valueContext.GetText();
                return(value.Length == 2
                    ? string.Empty
                    : value.Substring(1, value.Length - 2).Replace("\\" + replace, replace));
            }

            if (ExpressionValue.TryParse <T>(parameter, valueContext.GetText(), jsonNamingPolicy, out var expression))
            {
                return(expression);
            }

            return(valueContext.GetText());
        }
 public InvalidConversionException(RSqlQueryParser.ValueContext origin, Type type,
                                   Exception innerException = null) : base(origin,
                                                                           $"{origin?.GetText()} is not convertible to {type?.Namespace}.{type?.Name}",
                                                                           innerException)
 {
 }