This commit is contained in:
oneao committed 2026-05-05 21:34:56 +08:00
1 parent 4d60582d4f
commit 2fb19021ae
40 files changed
+2552 -348

No files matched your search

+5 -3
View File
@@ -31,11 +31,12 @@ 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 {
if _, err := validateInsertData(table, keyField, data); err != nil {
return err
}
ensureID(data, keyField)
data = applyMetaFields(ctx, table, data, true)
sql, args := buildInsertSQL(table, data)
@@ -51,7 +52,7 @@ func (c *Client) BatchInsert(ctx context.Context, table, keyField string, list [
}
first := list[0]
if _, err := validateData(table, keyField, first); err != nil {
if _, err := validateInsertData(table, keyField, first); err != nil {
return err
}
@@ -71,6 +72,7 @@ func (c *Client) BatchInsert(ctx context.Context, table, keyField string, list [
)
for _, row := range list {
ensureID(row, keyField)
row = applyMetaFields(ctx, table, row, true)
@@ -1,6 +1,7 @@
package db
import (
"allapp-go/pkg/uniqueid"
"fmt"
"regexp"
"strings"
@@ -58,3 +59,30 @@ func validateData(table string, keyField string, data map[string]any) (any, erro
return val, nil
}
// 校验数据
func validateInsertData(table string, keyField string, data map[string]any) (any, error) {
if table == "" {
return nil, fmt.Errorf("表名不能为空")
}
if len(data) == 0 {
return nil, fmt.Errorf("数据不能为空")
}
if keyField == "" {
return nil, fmt.Errorf("主键字段不能为空")
}
val, _ := data[keyField]
return val, nil
}
func ensureID(data map[string]any, keyField string) {
v, ok := data[keyField]
if !ok || v == nil || v == "" {
data[keyField] = uniqueid.NextId()
}
}