public override Expression CompileBulkImporter(EnumStorage enumStorage, Expression writer, ParameterExpression document, ParameterExpression alias, ParameterExpression serializer)
        {
            var method = writeMethod.MakeGenericMethod(typeof(string));
            var dbType = Expression.Constant(DbType);

            return Expression.Call(writer, method, alias, dbType);
        }
Beispiel #2
0
        public EnumIsOneOfWhereFragment(object values, EnumStorage enumStorage, string locator)
        {
            var array = values.As<Array>();
            if (enumStorage == EnumStorage.AsInteger)
            {
                var numbers = new int[array.Length];

                for (int i = 0; i < array.Length; i++)
                {
                    numbers[i] = array.GetValue(i).As<int>();
                }

                _values = numbers;
                _dbType = NpgsqlDbType.Integer | NpgsqlDbType.Array;
            }
            else
            {
                var strings = new string[array.Length];

                for (int i = 0; i < array.Length; i++)
                {
                    strings[i] = array.GetValue(i).ToString();
                }

                _values = strings;
                _dbType = NpgsqlDbType.Varchar | NpgsqlDbType.Array;
            }

            _locator = locator;
        }
        public override Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion)
        {
            var argName = Expression.Constant(Arg);

            var serializer = Expression.Call(updateBatch, _serializer);
            var json = Expression.Call(serializer, _tojson, doc);

            return Expression.Call(call, _paramMethod, argName, json, Expression.Constant(NpgsqlDbType.Jsonb));
        }
        public override Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion)
        {
            var getType = Expression.Call(doc, _getType);
            var getName = Expression.Call(getType, _fullName);

            var argName = Expression.Constant(Arg);
            var dbType = Expression.Constant(DbType);

            return Expression.Call(call, _paramMethod, argName, getName, dbType);
        }
        public override Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion)
        {
            var argName = Expression.Constant(Arg);
            var dbType = Expression.Constant(NpgsqlDbType.Varchar);

            var type = Expression.Call(doc, _getType);
            var alias = Expression.Call(mapping, _getAlias, type);

            return Expression.Call(call, _paramMethod, argName, alias, dbType);
        }
Beispiel #6
0
        public virtual Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion)
        {
            var argName = Expression.Constant(Arg);

            var memberType = Members.Last().GetMemberType();
            var body = LambdaBuilder.ToExpression(enumStorage, Members, doc);
            if (!memberType.GetTypeInfo().IsClass)
            {
                body = Expression.Convert(body, typeof(object));
            }


            return Expression.Call(call, _paramMethod, argName, body, Expression.Constant(DbType));
        }
Beispiel #7
0
        public void can_build_getter_for_enum_expression(EnumStorage enumStorage)
        {
            Expression <Func <Target, Colors> > expression = t => t.Color;
            var visitor = new FindMembers();

            visitor.Visit(expression);

            var members = visitor.Members.ToArray();
            var getter  = LambdaBuilder.Getter <Target, Colors>(enumStorage, members);

            var target = new Target {
                Inner = new Target {
                    Color = Colors.Blue
                }
            };

            getter(target).ShouldBe(target.Color);
        }
        public static DuplicatedField For <T>(EnumStorage enumStorage, Expression <Func <T, object> > expression, bool useTimestampWithoutTimeZoneForDateTime = true, string pgType = null)
        {
            var accessor = ReflectionHelper.GetAccessor(expression);

            // Hokey, but it's just for testing for now.
            if (accessor is PropertyChain)
            {
                throw new NotSupportedException("Not yet supporting deep properties yet. Soon.");
            }

            var duplicate = new DuplicatedField(enumStorage, new MemberInfo[] { accessor.InnerProperty }, useTimestampWithoutTimeZoneForDateTime);

            if (pgType.IsNotEmpty())
            {
                duplicate.PgType = pgType;
            }
            return(duplicate);
        }
Beispiel #9
0
        public JsonLocatorField(string dataLocator, StoreOptions options, EnumStorage enumStyle, Casing casing, MemberInfo member) : base(enumStyle, member)
        {
            var memberType = member.GetMemberType();
            var memberName = member.Name.FormatCase(casing);

            var isStringEnum = memberType.IsEnum && enumStyle == EnumStorage.AsString;

            if (memberType == typeof(string) || isStringEnum)
            {
                SqlLocator = $"{dataLocator} ->> '{memberName}'";
            }
            else if (TypeMappings.TimespanTypes.Contains(memberType))
            {
                SqlLocator       = $"{options.DatabaseSchemaName}.mt_immutable_timestamp({dataLocator} ->> '{memberName}')";
                SelectionLocator = $"CAST({dataLocator} ->> '{memberName}' as {PgType})";
            }
            else if (TypeMappings.TimespanZTypes.Contains(memberType))
            {
                SqlLocator       = $"{options.DatabaseSchemaName}.mt_immutable_timestamptz({dataLocator} ->> '{memberName}')";
                SelectionLocator = $"CAST({dataLocator} ->> '{memberName}' as {PgType})";
            }
            else if (memberType.IsArray)
            {
                SqlLocator = $"CAST({dataLocator} ->> '{memberName}' as jsonb)";
            }
            else
            {
                SqlLocator = $"CAST({dataLocator} ->> '{memberName}' as {PgType})";
            }

            if (isStringEnum)
            {
                _parseObject = expression =>
                {
                    var raw = expression.Value();
                    return(Enum.GetName(MemberType, raw));
                };
            }

            if (SelectionLocator.IsEmpty())
            {
                SelectionLocator = SqlLocator;
            }
        }
Beispiel #10
0
        public virtual Expression CompileBulkImporter(EnumStorage enumStorage, Expression writer, ParameterExpression document, ParameterExpression alias, ParameterExpression serializer)
        {
            var memberType = Members.Last().GetMemberType();


            var value = LambdaBuilder.ToExpression(enumStorage, Members, document);

            if (memberType.IsEnum)
            {
                memberType = typeof(string);
                value      = LambdaBuilder.ToExpression(EnumStorage.AsString, Members, document);
            }

            var method = writeMethod.MakeGenericMethod(memberType);

            var dbType = Expression.Constant(DbType);

            return(Expression.Call(writer, method, value, dbType));
        }
Beispiel #11
0
        public virtual Expression CompileBulkImporter(EnumStorage enumStorage, Expression writer, ParameterExpression document, ParameterExpression alias, ParameterExpression serializer)
        {
            var memberType = Members.Last().GetMemberType();


            var value = LambdaBuilder.ToExpression(enumStorage, Members, document);

            if (memberType.GetTypeInfo().IsEnum)
            {
                memberType = typeof(string);
                value = LambdaBuilder.ToExpression(EnumStorage.AsString, Members, document);
            }

            var method = writeMethod.MakeGenericMethod(memberType);

            var dbType = Expression.Constant(DbType);

            return Expression.Call(writer, method, value, dbType);
        }
Beispiel #12
0
        public DuplicatedField(EnumStorage enumStorage, MemberInfo[] memberPath) : base(enumStorage, memberPath)
        {
            ColumnName = MemberName.ToTableAlias();

            if (MemberType.IsEnum)
            {
                if (enumStorage == EnumStorage.AsString)
                {
                    DbType = NpgsqlDbType.Varchar;
                    PgType = "varchar";

                    _parseObject = expression =>
                    {
                        var raw = expression.Value();
                        return(Enum.GetName(MemberType, raw));
                    };
                }
                else
                {
                    DbType = NpgsqlDbType.Integer;
                    PgType = "integer";
                }
            }
            else if (MemberType.IsDateTime())
            {
                PgType = "timestamp without time zone";
                DbType = NpgsqlDbType.Timestamp;
            }
            else if (MemberType.IsDateTime())
            {
                PgType = "timestamp without time zone";
                DbType = NpgsqlDbType.Timestamp;
            }
            else if (MemberType == typeof(DateTimeOffset) || MemberType == typeof(DateTimeOffset?))
            {
                PgType = "timestamp with time zone";
                DbType = NpgsqlDbType.TimestampTz;
            }
            else
            {
                DbType = TypeMappings.ToDbType(MemberType);
            }
        }
Beispiel #13
0
        public DuplicatedField(EnumStorage enumStorage, IField innerField,
                               bool useTimestampWithoutTimeZoneForDateTime = true, bool notNull = false) : base(enumStorage, innerField.Members)
        {
            InnerField = innerField;
            NotNull    = notNull;
            ColumnName = MemberName.ToTableAlias();
            this.useTimestampWithoutTimeZoneForDateTime = useTimestampWithoutTimeZoneForDateTime;

            if (FieldType.IsEnum)
            {
                if (enumStorage == EnumStorage.AsString)
                {
                    DbType = NpgsqlDbType.Varchar;
                    PgType = "varchar";

                    _parseObject = expression =>
                    {
                        var raw = expression.Value();
                        return(Enum.GetName(FieldType, raw));
                    };
                }
                else
                {
                    DbType = NpgsqlDbType.Integer;
                    PgType = "integer";
                }
            }
            else if (FieldType.IsDateTime())
            {
                PgType = this.useTimestampWithoutTimeZoneForDateTime ? "timestamp without time zone" : "timestamp with time zone";
                DbType = this.useTimestampWithoutTimeZoneForDateTime ? NpgsqlDbType.Timestamp : NpgsqlDbType.TimestampTz;
            }
            else if (FieldType == typeof(DateTimeOffset) || FieldType == typeof(DateTimeOffset?))
            {
                PgType = "timestamp with time zone";
                DbType = NpgsqlDbType.TimestampTz;
            }
            else
            {
                DbType = TypeMappings.ToDbType(FieldType);
            }
        }
Beispiel #14
0
        public override Expression CompileBulkImporter(EnumStorage enumStorage, Expression writer, ParameterExpression document, ParameterExpression alias, ParameterExpression serializer, ParameterExpression textWriter, bool useCharBufferPooling)
        {
            if (useCharBufferPooling)
            {
                var method = writeMethod.MakeGenericMethod(typeof(ArraySegment <char>));
                var dbType = Expression.Constant(DbType);

                return(Expression.Block(
                           Expression.Call(serializer, _tojsonWithWriter, document, textWriter),
                           Expression.Call(writer, method, Expression.Call(textWriter, _toSegment), dbType)
                           ));
            }
            else
            {
                var json   = Expression.Call(serializer, _tojson, document);
                var method = writeMethod.MakeGenericMethod(typeof(string));
                var dbType = Expression.Constant(DbType);

                return(Expression.Call(writer, method, json, dbType));
            }
        }
Beispiel #15
0
        public DuplicatedField(EnumStorage enumStorage, MemberInfo[] memberPath) : base(memberPath)
        {
            _enumStorage = enumStorage;
            _dbType      = TypeMappings.ToDbType(MemberType);



            ColumnName = MemberName.ToTableAlias();

            if (MemberType.IsEnum)
            {
                _parseObject = expression =>
                {
                    var raw = expression.Value();
                    return(Enum.GetName(MemberType, raw));
                };

                _dbType = NpgsqlDbType.Varchar;
                PgType  = "varchar";
            }
        }
Beispiel #16
0
        public JsonLocatorField(StoreOptions options, EnumStorage enumStyle, MemberInfo member) : base(member)
        {
            var memberType = member.GetMemberType();

            var isStringEnum = memberType.GetTypeInfo().IsEnum&& enumStyle == EnumStorage.AsString;

            if (memberType == typeof(string) || isStringEnum)
            {
                SqlLocator = $"d.data ->> '{member.Name}'";
            }
            else if (memberType == typeof(DateTime) || memberType == typeof(DateTime?))
            {
                SqlLocator       = $"{options.DatabaseSchemaName}.mt_immutable_timestamp(d.data ->> '{member.Name}')";
                SelectionLocator = $"CAST(d.data ->> '{member.Name}' as {PgType})";
            }
            else if (memberType == typeof(DateTimeOffset) || memberType == typeof(DateTimeOffset?))
            {
                SqlLocator       = $"{options.DatabaseSchemaName}.mt_immutable_timestamp(d.data ->> '{member.Name}')";
                SelectionLocator = $"CAST(d.data ->> '{member.Name}' as {PgType})";
            }
            else
            {
                SqlLocator = $"CAST(d.data ->> '{member.Name}' as {PgType})";
            }

            if (isStringEnum)
            {
                _parseObject = expression =>
                {
                    var raw = expression.Value();
                    return(Enum.GetName(MemberType, raw));
                };
            }

            if (SelectionLocator.IsEmpty())
            {
                SelectionLocator = SqlLocator;
            }
        }
Beispiel #17
0
        public void get_sum_of_integers_with_where_with_nullable_enum(EnumStorage enumStorage)
        {
            StoreOptions(o => o.UseDefaultSerialization(enumStorage));

            theSession.Store(new Target {
                NullableColor = Colors.Blue, Number = 1
            });
            theSession.Store(new Target {
                NullableColor = Colors.Red, Number = 2
            });
            theSession.Store(new Target {
                NullableColor = Colors.Green, Number = 3
            });
            theSession.Store(new Target {
                NullableColor = null, Number = 4
            });

            theSession.SaveChanges();
            theSession.Query <Target>()
            .Where(x => x.NullableColor != null)
            .Sum(x => x.Number)
            .ShouldBe(6);
        }
Beispiel #18
0
        public static Expression ToExpression(EnumStorage enumStorage, MemberInfo[] members, ParameterExpression target)
        {
            Expression body = target;

            foreach (var member in members)
            {
                if (member is PropertyInfo)
                {
                    var propertyInfo = member.As <PropertyInfo>();
                    var getMethod    = propertyInfo.GetGetMethod();

                    body = Expression.Call(body, getMethod);
                }
                else
                {
                    var field = member.As <FieldInfo>();
                    body = Expression.Field(body, field);
                }
            }

            var memberType = members.Last().GetMemberType();

            if (memberType.GetTypeInfo().IsEnum)
            {
                if (enumStorage == EnumStorage.AsString)
                {
                    body = Expression.Call(_getEnumStringValue, Expression.Constant(memberType), Expression.Convert(body, typeof(object)));
                }
                else
                {
                    body = Expression.Call(_getEnumIntValue, Expression.Convert(body, typeof(object)));
                }
            }

            return(body);
        }
Beispiel #19
0
        public JsonLocatorField(string dataLocator, EnumStorage enumStyle, Casing casing, MemberInfo[] members) : base(enumStyle, members)
        {
            var locator = dataLocator;

            for (int i = 0; i < members.Length - 1; i++)
            {
                locator += $" -> '{members[i].Name.FormatCase(casing)}'";
            }

            locator += $" ->> '{members.Last().Name.FormatCase(casing)}'";

            SqlLocator = MemberType == typeof(string) ? locator : locator.ApplyCastToLocator(enumStyle, MemberType);

            var isStringEnum = MemberType.IsEnum && enumStyle == EnumStorage.AsString;

            if (isStringEnum)
            {
                _parseObject = expression =>
                {
                    var raw = expression.Value();
                    return(Enum.GetName(MemberType, raw));
                };
            }
        }
Beispiel #20
0
        public override Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion, ParameterExpression tenantId, bool useCharBufferPooling)
        {
            var argName    = Expression.Constant(Arg);
            var serializer = Expression.Call(updateBatch, _serializer);
            var jsonb      = Expression.Constant(NpgsqlDbType.Jsonb);

            if (useCharBufferPooling == false)
            {
                var json = Expression.Call(serializer, _tojson, doc);
                return(Expression.Call(call, _paramMethod, argName, json, jsonb));
            }
            else
            {
                var writer  = Expression.Variable(typeof(CharArrayTextWriter), "writer");
                var segment = Expression.Variable(typeof(ArraySegment <char>), "segment");

                return(Expression.Block(new[] { writer, segment },
                                        Expression.Assign(writer, Expression.Call(updateBatch, _getWriter)),
                                        Expression.Call(serializer, _tojsonWithWriter, doc, writer),
                                        Expression.Assign(segment, Expression.Call(writer, _writerToSegment)),
                                        Expression.Call(call, _paramWithJsonBody, argName, segment)
                                        ));
            }
        }
Beispiel #21
0
        private static IList <IDbParameterSetter> findSetters(IQueryableDocument mapping, Type queryType, Expression expression, EnumStorage enumStorage)
        {
            var visitor = new CompiledQueryMemberExpressionVisitor(mapping, queryType, enumStorage);

            visitor.Visit(expression);
            var parameterSetters = visitor.ParameterSetters;

            return(parameterSetters);
        }
Beispiel #22
0
 public CompiledQueryMemberExpressionVisitor(IQueryableDocument mapping, Type queryType, EnumStorage enumStorage)
 {
     _mapping     = mapping;
     _queryType   = queryType;
     _enumStorage = enumStorage;
 }
Beispiel #23
0
        public override Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion, ParameterExpression tenantId, bool useCharBufferPooling)
        {
            var dbType = Expression.Constant(DbType);

            return(Expression.Call(call, _paramMethod, Expression.Constant(Arg), Expression.Convert(newVersion, typeof(object)), dbType));
        }
        public override Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion)
        {
            var argName = Expression.Constant(Arg);

            return Expression.Call(call, _paramMethod, argName, Expression.Convert(currentVersion, typeof(object)), Expression.Constant(DbType));
        }
Beispiel #25
0
 protected Field(EnumStorage enumStorage, MemberInfo member, bool notNull = false) : this(enumStorage, new[] { member }, notNull)
 {
 }
Beispiel #26
0
 public JsonLocatorField(string dataLocator, EnumStorage enumStyle, Casing casing, MemberInfo[] members) :
     this(dataLocator, null, enumStyle, casing, members, null)
 {
 }
        private static object GetMemberValue(MemberInfo member, object expressionValue, EnumStorage enumStorage)
        {
            var value = expressionValue;

            var memberType = member.GetMemberType();

            if (memberType.IsEnum && enumStorage == EnumStorage.AsString)
            {
                value = Enum.GetName(memberType, value);
            }

            return(value);
        }
Beispiel #28
0
 public override Expression CompileBulkImporter(EnumStorage enumStorage, Expression writer,
                                                ParameterExpression document,
                                                ParameterExpression alias, ParameterExpression serializer)
 {
     throw new NotSupportedException("This should not be used for CurrentVersionArgument");
 }
 public override Expression CompileBulkImporter(EnumStorage enumStorage, Expression writer,
     ParameterExpression document,
     ParameterExpression alias, ParameterExpression serializer)
 {
     throw new NotSupportedException("This should not be used for CurrentVersionArgument");
 }
Beispiel #30
0
 public static Expression ToExpression(EnumStorage enumStorage, MemberInfo[] members, ParameterExpression target)
 {
Beispiel #31
0
 public abstract string GetDatabaseType(Type memberType, EnumStorage enumStyle);
Beispiel #32
0
 /// <summary>
 ///     Use the default serialization (ilmerged Newtonsoft.Json) with Enum values
 ///     stored as either integers or strings
 /// </summary>
 /// <param name="enumStyle"></param>
 public void UseDefaultSerialization(EnumStorage enumStyle)
 {
     Serializer(new JsonNetSerializer {
         EnumStorage = enumStyle
     });
 }
Beispiel #33
0
        public static JsonLocatorField For <T>(EnumStorage enumStyle, Casing casing, Expression <Func <T, object> > expression)
        {
            var property = ReflectionHelper.GetProperty(expression);

            return(new JsonLocatorField("d.data", new StoreOptions(), enumStyle, casing, property));
        }
Beispiel #34
0
        public override Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion)
        {
            var argName = Expression.Constant(Arg);

            return(Expression.Call(call, _paramMethod, argName, Expression.Convert(currentVersion, typeof(object)), Expression.Constant(DbType)));
        }
Beispiel #35
0
 /// <summary>
 ///     Use the default serialization (ilmerged Newtonsoft.Json) with Enum values
 ///     stored as either integers or strings
 /// </summary>
 /// <param name="enumStyle"></param>
 /// <param name="casing">Casing style to be used in serialization</param>
 /// <param name="collectionStorage">Allow to set collection storage as raw arrays (without explicit types)</param>
 public void UseDefaultSerialization(EnumStorage enumStyle = EnumStorage.AsInteger, Casing casing = Casing.Default, CollectionStorage collectionStorage = CollectionStorage.Default)
 {
     Serializer(new JsonNetSerializer {
         EnumStorage = enumStyle, Casing = casing, CollectionStorage = collectionStorage
     });
 }
Beispiel #36
0
        public void duplicated_field_enum_storage_should_be_taken_from_store_options_enum_storage_by_default(EnumStorage enumStorage, NpgsqlDbType expectedNpgsqlDbType)
        {
            var storeOptions = new StoreOptions();

            storeOptions.UseDefaultSerialization(enumStorage);

            var mapping = new DocumentMapping <Target>(storeOptions);

            var duplicatedField = mapping.DuplicateField(nameof(Target.Color));

            duplicatedField.DbType.ShouldBe(expectedNpgsqlDbType);
        }
Beispiel #37
0
        public void duplicated_field_enum_storage_should_be_taken_from_store_options_duplicated_field_enum_storage_when_it_was_changed(EnumStorage enumStorage, NpgsqlDbType expectedNpgsqlDbType)
        {
            var storeOptions = new StoreOptions();

            storeOptions.DuplicatedFieldEnumStorage = enumStorage;

            var mapping = new DocumentMapping <Target>(storeOptions);

            var duplicatedField = mapping.DuplicateField(nameof(Target.Color));

            duplicatedField.DbType.ShouldBe(expectedNpgsqlDbType);
        }
Beispiel #38
0
 public JsonLocatorField(string dataLocator, StoreOptions options, EnumStorage enumStyle, Casing casing, MemberInfo member) :
     this(dataLocator, options, enumStyle, casing, new[] { member }, null)
 {
 }
 public override Expression CompileBulkImporter(EnumStorage enumStorage, Expression writer,
     ParameterExpression document,
     ParameterExpression alias, ParameterExpression serializer)
 {
     return null;
 }
Beispiel #40
0
 protected Field(EnumStorage enumStorage, MemberInfo member) : this(enumStorage, new[] { member })
 {
 }
Beispiel #41
0
        public static string ApplyCastToLocator(this string locator, EnumStorage enumStyle, Type memberType)
        {
            if (memberType.GetTypeInfo().IsEnum)
            {
                return enumStyle == EnumStorage.AsInteger ? "({0})::int".ToFormat(locator) : locator;
            }

            if (!PgTypes.ContainsKey(memberType))
                throw new ArgumentOutOfRangeException(nameof(memberType),
                    "There is not a known Postgresql cast for member type " + memberType.FullName);

            return "CAST({0} as {1})".ToFormat(locator, PgTypes[memberType]);
        }