/// <summary> /// 根据查询条件创建Criteria /// </summary> /// <param name="session"></param> /// <param name="searchExpression"></param> /// <param name="searchOrders"></param> /// <param name="hasCollection"></param> /// <returns></returns> public static NHibernate.ICriteria CreateCriteria(NHibernate.ISession session, ISearchExpression searchExpression, IList <ISearchOrder> searchOrders, ref bool hasCollection) { hasCollection = false; int paramCnt = 0; NHibernate.ICriteria criteria = session.CreateCriteria(typeof(T), "Current"); Dictionary <string, ICriteria> aliases = new Dictionary <string, ICriteria>(); aliases.Add("Current", criteria); NHibernate.Criterion.ICriterion criterion = GetCriterion(session.SessionFactory, ref criteria, aliases, searchExpression, ref hasCollection, ref paramCnt); if (criterion != null) { criteria.Add(criterion); } if (paramCnt > 2100) { throw new NotSupportedException("您输入的参数过多,系统最多只支持2100个参数!"); } if (searchOrders != null) { foreach (ISearchOrder so in searchOrders) { criteria.AddOrder(GetOrder(ref criteria, aliases, so)); } } return(aliases["Current"]); }
//TODO: use nhibernate join private async void LoadWordsCollection(NHibernate.Criterion.ICriterion expression = null) { var allWords = new List <Word>(); WordsCollection.Clear(); var collection = await WordsService.GetDataAsync <Word>(expression); if (collection != null) { foreach (var item in collection) { if (!allWords.Exists(x => x.Id == item.Id)) { allWords.Add(item); } } } foreach (var item in allWords) { WordsCollection.Add(new WordViewModel { WordToDisplay = item }); } var alreadyAdded = allWords.Count; //only in case of searching it does matter if (expression != null) { var collection2 = await WordsService.GetDataAsync <Translation>(expression); if (collection2 != null) { foreach (var item in collection2) { foreach (var w in item.WordsStoredIn) { if (!allWords.Exists(x => x.Id == w.Id)) { allWords.Add(w); } } } } } foreach (var item in allWords.Skip(alreadyAdded)) { WordsCollection.Add(new WordViewModel { WordToDisplay = item }); } allWords = null; }
public static void AddCriteriaExpressions(ICriteria criteria, DataObject objectDefinition, DataFilter dataFilter) { if (dataFilter != null) { if (dataFilter.Expressions != null) { List <NHibernate.Criterion.ICriterion> criterions = new List <NHibernate.Criterion.ICriterion>(); string wildcard = "%"; foreach (Expression expression in dataFilter.Expressions) { DataProperty dataProperty = objectDefinition.dataProperties.Find(x => x.propertyName.ToUpper() == expression.PropertyName.ToUpper()); if (dataProperty != null) { string propertyName = dataProperty.propertyName; Values valuesStr = expression.Values; string valueStr = valuesStr.First(); object value = ConvertValue(valueStr, dataProperty.dataType); NHibernate.Criterion.ICriterion criterion = null; switch (expression.RelationalOperator) { case RelationalOperator.EqualTo: criterion = NHibernate.Criterion.Expression.Eq(propertyName, value); break; case RelationalOperator.NotEqualTo: criterion = NHibernate.Criterion.Expression.Not(NHibernate.Criterion.Expression.Eq(propertyName, value)); break; case RelationalOperator.LesserThan: criterion = NHibernate.Criterion.Expression.Lt(propertyName, value); break; case RelationalOperator.LesserThanOrEqual: criterion = NHibernate.Criterion.Expression.Le(propertyName, value); break; case RelationalOperator.GreaterThan: criterion = NHibernate.Criterion.Expression.Gt(propertyName, value); break; case RelationalOperator.GreaterThanOrEqual: criterion = NHibernate.Criterion.Expression.Ge(propertyName, value); break; case RelationalOperator.StartsWith: criterion = NHibernate.Criterion.Expression.Like(propertyName, value + wildcard); break; case RelationalOperator.EndsWith: criterion = NHibernate.Criterion.Expression.Like(propertyName, wildcard + value); break; case RelationalOperator.Contains: criterion = NHibernate.Criterion.Expression.Like(propertyName, wildcard + value + wildcard); break; //337 case RelationalOperator.IsNull: criterion = NHibernate.Criterion.Expression.IsNull(propertyName); break; case RelationalOperator.IsNotNull: criterion = NHibernate.Criterion.Expression.IsNotNull(propertyName); break; //337 case RelationalOperator.In: if (dataProperty.dataType == DataType.String) { criterion = NHibernate.Criterion.Expression.In(propertyName, valuesStr); } else { List <object> values = new List <object>(); foreach (string valStr in valuesStr) { values.Add(ConvertValue(valStr, dataProperty.dataType)); } criterion = NHibernate.Criterion.Expression.In(propertyName, values); } break; } criterions.Add(criterion); } } // connect criterions with logical operators //TODO: process grouping if (criterions.Count == 1) { criteria.Add(criterions.First()); } else if (criterions.Count > 1) { NHibernate.Criterion.ICriterion lhs = criterions.First(); for (int i = 1; i < dataFilter.Expressions.Count; i++) { Expression expression = dataFilter.Expressions[i]; if (expression.LogicalOperator != LogicalOperator.None) { NHibernate.Criterion.ICriterion rhs = criterions[i]; switch (expression.LogicalOperator) { case LogicalOperator.And: lhs = NHibernate.Criterion.Expression.And(lhs, rhs); break; case LogicalOperator.Or: lhs = NHibernate.Criterion.Expression.Or(lhs, rhs); break; //case LogicalOperator.Not: // lhs = NHibernate.Criterion.Expression.Not(lhs); // break; //case LogicalOperator.AndNot: // lhs = NHibernate.Criterion.Expression.And(lhs, rhs); // break; //case LogicalOperator.OrNot: // lhs = NHibernate.Criterion.Expression.Or(lhs, rhs); // break; } } } criteria.Add(lhs); } } if (dataFilter.OrderExpressions != null) { foreach (OrderExpression expression in dataFilter.OrderExpressions) { DataProperty dataProperty = objectDefinition.dataProperties.Find(x => x.propertyName.ToUpper() == expression.PropertyName.ToUpper()); string propertyName = dataProperty.propertyName; if (expression.SortOrder == SortOrder.Asc) { criteria.AddOrder(NHibernate.Criterion.Order.Asc(propertyName)); } if (expression.SortOrder == SortOrder.Desc) { criteria.AddOrder(NHibernate.Criterion.Order.Desc(propertyName)); } } } } }
public static NHibernateICriterion ToNHibernateCriterion(Dddml.Support.Criterion.ICriterion criterion) { NHibernateICriterion cr = null; if (criterion is SimpleExpression) { var e = criterion as SimpleExpression; if (e.Op.Trim().Equals(Restrictions.OpEq, StringComparison.InvariantCultureIgnoreCase)) { cr = NHibernateRestrictions.Eq(e.PropertyName, e.Value); } if (e.Op.Trim().Equals(Restrictions.OpGt, StringComparison.InvariantCultureIgnoreCase)) { cr = NHibernateRestrictions.Gt(e.PropertyName, e.Value); } if (e.Op.Trim().Equals(Restrictions.OpLt, StringComparison.InvariantCultureIgnoreCase)) { cr = NHibernateRestrictions.Lt(e.PropertyName, e.Value); } if (e.Op.Trim().Equals(Restrictions.OpGe, StringComparison.InvariantCultureIgnoreCase)) { cr = NHibernateRestrictions.Ge(e.PropertyName, e.Value); } if (e.Op.Trim().Equals(Restrictions.OpLe, StringComparison.InvariantCultureIgnoreCase)) { cr = NHibernateRestrictions.Le(e.PropertyName, e.Value); } if (e.Op.Trim().Equals(Restrictions.OpLike, StringComparison.InvariantCultureIgnoreCase)) { cr = NHibernateRestrictions.Like(e.PropertyName, e.Value); } } else { if (criterion is InsensitiveLikeExpression) { var e = criterion as InsensitiveLikeExpression; cr = NHibernateRestrictions.InsensitiveLike(e.PropertyName, e.Value); } if (criterion is InExpression) { var e = criterion as InExpression; cr = NHibernateRestrictions.In(e.PropertyName, e.Values); } if (criterion is NullExpression) { var e = criterion as NullExpression; cr = NHibernateRestrictions.IsNull(e.PropertyName); } if (criterion is NotNullExpression) { var e = criterion as NotNullExpression; cr = NHibernateRestrictions.IsNotNull(e.PropertyName); } if (criterion is BetweenExpression) { var e = criterion as BetweenExpression; cr = NHibernateRestrictions.Between(e.PropertyName, e.Lo, e.Hi); } if (criterion is AndExpression) { var e = criterion as AndExpression; cr = NHibernateRestrictions.And(ToNHibernateCriterion(e.LeftHandSide), ToNHibernateCriterion(e.RightHandSide)); } if (criterion is OrExpression) { var e = criterion as OrExpression; cr = NHibernateRestrictions.Or(ToNHibernateCriterion(e.LeftHandSide), ToNHibernateCriterion(e.RightHandSide)); } if (criterion is NotExpression) { var e = criterion as NotExpression; cr = NHibernateRestrictions.Not(ToNHibernateCriterion(e.Criterion)); } if (criterion is Disjunction) { var e = criterion as Disjunction; var j = NHibernateRestrictions.Disjunction(); foreach (var c in e.Criteria) { j.Add(ToNHibernateCriterion(c)); } cr = j; } if (criterion is Conjunction) { var e = criterion as Conjunction; var j = NHibernateRestrictions.Conjunction(); foreach (var c in e.Criteria) { j.Add(ToNHibernateCriterion(c)); } cr = j; } if (criterion is EqPropertyExpression) { var e = criterion as EqPropertyExpression; cr = NHibernateRestrictions.EqProperty(e.LhsPropertyName, e.RhsPropertyName); } if (criterion is GtPropertyExpression) { var e = criterion as GtPropertyExpression; cr = NHibernateRestrictions.GtProperty(e.LhsPropertyName, e.RhsPropertyName); } if (criterion is LtPropertyExpression) { var e = criterion as LtPropertyExpression; cr = NHibernateRestrictions.LtProperty(e.LhsPropertyName, e.RhsPropertyName); } if (criterion is GePropertyExpression) { var e = criterion as GePropertyExpression; cr = NHibernateRestrictions.GeProperty(e.LhsPropertyName, e.RhsPropertyName); } if (criterion is LePropertyExpression) { var e = criterion as LePropertyExpression; cr = NHibernateRestrictions.LeProperty(e.LhsPropertyName, e.RhsPropertyName); } } if (cr == null) { throw new NotSupportedException(String.Format("Not supported criterion. type name: {0}, {1}", criterion.GetType().Name, criterion)); } return(cr); }
/// <summary> /// Not implemented /// </summary> /// <param name="expression">Parameter expression not used</param> /// <returns>Operation not implemented</returns> public IQueryOver <T, T> Where(NHibernate.Criterion.ICriterion expression) { throw new NotImplementedException(); }
/// <summary> /// Not implemented /// </summary> /// <typeparam name="U">Type parameter not used</typeparam> /// <param name="path">Parameter path not used</param> /// <param name="alias">Parameter alias not used</param> /// <param name="joinType">Parameter joinType not used</param> /// <param name="withClause">Parameter withClause not used</param> /// <returns>Operation not implemented</returns> public IQueryOver <T, U> JoinQueryOver <U>(System.Linq.Expressions.Expression <Func <T, U> > path, System.Linq.Expressions.Expression <Func <U> > alias, NHibernate.SqlCommand.JoinType joinType, NHibernate.Criterion.ICriterion withClause) { throw new NotImplementedException(); }
// 如果是Component,则用入款金额.数额,不需要用:,不需要创建子Criteria。 private static NHibernate.Criterion.ICriterion GetCriterion(ISessionFactory sessionFactory, ref ICriteria originalCriteria, Dictionary <string, ICriteria> aliases, ISearchExpression se, ref bool hasCollection, ref int paramCnt) { if (se == null) { return(null); } if (se is Feng.Search.QueryExpression) { return(null); } SimpleExpression cse = se as SimpleExpression; if (cse == null) { LogicalExpression le = se as LogicalExpression; NHibernate.Criterion.ICriterion left = GetCriterion(sessionFactory, ref originalCriteria, aliases, le.LeftHandSide, ref hasCollection, ref paramCnt); NHibernate.Criterion.ICriterion right = GetCriterion(sessionFactory, ref originalCriteria, aliases, le.RightHandSide, ref hasCollection, ref paramCnt); switch (le.LogicOperator) { case LogicalOperator.And: return(NHibernate.Criterion.Expression.And(left, right)); case LogicalOperator.Or: return(NHibernate.Criterion.Expression.Or(left, right)); case LogicalOperator.Not: return(NHibernate.Criterion.Expression.Not(left)); default: System.Diagnostics.Debug.Assert(false, "Not supported LogicOperator"); return(null); } } else { // No PropertyName Process if (cse.Operator == SimpleOperator.Sql) { return(NHibernate.Criterion.Expression.Sql(cse.FullPropertyName)); } string propertyName = TryCreateCriteria(cse.FullPropertyName, ref originalCriteria, aliases); if (!string.IsNullOrEmpty(cse.FullPropertyName)) { // Convert Data to dest type bool hasC; Type destType; if (cse.Operator != SimpleOperator.EqProperty) { destType = NHibernateHelper.GetPropertyType(sessionFactory, typeof(T), cse.FullPropertyName, out hasC); hasCollection |= hasC; } else { destType = typeof(string); } IList array = cse.Values as IList; if (array != null) { for (int i = 0; i < array.Count; ++i) { if (destType != array[i].GetType()) { array[i] = ConvertHelper.ChangeType(array[i], destType); } } paramCnt += array.Count; } else { if (cse.Values != null) { if (destType != cse.Values.GetType()) { cse.Values = ConvertHelper.ChangeType(cse.Values, destType); } } paramCnt++; } } switch (cse.Operator) { case SimpleOperator.Eq: return(NHibernate.Criterion.Expression.Eq(propertyName, cse.Values)); case SimpleOperator.NotEq: return(NHibernate.Criterion.Expression.Not(NHibernate.Criterion.Expression.Eq(propertyName, cse.Values))); case SimpleOperator.EqProperty: { if (cse.Values == null) { throw new ArgumentException("EqProperty's Value should not be null!", "cse"); } string toPropertyName = TryCreateCriteria(cse.Values.ToString(), ref originalCriteria, aliases); return(NHibernate.Criterion.Expression.EqProperty(propertyName, toPropertyName)); } case SimpleOperator.Like: { int?propertyLength = NHibernateHelper.GetPropertyLength(sessionFactory, typeof(T), cse.FullPropertyName); return(GetLikeCriterion(propertyName, cse.Values, propertyLength)); } case SimpleOperator.InG: return(NHibernate.Criterion.Expression.In(propertyName, cse.Values as IList)); case SimpleOperator.IsNotNull: return(NHibernate.Criterion.Expression.IsNotNull(propertyName)); case SimpleOperator.IsNull: return(NHibernate.Criterion.Expression.IsNull(propertyName)); case SimpleOperator.Gt: return(NHibernate.Criterion.Expression.Gt(propertyName, cse.Values)); case SimpleOperator.Lt: return(NHibernate.Criterion.Expression.Lt(propertyName, cse.Values)); case SimpleOperator.Ge: return(NHibernate.Criterion.Expression.Ge(propertyName, cse.Values)); case SimpleOperator.Le: return(NHibernate.Criterion.Expression.Le(propertyName, cse.Values)); case SimpleOperator.GInG: { int?propertyLength = NHibernateHelper.GetPropertyLength(sessionFactory, typeof(T), cse.FullPropertyName); IList data = cse.Values as IList; // 通过查询控件控制 // 当用于客户用途查找类型时,只能Like //if (data.Count <= 1) { NHibernate.Criterion.ICriterion criterion = GetLikeCriterion(propertyName, data[0], propertyLength); for (int i = 1; i < data.Count; ++i) { criterion = NHibernate.Criterion.Expression.Or(criterion, GetLikeCriterion(propertyName, data[i], propertyLength)); } return(criterion); } //// 当多行时,不在模糊查找 //else //{ // return NHibernate.Criterion.Expression.In(propertyName, data); //} } case SimpleOperator.Any: return(NHibernate.Criterion.Expression.EqProperty(propertyName, propertyName)); case SimpleOperator.Sql: return(NHibernate.Criterion.Expression.Sql(cse.FullPropertyName)); default: System.Diagnostics.Debug.Assert(false, "Not supported SimpleOperator"); return(null); } } }