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) {
|
|
|
|
|
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
|
|
|
|
|
sq, args, err := BuildQuerySql[T](q)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-06 09:58:24 +00:00
|
|
|
|
err = db.Select(ctx, &r, sq, args...)
|
2022-11-05 12:59:49 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|