Files
workspace/code/allapp/allapp-go-v3/pkg/db/validate.go
T
2026-04-20 17:20:47 +08:00

61 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package db
import (
"fmt"
"regexp"
"strings"
)
var identRe = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
// 安全标识符校验
func safeIdent(name string) bool {
return identRe.MatchString(name)
}
// 表名引用(支持 schema)
func quoteTable(table string) string {
parts := strings.Split(table, ".")
for i, p := range parts {
if !safeIdent(p) {
panic(fmt.Sprintf("invalid table: %s", p))
}
parts[i] = `"` + p + `"`
}
return strings.Join(parts, ".")
}
// 字段引用
func quoteCol(col string) string {
if !safeIdent(col) {
panic(fmt.Sprintf("invalid column: %s", col))
}
return `"` + col + `"`
}
// 校验数据
func validateData(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, ok := data[keyField]
if !ok {
return nil, fmt.Errorf("缺少主键字段: %s", keyField)
}
if val == nil {
return nil, fmt.Errorf("主键字段值不能为空")
}
return val, nil
}