Exemplo n.º 1
0
 public JoinExpression(Entity entity, WhereExpression where, JoinType jointype)
 {
     this.Entity   = entity;
     this.Where    = where;
     this.JoinType = jointype;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 创建更新DbCommand
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fields"></param>
        /// <param name="values"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DbCommand CreateUpdateCommand <T>(Field[] fields, object[] values, WhereExpression where)
            where T : Entity
        {
            Guard.Check(!EntityHelper.IsReadOnly <T>(), TextResource.CanNotReadOnly.FormatWith(EntityHelper.GetName <T>()));
            if (null == fields || fields.Length == 0 || null == values || values.Length == 0)
            {
                return(null);
            }
            Guard.Check(fields.Length == values.Length, TextResource.ArgumentShouldEqual.FormatWith("字段的个数", "值的个数"));
            int   length        = fields.Length;
            Field identityField = EntityHelper.GetIdentityField <T>();
            bool  identityExist = !Field.IsNullOrEmpty(identityField);

            if (WhereExpression.IsNullOrEmpty(where))
            {
                where = WhereExpression.ALL;
            }

            StringBuilder sql = new StringBuilder();

            sql.Append(ExpressionTranslater.SQL_UPDDATE);
            sql.Append(db.DbProvider.BuildTableName(EntityHelper.GetName <T>()));
            sql.Append(ExpressionTranslater.SQL_UPDATE_SET);
            List <QueryParameter> list   = new List <QueryParameter>();
            StringBuilder         colums = new StringBuilder();
            var appended = false;

            for (int i = 0; i < length; i++)
            {
                if (identityExist && fields[i].PropertyName.Equals(identityField.PropertyName))
                {
                    continue;
                }
                if (appended)
                {
                    colums.Append(",");
                }
                appended = true;
                colums.Append(fields[i].FieldName);
                colums.Append("=");
                if (values[i] is Field)
                {
                    Field fieldValue = (Field)values[i];
                    colums.Append(fieldValue.FieldNameWithTable);
                }
                else
                {
                    string pname = CreateParmName();
                    colums.Append(pname);
                    var p = new QueryParameter(pname, values[i]);
                    list.Add(p);
                }
            }
            sql.Append(colums);
            var translater = CreateTranslater();

            sql.Append(ExpressionTranslater.SQL_WHERE);
            translater.SetParmStartIndex(paramCounter);
            sql.Append(translater.TranslateWhere(where));
            list.AddRange(translater.Params);

            DbCommand cmd = db.GetSqlStringCommand(sql.ToString());

            db.AddCommandParameter(cmd, list.ToArray());
            return(cmd);
        }
Exemplo n.º 3
0
 public JoinExpression(Entity entity, WhereExpression where)
     : this(entity, where, JoinType.InnerJoin)
 {
 }
Exemplo n.º 4
0
 /// <summary>
 /// 创建删除DbCommand
 /// </summary>
 /// <param name="where"></param>
 /// <returns></returns>
 public DbCommand CreateDeleteCommand <T>(WhereExpression where)
     where T : Entity
 {
     return(CreateDeleteCommand(EntityHelper.GetName <T>(), where));
 }
Exemplo n.º 5
0
        /// <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());
        }
Exemplo n.º 6
0
 /// <summary>
 /// 查询  更新EntityQueryExpress,并返回 EntityQueryable以便继续操作
 /// </summary>
 /// <param name="expression"></param>
 /// <returns></returns>
 public EntityQueryable <T> Where(WhereExpression expression)
 {
     this.Expression.AddWhere(expression);
     return(this);
 }
Exemplo n.º 7
0
 public EntityQueryable <T> Join <TJOIN>(WhereExpression expression)
     where TJOIN : Entity
 {
     return(Join <TJOIN>(expression, JoinType.InnerJoin));
 }
Exemplo n.º 8
0
 public EntityQueryable <T> Having(WhereExpression exp)
 {
     this.Expression.AddGroupByHaving(exp);
     return(this);
 }