query完善
This commit is contained in:
parent
819f24460e
commit
1c0c4547d3
@ -1,5 +1,9 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func IsContainInArr[T comparable](a T, arr []T) bool {
|
||||
for _, v := range arr {
|
||||
if a == v {
|
||||
@ -8,3 +12,13 @@ func IsContainInArr[T comparable](a T, arr []T) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func StructColumn[T any, M any](arr []M, field string) (r []T) {
|
||||
for i := 0; i < len(arr); i++ {
|
||||
v := reflect.ValueOf(arr[i]).FieldByName(field).Interface()
|
||||
if val, ok := v.(T); ok {
|
||||
r = append(r, val)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
75
helper/func_test.go
Normal file
75
helper/func_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type x struct {
|
||||
Id uint64
|
||||
}
|
||||
|
||||
func c(x []*x) (r []uint64) {
|
||||
for i := 0; i < len(x); i++ {
|
||||
r = append(r, x[i].Id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getX() (r []*x) {
|
||||
for i := 0; i < 10; i++ {
|
||||
r = append(r, &x{
|
||||
uint64(i),
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func BenchmarkOr(b *testing.B) {
|
||||
y := getX()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
c(y)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStructColumn(b *testing.B) {
|
||||
y := getX()
|
||||
fmt.Println(y)
|
||||
b.ResetTimer()
|
||||
//b.N = 2
|
||||
for i := 0; i < 1; i++ {
|
||||
StructColumn[int, *x](y, "Id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructColumn(t *testing.T) {
|
||||
type args struct {
|
||||
arr []x
|
||||
field string
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantR []uint64
|
||||
}{
|
||||
{name: "test1", args: args{
|
||||
arr: []x{
|
||||
{Id: 1},
|
||||
{2},
|
||||
{4},
|
||||
{6},
|
||||
},
|
||||
field: "Id",
|
||||
}, wantR: []uint64{1, 2, 4, 6}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if gotR := StructColumn[uint64, x](tt.args.arr, tt.args.field); !reflect.DeepEqual(gotR, tt.wantR) {
|
||||
t.Errorf("StructColumn() = %v, want %v", gotR, tt.wantR)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -8,14 +8,18 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type model interface {
|
||||
type Model interface {
|
||||
PrimaryKey() string
|
||||
Table() string
|
||||
}
|
||||
|
||||
type ParseWhere interface {
|
||||
ParseWhere(in ...[]interface{}) (string, []interface{})
|
||||
}
|
||||
|
||||
type SqlBuilder [][]string
|
||||
|
||||
func (w SqlBuilder) parseWhere(in ...[]interface{}) (string, []interface{}) {
|
||||
func (w SqlBuilder) ParseWhere(in ...[]interface{}) (string, []interface{}) {
|
||||
var s strings.Builder
|
||||
args := make([]interface{}, 0, len(w))
|
||||
c := 0
|
||||
@ -118,9 +122,9 @@ func (w SqlBuilder) parseJoin() string {
|
||||
return s.String()
|
||||
}
|
||||
|
||||
func SimplePagination[T model](where SqlBuilder, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]interface{}) (r []T, total int, err error) {
|
||||
func SimplePagination[T Model](where ParseWhere, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]interface{}) (r []T, total int, err error) {
|
||||
var rr T
|
||||
w, args := where.parseWhere(in...)
|
||||
w, args := where.ParseWhere(in...)
|
||||
n := struct {
|
||||
N int `db:"n" json:"n"`
|
||||
}{}
|
||||
@ -151,7 +155,7 @@ func SimplePagination[T model](where SqlBuilder, fields string, page, pageSize i
|
||||
return
|
||||
}
|
||||
|
||||
func FindOneById[T model](id int) (T, error) {
|
||||
func FindOneById[T Model](id int) (T, error) {
|
||||
var r T
|
||||
sql := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey())
|
||||
err := db.Db.Get(&r, sql, id)
|
||||
@ -161,9 +165,9 @@ func FindOneById[T model](id int) (T, error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func FirstOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T, error) {
|
||||
func FirstOne[T Model](where ParseWhere, fields string, in ...[]interface{}) (T, error) {
|
||||
var r T
|
||||
w, args := where.parseWhere(in...)
|
||||
w, args := where.ParseWhere(in...)
|
||||
tp := "select %s from %s %s"
|
||||
sql := fmt.Sprintf(tp, fields, r.Table(), w)
|
||||
err := db.Db.Get(&r, sql, args...)
|
||||
@ -173,9 +177,9 @@ func FirstOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T,
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func LastOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T, error) {
|
||||
func LastOne[T Model](where ParseWhere, fields string, in ...[]interface{}) (T, error) {
|
||||
var r T
|
||||
w, args := where.parseWhere(in...)
|
||||
w, args := where.ParseWhere(in...)
|
||||
tp := "select %s from %s %s order by %s desc limit 1"
|
||||
sql := fmt.Sprintf(tp, fields, r.Table(), w, r.PrimaryKey())
|
||||
err := db.Db.Get(&r, sql, args...)
|
||||
@ -185,10 +189,10 @@ func LastOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T,
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func SimpleFind[T model](where SqlBuilder, fields string, in ...[]interface{}) ([]T, error) {
|
||||
func SimpleFind[T Model](where ParseWhere, fields string, in ...[]interface{}) ([]T, error) {
|
||||
var r []T
|
||||
var rr T
|
||||
w, args := where.parseWhere(in...)
|
||||
w, args := where.ParseWhere(in...)
|
||||
tp := "select %s from %s %s"
|
||||
sql := fmt.Sprintf(tp, fields, rr.Table(), w)
|
||||
err := db.Db.Select(&r, sql, args...)
|
||||
@ -198,7 +202,7 @@ func SimpleFind[T model](where SqlBuilder, fields string, in ...[]interface{}) (
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func Select[T model](sql string, params ...interface{}) ([]T, error) {
|
||||
func Select[T Model](sql string, params ...interface{}) ([]T, error) {
|
||||
var r []T
|
||||
var rr T
|
||||
sql = strings.Replace(sql, "{table}", rr.Table(), -1)
|
||||
@ -209,9 +213,9 @@ func Select[T model](sql string, params ...interface{}) ([]T, error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func Find[T model](where SqlBuilder, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) {
|
||||
func Find[T Model](where ParseWhere, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) {
|
||||
var rr T
|
||||
w, args := where.parseWhere(in...)
|
||||
w, args := where.ParseWhere(in...)
|
||||
j := join.parseJoin()
|
||||
tp := "select %s from %s %s %s %s %s"
|
||||
l := ""
|
||||
@ -223,7 +227,7 @@ func Find[T model](where SqlBuilder, fields string, order SqlBuilder, join SqlBu
|
||||
return
|
||||
}
|
||||
|
||||
func Get[T model](sql string, params ...interface{}) (r T, err error) {
|
||||
func Get[T Model](sql string, params ...interface{}) (r T, err error) {
|
||||
sql = strings.Replace(sql, "{table}", r.Table(), -1)
|
||||
err = db.Db.Get(&r, sql, params...)
|
||||
return
|
||||
|
@ -26,6 +26,9 @@ type WpPosts struct {
|
||||
PostType string `gorm:"column:post_type" db:"post_type" json:"post_type" form:"post_type"`
|
||||
PostMimeType string `gorm:"column:post_mime_type" db:"post_mime_type" json:"post_mime_type" form:"post_mime_type"`
|
||||
CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"`
|
||||
|
||||
//扩展字段
|
||||
CategoryName string `db:"category_name" json:"category_name"`
|
||||
}
|
||||
|
||||
func (w WpPosts) PrimaryKey() string {
|
||||
|
Loading…
Reference in New Issue
Block a user