u
This commit is contained in:
1 parent
46cbb2f50d
commit
e3d765a9d4
15 files changed
+903
-190
No files matched your search
@@ -31,6 +31,7 @@ func buildInsertSQL(table string, data map[string]any) (string, []any) {
|
||||
}
|
||||
|
||||
func (c *Client) Insert(ctx context.Context, table, keyField string, data map[string]any) error {
|
||||
|
||||
if _, err := validateData(table, keyField, data); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -38,11 +39,13 @@ func (c *Client) Insert(ctx context.Context, table, keyField string, data map[st
|
||||
data = applyMetaFields(ctx, table, data, true)
|
||||
|
||||
sql, args := buildInsertSQL(table, data)
|
||||
_, err := c.exec().Exec(ctx, sql, args...)
|
||||
|
||||
_, err := c.pool.Exec(ctx, sql, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) BatchInsert(ctx context.Context, table, keyField string, list []map[string]any) error {
|
||||
|
||||
if len(list) == 0 {
|
||||
return fmt.Errorf("empty data")
|
||||
}
|
||||
@@ -54,6 +57,7 @@ func (c *Client) BatchInsert(ctx context.Context, table, keyField string, list [
|
||||
|
||||
tableSQL := quoteTable(table)
|
||||
|
||||
// 固定字段顺序(稳定性关键)
|
||||
var cols []string
|
||||
for k := range first {
|
||||
cols = append(cols, k)
|
||||
@@ -67,9 +71,11 @@ func (c *Client) BatchInsert(ctx context.Context, table, keyField string, list [
|
||||
)
|
||||
|
||||
for _, row := range list {
|
||||
|
||||
row = applyMetaFields(ctx, table, row, true)
|
||||
|
||||
var place []string
|
||||
|
||||
for _, col := range cols {
|
||||
v, ok := row[col]
|
||||
if !ok {
|
||||
@@ -85,8 +91,8 @@ func (c *Client) BatchInsert(ctx context.Context, table, keyField string, list [
|
||||
}
|
||||
|
||||
var quotedCols []string
|
||||
for _, c := range cols {
|
||||
quotedCols = append(quotedCols, quoteCol(c))
|
||||
for _, col := range cols {
|
||||
quotedCols = append(quotedCols, quoteCol(col))
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf(
|
||||
@@ -96,6 +102,6 @@ func (c *Client) BatchInsert(ctx context.Context, table, keyField string, list [
|
||||
strings.Join(values, ", "),
|
||||
)
|
||||
|
||||
_, err := c.exec().Exec(ctx, sql, args...)
|
||||
_, err := c.pool.Exec(ctx, sql, args...) // ⭐关键修复
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user