/* 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.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.Search
{
///
/// Base class for search queries.
///
public abstract class SearchQueryDefinition
{
///
/// Renders the query to a .
///
/// A .
public abstract BsonValue Render();
///
/// Performs an implicit conversion from a string to .
///
/// The string.
///
/// The result of the conversion.
///
public static implicit operator SearchQueryDefinition(string query) =>
new SingleSearchQueryDefinition(query);
///
/// Performs an implicit conversion from an array of strings to .
///
/// The array of strings.
///
/// The result of the conversion.
///
public static implicit operator SearchQueryDefinition(string[] queries) =>
new MultiSearchQueryDefinition(queries);
///
/// Performs an implicit conversion from a list of strings to .
///
/// The list of strings.
///
/// The result of the conversion.
///
public static implicit operator SearchQueryDefinition(List queries) =>
new MultiSearchQueryDefinition(queries);
}
///
/// A query definition for a single string.
///
public sealed class SingleSearchQueryDefinition : SearchQueryDefinition
{
private readonly string _query;
///
/// Initializes a new instance of the class.
///
/// The query string.
public SingleSearchQueryDefinition(string query)
{
_query = Ensure.IsNotNull(query, nameof(query));
}
///
public override BsonValue Render() => new BsonString(_query);
}
///
/// A query definition for multiple strings.
///
public sealed class MultiSearchQueryDefinition : SearchQueryDefinition
{
private readonly string[] _queries;
///
/// Initializes a new instance of the class.
///
/// The query strings.
public MultiSearchQueryDefinition(IEnumerable queries)
{
_queries = Ensure.IsNotNull(queries, nameof(queries)).ToArray();
}
///
public override BsonValue Render() => new BsonArray(_queries);
}
}