优化代码
This commit is contained in:
parent
0f0c3997c0
commit
11e0c32c6f
@ -28,3 +28,13 @@ 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()
|
||||||
|
}
|
||||||
|
@ -2,7 +2,6 @@ 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"
|
||||||
@ -59,13 +58,12 @@ 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) {
|
||||||
var r T
|
return gets[T](globalBb, ctx, QueryCondition{
|
||||||
sq := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey())
|
Fields: "*",
|
||||||
err := globalBb.Get(ctx, &r, sq, id)
|
Where: SqlBuilder{
|
||||||
if err != nil {
|
{PrimaryKey[T](), "=", number.ToString(id), "int"},
|
||||||
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) {
|
||||||
@ -84,23 +82,13 @@ 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) {
|
||||||
var r T
|
return gets[T](globalBb, ctx, Conditions(
|
||||||
var w string
|
Where(where),
|
||||||
var args []any
|
Fields(fields),
|
||||||
var err error
|
In(in...),
|
||||||
if where != nil {
|
Order(SqlBuilder{{PrimaryKey[T](), "desc"}}),
|
||||||
w, args, err = where.ParseWhere(&in)
|
Limit(1),
|
||||||
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) {
|
||||||
@ -116,10 +104,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
|
||||||
var rr T
|
sql = strings.Replace(sql, "{table}", Table[T](), -1)
|
||||||
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
|
||||||
@ -146,6 +134,7 @@ 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...)
|
||||||
|
@ -218,7 +218,6 @@ 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)
|
||||||
@ -252,7 +251,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 := rr.Table()
|
table := Table[T]()
|
||||||
if q.From != "" {
|
if q.From != "" {
|
||||||
table = q.From
|
table = q.From
|
||||||
}
|
}
|
||||||
|
@ -333,7 +333,7 @@ func Test_getField(t *testing.T) {
|
|||||||
db := glob
|
db := glob
|
||||||
field := "count(*)"
|
field := "count(*)"
|
||||||
q := Conditions()
|
q := Conditions()
|
||||||
wantR := "387"
|
wantR := "385"
|
||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user