123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- using Ant.Descriptors;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Text;
- namespace Ant.ORM
- {
- /// <summary>
- /// 自定义查询
- /// </summary>
- public static class Predicates
- {
- /// <summary>
- /// Factory method that creates a new IFieldPredicate predicate: [FieldName] [Operator] [Value].
- /// Example: WHERE FirstName = 'Foo'
- /// </summary>
- /// <typeparam name="T">The type of the entity.</typeparam>
- /// <param name="expression">An expression that returns the left operand [FieldName].</param>
- /// <param name="op">The comparison operator.</param>
- /// <param name="value">The value for the predicate.</param>
- /// <param name="not">Effectively inverts the comparison operator. Example: WHERE FirstName <> 'Foo'.</param>
- /// <returns>An instance of IFieldPredicate.</returns>
- public static IFieldPredicate QueryField<T>(Expression<Func<T, object>> expression, Operator op, object value, bool not = false) where T : class
- {
- PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
- MetaData typeDesc = new MetaData();
- typeDesc.FiledMeta = MetaDataManager.GetMetaData(typeof(T));
- string strtable = typeof(T).Name;
- return new FieldPredicate<T>
- {
- TableName = typeDesc.FiledMeta.Table.Name,
- PropertyName = propertyInfo.Name,
- Operators = op,
- Value = value,
- Not = not
- };
- }
- /// <summary>
- /// Factory method that creates a new IPropertyPredicate predicate: [FieldName1] [Operator] [FieldName2]
- /// Example: WHERE FirstName = LastName
- /// </summary>
- /// <typeparam name="T">The type of the entity for the left operand.</typeparam>
- /// <typeparam name="T2">The type of the entity for the right operand.</typeparam>
- /// <param name="expression">An expression that returns the left operand [FieldName1].</param>
- /// <param name="op">The comparison operator.</param>
- /// <param name="expression2">An expression that returns the right operand [FieldName2].</param>
- /// <param name="not">Effectively inverts the comparison operator. Example: WHERE FirstName <> LastName </param>
- /// <returns>An instance of IPropertyPredicate.</returns>
- public static IPropertyPredicate Property<T, T2>(Expression<Func<T, object>> expression, Operator op, Expression<Func<T2, object>> 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<T, T2>
- {
- PropertyName = propertyInfo.Name,
- PropertyName2 = propertyInfo2.Name,
- Operators = op,
- Not = not
- };
- }
- /// <summary>
- /// 工厂方法创建一个新的ipredicategroup谓词。
- /// Factory method that creates a new IPredicateGroup predicate.
- /// Predicate groups can be joined together with other predicate groups.
- /// </summary>
- /// <param name="op">The grouping operator to use when joining the predicates (AND / OR).</param>
- /// <param name="predicate">A list of predicates to group.</param>
- /// <returns>An instance of IPredicateGroup.</returns>
- public static IPredicateGroup SqlGroup(GroupOperator op, params IPredicate[] predicate)
- {
- return new PredicateGroup
- {
- Operator = op,
- Predicates = predicate
- };
- }
- /// <summary>
- /// 把查询条件进行设置分组
- /// </summary>
- /// <param name="op"></param>
- /// <param name="predicate"></param>
- /// <returns></returns>
- public static IPredicateGroup SqlGroup(GroupOperator op, List<IPredicate> predicate)
- {
- return new PredicateGroup
- {
- Operator = op,
- Predicates = predicate
- };
- }
- /// <summary>
- /// Factory method that creates a new IExistsPredicate predicate.
- /// </summary>
- public static IExistsPredicate Exists<TSub>(IPredicate predicate, bool not = false)
- where TSub : class
- {
- string strtable = typeof(TSub).Name;
- return new ExistsPredicate<TSub>
- {
- Not = not,
- TableName = strtable,
- Predicate = predicate
- };
- }
- /// <summary>
- /// Factory method that creates a new IBetweenPredicate predicate.
- /// </summary>
- public static IBetweenPredicate Between<T>(Expression<Func<T, object>> expression, BetweenValues values, bool not = false)
- where T : class
- {
- PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
- string strtable = typeof(T).Name;
- return new BetweenPredicate<T>
- {
- Not = not,
- TableName = (strtable.ToLower().StartsWith("ent")) ? strtable.Substring(3) : strtable,
- PropertyName = propertyInfo.Name,
- Value = values
- };
- }
- /// <summary>
- /// 工厂方法,创建一个新的排序,控制结果将如何排序
- /// Factory method that creates a new Sort which controls how the results will be sorted.
- /// </summary>
- public static ISort Sort<T>(Expression<Func<T, object>> expression, bool ascending = true)
- {
- PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
- string strtable = typeof(T).Name;
- return new Sort
- {
- TableName = (strtable.ToLower().StartsWith("ent")) ? strtable.Substring(3) : strtable,
- PropertyName = propertyInfo.Name,
- Ascending = ascending
- };
- }
- }
- /// <summary>
- /// 自定义查询接口
- /// </summary>
- public interface IPredicate
- {
- string GetSql(FieldValueCollection filewhere, QueryCommand parameters);
- }
- public interface IBasePredicate : IPredicate
- {
- /// <summary>
- /// 表名
- /// </summary>
- string TableName { get; set; }
- /// <summary>
- /// 列名
- /// </summary>
- string PropertyName { get; set; }
- }
- public abstract class BasePredicate : IBasePredicate
- {
- /// <summary>
- /// 获取SQL语句
- /// </summary>
- /// <param name="md"></param>
- /// <param name="parameters"></param>
- /// <returns></returns>
- public abstract string GetSql(FieldValueCollection md, QueryCommand parameters);
- /// <summary>
- /// 列名
- /// </summary>
- public string PropertyName { get; set; }
- /// <summary>
- /// 表名
- /// </summary>
- public string TableName { get; set; }
- protected virtual string GetColumnName(Type entityType, string propertyName)
- {
- return "";
- //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 Operators { get; set; }
- bool Not { get; set; }
- }
- public abstract class ComparePredicate : BasePredicate
- {
- public Operator Operators { get; set; }
- public bool Not { get; set; }
- public virtual string GetOperatorString()
- {
- switch (Operators)
- {
- case Operator.EqualSmaller:
- return Not ? "<=" : ">";
- case Operator.EqualLarger:
- return Not ? "<" : ">=";
- case Operator.Smaller:
- return Not ? ">=" : "<";
- case Operator.Larger:
- return Not ? ">" : "<=";
- case Operator.Like:
- return Not ? "NOT LIKE" : "LIKE";
- case Operator.NotEqual:
- return "<>";
- case Operator.Equal:
- return "=";
- case Operator.In:
- return "in";
- default:
- return Not ? "<>" : "=";
- }
- }
- }
- public interface IFieldPredicate : IComparePredicate
- {
- object Value { get; set; }
- }
- public class FieldPredicate<T> : ComparePredicate, IFieldPredicate
- where T : class
- {
- public object Value { get; set; }
- /// <summary>
- /// 获取拼接SQL语句
- /// </summary>
- /// <param name="parameters"></param>
- /// <returns></returns>
- public override string GetSql(FieldValueCollection md, QueryCommand parameters)
- {
- if (Value == null)
- {
- return string.Format("([{0}].{1} IS {2} NULL)", TableName, PropertyName, Not ? "NOT" : string.Empty);
- }
- if (Value is IEnumerable && !(Value is string))
- {
- List<string> paramlist = new List<string>();
- foreach (var value in (IEnumerable)Value)
- {
- string valueParameterName = parameters.SetParameterName(this.PropertyName, value, md);
- paramlist.Add(valueParameterName);
- }
- string paramStrings = paramlist.Aggregate(new StringBuilder(), (sb, s) => sb.Append((sb.Length != 0 ? ", " : string.Empty) + s), sb => sb.ToString());
- return string.Format("([{0}].{1} {2} IN ({3}))", TableName, PropertyName, Not ? "NOT " : string.Empty, paramStrings);
- }
- string parameterName = parameters.SetParameterName(this.PropertyName, this.Value, md);
- return string.Format("([{0}].{1} {2} {3})", TableName, PropertyName, GetOperatorString(), parameters.DbParmChar + parameterName);
- }
- }
- public interface IPropertyPredicate : IComparePredicate
- {
- string PropertyName2 { get; set; }
- }
- public class PropertyPredicate<T, T2> : ComparePredicate, IPropertyPredicate
- where T : class
- where T2 : class
- {
- public string PropertyName2 { get; set; }
- public override string GetSql(FieldValueCollection md, QueryCommand parameters)
- {
- string columnName = GetColumnName(typeof(T), PropertyName);
- string columnName2 = GetColumnName(typeof(T2), PropertyName2);
- return string.Format("({0} {1} {2})", columnName, GetOperatorString(), columnName2);
- }
- }
- #region BetweenAnd
- /// <summary>
- ///
- /// </summary>
- 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; }
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class BetweenPredicate<T> : BasePredicate, IBetweenPredicate
- where T : class
- {
- public override string GetSql(FieldValueCollection md, QueryCommand parameters)
- {
- string propertyName1 = parameters.SetParameterName(this.PropertyName, this.Value.Value1, md);
- string propertyName2 = parameters.SetParameterName(this.PropertyName, this.Value.Value2, md);
- return string.Format("([{0}].{1} {2} BETWEEN {3} AND {4})", TableName, PropertyName, Not ? "NOT" : string.Empty, parameters.DbParmChar + propertyName1, parameters.DbParmChar + propertyName2);
- }
- public BetweenValues Value { get; set; }
- public bool Not { get; set; }
- }
- #endregion
- #region 查询条件分组
- public interface IPredicateGroup : IPredicate
- {
- GroupOperator Operator { get; set; }
- IList<IPredicate> Predicates { get; set; }
- }
- /// <summary>
- /// 组装Predicate一起使用指定的组操作
- /// </summary>
- public class PredicateGroup : IPredicateGroup
- {
- public GroupOperator Operator { get; set; }
- public IList<IPredicate> Predicates { get; set; }
- public string GetSql(FieldValueCollection md, QueryCommand parameters)
- {
- string seperator = Operator == GroupOperator.And ? " AND " : " OR ";
- List<string> colum = new List<string>();
- return "(" + Predicates.Aggregate(new StringBuilder(), (sb, p) => (sb.Length == 0 ? sb : sb.Append(seperator)).Append(p.GetSql(md, parameters)),
- sb =>
- {
- var s = sb.ToString();
- if (s.Length == 0) return "";
- return s;
- }) + ")";
- }
- }
- #endregion
- public interface IExistsPredicate : IPredicate
- {
- IPredicate Predicate { get; set; }
- bool Not { get; set; }
- }
- public class ExistsPredicate<TSub> : IExistsPredicate
- where TSub : class
- {
- public IPredicate Predicate { get; set; }
- /// <summary>
- /// 表名
- /// </summary>
- public string TableName { get; set; }
- /// <summary>
- /// 非
- /// </summary>
- public bool Not { get; set; }
- /// <summary>
- /// 获取拼接SQL语句
- /// </summary>
- /// <param name="md"></param>
- /// <param name="parameters"></param>
- /// <returns></returns>
- public string GetSql(FieldValueCollection md, QueryCommand parameters)
- {
- string sql = string.Format("({0}EXISTS (SELECT 1 FROM [{1}] WHERE {2}))", Not ? "NOT " : string.Empty, TableName, Predicate.GetSql(md, parameters));
- return sql;
- }
- }
- /// <summary>
- /// 排序接口定义
- /// </summary>
- public interface ISort
- {
- /// <summary>
- /// 表名
- /// </summary>
- string TableName { get; set; }
- /// <summary>
- /// 排序字段名
- /// </summary>
- string PropertyName { get; set; }
- /// <summary>
- /// 升序
- /// </summary>
- bool Ascending { get; set; }
- }
- public class Sort : ISort
- {
- /// <summary>
- /// 表名
- /// </summary>
- public string TableName { get; set; }
- /// <summary>
- /// 排序字段名
- /// </summary>
- public string PropertyName { get; set; }
- /// <summary>
- /// 升序
- /// </summary>
- public bool Ascending { get; set; }
- }
- /// <summary>
- /// 查询方式用and和or进行拼接枚举
- /// </summary>
- public enum GroupOperator
- {
- And,
- Or
- }
- }
|