/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Linq;
namespace MongoDB.Driver
{
///
/// Base class for filters.
///
/// The type of the document.
public abstract class FilterDefinition
{
private static readonly FilterDefinition __empty = new EmptyFilterDefinition();
///
/// Gets an empty filter. An empty filter matches everything.
///
public static FilterDefinition Empty
{
get { return __empty; }
}
///
/// Renders the filter to a .
///
/// The render arguments.
/// A .
public abstract BsonDocument Render(RenderArgs args);
///
/// Performs an implicit conversion from to .
///
/// The document.
///
/// The result of the conversion.
///
public static implicit operator FilterDefinition(BsonDocument document)
{
if (document == null)
{
return null;
}
return new BsonDocumentFilterDefinition(document);
}
///
/// Performs an implicit conversion from a predicate expression to .
///
/// The predicate.
///
/// The result of the conversion.
///
public static implicit operator FilterDefinition(Expression> predicate)
{
if (predicate == null)
{
return null;
}
return new ExpressionFilterDefinition(predicate);
}
///
/// Performs an implicit conversion from to .
///
/// The JSON string.
///
/// The result of the conversion.
///
public static implicit operator FilterDefinition(string json)
{
if (json == null)
{
return null;
}
return new JsonFilterDefinition(json);
}
///
/// Implements the operator &.
///
/// The LHS.
/// The RHS.
///
/// The result of the operator.
///
public static FilterDefinition operator &(FilterDefinition lhs, FilterDefinition rhs)
{
return new AndFilterDefinition(new[] { lhs, rhs });
}
///
/// Implements the operator |.
///
/// The LHS.
/// The RHS.
///
/// The result of the operator.
///
public static FilterDefinition operator |(FilterDefinition lhs, FilterDefinition rhs)
{
return new OrFilterDefinition(new[] { lhs, rhs });
}
///
/// Implements the operator !.
///
/// The op.
///
/// The result of the operator.
///
public static FilterDefinition operator !(FilterDefinition op)
{
return new NotFilterDefinition(op);
}
}
///
/// A based filter.
///
/// The type of the document.
public sealed class BsonDocumentFilterDefinition : FilterDefinition
{
private readonly BsonDocument _document;
///
/// Initializes a new instance of the class.
///
/// The document.
public BsonDocumentFilterDefinition(BsonDocument document)
{
_document = Ensure.IsNotNull(document, nameof(document));
}
///
/// Gets the document.
///
public BsonDocument Document
{
get { return _document; }
}
///
public override BsonDocument Render(RenderArgs args)
{
return _document;
}
}
internal sealed class EmptyFilterDefinition : FilterDefinition
{
///
public override BsonDocument Render(RenderArgs args)
{
return new BsonDocument();
}
}
///
/// An based filter.
///
/// The type of the document.
public sealed class ExpressionFilterDefinition : FilterDefinition
{
private readonly Expression> _expression;
///
/// Initializes a new instance of the class.
///
/// The expression.
public ExpressionFilterDefinition(Expression> expression)
{
_expression = Ensure.IsNotNull(expression, nameof(expression));
}
///
/// Gets the expression.
///
public Expression> Expression
{
get { return _expression; }
}
///
public override BsonDocument Render(RenderArgs args)
{
if (args.RenderForElemMatch)
{
return LinqProviderAdapter.TranslateExpressionToElemMatchFilter(_expression, elementSerializer: args.DocumentSerializer, args.SerializerRegistry, args.TranslationOptions);
}
else
{
return LinqProviderAdapter.TranslateExpressionToFilter(_expression, args.DocumentSerializer, args.SerializerRegistry, args.TranslationOptions);
}
}
}
///
/// A JSON based filter.
///
/// The type of the document.
public sealed class JsonFilterDefinition : FilterDefinition
{
private readonly string _json;
///
/// Initializes a new instance of the class.
///
/// The json.
public JsonFilterDefinition(string json)
{
_json = Ensure.IsNotNullOrEmpty(json, nameof(json));
}
///
/// Gets the json.
///
public string Json
{
get { return _json; }
}
///
public override BsonDocument Render(RenderArgs args)
{
return BsonDocument.Parse(_json);
}
}
///
/// An based filter.
///
/// The type of the document.
public sealed class ObjectFilterDefinition : FilterDefinition
{
private readonly object _obj;
///
/// Initializes a new instance of the class.
///
/// The object.
public ObjectFilterDefinition(object obj)
{
_obj = Ensure.IsNotNull(obj, nameof(obj));
}
///
/// Gets the object.
///
public object Object
{
get { return _obj; }
}
///
public override BsonDocument Render(RenderArgs args)
{
var serializer = args.SerializerRegistry.GetSerializer(_obj.GetType());
return new BsonDocumentWrapper(_obj, serializer);
}
}
}