Ejemplo n.º 1
0
 protected override Expression VisitConstant(ConstantExpression node)
 {
     if (node.Value == null)
     {
         Clause.Append("NULL");
     }
     else
     {
         var value = QueryEscape.EscapeFieldOrTag(node.Value);
         Clause.Append(value);
     }
     return(node);
 }
Ejemplo n.º 2
0
        internal PropertyExpressionInfo(string key, PropertyInfo property)
        {
            Property = property;

            // Instance type of target entity class
            ParameterExpression instanceParam = Expression.Parameter(typeof(TInfluxRow), "x");

            // Instance type of target entity class
            ParameterExpression valueParam = Expression.Parameter(typeof(object), "y");

            // instance.Property
            MemberExpression getProperty = Expression.Property(instanceParam, property);

            // instance.Property = row[property]
            BinaryExpression assignProperty = Expression.Assign(getProperty, Expression.Convert(valueParam, property.PropertyType));

            var getterLambda = Expression.Lambda <Func <TInfluxRow, object> >(Expression.Convert(getProperty, typeof(object)), true, instanceParam);

            GetValue = getterLambda.Compile();

            var setterLambda = Expression.Lambda <Action <TInfluxRow, object> >(assignProperty, true, instanceParam, valueParam);

            SetValue = setterLambda.Compile();

            var type = property.PropertyType;

            if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                // unwrap the nullable type
                Type = type.GetGenericArguments()[0];
            }
            else
            {
                Type = type;
            }

            IsEnum                  = Type.GetTypeInfo().IsEnum;
            IsDateTime              = Type == typeof(DateTime);
            LineProtocolEscapedKey  = LineProtocolEscape.EscapeKey(key);
            QueryProtocolEscapedKey = QueryEscape.EscapeKey(key);
            Key = key;

            // ensure we can convert between string/enum
            if (IsEnum)
            {
                EnumToString = new Dictionary <Enum, string>();
                StringToEnum = new Dictionary <string, Enum>();

                var values = Enum.GetValues(Type);
                foreach (Enum value in values)
                {
                    string stringValue = value.ToString();
                    var    memberInfo  = Type.GetField(stringValue);
                    if (memberInfo != null)
                    {
                        var attribute = memberInfo.GetCustomAttribute <EnumMemberAttribute>();
                        if (attribute != null)
                        {
                            stringValue = attribute.Value;
                        }
                    }

                    EnumToString.Add(value, stringValue);
                    StringToEnum.Add(stringValue, value);
                }
            }
        }