update

2881099
2023-03-10 20:34:26 +08:00
parent 115e9f1067
commit 07a2bb72a1
3 changed files with 101 additions and 2 deletions

1
API.md

@@ -233,6 +233,7 @@ DbContext 自身 = 完整事务BaseRepository 不一定有事务(可通过
| ToSql | string | | 返回即将执行的SQL语句 |
| ExecuteAffrows | long | | 执行SQL语句返回影响的行数 |
| ExecuteUpdated | List\<T1\> | | 执行SQL语句返回更新后的记录 |
| Join | IUpdateJoin | | 联表更新 |
---

@@ -250,7 +250,55 @@ The benefits of using this program for dang complex update:
- Data can be previewed before updating to prevent wrong update operations;
- Support complex update operations, for example: Use `Limit(10)` on `ISelect` to update the first 10 records that meet the conditions;
## 11、BulkCopy Batch Update
## 11、UpdateJoin
v3.2.692+ (High risk operation, high risk operation, high risk operation, please use with caution, test and check the content returned by ToSql)
```csharp
fsql.Update<T1>()
.Join<T2>((a, b) => a.id == b.groupid)
.Set((a, b) => a.bname == b.name)
.Set((a, b) => a.bcode == b.id + a.code)
.Set(a => a.flag, 1) //Fixed value
.Where((a, b) => a.id > 0 && b.id > 0)
.ExecuteAffrows();
```
The SQL generated by different databases is different. Take MySql as an example:
```sql
UPDATE `T1` a
INNER JOIN `T2` b ON (a.`id` = b.`groupid`)
SET a.`bname` = b.`name`, a.`bcode` = concat(b.`id`, a.`code`), a.`flag` = 1
WHERE a.`id` > 0 AND b.`id` > 0
```
More complex joint table update:
```csharp
var query = fsql.Select<T2, T3>()
.InnerJoin(...)
.Where(...)
.WithTempQuery((a, b) => new { item1 = a, item2 = b });
fsql.Update<T1>()
.Join(query, (a, b) => a.id == b.item1.groupid)
.Set((a, b) => a.bcode == b.item2.xcode)
.ExecuteAffrows();
```
```sql
UPDATE `T1` a
INNER JOIN (
SELECT ...
FROM `t2` a
INNER JOIN ...
Where ...
) b ON (a.`id` = b.`groupid`)
SET a.`bcode` = b.`xcode`
```
## 12、BulkCopy Batch Update
FreeSql.Provider.SqlServer、FreeSql.Provider.MySqlConnector、FreeSql.Provider. PostgreSQL
@@ -293,3 +341,4 @@ fsql.Update<T1>().SetSource(list).ExecuteSqlBulkCopy();
| ToSql | string | | Return the SQL statement to be executed |
| ExecuteAffrows | long | | Execute SQL statement and return the number of rows affected |
| ExecuteUpdated | List\<T1\> | | Execute SQL statement and return the updated record |
| Join | IUpdateJoin | | 联表更新 |

@@ -253,7 +253,55 @@ UPDATE `T1` SET Title = '111' WHERE id in (select a.id from T1 a left join Optio
- 更新前可预览测试数据,防止错误更新操作;
- 支持复杂的更新操作,例如:`ISelect` 上使用 `Limit(10)` 更新附合条件的前 10 条记录;
## 11、BulkCopy 批量更新
## 11、联表更新 UpdateJoin
v3.2.692+(高风险操作,高风险操作,高风险操作,请谨慎谨慎谨慎使用,测试并核对 ToSql 返回的内容)
```csharp
fsql.Update<T1>()
.Join<T2>((a, b) => a.id == b.groupid)
.Set((a, b) => a.bname == b.name) //其他表字段
.Set((a, b) => a.bcode == b.id + a.code)
.Set(a => a.flag, 1) //固定值
.Where((a, b) => a.id > 0 && b.id > 0)
.ExecuteAffrows();
```
不同数据库产生的 SQL 不一样,以 MySql 为例:
```sql
UPDATE `T1` a
INNER JOIN `T2` b ON (a.`id` = b.`groupid`)
SET a.`bname` = b.`name`, a.`bcode` = concat(b.`id`, a.`code`), a.`flag` = 1
WHERE a.`id` > 0 AND b.`id` > 0
```
更复杂的联表更新:
```csharp
var query = fsql.Select<T2, T3>()
.InnerJoin(...)
.Where(...)
.WithTempQuery((a, b) => new { item1 = a, item2 = b });
fsql.Update<T1>()
.Join(query, (a, b) => a.id == b.item1.groupid)
.Set((a, b) => a.bcode == b.item2.xcode)
.ExecuteAffrows();
```
```sql
UPDATE `T1` a
INNER JOIN (
SELECT ...
FROM `t2` a
INNER JOIN ...
Where ...
) b ON (a.`id` = b.`groupid`)
SET a.`bcode` = b.`xcode`
```
## 12、BulkCopy 批量更新
FreeSql.Provider.SqlServer、FreeSql.Provider.MySqlConnector、FreeSql.Provider.PostgreSQL
@@ -296,3 +344,4 @@ fsql.Update<T1>().SetSource(list).ExecuteSqlBulkCopy();
| ToSql | string | | 返回即将执行的 SQL 语句 |
| ExecuteAffrows | long | | 执行 SQL 语句,返回影响的行数 |
| ExecuteUpdated | List\<T1\> | | 执行 SQL 语句,返回更新后的记录 |
| Join | IUpdateJoin | | 联表更新 |