Skip to content

Instantly share code, notes, and snippets.

@e-tobi
Created May 10, 2012 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-tobi/2652517 to your computer and use it in GitHub Desktop.
Save e-tobi/2652517 to your computer and use it in GitHub Desktop.
type safe RavenDb Advanced.LuceneQuery
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Raven.Abstractions.Data;
namespace Raven.Client
{
public static class StronglyTypedDocumentQueryExtensions
{
/// <summary>
/// Matches field with the exact value
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <remarks>
/// Defaults to NotAnalyzed
/// </remarks>
public static IDocumentQuery<T> WhereEquals<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value)
{
return qry.WhereEquals(PropetyToFieldName(propertySelector), value);
}
/// <summary>
/// Matches filed with the exact value
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <remarks>
/// Defaults to allow wildcards only if analyzed
/// </remarks>
public static IDocumentQuery<T> WhereEquals<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value, bool isAnalyzed)
{
return qry.WhereEquals(PropetyToFieldName(propertySelector), value, isAnalyzed);
}
/// <summary>
/// Check that the field has one of the specified value
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
public static IDocumentQuery<T> WhereIn<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, IEnumerable<TValue> values)
{
return qry.WhereIn(PropetyToFieldName(propertySelector), values.Cast<object>());
}
/// <summary>
/// Matches fields which starts with the specified value.
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "value">The value.</param>
public static IDocumentQuery<T> WhereStartsWith<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value)
{
return qry.WhereStartsWith(PropetyToFieldName(propertySelector), value);
}
/// <summary>
/// Matches fields which ends with the specified value.
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "value">The value.</param>
public static IDocumentQuery<T> WhereEndsWith<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value)
{
return qry.WhereEndsWith(PropetyToFieldName(propertySelector), value);
}
/// <summary>
/// Matches fields where the value is between the specified start and end, exclusive
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "start">The start.</param>
/// <param name = "end">The end.</param>
public static IDocumentQuery<T> WhereBetween<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue start, TValue end)
{
return qry.WhereBetween(PropetyToFieldName(propertySelector), start, end);
}
/// <summary>
/// Matches fields where the value is between the specified start and end, inclusive
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "start">The start.</param>
/// <param name = "end">The end.</param>
public static IDocumentQuery<T> WhereBetweenOrEqual<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue start, TValue end)
{
return qry.WhereBetweenOrEqual(PropetyToFieldName(propertySelector), start, end);
}
/// <summary>
/// Matches fields where the value is greater than the specified value
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "value">The value.</param>
public static IDocumentQuery<T> WhereGreaterThan<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value)
{
return qry.WhereGreaterThan(PropetyToFieldName(propertySelector), value);
}
/// <summary>
/// Matches fields where the value is greater than or equal to the specified value
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "value">The value.</param>
public static IDocumentQuery<T> WhereGreaterThanOrEqual<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value)
{
return qry.WhereGreaterThanOrEqual(PropetyToFieldName(propertySelector), value);
}
/// <summary>
/// Matches fields where the value is less than the specified value
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "value">The value.</param>
public static IDocumentQuery<T> WhereLessThan<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value)
{
return qry.WhereLessThan(PropetyToFieldName(propertySelector), value);
}
/// <summary>
/// Matches fields where the value is less than or equal to the specified value
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "value">The value.</param>
public static IDocumentQuery<T> WhereLessThanOrEqual<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, TValue value)
{
return qry.WhereLessThanOrEqual(PropetyToFieldName(propertySelector), value);
}
/// <summary>
/// Order the results by the specified fields
/// The fields are the names of the fields to sort, defaulting to sorting by ascending.
/// You can prefix a field name with '-' to indicate sorting by descending or '+' to sort by ascending
/// </summary>
/// <param name = "propertySelectors">Property selectors for the fields.</param>
public static IDocumentQuery<T> OrderBy<T>(this IDocumentQuery<T> qry, params Expression<Func<T, object>>[] propertySelectors)
{
return qry.OrderBy(propertySelectors.Select(PropetyToFieldName).ToArray());
}
/// <summary>
/// Adds an ordering for a specific field to the query
/// </summary>
/// <param name = "propertySelector">Property selector for the field.</param>
/// <param name = "descending">if set to <c>true</c> [descending].</param>
public static IDocumentQuery<T> AddOrder<T, TValue>(this IDocumentQuery<T> qry, Expression<Func<T, TValue>> propertySelector, bool descending)
{
return qry.AddOrder(PropetyToFieldName(propertySelector), descending, typeof(TValue));
}
/// <summary>
/// Perform a search for documents which fields that match the searchTerms.
/// If there is more than a single term, each of them will be checked independently.
/// </summary>
public static IDocumentQuery<T> Search<T>(this IDocumentQuery<T> qry, Expression<Func<T, object>> propertySelector, string searchTerms)
{
return qry.Search(PropetyToFieldName(propertySelector), searchTerms);
}
///<summary>
/// Instruct the index to group by the specified fields using the specified aggregation operation
///</summary>
///<remarks>
/// This is only valid on dynamic indexes queries
///</remarks>
public static IDocumentQuery<T> GroupBy<T>(this IDocumentQuery<T> qry, AggregationOperation aggregationOperation, params Expression<Func<T, object>>[] propertySelectors)
{
return qry.GroupBy(aggregationOperation, propertySelectors.Select(PropetyToFieldName).ToArray());
}
private static string PropetyToFieldName<T, TValue>(Expression<Func<T, TValue>> propertySelector)
{
var memberExpression = propertySelector.Body as MemberExpression;
if (memberExpression != null)
{
return memberExpression.Member.Name;
}
throw new ArgumentException("Can not resolve property name");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment