join
This commit is contained in:
parent
14948929b1
commit
03cd4370ce
6
main.go
6
main.go
|
@ -19,9 +19,11 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
T, err := models.WpPostsM.FindOneById(1)
|
T, t, err := models.WpPostsM.SimplePagination(nil, "wp_posts.ID,b.meta_id post_author", 4, 2, nil, models.SqlBuilder{{
|
||||||
|
"left join", "wp_postmeta b", "b.post_id=wp_posts.ID",
|
||||||
|
}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(T)
|
fmt.Println(T, t)
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,15 +74,32 @@ func (w SqlBuilder) parseOrderBy() string {
|
||||||
}
|
}
|
||||||
return ss
|
return ss
|
||||||
}
|
}
|
||||||
|
func (w SqlBuilder) parseJoin() string {
|
||||||
|
s := strings.Builder{}
|
||||||
|
for _, ss := range w {
|
||||||
|
l := len(ss)
|
||||||
|
for j := 0; j < l; j++ {
|
||||||
|
s.WriteString(" ")
|
||||||
|
if (l == 4 && j == 3) || (l == 3 && j == 2) {
|
||||||
|
s.WriteString("on ")
|
||||||
|
}
|
||||||
|
s.WriteString(ss[j])
|
||||||
|
s.WriteString(" ")
|
||||||
|
}
|
||||||
|
|
||||||
func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSize int, order SqlBuilder) (r []T, total int, err error) {
|
}
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder) (r []T, total int, err error) {
|
||||||
var rr T
|
var rr T
|
||||||
w, args := where.parseWhere()
|
w, args := where.parseWhere()
|
||||||
n := struct {
|
n := struct {
|
||||||
N int `db:"n" json:"n"`
|
N int `db:"n" json:"n"`
|
||||||
}{}
|
}{}
|
||||||
tpx := "select count(*) n from %s %s limit 1"
|
j := join.parseJoin()
|
||||||
sq := fmt.Sprintf(tpx, rr.Table(), w)
|
tpx := "select count(*) n from %s %s %s limit 1"
|
||||||
|
sq := fmt.Sprintf(tpx, rr.Table(), j, w)
|
||||||
err = db.Db.Get(&n, sq, args...)
|
err = db.Db.Get(&n, sq, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -98,8 +115,8 @@ func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSi
|
||||||
if offset >= total {
|
if offset >= total {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tp := "select %s from %s %s %s limit %d,%d"
|
tp := "select %s from %s %s %s %s limit %d,%d"
|
||||||
sql := fmt.Sprintf(tp, fields, rr.Table(), w, order.parseOrderBy(), offset, pageSize)
|
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, 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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user