public void AddWhere(WhereExpression expression)
 {
     if (expression == null)
     {
         return;
     }
     if (this.Where == null)
     {
         this.Where = expression;
     }
     else
     {
         this.Where = WhereExpression.Create(this.Where, expression, QueryOperator.And);
     }
 }
Exemple #2
0
        public static WhereExpression GetPrimaryKeyWhere(Entity entity)
        {
            var pkFilds = entity.GetPrimaryKeyFields();
            var exp     = new WhereExpression();

            foreach (var field in pkFilds)
            {
                var newExp = field == entity.GetValue(field.PropertyName);
                if (WhereExpression.IsNullOrEmpty(exp))
                {
                    exp = newExp;
                }
                else
                {
                    exp = WhereExpression.Create(exp, newExp, QueryOperator.And);
                }
            }
            return(exp);
        }
Exemple #3
0
        public static WhereExpression GetPrimaryKeyWhere <T>(object[] pkValues)
            where T : Entity
        {
            var entity  = EntityHelper.GetTable <T>();
            var pkFilds = entity.GetPrimaryKeyFields();
            var exp     = new WhereExpression();

            for (var i = 0; i < pkFilds.Length; i++)
            {
                var field = pkFilds[i];
                var value = pkValues[i];
                if (WhereExpression.IsNullOrEmpty(exp))
                {
                    exp = field == value;
                }
                else
                {
                    exp = WhereExpression.Create(exp, field == value, QueryOperator.Or);
                }
            }
            return(exp);
        }
Exemple #4
0
        /// <summary>
        /// 跳过N行
        /// 实现方案: 根据排序和条件检索出全部数据
        /// 再加入一个条件:id 大于/或小于(需要根据排序来判断) 前N行记录ID的最大/最小值
        /// </summary>
        /// <param name="translater"></param>
        protected virtual void PrepareSkip(ExpressionTranslater translater)
        {
            var exp     = translater.Expression;
            var idField = exp.From.GetIdentityField();

            if ((idField as object) == null)
            {
                throw new NotImplementedException(TextResource.AutoKeyToSearchAllow);
            }
            var IsIdentityFieldDesc = exp.IsIdentityFieldDesc;
            //子查询
            var subTableName = "subtable";
            var subId        = idField.SetTableName(subTableName);
            var subTable     = DataHelper.Clone(exp.From) as Entity;

            subTable.SetAliasName(subTableName);
            var subExp = new EntityQueryExpression(subTable);

            //前N行记录ID的最大/最小值
            if (IsIdentityFieldDesc)
            {
                subExp.Select = new SelectExpression(new Field[] { subId.Min() });
            }
            else
            {
                subExp.Select = new SelectExpression(new Field[] { subId.Max() });
            }
            //复制条件与排序
            subExp.Where = exp.Where.Clone();
            subExp.Where.SetTable(subTableName);
            subExp.OrderByList = exp.OrderByList.Clone().SetTable(subTableName);
            //再加入一个条件:id 大于/或小于(需要根据排序来判断) 前N行记录ID的最大/最小值
            var newWhere = WhereExpression.Create(exp.From.GetIdentityField(), subExp,
                                                  IsIdentityFieldDesc ? QueryOperator.Less : QueryOperator.Greater);

            exp.Where = WhereExpression.Create(exp.Where, newWhere, QueryOperator.And);
        }
        /// <summary>
        /// 翻译查询条件表达式
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public string TranslateWhere(WhereExpression exp)
        {
            StringBuilder str = new StringBuilder();

            if (exp.IsSingle)
            {
                if (exp.Operator == QueryOperator.BitwiseNOT)
                {
                    if (exp.Quote != null && exp.Quote.Operator == QueryOperator.In)
                    {
                        return(TranslateWhere(WhereExpression.Create(exp.Quote.LeftField, exp.Quote.Value, QueryOperator.NotIn)));
                    }
                }
                if ((exp.LeftField as object) != null && (exp.RightField as object) != null)
                {
                    str.Append(exp.LeftField.FieldNameWithTable);
                    str.Append(DataHelper.GetOperatorQueryText(exp.Operator));
                    str.Append(exp.RightField.FieldNameWithTable);
                }
                else if ((exp.LeftField as object) != null)
                {
                    str.Append(exp.LeftField.FieldNameWithTable);
                    str.Append(DataHelper.GetOperatorQueryText(exp.Operator));
                    //子查询 这里只暂时支持写在左边
                    if (exp.SubExpression != null)
                    {
                        str.Append("(" + this.TranslateEntityQuery(exp.SubExpression) + ")");
                    }
                    else if (exp.Operator == QueryOperator.In || exp.Operator == QueryOperator.NotIn)
                    {
                        str.Append("(");
                        if (exp.Value != null)
                        {
                            var appended = false;
                            foreach (var value in (IEnumerable)exp.Value)
                            {
                                var parm = new QueryParameter(CreateParmName(), value);
                                this.Params.Add(parm);
                                if (appended)
                                {
                                    str.Append(",");
                                }
                                str.Append(parm.Name);
                                appended = true;
                            }
                        }
                        str.Append(")");
                    }
                    else if (exp.Operator != QueryOperator.IsNotNULL && exp.Operator != QueryOperator.IsNULL)
                    {
                        var parm = new QueryParameter(CreateParmName(), exp.Value);
                        this.Params.Add(parm);
                        str.Append(parm.Name);
                    }
                }
                else if ((exp.RightField as object) != null)
                {
                    var parm = new QueryParameter(CreateParmName(), exp.Value);
                    str.Append(parm.Name);
                    str.Append(DataHelper.GetOperatorQueryText(exp.Operator));
                    str.Append(exp.RightField.FieldNameWithTable);
                    this.Params.Add(parm);
                }
            }
            else
            {
                str.Append("(" + this.TranslateWhere(exp.Left) + ")");
                str.Append(DataHelper.GetOperatorQueryText(exp.Operator));
                str.Append("(" + this.TranslateWhere(exp.Right) + ")");
            }
            if (str.Length < 8)
            {
                return("");
            }
            return(str.ToString());
        }