2023-01-12 12:42:16 +00:00
|
|
|
|
package model
|
2022-11-05 12:59:49 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2022-11-07 08:04:13 +00:00
|
|
|
|
"context"
|
2023-02-25 15:10:42 +00:00
|
|
|
|
"github.com/fthvgb1/wp-go/helper/number"
|
|
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-02-04 16:07:10 +00:00
|
|
|
|
"golang.org/x/exp/constraints"
|
2022-11-05 12:59:49 +00:00
|
|
|
|
"math/rand"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-25 15:10:42 +00:00
|
|
|
|
func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []T, total int, err error) {
|
2023-02-26 13:55:03 +00:00
|
|
|
|
if q.Page < 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-25 15:10:42 +00:00
|
|
|
|
qx := QueryCondition{
|
|
|
|
|
Where: q.Where,
|
|
|
|
|
Having: q.Having,
|
|
|
|
|
Join: q.Join,
|
|
|
|
|
In: q.In,
|
|
|
|
|
Group: q.Group,
|
|
|
|
|
From: q.From,
|
|
|
|
|
}
|
|
|
|
|
if q.Group != "" {
|
|
|
|
|
qx.Fields = q.Fields
|
|
|
|
|
sq, in, er := BuildQuerySql[T](qx)
|
|
|
|
|
qx.In = [][]any{in}
|
|
|
|
|
if er != nil {
|
|
|
|
|
err = er
|
|
|
|
|
return
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
2023-02-25 15:10:42 +00:00
|
|
|
|
qx.From = str.Join("( ", sq, " ) ", "table", number.ToString(rand.Int()))
|
|
|
|
|
qx = QueryCondition{
|
|
|
|
|
From: qx.From,
|
|
|
|
|
In: qx.In,
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 15:10:42 +00:00
|
|
|
|
n, err := GetField[T](ctx, "count(*)", qx)
|
|
|
|
|
total = str.ToInt[int](n)
|
|
|
|
|
if err != nil || total < 1 {
|
2022-11-05 12:59:49 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
offset := 0
|
2023-02-25 15:10:42 +00:00
|
|
|
|
if q.Page > 1 {
|
|
|
|
|
offset = (q.Page - 1) * q.Limit
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
if offset >= total {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-25 15:10:42 +00:00
|
|
|
|
q.Offset = offset
|
2023-02-26 13:55:03 +00:00
|
|
|
|
m := ctx.Value("handle=>")
|
|
|
|
|
if m != nil {
|
|
|
|
|
mm, ok := m.(string)
|
|
|
|
|
if ok && mm == "toMap" {
|
|
|
|
|
v := ctx.Value("map")
|
|
|
|
|
mx, er := findToStringMap[T](db, ctx, q)
|
|
|
|
|
if er != nil {
|
|
|
|
|
err = er
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
vv := v.(*[]map[string]string)
|
|
|
|
|
*vv = mx
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
2023-02-26 13:55:03 +00:00
|
|
|
|
r, err = finds[T](db, ctx, q)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func paginationToMap[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) {
|
|
|
|
|
ctx = context.WithValue(ctx, "handle=>", "toMap")
|
|
|
|
|
ctx = context.WithValue(ctx, "map", &r)
|
|
|
|
|
_, total, err = pagination[T](db, ctx, q)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PaginationToMap[T Model](ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) {
|
2023-02-26 14:01:08 +00:00
|
|
|
|
return paginationToMap[T](globalBb, ctx, q)
|
2023-02-26 13:55:03 +00:00
|
|
|
|
}
|
|
|
|
|
func PaginationToMapFromDB[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r []map[string]string, total int, err error) {
|
2023-02-26 14:01:08 +00:00
|
|
|
|
return paginationToMap[T](db, ctx, q)
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-04 16:07:10 +00:00
|
|
|
|
func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T, error) {
|
2023-02-25 15:38:12 +00:00
|
|
|
|
return gets[T](globalBb, ctx, QueryCondition{
|
|
|
|
|
Fields: "*",
|
|
|
|
|
Where: SqlBuilder{
|
|
|
|
|
{PrimaryKey[T](), "=", number.ToString(id), "int"},
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 13:49:59 +00:00
|
|
|
|
func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, order SqlBuilder, in ...[]any) (r T, err error) {
|
2023-02-25 15:10:42 +00:00
|
|
|
|
s, args, err := BuildQuerySql[T](QueryCondition{
|
|
|
|
|
Where: where,
|
|
|
|
|
Fields: fields,
|
|
|
|
|
Order: order,
|
|
|
|
|
In: in,
|
|
|
|
|
Limit: 1,
|
2023-02-21 13:49:59 +00:00
|
|
|
|
})
|
2022-11-05 12:59:49 +00:00
|
|
|
|
if err != nil {
|
2023-02-21 13:49:59 +00:00
|
|
|
|
return
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
2023-02-21 13:49:59 +00:00
|
|
|
|
err = globalBb.Get(ctx, &r, s, args...)
|
|
|
|
|
return
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 08:04:13 +00:00
|
|
|
|
func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (T, error) {
|
2023-02-25 15:38:12 +00:00
|
|
|
|
return gets[T](globalBb, ctx, Conditions(
|
|
|
|
|
Where(where),
|
|
|
|
|
Fields(fields),
|
|
|
|
|
In(in...),
|
|
|
|
|
Order(SqlBuilder{{PrimaryKey[T](), "desc"}}),
|
|
|
|
|
Limit(1),
|
|
|
|
|
))
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 13:49:59 +00:00
|
|
|
|
func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (r []T, err error) {
|
2023-02-25 15:10:42 +00:00
|
|
|
|
s, args, err := BuildQuerySql[T](QueryCondition{
|
|
|
|
|
Where: where,
|
|
|
|
|
Fields: fields,
|
|
|
|
|
In: in,
|
2023-02-21 13:49:59 +00:00
|
|
|
|
})
|
2022-11-05 12:59:49 +00:00
|
|
|
|
if err != nil {
|
2023-02-21 13:49:59 +00:00
|
|
|
|
return
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
2023-02-21 13:49:59 +00:00
|
|
|
|
err = globalBb.Select(ctx, &r, s, args...)
|
2022-11-05 12:59:49 +00:00
|
|
|
|
return r, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 15:38:12 +00:00
|
|
|
|
// Select 如果查询的为T的表名,可以使用 {table}来代替
|
2022-11-07 08:04:13 +00:00
|
|
|
|
func Select[T Model](ctx context.Context, sql string, params ...any) ([]T, error) {
|
2022-11-05 12:59:49 +00:00
|
|
|
|
var r []T
|
2023-02-25 15:38:12 +00:00
|
|
|
|
sql = strings.Replace(sql, "{table}", Table[T](), -1)
|
2022-11-07 08:04:13 +00:00
|
|
|
|
err := globalBb.Select(ctx, &r, sql, params...)
|
2022-11-05 12:59:49 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return r, err
|
|
|
|
|
}
|
|
|
|
|
return r, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 08:04:13 +00:00
|
|
|
|
func Find[T Model](ctx context.Context, where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, having SqlBuilder, limit int, in ...[]any) (r []T, err error) {
|
2023-02-21 13:49:59 +00:00
|
|
|
|
q := QueryCondition{
|
2023-02-25 15:10:42 +00:00
|
|
|
|
Where: where,
|
|
|
|
|
Fields: fields,
|
|
|
|
|
Group: group,
|
|
|
|
|
Order: order,
|
|
|
|
|
Join: join,
|
|
|
|
|
Having: having,
|
|
|
|
|
Limit: limit,
|
|
|
|
|
In: in,
|
|
|
|
|
}
|
|
|
|
|
s, args, err := BuildQuerySql[T](q)
|
2023-02-21 13:49:59 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2022-11-05 12:59:49 +00:00
|
|
|
|
}
|
2023-02-21 13:49:59 +00:00
|
|
|
|
err = globalBb.Select(ctx, &r, s, args...)
|
2022-11-05 12:59:49 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 15:38:12 +00:00
|
|
|
|
// Get 可以使用 {table}来替代 T的表名
|
2022-11-07 08:04:13 +00:00
|
|
|
|
func Get[T Model](ctx context.Context, sql string, params ...any) (r T, err error) {
|
2022-11-05 12:59:49 +00:00
|
|
|
|
sql = strings.Replace(sql, "{table}", r.Table(), -1)
|
2022-11-07 08:04:13 +00:00
|
|
|
|
err = globalBb.Get(ctx, &r, sql, params...)
|
2022-11-05 12:59:49 +00:00
|
|
|
|
return
|
|
|
|
|
}
|