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 ctx context.Context
func InitActionsCommonCache() {
c := config.GetConfig()
archivesCaches = &Arch{
@ -93,6 +95,8 @@ func InitActionsCommonCache() {
newCommentCache = cachemanager.MapCacheBy[string, string](nil, 15*time.Minute)
ctx = context.Background()
InitFeed()
}

View File

@ -28,13 +28,3 @@ type dbQuery interface {
}
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 (
"context"
"fmt"
"github.com/fthvgb1/wp-go/helper/number"
str "github.com/fthvgb1/wp-go/helper/strings"
"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) {
return gets[T](globalBb, ctx, QueryCondition{
Fields: "*",
Where: SqlBuilder{
{PrimaryKey[T](), "=", number.ToString(id), "int"},
},
})
var r T
sq := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey())
err := globalBb.Get(ctx, &r, sq, id)
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) {
@ -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) {
return gets[T](globalBb, ctx, Conditions(
Where(where),
Fields(fields),
In(in...),
Order(SqlBuilder{{PrimaryKey[T](), "desc"}}),
Limit(1),
))
var r T
var w string
var args []any
var err error
if where != nil {
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) {
@ -104,10 +116,10 @@ func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, i
return r, nil
}
// Select 如果查询的为T的表名可以使用 {table}来代替
func Select[T Model](ctx context.Context, sql string, params ...any) ([]T, error) {
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...)
if err != nil {
return r, err
@ -134,7 +146,6 @@ func Find[T Model](ctx context.Context, where ParseWhere, fields, group string,
return
}
// Get 可以使用 {table}来替代 T的表名
func Get[T Model](ctx context.Context, sql string, params ...any) (r T, err error) {
sql = strings.Replace(sql, "{table}", r.Table(), -1)
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) {
var rr T
w := ""
if q.Where != nil {
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"
l := ""
table := Table[T]()
table := rr.Table()
if q.From != "" {
table = q.From
}

View File

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