/// <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); }
/// <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()); }