This commit is contained in:
oneao committed 2026-06-25 16:47:54 +08:00
1 parent 513a991b57
commit 8f8e8d03c8
22 files changed
+2212 -213

No files matched your search

+63
View File
@@ -50,6 +50,69 @@ func (c *Client) LoadData(
return c.query(ctx, sql, args...)
}
func (c *Client) LoadDataPage(
ctx context.Context,
viewName string,
searchCondition string,
orderBy string,
searchColumns []string,
args []any,
page int,
pageSize int,
) ([]map[string]any, int, error) {
selectCols := "*"
if len(searchColumns) > 0 {
selectCols = strings.Join(searchColumns, ", ")
}
where := ""
if searchCondition != "" {
where = " WHERE " + searchCondition
}
// 1. COUNT 查询
countSQL := fmt.Sprintf("SELECT COUNT(*) FROM %s%s", viewName, where)
countSQL = convertPlaceholder(countSQL)
var total int
err := c.pool.QueryRow(ctx, countSQL, args...).Scan(&total)
if err != nil {
return nil, 0, err
}
// 2. 分页数据查询
order := ""
if orderBy != "" {
order = " ORDER BY " + orderBy
}
offset := (page - 1) * pageSize
dataSQL := fmt.Sprintf(
"SELECT %s FROM %s%s%s LIMIT %d OFFSET %d",
selectCols,
viewName,
where,
order,
pageSize,
offset,
)
logger.FromCtx(ctx).Info("LoadDataPage",
zap.String("countSQL", countSQL),
zap.String("dataSQL", dataSQL),
zap.Any("args", args),
zap.Int("total", total),
)
rows, err := c.query(ctx, dataSQL, args...)
if err != nil {
return nil, 0, err
}
return rows, total, nil
}
func (c *Client) LoadDataBySQL(
ctx context.Context,
sql string,