This commit is contained in:
xing 2022-11-04 17:42:11 +08:00
parent f0fc4e3764
commit c759dfcdf6
6 changed files with 65 additions and 31 deletions

View File

@ -23,7 +23,7 @@ func recentComments(...any) (r []models.WpComments, err error) {
{"post_status", "publish"}, {"post_status", "publish"},
}, "comment_ID,comment_author,comment_post_ID,post_title", "", models.SqlBuilder{{"comment_date_gmt", "desc"}}, models.SqlBuilder{ }, "comment_ID,comment_author,comment_post_ID,post_title", "", models.SqlBuilder{{"comment_date_gmt", "desc"}}, models.SqlBuilder{
{"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"}, {"a", "left join", "wp_posts b", "a.comment_post_ID=b.ID"},
}, 10) }, nil, 10)
} }
func PostComments(ctx context.Context, Id uint64) ([]models.WpComments, error) { func PostComments(ctx context.Context, Id uint64) ([]models.WpComments, error) {
@ -42,7 +42,7 @@ func postComments(args ...any) ([]uint64, error) {
}, "comment_ID", "", models.SqlBuilder{ }, "comment_ID", "", models.SqlBuilder{
{"comment_date_gmt", "asc"}, {"comment_date_gmt", "asc"},
{"comment_ID", "asc"}, {"comment_ID", "asc"},
}, nil, 0) }, nil, nil, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -64,7 +64,7 @@ func getCommentByIds(args ...any) (map[uint64]models.WpComments, error) {
m := make(map[uint64]models.WpComments) m := make(map[uint64]models.WpComments)
r, err := models.Find[models.WpComments](models.SqlBuilder{ r, err := models.Find[models.WpComments](models.SqlBuilder{
{"comment_ID", "in", ""}, {"comment_approved", "1"}, {"comment_ID", "in", ""}, {"comment_approved", "1"},
}, "*", "", nil, nil, 0, helper.SliceMap(ids, helper.ToAny[uint64])) }, "*", "", nil, nil, nil, 0, helper.SliceMap(ids, helper.ToAny[uint64]))
if err != nil { if err != nil {
return m, err return m, err
} }

View File

@ -105,7 +105,7 @@ type PostContext struct {
func archives() ([]models.PostArchive, error) { func archives() ([]models.PostArchive, error) {
return models.Find[models.PostArchive](models.SqlBuilder{ return models.Find[models.PostArchive](models.SqlBuilder{
{"post_type", "post"}, {"post_status", "publish"}, {"post_type", "post"}, {"post_status", "publish"},
}, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0) }, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, nil, 0)
} }
func Archives() (r []models.PostArchive) { func Archives() (r []models.PostArchive) {
@ -127,7 +127,7 @@ func categories(...any) (terms []models.WpTermsMy, err error) {
{"t.name", "asc"}, {"t.name", "asc"},
}, models.SqlBuilder{ }, models.SqlBuilder{
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"}, {"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
}, 0, in) }, nil, 0, in)
for i := 0; i < len(terms); i++ { for i := 0; i < len(terms); i++ {
if v, ok := models.Terms[terms[i].WpTerms.TermId]; ok { if v, ok := models.Terms[terms[i].WpTerms.TermId]; ok {
terms[i].WpTerms = v terms[i].WpTerms = v

View File

@ -42,7 +42,7 @@ func getPostsByIds(ids ...any) (m map[uint64]models.WpPosts, err error) {
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id", "left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
}, { }, {
"left join", "wp_terms d", "c.term_id=d.term_id", "left join", "wp_terms d", "c.term_id=d.term_id",
}}, 0, arg) }}, nil, 0, arg)
if err != nil { if err != nil {
return m, err return m, err
} }
@ -97,7 +97,7 @@ func searchPostIds(args ...any) (ids PostIds, err error) {
join := args[4].(models.SqlBuilder) join := args[4].(models.SqlBuilder)
postType := args[5].([]any) postType := args[5].([]any)
postStatus := args[6].([]any) postStatus := args[6].([]any)
res, total, err := models.SimplePagination[models.WpPosts](where, "ID", "", page, limit, order, join, postType, postStatus) res, total, err := models.SimplePagination[models.WpPosts](where, "ID", "", page, limit, order, join, nil, postType, postStatus)
for _, posts := range res { for _, posts := range res {
ids.Ids = append(ids.Ids, posts.Id) ids.Ids = append(ids.Ids, posts.Id)
} }
@ -109,7 +109,7 @@ func searchPostIds(args ...any) (ids PostIds, err error) {
} }
func getMaxPostId(...any) ([]uint64, error) { func getMaxPostId(...any) ([]uint64, error) {
r, err := models.Find[models.WpPosts](models.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID", "", nil, nil, 0) r, err := models.Find[models.WpPosts](models.SqlBuilder{{"post_type", "post"}, {"post_status", "publish"}}, "max(ID) ID", "", nil, nil, nil, 0)
var id uint64 var id uint64
if len(r) > 0 { if len(r) > 0 {
id = r[0].Id id = r[0].Id
@ -133,7 +133,7 @@ func RecentPosts(ctx context.Context, n int) (r []models.WpPosts) {
func recentPosts(...any) (r []models.WpPosts, err error) { func recentPosts(...any) (r []models.WpPosts, err error) {
r, err = models.Find[models.WpPosts](models.SqlBuilder{{ r, err = models.Find[models.WpPosts](models.SqlBuilder{{
"post_type", "post", "post_type", "post",
}, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 10) }, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, nil, 10)
for i, post := range r { for i, post := range r {
if post.PostPassword != "" { if post.PostPassword != "" {
PasswordProjectTitle(&r[i]) PasswordProjectTitle(&r[i])
@ -207,7 +207,7 @@ func monthPost(args ...any) (r []uint64, err error) {
} }
postType := []any{"post"} postType := []any{"post"}
status := []any{"publish"} status := []any{"publish"}
ids, err := models.Find[models.WpPosts](where, "ID", "", models.SqlBuilder{{"Id", "asc"}}, nil, 0, postType, status) ids, err := models.Find[models.WpPosts](where, "ID", "", models.SqlBuilder{{"Id", "asc"}}, nil, nil, 0, postType, status)
if err != nil { if err != nil {
return return
} }

View File

@ -9,7 +9,7 @@ import (
func getUsers(...any) (m map[uint64]models.WpUsers, err error) { func getUsers(...any) (m map[uint64]models.WpUsers, err error) {
m = make(map[uint64]models.WpUsers) m = make(map[uint64]models.WpUsers)
r, err := models.Find[models.WpUsers](nil, "*", "", nil, nil, 0) r, err := models.Find[models.WpUsers](nil, "*", "", nil, nil, nil, 0)
for _, user := range r { for _, user := range r {
m[user.Id] = user m[user.Id] = user
} }

View File

@ -22,14 +22,14 @@ func InitOptions() error {
} }
func InitTerms() (err error) { func InitTerms() (err error) {
terms, err := Find[WpTerms](nil, "*", "", nil, nil, 0) terms, err := Find[WpTerms](nil, "*", "", nil, nil, nil, 0)
if err != nil { if err != nil {
return err return err
} }
for _, wpTerms := range terms { for _, wpTerms := range terms {
Terms[wpTerms.TermId] = wpTerms Terms[wpTerms.TermId] = wpTerms
} }
termTax, err := Find[WpTermTaxonomy](nil, "*", "", nil, nil, 0) termTax, err := Find[WpTermTaxonomy](nil, "*", "", nil, nil, nil, 0)
if err != nil { if err != nil {
return err return err
} }

View File

@ -18,7 +18,7 @@ type Model interface {
} }
type ParseWhere interface { type ParseWhere interface {
ParseWhere(...[]any) (string, []any, error) ParseWhere(*[][]any) (string, []any, error)
} }
type SqlBuilder [][]string type SqlBuilder [][]string
@ -40,10 +40,10 @@ func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
} }
} }
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in [][]any) (t bool) { func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in *[][]any) (t bool) {
if helper.IsContainInArr(ss[1], []string{"in", "not in"}) && len(in) > 0 { if helper.IsContainInArr(ss[1], []string{"in", "not in"}) && len(*in) > 0 {
s.WriteString(" (") s.WriteString(" (")
for _, p := range in[*c] { for _, p := range (*in)[*c] {
s.WriteString("?,") s.WriteString("?,")
*args = append(*args, p) *args = append(*args, p)
} }
@ -76,7 +76,7 @@ func (w SqlBuilder) parseType(ss []string, args *[]any) error {
return nil return nil
} }
func (w SqlBuilder) ParseWhere(in ...[]any) (string, []any, error) { func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
var s strings.Builder var s strings.Builder
args := make([]any, 0, len(w)) args := make([]any, 0, len(w))
c := 0 c := 0
@ -138,6 +138,9 @@ func (w SqlBuilder) ParseWhere(in ...[]any) (string, []any, error) {
s.WriteString(ss) s.WriteString(ss)
ss = s.String() ss = s.String()
} }
if len(*in) > c {
*in = (*in)[c:]
}
return ss, args, nil return ss, args, nil
} }
@ -178,12 +181,22 @@ func (w SqlBuilder) parseJoin() string {
return s.String() return s.String()
} }
func SimplePagination[T Model](where ParseWhere, fields, group string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]any) (r []T, total int, err error) { func SimplePagination[T Model](where ParseWhere, fields, group string, page, pageSize int, order SqlBuilder, join SqlBuilder, having SqlBuilder, in ...[]any) (r []T, total int, err error) {
var rr T var rr T
w, args, err := where.ParseWhere(in...) w, args, err := where.ParseWhere(&in)
if err != nil { if err != nil {
return r, total, err return r, total, err
} }
h := ""
if having != nil {
hh, arg, err := having.ParseWhere(&in)
if err != nil {
return r, total, err
}
args = append(args, arg...)
h = strings.Replace(hh, " where", " having", 1)
}
n := struct { n := struct {
N int `db:"n" json:"n"` N int `db:"n" json:"n"`
}{} }{}
@ -194,15 +207,27 @@ func SimplePagination[T Model](where ParseWhere, fields, group string, page, pag
g.WriteString(group) g.WriteString(group)
groupBy = g.String() groupBy = g.String()
} }
if having != nil {
tm := map[string]struct{}{}
for _, s := range strings.Split(group, ",") {
tm[s] = struct{}{}
}
for _, ss := range having {
if _, ok := tm[ss[0]]; !ok {
group = fmt.Sprintf("%s,%s", group, ss[0])
}
}
group = strings.Trim(group, ",")
}
j := join.parseJoin() j := join.parseJoin()
if group == "" { if group == "" {
tpx := "select count(*) n from %s %s %s limit 1" tpx := "select count(*) n from %s %s %s limit 1"
sq := fmt.Sprintf(tpx, rr.Table(), j, w) sq := fmt.Sprintf(tpx, rr.Table(), j, w)
err = db.Db.Get(&n, sq, args...) err = db.Db.Get(&n, sq, args...)
} else { } else {
tpx := "select count(*) n from (select %s from %s %s %s %s ) %s" tpx := "select count(*) n from (select %s from %s %s %s %s %s ) %s"
rand.Seed(int64(time.Now().Nanosecond())) rand.Seed(int64(time.Now().Nanosecond()))
sq := fmt.Sprintf(tpx, group, rr.Table(), j, w, groupBy, fmt.Sprintf("table%d", rand.Int())) sq := fmt.Sprintf(tpx, group, rr.Table(), j, w, groupBy, h, fmt.Sprintf("table%d", rand.Int()))
err = db.Db.Get(&n, sq, args...) err = db.Db.Get(&n, sq, args...)
} }
@ -220,8 +245,8 @@ func SimplePagination[T Model](where ParseWhere, fields, group string, page, pag
if offset >= total { if offset >= total {
return return
} }
tp := "select %s from %s %s %s %s %s limit %d,%d" tp := "select %s from %s %s %s %s %s %s limit %d,%d"
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, order.parseOrderBy(), offset, pageSize) sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, h, order.parseOrderBy(), offset, pageSize)
err = db.Db.Select(&r, sql, args...) err = db.Db.Select(&r, sql, args...)
if err != nil { if err != nil {
return return
@ -241,7 +266,7 @@ func FindOneById[T Model, I helper.IntNumber](id I) (T, error) {
func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error) { func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error) {
var r T var r T
w, args, err := where.ParseWhere(in...) w, args, err := where.ParseWhere(&in)
if err != nil { if err != nil {
return r, err return r, err
} }
@ -256,7 +281,7 @@ func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...
func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) { func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) {
var r T var r T
w, args, err := where.ParseWhere(in...) w, args, err := where.ParseWhere(&in)
if err != nil { if err != nil {
return r, err return r, err
} }
@ -272,7 +297,7 @@ func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) {
func SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) { func SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) {
var r []T var r []T
var rr T var rr T
w, args, err := where.ParseWhere(in...) w, args, err := where.ParseWhere(&in)
if err != nil { if err != nil {
return r, err return r, err
} }
@ -296,16 +321,25 @@ func Select[T Model](sql string, params ...any) ([]T, error) {
return r, nil return r, nil
} }
func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]any) (r []T, err error) { func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, having SqlBuilder, limit int, in ...[]any) (r []T, err error) {
var rr T var rr T
w := "" w := ""
var args []any var args []any
if where != nil { if where != nil {
w, args, err = where.ParseWhere(in...) w, args, err = where.ParseWhere(&in)
if err != nil { if err != nil {
return r, err return r, err
} }
} }
h := ""
if having != nil {
hh, arg, err := having.ParseWhere(&in)
if err != nil {
return r, err
}
args = append(args, arg...)
h = strings.Replace(hh, " where", " having", 1)
}
j := join.parseJoin() j := join.parseJoin()
groupBy := "" groupBy := ""
@ -315,12 +349,12 @@ func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, joi
g.WriteString(group) g.WriteString(group)
groupBy = g.String() groupBy = g.String()
} }
tp := "select %s from %s %s %s %s %s %s" tp := "select %s from %s %s %s %s %s %s %s"
l := "" l := ""
if limit > 0 { if limit > 0 {
l = fmt.Sprintf(" limit %d", limit) l = fmt.Sprintf(" limit %d", limit)
} }
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, order.parseOrderBy(), l) sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, h, order.parseOrderBy(), l)
err = db.Db.Select(&r, sql, args...) err = db.Db.Select(&r, sql, args...)
return return
} }