mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	补充 开放 IUpdate UpdateColumns 方法功能;
现实更新实体时,只更新指定的列(与 IgnoreColumns 对应);
This commit is contained in:
		@@ -84,6 +84,17 @@ namespace FreeSql.Tests.MySql {
 | 
				
			|||||||
			Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
 | 
								Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		[Fact]
 | 
							[Fact]
 | 
				
			||||||
 | 
							public void UpdateColumns() {
 | 
				
			||||||
 | 
								var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
 | 
				
			||||||
 | 
								Assert.Equal("UPDATE `tb_topic` SET `Title` = ?p_0 WHERE (`Id` = 1)", sql);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								sql = g.mysql.Update<TestEnumUpdateTb>().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).UpdateColumns(a => a.type).ToSql().Replace("\r\n", "");
 | 
				
			||||||
 | 
								Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = ?p_0 WHERE (`id` = 0)", sql);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								sql = g.mysql.Update<TestEnumUpdateTb>().NoneParameter().SetSource(new TestEnumUpdateTb { type = TestEnumUpdateTbType.sum211 }).UpdateColumns(a => a.type).ToSql().Replace("\r\n", "");
 | 
				
			||||||
 | 
								Assert.Equal("UPDATE `TestEnumUpdateTb` SET `type` = 'sum211' WHERE (`id` = 0)", sql);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							[Fact]
 | 
				
			||||||
		public void Set() {
 | 
							public void Set() {
 | 
				
			||||||
			var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
								var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
				
			||||||
			Assert.Equal("UPDATE `tb_topic` SET `Title` = ?p_0 WHERE (`Id` = 1)", sql);
 | 
								Assert.Equal("UPDATE `tb_topic` SET `Title` = ?p_0 WHERE (`Id` = 1)", sql);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,6 +49,11 @@ namespace FreeSql.Tests.Oracle {
 | 
				
			|||||||
			Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = :p_0 WHERE (\"ID\" = 1)", sql);
 | 
								Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = :p_0 WHERE (\"ID\" = 1)", sql);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		[Fact]
 | 
							[Fact]
 | 
				
			||||||
 | 
							public void UpdateColumns() {
 | 
				
			||||||
 | 
								var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
 | 
				
			||||||
 | 
								Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = :p_0 WHERE (\"ID\" = 1)", sql);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							[Fact]
 | 
				
			||||||
		public void Set() {
 | 
							public void Set() {
 | 
				
			||||||
			var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
								var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
				
			||||||
			Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = :p_0 WHERE (\"ID\" = 1)", sql);
 | 
								Assert.Equal("UPDATE \"TB_TOPIC\" SET \"TITLE\" = :p_0 WHERE (\"ID\" = 1)", sql);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,6 +49,11 @@ namespace FreeSql.Tests.PostgreSQL {
 | 
				
			|||||||
			Assert.Equal("UPDATE \"tb_topic\" SET \"title\" = @p_0 WHERE (\"id\" = 1)", sql);
 | 
								Assert.Equal("UPDATE \"tb_topic\" SET \"title\" = @p_0 WHERE (\"id\" = 1)", sql);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		[Fact]
 | 
							[Fact]
 | 
				
			||||||
 | 
							public void UpdateColumns() {
 | 
				
			||||||
 | 
								var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
 | 
				
			||||||
 | 
								Assert.Equal("UPDATE \"tb_topic\" SET \"title\" = @p_0 WHERE (\"id\" = 1)", sql);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							[Fact]
 | 
				
			||||||
		public void Set() {
 | 
							public void Set() {
 | 
				
			||||||
			var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
								var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
				
			||||||
			Assert.Equal("UPDATE \"tb_topic\" SET \"title\" = @p_0 WHERE (\"id\" = 1)", sql);
 | 
								Assert.Equal("UPDATE \"tb_topic\" SET \"title\" = @p_0 WHERE (\"id\" = 1)", sql);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,6 +60,11 @@ namespace FreeSql.Tests.SqlServer {
 | 
				
			|||||||
			Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0 WHERE ([Id] = 1)", sql);
 | 
								Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0 WHERE ([Id] = 1)", sql);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		[Fact]
 | 
							[Fact]
 | 
				
			||||||
 | 
							public void UpdateColumns() {
 | 
				
			||||||
 | 
								var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
 | 
				
			||||||
 | 
								Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0 WHERE ([Id] = 1)", sql);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							[Fact]
 | 
				
			||||||
		public void Set() {
 | 
							public void Set() {
 | 
				
			||||||
			var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
								var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
				
			||||||
			Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0 WHERE ([Id] = 1)", sql);
 | 
								Assert.Equal("UPDATE [tb_topic] SET [Title] = @p_0 WHERE ([Id] = 1)", sql);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,6 +50,11 @@ namespace FreeSql.Tests.Sqlite {
 | 
				
			|||||||
			Assert.Equal("UPDATE \"tb_topic\" SET \"Title\" = @p_0 WHERE (\"Id\" = 1)", sql);
 | 
								Assert.Equal("UPDATE \"tb_topic\" SET \"Title\" = @p_0 WHERE (\"Id\" = 1)", sql);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		[Fact]
 | 
							[Fact]
 | 
				
			||||||
 | 
							public void UpdateColumns() {
 | 
				
			||||||
 | 
								var sql = update.SetSource(new Topic { Id = 1, Title = "newtitle" }).UpdateColumns(a => a.Title).ToSql().Replace("\r\n", "");
 | 
				
			||||||
 | 
								Assert.Equal("UPDATE \"tb_topic\" SET \"Title\" = @p_0 WHERE (\"Id\" = 1)", sql);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							[Fact]
 | 
				
			||||||
		public void Set() {
 | 
							public void Set() {
 | 
				
			||||||
			var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
								var sql = update.Where(a => a.Id == 1).Set(a => a.Title, "newtitle").ToSql().Replace("\r\n", "");
 | 
				
			||||||
			Assert.Equal("UPDATE \"tb_topic\" SET \"Title\" = @p_0 WHERE (\"Id\" = 1)", sql);
 | 
								Assert.Equal("UPDATE \"tb_topic\" SET \"Title\" = @p_0 WHERE (\"Id\" = 1)", sql);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -51,6 +51,19 @@ namespace FreeSql {
 | 
				
			|||||||
		/// <returns></returns>
 | 
							/// <returns></returns>
 | 
				
			||||||
		IUpdate<T1> IgnoreColumns(string[] columns);
 | 
							IUpdate<T1> IgnoreColumns(string[] columns);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/// <summary>
 | 
				
			||||||
 | 
							/// 指定的列,UpdateColumns(a => a.Name) | UpdateColumns(a => new{a.Name,a.Time}) | UpdateColumns(a => new[]{"name","time"})
 | 
				
			||||||
 | 
							/// </summary>
 | 
				
			||||||
 | 
							/// <param name="columns">lambda选择列</param>
 | 
				
			||||||
 | 
							/// <returns></returns>
 | 
				
			||||||
 | 
							IUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns);
 | 
				
			||||||
 | 
							/// <summary>
 | 
				
			||||||
 | 
							/// 指定的列
 | 
				
			||||||
 | 
							/// </summary>
 | 
				
			||||||
 | 
							/// <param name="columns"></param>
 | 
				
			||||||
 | 
							/// <returns></returns>
 | 
				
			||||||
 | 
							IUpdate<T1> UpdateColumns(string[] columns);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		/// <summary>
 | 
							/// <summary>
 | 
				
			||||||
		/// 设置列的新值,Set(a => a.Name, "newvalue")
 | 
							/// 设置列的新值,Set(a => a.Name, "newvalue")
 | 
				
			||||||
		/// </summary>
 | 
							/// </summary>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -264,11 +264,28 @@ namespace FreeSql.Internal.CommonProvider {
 | 
				
			|||||||
			foreach (var col in cols) _ignore.Add(col, true);
 | 
								foreach (var col in cols) _ignore.Add(col, true);
 | 
				
			||||||
			return this;
 | 
								return this;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							public IUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns) {
 | 
				
			||||||
 | 
								var cols = _commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, columns?.Body, false, null).ToDictionary(a => a, a => true);
 | 
				
			||||||
 | 
								_ignore.Clear();
 | 
				
			||||||
 | 
								foreach (var col in _table.Columns.Values)
 | 
				
			||||||
 | 
									if (cols.ContainsKey(col.Attribute.Name) == false)
 | 
				
			||||||
 | 
										_ignore.Add(col.Attribute.Name, true);
 | 
				
			||||||
 | 
								return this;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public IUpdate<T1> IgnoreColumns(string[] columns) {
 | 
							public IUpdate<T1> IgnoreColumns(string[] columns) {
 | 
				
			||||||
			_ignore.Clear();
 | 
								_ignore.Clear();
 | 
				
			||||||
			foreach (var col in columns) _ignore.Add(col, true);
 | 
								foreach (var col in columns) _ignore.Add(col, true);
 | 
				
			||||||
			return this;
 | 
								return this;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							public IUpdate<T1> UpdateColumns(string[] columns) {
 | 
				
			||||||
 | 
								var cols = columns.ToDictionary(a => a);
 | 
				
			||||||
 | 
								_ignore.Clear();
 | 
				
			||||||
 | 
								foreach (var col in _table.Columns.Values)
 | 
				
			||||||
 | 
									if (cols.ContainsKey(col.Attribute.Name) == false)
 | 
				
			||||||
 | 
										_ignore.Add(col.Attribute.Name, true);
 | 
				
			||||||
 | 
								return this;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		public IUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source });
 | 
							public IUpdate<T1> SetSource(T1 source) => this.SetSource(new[] { source });
 | 
				
			||||||
		public IUpdate<T1> SetSource(IEnumerable<T1> source) {
 | 
							public IUpdate<T1> SetSource(IEnumerable<T1> source) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user