u
This commit is contained in:
1 parent
46cbb2f50d
commit
e3d765a9d4
15 files changed
+903
-190
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func (c *Client) Update(ctx context.Context, table, keyField string, data map[string]any) error {
|
||||
|
||||
keyVal, err := validateData(table, keyField, data)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -15,9 +16,6 @@ func (c *Client) Update(ctx context.Context, table, keyField string, data map[st
|
||||
|
||||
data = applyMetaFields(ctx, table, data, false)
|
||||
|
||||
tableSQL := quoteTable(table)
|
||||
keySQL := quoteCol(keyField)
|
||||
|
||||
var (
|
||||
set []string
|
||||
args []any
|
||||
@@ -38,17 +36,18 @@ func (c *Client) Update(ctx context.Context, table, keyField string, data map[st
|
||||
|
||||
sql := fmt.Sprintf(
|
||||
"UPDATE %s SET %s WHERE %s=$%d",
|
||||
tableSQL,
|
||||
quoteTable(table),
|
||||
strings.Join(set, ", "),
|
||||
keySQL,
|
||||
quoteCol(keyField),
|
||||
i,
|
||||
)
|
||||
|
||||
_, err = c.exec().Exec(ctx, sql, args...)
|
||||
_, err = c.pool.Exec(ctx, sql, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) BatchUpdate(ctx context.Context, table, keyField string, list []map[string]any) error {
|
||||
|
||||
if len(list) == 0 {
|
||||
return fmt.Errorf("empty data")
|
||||
}
|
||||
@@ -75,6 +74,7 @@ func (c *Client) BatchUpdate(ctx context.Context, table, keyField string, list [
|
||||
sets []string
|
||||
)
|
||||
|
||||
// CASE 构建
|
||||
for _, col := range cols {
|
||||
colSQL := quoteCol(col)
|
||||
|
||||
@@ -84,7 +84,7 @@ func (c *Client) BatchUpdate(ctx context.Context, table, keyField string, list [
|
||||
for _, row := range list {
|
||||
row = applyMetaFields(ctx, table, row, false)
|
||||
|
||||
keyVal, _ := row[keyField]
|
||||
keyVal := row[keyField]
|
||||
val := row[col]
|
||||
|
||||
caseSQL.WriteString(fmt.Sprintf(
|
||||
@@ -101,9 +101,10 @@ func (c *Client) BatchUpdate(ctx context.Context, table, keyField string, list [
|
||||
sets = append(sets, caseSQL.String())
|
||||
}
|
||||
|
||||
var where []string
|
||||
// ⭐修复 IN 写法(关键)
|
||||
var inPlaceholders []string
|
||||
for _, row := range list {
|
||||
where = append(where, fmt.Sprintf("$%d", argIndex))
|
||||
inPlaceholders = append(inPlaceholders, fmt.Sprintf("$%d", argIndex))
|
||||
args = append(args, row[keyField])
|
||||
argIndex++
|
||||
}
|
||||
@@ -113,9 +114,9 @@ func (c *Client) BatchUpdate(ctx context.Context, table, keyField string, list [
|
||||
tableSQL,
|
||||
strings.Join(sets, ", "),
|
||||
keySQL,
|
||||
strings.Join(where, ", "),
|
||||
strings.Join(inPlaceholders, ", "),
|
||||
)
|
||||
|
||||
_, err := c.exec().Exec(ctx, sql, args...)
|
||||
_, err := c.pool.Exec(ctx, sql, args...) // ⭐修复点
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user