public string BuildUpdate <TEntity>(TEntity entity, Expression <Func <TEntity, bool> > where, out object argumnets) { var table = this._dialectBuilder.GetTable(typeof(TEntity)); // columns var updateProperties = ReflectionHelper.GetBuildSqlProperties(typeof(TEntity)); var updateFields = string.Join(",", updateProperties.Select(name => BuilderConditionalEquality(_dialectBuilder.GetColumn(name), _AT, name))); // where Dictionary <string, object> queryArgments = new Dictionary <string, object>(); var condition = _lamdaBuilder.BuildWhere(where, out queryArgments); var sql = string.Format("UPDATE {0} SET {1} WHERE {2} ", table, updateFields, condition); // Merger args var columnInfos = ReflectionHelper.GetUpdateColumns(typeof(TEntity)); foreach (var pi in columnInfos) { var key = _dialectBuilder.DBDialect.VariableFormat + ReflectionHelper.GetColumnName(pi); if (!queryArgments.ContainsKey(key)) { object value = pi.PropertyType.IsEnum ? (int)pi.GetValue(entity) : pi.GetValue(entity); queryArgments.Add(key, value); } } argumnets = queryArgments; return(sql); }
public void Build_One_Constant() { //Arrange Expression <Func <Entity_Lamda_To_Sql, bool> > predicate = i => i.Code == "No123"; Dictionary <string, object> args = new Dictionary <string, object>(); //Action var actual = _builder.BuildWhere(predicate, out args).Trim(); // Assert var expected = "[Code] = @P0"; Assert.AreEqual(expected, actual); Assert.AreEqual("No123", args["@P0"]); }