using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
namespace DapperExtensions
{
public static class Predicates
{
///
/// Factory method that creates a new IFieldPredicate predicate: [FieldName] [Operator] [Value].
/// Example: WHERE FirstName = 'Foo'
///
/// The type of the entity.
/// An expression that returns the left operand [FieldName].
/// The comparison operator.
/// The value for the predicate.
/// Effectively inverts the comparison operator. Example: WHERE FirstName <> 'Foo'.
/// An instance of IFieldPredicate.
public static IFieldPredicate Field(Expression> expression, Operator op, object value, bool not = false) where T : class
{
PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
return new FieldPredicate
{
PropertyName = propertyInfo.Name,
Operator = op,
Value = value,
Not = not
};
}
///
/// Factory method that creates a new IPropertyPredicate predicate: [FieldName1] [Operator] [FieldName2]
/// Example: WHERE FirstName = LastName
///
/// The type of the entity for the left operand.
/// The type of the entity for the right operand.
/// An expression that returns the left operand [FieldName1].
/// The comparison operator.
/// An expression that returns the right operand [FieldName2].
/// Effectively inverts the comparison operator. Example: WHERE FirstName <> LastName
/// An instance of IPropertyPredicate.
public static IPropertyPredicate Property(Expression> expression, Operator op, Expression> expression2, bool not = false)
where T : class
where T2 : class
{
PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
PropertyInfo propertyInfo2 = ReflectionHelper.GetProperty(expression2) as PropertyInfo;
return new PropertyPredicate
{
PropertyName = propertyInfo.Name,
PropertyName2 = propertyInfo2.Name,
Operator = op,
Not = not
};
}
///
/// Factory method that creates a new IPredicateGroup predicate.
/// Predicate groups can be joined together with other predicate groups.
///
/// The grouping operator to use when joining the predicates (AND / OR).
/// A list of predicates to group.
/// An instance of IPredicateGroup.
public static IPredicateGroup Group(GroupOperator op, params IPredicate[] predicate)
{
return new PredicateGroup
{
Operator = op,
Predicates = predicate
};
}
///
/// Factory method that creates a new IExistsPredicate predicate.
///
public static IExistsPredicate Exists(IPredicate predicate, bool not = false)
where TSub : class
{
return new ExistsPredicate
{
Not = not,
Predicate = predicate
};
}
///
/// Factory method that creates a new IBetweenPredicate predicate.
///
public static IBetweenPredicate Between(Expression> expression, BetweenValues values, bool not = false)
where T : class
{
PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
return new BetweenPredicate
{
Not = not,
PropertyName = propertyInfo.Name,
Value = values
};
}
///
/// Factory method that creates a new Sort which controls how the results will be sorted.
///
public static ISort Sort(Expression> expression, bool ascending = true)
{
PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
return new Sort
{
PropertyName = propertyInfo.Name,
Ascending = ascending
};
}
}
public interface IPredicate
{
string GetSql(ISqlGenerator sqlGenerator, IDictionary parameters);
}
public interface IBasePredicate : IPredicate
{
string PropertyName { get; set; }
}
public abstract class BasePredicate : IBasePredicate
{
public abstract string GetSql(ISqlGenerator sqlGenerator, IDictionary parameters);
public string PropertyName { get; set; }
protected virtual string GetColumnName(Type entityType, ISqlGenerator sqlGenerator, string propertyName)
{
IClassMapper map = sqlGenerator.Configuration.GetMap(entityType);
if (map == null)
{
throw new NullReferenceException(string.Format("Map was not found for {0}", entityType));
}
IPropertyMap propertyMap = map.Properties.SingleOrDefault(p => p.Name == propertyName);
if (propertyMap == null)
{
throw new NullReferenceException(string.Format("{0} was not found for {1}", propertyName, entityType));
}
return sqlGenerator.GetColumnName(map, propertyMap, false);
}
}
public interface IComparePredicate : IBasePredicate
{
Operator Operator { get; set; }
bool Not { get; set; }
}
public abstract class ComparePredicate : BasePredicate
{
public Operator Operator { get; set; }
public bool Not { get; set; }
public virtual string GetOperatorString()
{
switch (Operator)
{
case Operator.Gt:
return Not ? "<=" : ">";
case Operator.Ge:
return Not ? "<" : ">=";
case Operator.Lt:
return Not ? ">=" : "<";
case Operator.Le:
return Not ? ">" : "<=";
case Operator.Like:
return Not ? "NOT LIKE" : "LIKE";
default:
return Not ? "<>" : "=";
}
}
}
public interface IFieldPredicate : IComparePredicate
{
object Value { get; set; }
}
public class FieldPredicate : ComparePredicate, IFieldPredicate
where T : class
{
public object Value { get; set; }
public override string GetSql(ISqlGenerator sqlGenerator, IDictionary parameters)
{
string columnName = GetColumnName(typeof(T), sqlGenerator, PropertyName);
if (Value == null)
{
return string.Format("({0} IS {1}NULL)", columnName, Not ? "NOT " : string.Empty);
}
if (Value is IEnumerable && !(Value is string))
{
if (Operator != Operator.Eq)
{
throw new ArgumentException("Operator must be set to Eq for Enumerable types");
}
List @params = new List();
foreach (var value in (IEnumerable)Value)
{
string valueParameterName = parameters.SetParameterName(this.PropertyName, value, sqlGenerator.Configuration.Dialect.ParameterPrefix);
@params.Add(valueParameterName);
}
string paramStrings = @params.Aggregate(new StringBuilder(), (sb, s) => sb.Append((sb.Length != 0 ? ", " : string.Empty) + s), sb => sb.ToString());
return string.Format("({0} {1}IN ({2}))", columnName, Not ? "NOT " : string.Empty, paramStrings);
}
string parameterName = parameters.SetParameterName(this.PropertyName, this.Value, sqlGenerator.Configuration.Dialect.ParameterPrefix);
return string.Format("({0} {1} {2})", columnName, GetOperatorString(), parameterName);
}
}
public interface IPropertyPredicate : IComparePredicate
{
string PropertyName2 { get; set; }
}
public class PropertyPredicate : ComparePredicate, IPropertyPredicate
where T : class
where T2 : class
{
public string PropertyName2 { get; set; }
public override string GetSql(ISqlGenerator sqlGenerator, IDictionary parameters)
{
string columnName = GetColumnName(typeof(T), sqlGenerator, PropertyName);
string columnName2 = GetColumnName(typeof(T2), sqlGenerator, PropertyName2);
return string.Format("({0} {1} {2})", columnName, GetOperatorString(), columnName2);
}
}
public struct BetweenValues
{
public object Value1 { get; set; }
public object Value2 { get; set; }
}
public interface IBetweenPredicate : IPredicate
{
string PropertyName { get; set; }
BetweenValues Value { get; set; }
bool Not { get; set; }
}
public class BetweenPredicate : BasePredicate, IBetweenPredicate
where T : class
{
public override string GetSql(ISqlGenerator sqlGenerator, IDictionary parameters)
{
string columnName = GetColumnName(typeof(T), sqlGenerator, PropertyName);
string propertyName1 = parameters.SetParameterName(this.PropertyName, this.Value.Value1, sqlGenerator.Configuration.Dialect.ParameterPrefix);
string propertyName2 = parameters.SetParameterName(this.PropertyName, this.Value.Value2, sqlGenerator.Configuration.Dialect.ParameterPrefix);
return string.Format("({0} {1}BETWEEN {2} AND {3})", columnName, Not ? "NOT " : string.Empty, propertyName1, propertyName2);
}
public BetweenValues Value { get; set; }
public bool Not { get; set; }
}
///
/// Comparison operator for predicates.
///
public enum Operator
{
///
/// Equal to
///
Eq,
///
/// Greater than
///
Gt,
///
/// Greater than or equal to
///
Ge,
///
/// Less than
///
Lt,
///
/// Less than or equal to
///
Le,
///
/// Like (You can use % in the value to do wilcard searching)
///
Like
}
public interface IPredicateGroup : IPredicate
{
GroupOperator Operator { get; set; }
IList Predicates { get; set; }
}
///
/// Groups IPredicates together using the specified group operator.
///
public class PredicateGroup : IPredicateGroup
{
public GroupOperator Operator { get; set; }
public IList Predicates { get; set; }
public string GetSql(ISqlGenerator sqlGenerator, IDictionary parameters)
{
string seperator = Operator == GroupOperator.And ? " AND " : " OR ";
return "(" + Predicates.Aggregate(new StringBuilder(),
(sb, p) => (sb.Length == 0 ? sb : sb.Append(seperator)).Append(p.GetSql(sqlGenerator, parameters)),
sb =>
{
var s = sb.ToString();
if (s.Length == 0) return sqlGenerator.Configuration.Dialect.EmptyExpression;
return s;
}
) + ")";
}
}
public interface IExistsPredicate : IPredicate
{
IPredicate Predicate { get; set; }
bool Not { get; set; }
}
public class ExistsPredicate : IExistsPredicate
where TSub : class
{
public IPredicate Predicate { get; set; }
public bool Not { get; set; }
public string GetSql(ISqlGenerator sqlGenerator, IDictionary parameters)
{
IClassMapper mapSub = GetClassMapper(typeof(TSub), sqlGenerator.Configuration);
string sql = string.Format("({0}EXISTS (SELECT 1 FROM {1} WHERE {2}))",
Not ? "NOT " : string.Empty,
sqlGenerator.GetTableName(mapSub),
Predicate.GetSql(sqlGenerator, parameters));
return sql;
}
protected virtual IClassMapper GetClassMapper(Type type, IDapperExtensionsConfiguration configuration)
{
IClassMapper map = configuration.GetMap(type);
if (map == null)
{
throw new NullReferenceException(string.Format("Map was not found for {0}", type));
}
return map;
}
}
public interface ISort
{
string PropertyName { get; set; }
bool Ascending { get; set; }
}
public class Sort : ISort
{
public string PropertyName { get; set; }
public bool Ascending { get; set; }
}
///
/// Operator to use when joining predicates in a PredicateGroup.
///
public enum GroupOperator
{
And,
Or
}
}