Compare commits

..

No commits in common. "11e0c32c6f558d80d142b2fc7866cbbd97d3f0ac" and "e0786f7f8b6a596dcecdfedcbc07db160ff32a7f" have entirely different histories.

5 changed files with 34 additions and 28 deletions

View File

@ -46,6 +46,8 @@ var allUsernameCache *cache.VarCache[map[string]struct{}]
var headerImagesCache *cache.MapCache[string, []models.PostThumbnail] var headerImagesCache *cache.MapCache[string, []models.PostThumbnail]
var ctx context.Context
func InitActionsCommonCache() { func InitActionsCommonCache() {
c := config.GetConfig() c := config.GetConfig()
archivesCaches = &Arch{ archivesCaches = &Arch{
@ -93,6 +95,8 @@ func InitActionsCommonCache() {
newCommentCache = cachemanager.MapCacheBy[string, string](nil, 15*time.Minute) newCommentCache = cachemanager.MapCacheBy[string, string](nil, 15*time.Minute)
ctx = context.Background()
InitFeed() InitFeed()
} }

View File

@ -28,13 +28,3 @@ type dbQuery interface {
} }
type SqlBuilder [][]string type SqlBuilder [][]string
func Table[T Model]() string {
var r T
return r.Table()
}
func PrimaryKey[T Model]() string {
var r T
return r.PrimaryKey()
}

View File

@ -2,6 +2,7 @@ package model
import ( import (
"context" "context"
"fmt"
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
@ -58,12 +59,13 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition) (r [
} }
func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T, error) { func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T, error) {
return gets[T](globalBb, ctx, QueryCondition{ var r T
Fields: "*", sq := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey())
Where: SqlBuilder{ err := globalBb.Get(ctx, &r, sq, id)
{PrimaryKey[T](), "=", number.ToString(id), "int"}, if err != nil {
}, return r, err
}) }
return r, nil
} }
func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, order SqlBuilder, in ...[]any) (r T, err error) { func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, order SqlBuilder, in ...[]any) (r T, err error) {
@ -82,13 +84,23 @@ func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, ord
} }
func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (T, error) { func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (T, error) {
return gets[T](globalBb, ctx, Conditions( var r T
Where(where), var w string
Fields(fields), var args []any
In(in...), var err error
Order(SqlBuilder{{PrimaryKey[T](), "desc"}}), if where != nil {
Limit(1), w, args, err = where.ParseWhere(&in)
)) if err != nil {
return r, err
}
}
tp := "select %s from %s %s order by %s desc limit 1"
sq := fmt.Sprintf(tp, fields, r.Table(), w, r.PrimaryKey())
err = globalBb.Get(ctx, &r, sq, args...)
if err != nil {
return r, err
}
return r, nil
} }
func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (r []T, err error) { func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (r []T, err error) {
@ -104,10 +116,10 @@ func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, i
return r, nil return r, nil
} }
// Select 如果查询的为T的表名可以使用 {table}来代替
func Select[T Model](ctx context.Context, sql string, params ...any) ([]T, error) { func Select[T Model](ctx context.Context, sql string, params ...any) ([]T, error) {
var r []T var r []T
sql = strings.Replace(sql, "{table}", Table[T](), -1) var rr T
sql = strings.Replace(sql, "{table}", rr.Table(), -1)
err := globalBb.Select(ctx, &r, sql, params...) err := globalBb.Select(ctx, &r, sql, params...)
if err != nil { if err != nil {
return r, err return r, err
@ -134,7 +146,6 @@ func Find[T Model](ctx context.Context, where ParseWhere, fields, group string,
return return
} }
// Get 可以使用 {table}来替代 T的表名
func Get[T Model](ctx context.Context, sql string, params ...any) (r T, err error) { func Get[T Model](ctx context.Context, sql string, params ...any) (r T, err error) {
sql = strings.Replace(sql, "{table}", r.Table(), -1) sql = strings.Replace(sql, "{table}", r.Table(), -1)
err = globalBb.Get(ctx, &r, sql, params...) err = globalBb.Get(ctx, &r, sql, params...)

View File

@ -218,6 +218,7 @@ func GetToStringMapFromDB[T Model](db dbQuery, ctx context.Context, q QueryCondi
} }
func BuildQuerySql[T Model](q QueryCondition) (r string, args []any, err error) { func BuildQuerySql[T Model](q QueryCondition) (r string, args []any, err error) {
var rr T
w := "" w := ""
if q.Where != nil { if q.Where != nil {
w, args, err = q.Where.ParseWhere(&q.In) w, args, err = q.Where.ParseWhere(&q.In)
@ -251,7 +252,7 @@ func BuildQuerySql[T Model](q QueryCondition) (r string, args []any, err error)
} }
tp := "select %s from %s %s %s %s %s %s %s" tp := "select %s from %s %s %s %s %s %s %s"
l := "" l := ""
table := Table[T]() table := rr.Table()
if q.From != "" { if q.From != "" {
table = q.From table = q.From
} }

View File

@ -333,7 +333,7 @@ func Test_getField(t *testing.T) {
db := glob db := glob
field := "count(*)" field := "count(*)"
q := Conditions() q := Conditions()
wantR := "385" wantR := "387"
wantErr := false wantErr := false
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
gotR, err := getField[options](db, ctx, field, q) gotR, err := getField[options](db, ctx, field, q)