This commit is contained in:
oneao committed 2026-04-17 17:25:15 +08:00
1 parent e893ef2e48
commit b6415c0d65
25 files changed
+1495 -112

No files matched your search

+27 -21
View File
@@ -1,9 +1,12 @@
package db
import (
"allapp-go/pkg/logger"
"context"
"fmt"
"strings"
"go.uber.org/zap"
)
func (c *Client) LoadData(
@@ -15,33 +18,32 @@ func (c *Client) LoadData(
args []any,
) ([]map[string]any, error) {
var where string
// WHERE 构造
if searchCondition != "" && len(searchColumns) > 0 {
var conditions []string
for _, col := range searchColumns {
conditions = append(conditions, fmt.Sprintf("%s LIKE ?", col))
}
where = " WHERE (" + strings.Join(conditions, " OR ") + ")"
// 自动加 %
for i := range args {
if s, ok := args[i].(string); ok {
args[i] = "%" + s + "%"
}
}
selectCols := "*"
if len(searchColumns) > 0 {
selectCols = strings.Join(searchColumns, ", ")
}
// ORDER BY
var order string
where := ""
if searchCondition != "" {
where = " WHERE " + searchCondition
}
order := ""
if orderBy != "" {
order = " ORDER BY " + orderBy
}
sql := fmt.Sprintf("SELECT * FROM %s%s%s", viewName, where, order)
sql := fmt.Sprintf("SELECT %s FROM %s%s%s",
selectCols,
viewName,
where,
order,
)
logger.FromCtx(ctx).Info("LoadData",
zap.String("sql", sql),
zap.Any("args", args),
)
return c.query(ctx, sql, args...)
}
@@ -51,6 +53,10 @@ func (c *Client) LoadDataBySQL(
sql string,
args []any,
) ([]map[string]any, error) {
logger.FromCtx(ctx).Info("LoadDataBySQL",
zap.String("sql", sql),
zap.Any("args", args),
)
return c.query(ctx, sql, args...)
}