Example #1
0
        protected override Expression VisitBinary(BinaryExpression node)
        {
            if (node.NodeType == ExpressionType.Equal)
            {
                var param = _parameterSetters.OfType <IContainmentParameterSetter>().LastOrDefault() ?? addParameter();

                var members = FindMembers.Determine(node.Left);
                var keys    = members.Select(x => x.Name).ToArray();

                if (node.Right.NodeType == ExpressionType.Constant)
                {
                    param.Constant(keys, node.Right.As <ConstantExpression>().Value);
                }
                else
                {
                    var member = FindMembers.Determine(node.Right).LastOrDefault();
                    if (member == null)
                    {
                        throw new NotSupportedException("Marten does not yet support this type of child collection querying");
                    }

                    param.AddElement(keys, member);
                }
            }


            return(base.VisitBinary(node));
        }
Example #2
0
            public DocumentMappingExpression <T> ForeignKey(Expression <Func <T, object> > expression, string schemaName,
                                                            string tableName, string columnName,
                                                            Action <ForeignKey>?foreignKeyConfiguration = null)
            {
                _builder.Alter = m =>
                {
                    var members = FindMembers.Determine(expression);

                    var duplicateField = m.DuplicateField(members);

                    var foreignKey =
                        new ForeignKey($"{m.TableName.Name}_{duplicateField.ColumnName}_fkey")
                    {
                        LinkedTable = new DbObjectName(schemaName ?? m.DatabaseSchemaName, tableName),
                        ColumnNames = new[] { duplicateField.ColumnName },
                        LinkedNames = new[] { columnName }
                    };


                    foreignKeyConfiguration?.Invoke(foreignKey);
                    m.ForeignKeys.Add(foreignKey);
                };

                return(this);
            }
Example #3
0
        public SelectManyQuery(IDocumentSchema schema, IQueryableDocument mapping, QueryModel query, int index)
        {
            Index = index;

            _schema = schema;
            _query  = query;

            _from = query.BodyClauses[index - 1].As <AdditionalFromClause>();

            var members = FindMembers.Determine(_from.FromExpression);

            _field = mapping.FieldFor(members);

            IsDistinct = query.HasOperator <DistinctResultOperator>();

            var next = query.BodyClauses.Skip(index + 1).FirstOrDefault(x => x is AdditionalFromClause);

            if (next != null)
            {
                throw new NotSupportedException("Not yet supporting SelectMany().SelectMany()");
            }
            else
            {
                _take = _query.BodyClauses.Count - index;
            }


            _tableAlias   = "sub" + Index;
            _documentType = _field.MemberType.GetElementType();
            _document     = _schema.StoreOptions.GetChildDocument(_tableAlias + ".x", _documentType);
        }
Example #4
0
        public ISqlFragment Parse(IFieldMapping mapping, ISerializer serializer, MethodCallExpression expression)
        {
            var members = FindMembers.Determine(expression);

            var locator = mapping.FieldFor(members).RawLocator;
            var values  = expression.Arguments.Last().Value();

            return(new WhereFragment($"mt_grams_vector({locator}) @@ mt_grams_query(?)", values));
        }
Example #5
0
        public StreamTableColumn(string name, Expression <Func <StreamAction, object> > memberExpression) : base(name, "varchar")
        {
            _memberExpression = memberExpression;
            _member           = FindMembers.Determine(memberExpression).Single();
            var memberType = _member.GetMemberType();

            Type         = TypeMappings.GetPgType(memberType, EnumStorage.AsInteger);
            NpgsqlDbType = TypeMappings.ToDbType(memberType);
        }
Example #6
0
        public EventTableColumn(string name, Expression <Func <IEvent, object> > eventMemberExpression) : base(name, "varchar")
        {
            _eventMemberExpression = eventMemberExpression;
            _member = FindMembers.Determine(eventMemberExpression).Single();
            var memberType = _member.GetMemberType();

            Type         = PostgresqlProvider.Instance.GetDatabaseType(memberType, EnumStorage.AsInteger);
            NpgsqlDbType = PostgresqlProvider.Instance.ToParameterType(memberType);
        }
        public static void AssignMemberFromReaderAsync <T>(this GeneratedMethod method, GeneratedType generatedType, int index,
                                                           Expression <Func <T, object> > memberExpression)
        {
            var member       = FindMembers.Determine(memberExpression).Single();
            var variableName = member.Name.ToCamelCase();

            method.Frames.Code($"var {variableName} = await reader.GetFieldValueAsync<{member.GetMemberType().FullNameInCode()}>({index}, {{0}});", Use.Type <CancellationToken>());

            method.Frames.SetMemberValue(member, variableName, typeof(T), generatedType);
        }
Example #8
0
                    public void MapTo(Expression <Func <T, TProperty> > memberExpression)
                    {
                        var member = FindMembers.Determine(memberExpression).Single();

                        _parent._builder.Alter = m =>
                        {
                            var metadataColumn = _source(m.Metadata);
                            metadataColumn.Enabled = true;
                            metadataColumn.Member  = member;
                        };
                    }
Example #9
0
        public async Task can_order_by_array_length()
        {
            var targets = Target.GenerateRandomData(100).ToArray();

            theStore.BulkInsert(targets);

            Expression <Func <Target, int> > expression = x => x.Children.Length;
            var memberInfos = FindMembers.Determine(expression.Body);

            memberInfos.Length.ShouldBe(2);

            (await theSession.Query <Target>().OrderBy(x => x.Children.Length).ToListAsync()).ShouldNotBeNull();
        }
Example #10
0
        public ISqlFragment Parse(IFieldMapping mapping, ISerializer serializer, MethodCallExpression expression)
        {
            var members = FindMembers.Determine(expression);

            var locator = mapping.FieldFor(members).TypedLocator;
            var values  = expression.Arguments.Last().Value();

            if (members.Last().GetMemberType().IsEnum)
            {
                return(new EnumIsOneOfWhereFragment(values, serializer.EnumStorage, locator));
            }

            return(new WhereFragment($"{locator} = ANY(?)", values));
        }
Example #11
0
            public DocumentMappingExpression <T> Identity(Expression <Func <T, object> > member)
            {
                alter = mapping =>
                {
                    var members = FindMembers.Determine(member);
                    if (members.Length != 1)
                    {
                        throw new InvalidOperationException($"The expression {member} is not valid as an id column in Marten");
                    }

                    mapping.IdMember = members.Single();
                };

                return(this);
            }
        public static void SetParameterFromMember <T>(this GeneratedMethod method, int index,
                                                      Expression <Func <T, object> > memberExpression)
        {
            var member     = FindMembers.Determine(memberExpression).Single();
            var memberType = member.GetMemberType();
            var pgType     = TypeMappings.ToDbType(memberType);

            if (memberType == typeof(string))
            {
                method.Frames.Code($"parameters[{index}].Value = {{0}}.{member.Name} != null ? (object){{0}}.{member.Name} : {typeof(DBNull).FullNameInCode()}.Value;", Use.Type <T>());
                method.Frames.Code($"parameters[{index}].NpgsqlDbType = {{0}};", pgType);
            }
            else
            {
                method.Frames.Code($"parameters[{index}].Value = {{0}}.{member.Name};", Use.Type <T>());
                method.Frames.Code($"parameters[{index}].NpgsqlDbType = {{0}};", pgType);
            }
        }
Example #13
0
 public IField FieldFor(Expression expression)
 {
     return(FieldFor(FindMembers.Determine(expression)));
 }
Example #14
0
 /// <summary>
 ///     Find a field by lambda expression representing a property or field
 /// </summary>
 /// <param name="expression"></param>
 /// <returns></returns>
 public IField FieldFor(Expression <Func <T, object> > expression)
 {
     return(FieldFor(FindMembers.Determine(expression)));
 }
Example #15
0
 public DocumentMappingExpression <T> VersionedWith(Expression <Func <T, Guid> > memberExpression)
 {
     alter = m => m.VersionMember = FindMembers.Determine(memberExpression).Single();
     return(this);
 }
Example #16
0
 public void Override(Expression <Func <T, long> > expression)
 {
     VersionMember = FindMembers.Determine(expression).Single();
 }