Beispiel #1
0
        public static void CreateDictionaryForSearch(BinaryExpression binary, IDictionary <string, object> dict)
        {
            var expressionValue  = MartenExpressionParser.Value(binary.Right);
            var memberExpression = binary.Left;

            CreateDictionaryForSearch(dict, memberExpression, expressionValue);
        }
Beispiel #2
0
        public string ToOrderClause(Ordering clause)
        {
            var locator = MartenExpressionParser.JsonLocator(typeof(T), clause.Expression);

            return(clause.OrderingDirection == OrderingDirection.Asc
                ? locator
                : locator + " desc");
        }
Beispiel #3
0
 public void Configure(MartenExpressionParser parser, IDocumentStorage storage, IDocumentMapping mapping, UpdateBatch batch)
 {
     if (Where == null)
     {
         batch.Delete(mapping.Table, Id, storage.IdType);
     }
     else
     {
         batch.DeleteWhere(mapping.Table, Where);
     }
 }
Beispiel #4
0
        // TODO -- pull this out somewhere else
        private IWhereFragment buildSimpleWhereClause(IQueryableDocument mapping, BinaryExpression binary)
        {
            var isValueExpressionOnRight = binary.Right.IsValueExpression();
            var jsonLocatorExpression    = isValueExpressionOnRight ? binary.Left : binary.Right;
            var valueExpression          = isValueExpressionOnRight ? binary.Right : binary.Left;

            var op = _operators[binary.NodeType];

            var isSubQuery = isValueExpressionOnRight
                ? binary.Left is SubQueryExpression
                : binary.Right is SubQueryExpression;

            if (isSubQuery)
            {
                return(buildChildCollectionQuery(mapping, jsonLocatorExpression.As <SubQueryExpression>().QueryModel, valueExpression, op));
            }

            var members = FindMembers.Determine(jsonLocatorExpression);

            var field = mapping.FieldFor(members);


            var value       = field.GetValue(valueExpression);
            var jsonLocator = field.SqlLocator;

            var useContainment = mapping.PropertySearching == PropertySearching.ContainmentOperator || field.ShouldUseContainmentOperator();

            var isDuplicated = (mapping.FieldFor(members) is DuplicatedField);

            if (useContainment &&
                binary.NodeType == ExpressionType.Equal && value != null && !isDuplicated)
            {
                return(new ContainmentWhereFragment(_serializer, binary));
            }



            if (value == null)
            {
                var sql = binary.NodeType == ExpressionType.NotEqual
                    ? $"({jsonLocator}) is not null"
                    : $"({jsonLocator}) is null";

                return(new WhereFragment(sql));
            }
            if (jsonLocatorExpression.NodeType == ExpressionType.Modulo)
            {
                var moduloByValue = MartenExpressionParser.moduloByValue(binary);
                return(new WhereFragment("{0} % {1} {2} ?".ToFormat(jsonLocator, moduloByValue, op), value));
            }


            return(new WhereFragment("{0} {1} ?".ToFormat(jsonLocator, op), value));
        }
Beispiel #5
0
 public void Configure(MartenExpressionParser parser, IDocumentStorage storage, IDocumentMapping mapping, UpdateBatch batch)
 {
     if (Query == null)
     {
         batch.Delete(mapping.QualifiedTableName, Id, storage.IdType);
     }
     else
     {
         var where = Query.BuildWhereClause();
         batch.DeleteWhere(mapping.QualifiedTableName, where);
     }
     
 }
Beispiel #6
0
        private string appendWhereClause(string sql, NpgsqlCommand command)
        {
            var wheres = _query.BodyClauses.OfType <WhereClause>().ToArray();

            if (wheres.Length == 0)
            {
                return(sql);
            }

            var where = wheres.Length == 1
                ? MartenExpressionParser.ParseWhereFragment(typeof(T), wheres.Single().Predicate)
                : new CompoundWhereFragment(typeof(T), "and", wheres);


            sql += " where " + where.ToSql(command);

            return(sql);
        }
 public WhereClauseVisitor(MartenExpressionParser parent, IFieldMapping mapping)
 {
     _parent  = parent;
     _mapping = mapping;
     _register.Push(x => _top = x);
 }
Beispiel #8
0
 public DocumentQuery(IDocumentMapping mapping, QueryModel query, ISerializer serializer)
 {
     _mapping = mapping;
     _query   = query;
     _parser  = new MartenExpressionParser(this, serializer);
 }
Beispiel #9
0
 public WhereClauseVisitor(MartenExpressionParser parent, IQueryableDocument mapping)
 {
     _parent = parent;
     _mapping = mapping;
     _register.Push(x => _top = x);
 }
Beispiel #10
0
 public DocumentQuery(IDocumentMapping mapping, QueryModel query, ISerializer serializer)
 {
     _mapping = mapping;
     _query = query;
     _parser = new MartenExpressionParser(this, serializer);
 }
        public string ToSql(NpgsqlCommand command)
        {
            var wheres = _expression
                         .QueryModel
                         .BodyClauses
                         .OfType <WhereClause>()
                         .Select(x => x.Predicate)
                         .ToArray();

            if (!wheres.All(x => x is BinaryExpression))
            {
                throw new NotImplementedException();
            }


            var visitor = new FindMembers();

            visitor.Visit(_expression.QueryModel.MainFromClause.FromExpression);

            var members           = visitor.Members;
            var binaryExpressions = wheres.OfType <BinaryExpression>().ToArray();
            var dictionary        = new Dictionary <string, object>();

            // Are we querying directly againt the elements as you would for primitive types?
            if (binaryExpressions.All(x => x.Left is QuerySourceReferenceExpression && x.Right is ConstantExpression))
            {
                if (binaryExpressions.Any(x => x.NodeType != ExpressionType.Equal))
                {
                    throw new NotSupportedException("Only the equality operator is supported on Collection.Any(x => x) searches directly against the element");
                }

                var values = binaryExpressions.Select(x => MartenExpressionParser.Value(x.Right)).ToArray();
                if (members.Count == 1)
                {
                    dictionary.Add(members.Single().Name, values);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            else
            {
                var search = new Dictionary <string, object>();
                binaryExpressions.Each(x => gatherSearch(x, search));


                if (members.Count == 1)
                {
                    dictionary.Add(members.Single().Name, new[] { search });
                }
                else
                {
                    throw new NotImplementedException();
                }
            }



            var json = _serializer.ToCleanJson(dictionary);

            return($"data @> '{json}'");
        }
Beispiel #12
0
 public CompoundWhereFragment(MartenExpressionParser parser, IDocumentMapping mapping, string separator, IEnumerable<WhereClause> wheres)
 {
     _separator = separator;
     _children = wheres.Select(x => parser.ParseWhereFragment(mapping, x.Predicate)).ToArray();
 }
Beispiel #13
0
 public CompoundWhereFragment(Type rootType, string separator, IEnumerable <WhereClause> wheres)
 {
     _separator = separator;
     _children  = wheres.Select(x => MartenExpressionParser.ParseWhereFragment(rootType, x.Predicate)).ToArray();
 }
Beispiel #14
0
 public NotVisitor(MartenExpressionParser.WhereClauseVisitor parent, IQueryableDocument mapping, Action<IWhereFragment> callback)
 {
     _parent = parent;
     _mapping = mapping;
     _callback = callback;
 }
Beispiel #15
0
 public DocumentQuery(DocumentMapping mapping, QueryModel query)
 {
     _mapping = mapping;
     _query   = query;
     _parser  = new MartenExpressionParser(this);
 }
Beispiel #16
0
 public CompoundWhereFragment(MartenExpressionParser parser, IFieldMapping mapping, string separator,
                              IEnumerable <WhereClause> wheres)
 {
     _separator = separator;
     _children  = wheres.Select(x => parser.ParseWhereFragment(mapping, x.Predicate)).ToList();
 }
 public WhereClauseVisitor(MartenExpressionParser parent, IQueryableDocument mapping)
 {
     _parent  = parent;
     _mapping = mapping;
     _register.Push(x => _top = x);
 }
Beispiel #18
0
        internal static IWhereFragment BuildWhereFragment(this IDocumentStorage storage, QueryModel query, MartenExpressionParser parser)
        {
            var wheres = query.AllBodyClauses().OfType <WhereClause>().ToArray();

            if (wheres.Length == 0)
            {
                return(storage.DefaultWhereFragment());
            }

            var @where = wheres.Length == 1
                ? parser.ParseWhereFragment(storage.Fields, wheres.Single().Predicate)
                : new CompoundWhereFragment(parser, storage.Fields, "and", wheres);

            return(storage.FilterDocuments(query, @where));
        }