private string GetQueryWhere(ISearchExpression se, NHibernate.ISessionFactory sessionFactory, Dictionary <string, object> paramNames)
        {
            SimpleExpression cse = se as SimpleExpression;

            if (cse == null)
            {
                LogicalExpression le = se as LogicalExpression;

                string left  = GetQueryWhere(le.LeftHandSide, sessionFactory, paramNames);
                string right = GetQueryWhere(le.RightHandSide, sessionFactory, paramNames);

                switch (le.LogicOperator)
                {
                case LogicalOperator.And:
                    return("(" + left + " and " + right + ")");

                case LogicalOperator.Or:
                    return("(" + left + " or " + right + ")");

                case LogicalOperator.Not:
                    return("(not " + left + ")");

                default:
                    System.Diagnostics.Debug.Assert(false, "Not supported LogicOperator");
                    return(null);
                }
            }
            else
            {
                // Change Type
                if (!string.IsNullOrEmpty(cse.FullPropertyName))
                {
                    // Convert Data to dest type
                    bool hasC;
                    Type destType = NHibernateHelper.GetPropertyType(sessionFactory, typeof(T), cse.FullPropertyName, out hasC);
                    //hasCollection |= hasC;
                    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++;
                    }
                }

                string propertyName = cse.FullPropertyName.Replace(":", ".");
                string paraName     = null;
                switch (cse.Operator)
                {
                case SimpleOperator.Eq:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " = " + ":" + paraName);

                case SimpleOperator.NotEq:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " != " + ":" + paraName);

                case SimpleOperator.EqProperty:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " = " + ":" + paraName);

                case SimpleOperator.Like:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = "%" + cse.Values + "%";
                    return(propertyName + " like " + ":" + paraName);

                case SimpleOperator.InG:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " in " + ":" + paraName);

                case SimpleOperator.IsNotNull:
                    return(propertyName + " is not null ");

                case SimpleOperator.IsNull:
                    return(propertyName + " is null ");

                case SimpleOperator.Gt:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " > " + ":" + paraName);

                case SimpleOperator.Lt:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " < " + ":" + paraName);

                case SimpleOperator.Ge:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " >= " + ":" + paraName);

                case SimpleOperator.Le:
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = cse.Values;
                    return(propertyName + " <= " + ":" + paraName);

                case SimpleOperator.GInG:
                {
                    IList data = cse.Values as IList;
                    // 当用于客户用途查找类型时,只能Like
                    StringBuilder sb = new StringBuilder();
                    sb.Append("(");
                    paraName             = CreateParamName(cse, paramNames);
                    paramNames[paraName] = "%" + data[0] + "%";
                    sb.Append(propertyName + " like " + ":" + paraName);
                    for (int i = 1; i < data.Count; ++i)
                    {
                        sb.Append(" or ");
                        paraName             = CreateParamName(cse, paramNames);
                        paramNames[paraName] = "%" + data[i] + "%";
                        sb.Append(propertyName + " like " + ":" + paraName);
                    }
                    sb.Append(")");
                    return(sb.ToString());
                }

                //case SimpleOperator.Any:
                //    return NHibernate.Criterion.Expression.EqProperty(propertyName, propertyName);
                //case SimpleOperator.Sql:
                //    return NHibernate.Criterion.Expression.Sql(propertyName);
                default:
                    System.Diagnostics.Debug.Assert(false, "Not supported SimpleOperator");
                    return(null);
                }
            }
        }
Exemple #2
0
        // 如果是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);
                }
            }
        }