u
This commit is contained in:
1 parent
b6415c0d65
commit
89aaef1d6b
29 files changed
+1118
-277
No files matched your search
@@ -29,12 +29,21 @@ func (c *Client) Update(
|
||||
data["update_time"] = now
|
||||
|
||||
userID, flag := middleware.GetUserID(ctx)
|
||||
|
||||
if flag {
|
||||
data["update_by"] = userID
|
||||
}
|
||||
}
|
||||
|
||||
// 👉 表名加引号(支持 schema)
|
||||
tableParts := strings.Split(table, ".")
|
||||
for i, p := range tableParts {
|
||||
tableParts[i] = `"` + p + `"`
|
||||
}
|
||||
table = strings.Join(tableParts, ".")
|
||||
|
||||
// 👉 keyField 加引号
|
||||
keyField = `"` + keyField + `"`
|
||||
|
||||
var (
|
||||
setClauses []string
|
||||
args []any
|
||||
@@ -45,12 +54,15 @@ func (c *Client) Update(
|
||||
if col == keyField {
|
||||
continue
|
||||
}
|
||||
setClauses = append(setClauses, fmt.Sprintf("%s=$%d", col, i))
|
||||
|
||||
// 👉 列名加引号
|
||||
colQuoted := `"` + col + `"`
|
||||
|
||||
setClauses = append(setClauses, fmt.Sprintf("%s=$%d", colQuoted, i))
|
||||
args = append(args, val)
|
||||
i++
|
||||
}
|
||||
|
||||
// WHERE 条件
|
||||
where := fmt.Sprintf("%s=$%d", keyField, i)
|
||||
args = append(args, keyVal)
|
||||
|
||||
@@ -76,13 +88,21 @@ func (c *Client) BatchUpdate(
|
||||
return fmt.Errorf("数据不能为空")
|
||||
}
|
||||
|
||||
// 用第一条数据确定字段
|
||||
first := dataList[0]
|
||||
if _, err := validateData(table, keyField, first); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 提取字段(排除主键)+ 排序(关键)
|
||||
// 👉 表名加引号(支持 schema)
|
||||
tableParts := strings.Split(table, ".")
|
||||
for i, p := range tableParts {
|
||||
tableParts[i] = `"` + p + `"`
|
||||
}
|
||||
table = strings.Join(tableParts, ".")
|
||||
|
||||
// 👉 keyField 加引号
|
||||
keyFieldQuoted := `"` + keyField + `"`
|
||||
|
||||
var columns []string
|
||||
for col := range first {
|
||||
if col != keyField {
|
||||
@@ -96,16 +116,15 @@ func (c *Client) BatchUpdate(
|
||||
argIndex = 1
|
||||
)
|
||||
|
||||
// CASE 语句
|
||||
var setClauses []string
|
||||
|
||||
for _, col := range columns {
|
||||
var caseBuilder strings.Builder
|
||||
colQuoted := `"` + col + `"`
|
||||
|
||||
caseBuilder.WriteString(fmt.Sprintf("%s = CASE %s ", col, keyField))
|
||||
var caseBuilder strings.Builder
|
||||
caseBuilder.WriteString(fmt.Sprintf("%s = CASE %s ", colQuoted, keyFieldQuoted))
|
||||
|
||||
for _, data := range dataList {
|
||||
// 校验 key
|
||||
keyVal, err := validateData(table, keyField, data)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -130,7 +149,6 @@ func (c *Client) BatchUpdate(
|
||||
setClauses = append(setClauses, caseBuilder.String())
|
||||
}
|
||||
|
||||
// WHERE IN
|
||||
var wherePlaceholders []string
|
||||
|
||||
for _, data := range dataList {
|
||||
@@ -148,7 +166,7 @@ func (c *Client) BatchUpdate(
|
||||
"UPDATE %s SET %s WHERE %s IN (%s)",
|
||||
table,
|
||||
strings.Join(setClauses, ", "),
|
||||
keyField,
|
||||
keyFieldQuoted,
|
||||
strings.Join(wherePlaceholders, ", "),
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user